blob: 82be5c0b8e6e4de426c9368db6d28c385ca88310 [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;
2211 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002212 else
2213 {
2214 /*
2215 * Get the number and item for the only or first index of the List.
2216 */
2217 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002218 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002219 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002220 /* is number or string */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002221 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002222 clear_tv(&var1);
2223
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002224 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002225 lp->ll_list = lp->ll_tv->vval.v_list;
2226 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2227 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002228 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002229 if (lp->ll_n1 < 0)
2230 {
2231 lp->ll_n1 = 0;
2232 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2233 }
2234 }
2235 if (lp->ll_li == NULL)
2236 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002237 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002238 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002239 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002240 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002241 }
2242
2243 /*
2244 * May need to find the item or absolute index for the second
2245 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002246 * When no index given: "lp->ll_empty2" is TRUE.
2247 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002248 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002249 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002250 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002251 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002252 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002253 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002254 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002255 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002256 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002257 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002258 {
2259 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002260 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002261 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002262 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002263 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002264 }
2265
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002266 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2267 if (lp->ll_n1 < 0)
2268 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2269 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002270 {
2271 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002272 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002273 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002274 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002275 }
2276
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002277 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002278 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002279 }
2280
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002281 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002282 return p;
2283}
2284
2285/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002286 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002287 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002288 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002289clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002290{
2291 vim_free(lp->ll_exp_name);
2292 vim_free(lp->ll_newkey);
2293}
2294
2295/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002296 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002297 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01002298 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
2299 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002300 */
2301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002302set_var_lval(
2303 lval_T *lp,
2304 char_u *endp,
2305 typval_T *rettv,
2306 int copy,
2307 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002308{
2309 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002310 listitem_T *ri;
2311 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002312
2313 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002314 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002315 cc = *endp;
2316 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002317 if (lp->ll_blob != NULL)
2318 {
2319 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002320
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002321 if (op != NULL && *op != '=')
2322 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002323 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002324 return;
2325 }
2326
2327 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2328 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002329 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002330
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002331 if (lp->ll_empty2)
2332 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
2333
2334 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002335 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002336 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002337 return;
2338 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002339 if (lp->ll_empty2)
2340 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002341
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002342 ir = 0;
2343 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
2344 blob_set(lp->ll_blob, il,
2345 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002346 }
2347 else
2348 {
2349 val = (int)tv_get_number_chk(rettv, &error);
2350 if (!error)
2351 {
2352 garray_T *gap = &lp->ll_blob->bv_ga;
2353
2354 // Allow for appending a byte. Setting a byte beyond
2355 // the end is an error otherwise.
2356 if (lp->ll_n1 < gap->ga_len
2357 || (lp->ll_n1 == gap->ga_len
2358 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
2359 {
2360 blob_set(lp->ll_blob, lp->ll_n1, val);
2361 if (lp->ll_n1 == gap->ga_len)
2362 ++gap->ga_len;
2363 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002364 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002365 }
2366 }
2367 }
2368 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002369 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002370 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002371
Bram Moolenaarff697e62019-02-12 22:28:33 +01002372 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01002373 di = NULL;
2374 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2375 &tv, &di, TRUE, FALSE) == OK)
2376 {
2377 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002378 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2379 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01002380 && tv_op(&tv, rettv, op) == OK)
2381 set_var(lp->ll_name, &tv, FALSE);
2382 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002383 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002384 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002385 else
2386 set_var(lp->ll_name, rettv, copy);
2387 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002388 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002389 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002390 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002391 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002392 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002393 else if (lp->ll_range)
2394 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002395 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002396 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002397
2398 /*
2399 * Check whether any of the list items is locked
2400 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002401 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002402 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002403 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002404 return;
2405 ri = ri->li_next;
2406 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2407 break;
2408 ll_li = ll_li->li_next;
2409 ++ll_n1;
2410 }
2411
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002412 /*
2413 * Assign the List values to the list items.
2414 */
2415 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002416 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002417 if (op != NULL && *op != '=')
2418 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2419 else
2420 {
2421 clear_tv(&lp->ll_li->li_tv);
2422 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2423 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002424 ri = ri->li_next;
2425 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2426 break;
2427 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002428 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002429 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002430 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002431 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002432 ri = NULL;
2433 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002434 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002435 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002436 lp->ll_li = lp->ll_li->li_next;
2437 ++lp->ll_n1;
2438 }
2439 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002440 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002441 else if (lp->ll_empty2
2442 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002443 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002444 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002445 }
2446 else
2447 {
2448 /*
2449 * Assign to a List or Dictionary item.
2450 */
2451 if (lp->ll_newkey != NULL)
2452 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002453 if (op != NULL && *op != '=')
2454 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002455 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002456 return;
2457 }
2458
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002459 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002460 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002461 if (di == NULL)
2462 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002463 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2464 {
2465 vim_free(di);
2466 return;
2467 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002468 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002469 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002470 else if (op != NULL && *op != '=')
2471 {
2472 tv_op(lp->ll_tv, rettv, op);
2473 return;
2474 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002475 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002476 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002477
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002478 /*
2479 * Assign the value to the variable or list item.
2480 */
2481 if (copy)
2482 copy_tv(rettv, lp->ll_tv);
2483 else
2484 {
2485 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002486 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002487 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002488 }
2489 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002490}
2491
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002492/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01002493 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2494 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002495 * Returns OK or FAIL.
2496 */
2497 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002498tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002499{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002500 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002501 char_u numbuf[NUMBUFLEN];
2502 char_u *s;
2503
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002504 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2505 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2506 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002507 {
2508 switch (tv1->v_type)
2509 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002510 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002511 case VAR_DICT:
2512 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002513 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002514 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002515 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002516 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002517 break;
2518
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002519 case VAR_BLOB:
2520 if (*op != '+' || tv2->v_type != VAR_BLOB)
2521 break;
2522 // BLOB += BLOB
2523 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2524 {
2525 blob_T *b1 = tv1->vval.v_blob;
2526 blob_T *b2 = tv2->vval.v_blob;
2527 int i, len = blob_len(b2);
2528 for (i = 0; i < len; i++)
2529 ga_append(&b1->bv_ga, blob_get(b2, i));
2530 }
2531 return OK;
2532
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002533 case VAR_LIST:
2534 if (*op != '+' || tv2->v_type != VAR_LIST)
2535 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002536 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002537 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2538 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2539 return OK;
2540
2541 case VAR_NUMBER:
2542 case VAR_STRING:
2543 if (tv2->v_type == VAR_LIST)
2544 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002545 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002546 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002547 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002548 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002549#ifdef FEAT_FLOAT
2550 if (tv2->v_type == VAR_FLOAT)
2551 {
2552 float_T f = n;
2553
Bram Moolenaarff697e62019-02-12 22:28:33 +01002554 if (*op == '%')
2555 break;
2556 switch (*op)
2557 {
2558 case '+': f += tv2->vval.v_float; break;
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 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002563 clear_tv(tv1);
2564 tv1->v_type = VAR_FLOAT;
2565 tv1->vval.v_float = f;
2566 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002567 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002568#endif
2569 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002570 switch (*op)
2571 {
2572 case '+': n += tv_get_number(tv2); break;
2573 case '-': n -= tv_get_number(tv2); break;
2574 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01002575 case '/': n = num_divide(n, tv_get_number(tv2)); break;
2576 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002577 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002578 clear_tv(tv1);
2579 tv1->v_type = VAR_NUMBER;
2580 tv1->vval.v_number = n;
2581 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002582 }
2583 else
2584 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002585 if (tv2->v_type == VAR_FLOAT)
2586 break;
2587
Bram Moolenaarff697e62019-02-12 22:28:33 +01002588 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002589 s = tv_get_string(tv1);
2590 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002591 clear_tv(tv1);
2592 tv1->v_type = VAR_STRING;
2593 tv1->vval.v_string = s;
2594 }
2595 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002596
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002597 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002598#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002599 {
2600 float_T f;
2601
Bram Moolenaarff697e62019-02-12 22:28:33 +01002602 if (*op == '%' || *op == '.'
2603 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002604 && tv2->v_type != VAR_NUMBER
2605 && tv2->v_type != VAR_STRING))
2606 break;
2607 if (tv2->v_type == VAR_FLOAT)
2608 f = tv2->vval.v_float;
2609 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002610 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01002611 switch (*op)
2612 {
2613 case '+': tv1->vval.v_float += f; break;
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 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002618 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002619#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002620 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002621 }
2622 }
2623
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002624 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002625 return FAIL;
2626}
2627
2628/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002629 * Evaluate the expression used in a ":for var in expr" command.
2630 * "arg" points to "var".
2631 * Set "*errp" to TRUE for an error, FALSE otherwise;
2632 * Return a pointer that holds the info. Null when there is an error.
2633 */
2634 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002635eval_for_line(
2636 char_u *arg,
2637 int *errp,
2638 char_u **nextcmdp,
2639 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002640{
Bram Moolenaar33570922005-01-25 22:26:29 +00002641 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002642 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002643 typval_T tv;
2644 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002645
2646 *errp = TRUE; /* default: there is an error */
2647
Bram Moolenaar33570922005-01-25 22:26:29 +00002648 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002649 if (fi == NULL)
2650 return NULL;
2651
2652 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2653 if (expr == NULL)
2654 return fi;
2655
2656 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002657 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002658 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002659 emsg(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002660 return fi;
2661 }
2662
2663 if (skip)
2664 ++emsg_skip;
2665 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2666 {
2667 *errp = FALSE;
2668 if (!skip)
2669 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002670 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002671 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002672 l = tv.vval.v_list;
2673 if (l == NULL)
2674 {
2675 // a null list is like an empty list: do nothing
2676 clear_tv(&tv);
2677 }
2678 else
2679 {
2680 // No need to increment the refcount, it's already set for
2681 // the list being used in "tv".
2682 fi->fi_list = l;
2683 list_add_watch(l, &fi->fi_lw);
2684 fi->fi_lw.lw_item = l->lv_first;
2685 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002686 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002687 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002688 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002689 fi->fi_bi = 0;
2690 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002691 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002692 typval_T btv;
2693
2694 // Make a copy, so that the iteration still works when the
2695 // blob is changed.
2696 blob_copy(&tv, &btv);
2697 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002698 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002699 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002700 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002701 else
2702 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002703 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002704 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002705 }
2706 }
2707 }
2708 if (skip)
2709 --emsg_skip;
2710
2711 return fi;
2712}
2713
2714/*
2715 * Use the first item in a ":for" list. Advance to the next.
2716 * Assign the values to the variable (list). "arg" points to the first one.
2717 * Return TRUE when a valid item was found, FALSE when at end of list or
2718 * something wrong.
2719 */
2720 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002721next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002722{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002723 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002724 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002725 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002726
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002727 if (fi->fi_blob != NULL)
2728 {
2729 typval_T tv;
2730
2731 if (fi->fi_bi >= blob_len(fi->fi_blob))
2732 return FALSE;
2733 tv.v_type = VAR_NUMBER;
2734 tv.v_lock = VAR_FIXED;
2735 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2736 ++fi->fi_bi;
2737 return ex_let_vars(arg, &tv, TRUE,
2738 fi->fi_semicolon, fi->fi_varcount, NULL) == OK;
2739 }
2740
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002741 item = fi->fi_lw.lw_item;
2742 if (item == NULL)
2743 result = FALSE;
2744 else
2745 {
2746 fi->fi_lw.lw_item = item->li_next;
2747 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2748 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2749 }
2750 return result;
2751}
2752
2753/*
2754 * Free the structure used to store info used by ":for".
2755 */
2756 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002757free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002758{
Bram Moolenaar33570922005-01-25 22:26:29 +00002759 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002760
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002761 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002762 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002763 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002764 list_unref(fi->fi_list);
2765 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002766 if (fi != NULL && fi->fi_blob != NULL)
2767 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002768 vim_free(fi);
2769}
2770
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2772
2773 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002774set_context_for_expression(
2775 expand_T *xp,
2776 char_u *arg,
2777 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778{
2779 int got_eq = FALSE;
2780 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002781 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002783 if (cmdidx == CMD_let)
2784 {
2785 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002786 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002787 {
2788 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002789 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002790 {
2791 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002792 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002793 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002794 break;
2795 }
2796 return;
2797 }
2798 }
2799 else
2800 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2801 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 while ((xp->xp_pattern = vim_strpbrk(arg,
2803 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2804 {
2805 c = *xp->xp_pattern;
2806 if (c == '&')
2807 {
2808 c = xp->xp_pattern[1];
2809 if (c == '&')
2810 {
2811 ++xp->xp_pattern;
2812 xp->xp_context = cmdidx != CMD_let || got_eq
2813 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2814 }
2815 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002816 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002818 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2819 xp->xp_pattern += 2;
2820
2821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 }
2823 else if (c == '$')
2824 {
2825 /* environment variable */
2826 xp->xp_context = EXPAND_ENV_VARS;
2827 }
2828 else if (c == '=')
2829 {
2830 got_eq = TRUE;
2831 xp->xp_context = EXPAND_EXPRESSION;
2832 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002833 else if (c == '#'
2834 && xp->xp_context == EXPAND_EXPRESSION)
2835 {
2836 /* Autoload function/variable contains '#'. */
2837 break;
2838 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002839 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 && xp->xp_context == EXPAND_FUNCTIONS
2841 && vim_strchr(xp->xp_pattern, '(') == NULL)
2842 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002843 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 break;
2845 }
2846 else if (cmdidx != CMD_let || got_eq)
2847 {
2848 if (c == '"') /* string */
2849 {
2850 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2851 if (c == '\\' && xp->xp_pattern[1] != NUL)
2852 ++xp->xp_pattern;
2853 xp->xp_context = EXPAND_NOTHING;
2854 }
2855 else if (c == '\'') /* literal string */
2856 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002857 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2859 /* skip */ ;
2860 xp->xp_context = EXPAND_NOTHING;
2861 }
2862 else if (c == '|')
2863 {
2864 if (xp->xp_pattern[1] == '|')
2865 {
2866 ++xp->xp_pattern;
2867 xp->xp_context = EXPAND_EXPRESSION;
2868 }
2869 else
2870 xp->xp_context = EXPAND_COMMANDS;
2871 }
2872 else
2873 xp->xp_context = EXPAND_EXPRESSION;
2874 }
2875 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002876 /* Doesn't look like something valid, expand as an expression
2877 * anyway. */
2878 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 arg = xp->xp_pattern;
2880 if (*arg != NUL)
2881 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2882 /* skip */ ;
2883 }
2884 xp->xp_pattern = arg;
2885}
2886
2887#endif /* FEAT_CMDL_COMPL */
2888
2889/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 * ":unlet[!] var1 ... " command.
2891 */
2892 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002893ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002895 ex_unletlock(eap, eap->arg, 0);
2896}
2897
2898/*
2899 * ":lockvar" and ":unlockvar" commands
2900 */
2901 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002902ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002903{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002905 int deep = 2;
2906
2907 if (eap->forceit)
2908 deep = -1;
2909 else if (vim_isdigit(*arg))
2910 {
2911 deep = getdigits(&arg);
2912 arg = skipwhite(arg);
2913 }
2914
2915 ex_unletlock(eap, arg, deep);
2916}
2917
2918/*
2919 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2920 */
2921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002922ex_unletlock(
2923 exarg_T *eap,
2924 char_u *argstart,
2925 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002926{
2927 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002930 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931
2932 do
2933 {
Bram Moolenaar137374f2018-05-13 15:59:50 +02002934 if (*arg == '$')
2935 {
2936 char_u *name = ++arg;
2937
2938 if (get_env_len(&arg) == 0)
2939 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002940 semsg(_(e_invarg2), name - 1);
Bram Moolenaar137374f2018-05-13 15:59:50 +02002941 return;
2942 }
2943 vim_unsetenv(name);
2944 arg = skipwhite(arg);
2945 continue;
2946 }
2947
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002948 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002949 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002950 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002951 if (lv.ll_name == NULL)
2952 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002953 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002954 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002956 if (name_end != NULL)
2957 {
2958 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002959 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002960 }
2961 if (!(eap->skip || error))
2962 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 break;
2964 }
2965
2966 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002967 {
2968 if (eap->cmdidx == CMD_unlet)
2969 {
2970 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2971 error = TRUE;
2972 }
2973 else
2974 {
2975 if (do_lock_var(&lv, name_end, deep,
2976 eap->cmdidx == CMD_lockvar) == FAIL)
2977 error = TRUE;
2978 }
2979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002981 if (!eap->skip)
2982 clear_lval(&lv);
2983
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 arg = skipwhite(name_end);
2985 } while (!ends_excmd(*arg));
2986
2987 eap->nextcmd = check_nextcmd(arg);
2988}
2989
Bram Moolenaar8c711452005-01-14 21:53:12 +00002990 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002991do_unlet_var(
2992 lval_T *lp,
2993 char_u *name_end,
2994 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002995{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002996 int ret = OK;
2997 int cc;
2998
2999 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003000 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003001 cc = *name_end;
3002 *name_end = NUL;
3003
3004 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003005 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003006 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003007 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003008 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003009 else if ((lp->ll_list != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003010 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003011 || (lp->ll_dict != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003012 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003013 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003014 else if (lp->ll_range)
3015 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003016 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003017 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003018 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003019
3020 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3021 {
3022 li = ll_li->li_next;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003023 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003024 return FAIL;
3025 ll_li = li;
3026 ++ll_n1;
3027 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003028
3029 /* Delete a range of List items. */
3030 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3031 {
3032 li = lp->ll_li->li_next;
3033 listitem_remove(lp->ll_list, lp->ll_li);
3034 lp->ll_li = li;
3035 ++lp->ll_n1;
3036 }
3037 }
3038 else
3039 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003040 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003041 /* unlet a List item. */
3042 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003043 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003044 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003045 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003046 }
3047
3048 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003049}
3050
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051/*
3052 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003053 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 */
3055 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003056do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057{
Bram Moolenaar33570922005-01-25 22:26:29 +00003058 hashtab_T *ht;
3059 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003060 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003061 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003062 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063
Bram Moolenaar33570922005-01-25 22:26:29 +00003064 ht = find_var_ht(name, &varname);
3065 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003067 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003068 if (d == NULL)
3069 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003070 if (ht == &globvarht)
3071 d = &globvardict;
3072 else if (ht == &compat_hashtab)
3073 d = &vimvardict;
3074 else
3075 {
3076 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3077 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3078 }
3079 if (d == NULL)
3080 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003081 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003082 return FAIL;
3083 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003084 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003085 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003086 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003087 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003088 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003089 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003090 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003091 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003092 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003093 || var_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003094 return FAIL;
3095
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003096 delete_var(ht, hi);
3097 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003100 if (forceit)
3101 return OK;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003102 semsg(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 return FAIL;
3104}
3105
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003106/*
3107 * Lock or unlock variable indicated by "lp".
3108 * "deep" is the levels to go (-1 for unlimited);
3109 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3110 */
3111 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003112do_lock_var(
3113 lval_T *lp,
3114 char_u *name_end,
3115 int deep,
3116 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003117{
3118 int ret = OK;
3119 int cc;
3120 dictitem_T *di;
3121
3122 if (deep == 0) /* nothing to do */
3123 return OK;
3124
3125 if (lp->ll_tv == NULL)
3126 {
3127 cc = *name_end;
3128 *name_end = NUL;
3129
3130 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003131 di = find_var(lp->ll_name, NULL, TRUE);
3132 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003133 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01003134 else if ((di->di_flags & DI_FLAGS_FIX)
3135 && di->di_tv.v_type != VAR_DICT
3136 && di->di_tv.v_type != VAR_LIST)
3137 /* For historic reasons this error is not given for a list or dict.
3138 * E.g., the b: dict could be locked/unlocked. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003139 semsg(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003140 else
3141 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01003142 if (lock)
3143 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003144 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01003145 di->di_flags &= ~DI_FLAGS_LOCK;
3146 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003147 }
3148 *name_end = cc;
3149 }
3150 else if (lp->ll_range)
3151 {
3152 listitem_T *li = lp->ll_li;
3153
3154 /* (un)lock a range of List items. */
3155 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3156 {
3157 item_lock(&li->li_tv, deep, lock);
3158 li = li->li_next;
3159 ++lp->ll_n1;
3160 }
3161 }
3162 else if (lp->ll_list != NULL)
3163 /* (un)lock a List item. */
3164 item_lock(&lp->ll_li->li_tv, deep, lock);
3165 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003166 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003167 item_lock(&lp->ll_di->di_tv, deep, lock);
3168
3169 return ret;
3170}
3171
3172/*
3173 * Lock or unlock an item. "deep" is nr of levels to go.
3174 */
3175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003176item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003177{
3178 static int recurse = 0;
3179 list_T *l;
3180 listitem_T *li;
3181 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003182 blob_T *b;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003183 hashitem_T *hi;
3184 int todo;
3185
3186 if (recurse >= DICT_MAXNEST)
3187 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003188 emsg(_("E743: variable nested too deep for (un)lock"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003189 return;
3190 }
3191 if (deep == 0)
3192 return;
3193 ++recurse;
3194
3195 /* lock/unlock the item itself */
3196 if (lock)
3197 tv->v_lock |= VAR_LOCKED;
3198 else
3199 tv->v_lock &= ~VAR_LOCKED;
3200
3201 switch (tv->v_type)
3202 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003203 case VAR_UNKNOWN:
3204 case VAR_NUMBER:
3205 case VAR_STRING:
3206 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003207 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003208 case VAR_FLOAT:
3209 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003210 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003211 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003212 break;
3213
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003214 case VAR_BLOB:
3215 if ((b = tv->vval.v_blob) != NULL)
3216 {
3217 if (lock)
3218 b->bv_lock |= VAR_LOCKED;
3219 else
3220 b->bv_lock &= ~VAR_LOCKED;
3221 }
3222 break;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003223 case VAR_LIST:
3224 if ((l = tv->vval.v_list) != NULL)
3225 {
3226 if (lock)
3227 l->lv_lock |= VAR_LOCKED;
3228 else
3229 l->lv_lock &= ~VAR_LOCKED;
3230 if (deep < 0 || deep > 1)
3231 /* recursive: lock/unlock the items the List contains */
3232 for (li = l->lv_first; li != NULL; li = li->li_next)
3233 item_lock(&li->li_tv, deep - 1, lock);
3234 }
3235 break;
3236 case VAR_DICT:
3237 if ((d = tv->vval.v_dict) != NULL)
3238 {
3239 if (lock)
3240 d->dv_lock |= VAR_LOCKED;
3241 else
3242 d->dv_lock &= ~VAR_LOCKED;
3243 if (deep < 0 || deep > 1)
3244 {
3245 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003246 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003247 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3248 {
3249 if (!HASHITEM_EMPTY(hi))
3250 {
3251 --todo;
3252 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3253 }
3254 }
3255 }
3256 }
3257 }
3258 --recurse;
3259}
3260
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3262/*
3263 * Delete all "menutrans_" variables.
3264 */
3265 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003266del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267{
Bram Moolenaar33570922005-01-25 22:26:29 +00003268 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003269 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270
Bram Moolenaar33570922005-01-25 22:26:29 +00003271 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003272 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003273 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003274 {
3275 if (!HASHITEM_EMPTY(hi))
3276 {
3277 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003278 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3279 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003280 }
3281 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003282 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283}
3284#endif
3285
3286#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3287
3288/*
3289 * Local string buffer for the next two functions to store a variable name
3290 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3291 * get_user_var_name().
3292 */
3293
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294static char_u *varnamebuf = NULL;
3295static int varnamebuflen = 0;
3296
3297/*
3298 * Function to concatenate a prefix and a variable name.
3299 */
3300 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003301cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302{
3303 int len;
3304
3305 len = (int)STRLEN(name) + 3;
3306 if (len > varnamebuflen)
3307 {
3308 vim_free(varnamebuf);
3309 len += 10; /* some additional space */
3310 varnamebuf = alloc(len);
3311 if (varnamebuf == NULL)
3312 {
3313 varnamebuflen = 0;
3314 return NULL;
3315 }
3316 varnamebuflen = len;
3317 }
3318 *varnamebuf = prefix;
3319 varnamebuf[1] = ':';
3320 STRCPY(varnamebuf + 2, name);
3321 return varnamebuf;
3322}
3323
3324/*
3325 * Function given to ExpandGeneric() to obtain the list of user defined
3326 * (global/buffer/window/built-in) variable names.
3327 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003329get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003331 static long_u gdone;
3332 static long_u bdone;
3333 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003334 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003335 static int vidx;
3336 static hashitem_T *hi;
3337 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338
3339 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003340 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003341 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003342 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003343 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003344
3345 /* Global variables */
3346 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003348 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003349 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003350 else
3351 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003352 while (HASHITEM_EMPTY(hi))
3353 ++hi;
3354 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3355 return cat_prefix_varname('g', hi->hi_key);
3356 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003358
3359 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003360 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003361 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003363 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003364 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003365 else
3366 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003367 while (HASHITEM_EMPTY(hi))
3368 ++hi;
3369 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003371
3372 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003373 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003374 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003376 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003377 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003378 else
3379 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003380 while (HASHITEM_EMPTY(hi))
3381 ++hi;
3382 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003384
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003385 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003386 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003387 if (tdone < ht->ht_used)
3388 {
3389 if (tdone++ == 0)
3390 hi = ht->ht_array;
3391 else
3392 ++hi;
3393 while (HASHITEM_EMPTY(hi))
3394 ++hi;
3395 return cat_prefix_varname('t', hi->hi_key);
3396 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003397
Bram Moolenaar33570922005-01-25 22:26:29 +00003398 /* v: variables */
3399 if (vidx < VV_LEN)
3400 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401
Bram Moolenaard23a8232018-02-10 18:45:26 +01003402 VIM_CLEAR(varnamebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 varnamebuflen = 0;
3404 return NULL;
3405}
3406
3407#endif /* FEAT_CMDL_COMPL */
3408
3409/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003410 * Return TRUE if "pat" matches "text".
3411 * Does not use 'cpo' and always uses 'magic'.
3412 */
3413 static int
3414pattern_match(char_u *pat, char_u *text, int ic)
3415{
3416 int matches = FALSE;
3417 char_u *save_cpo;
3418 regmatch_T regmatch;
3419
3420 /* avoid 'l' flag in 'cpoptions' */
3421 save_cpo = p_cpo;
3422 p_cpo = (char_u *)"";
3423 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3424 if (regmatch.regprog != NULL)
3425 {
3426 regmatch.rm_ic = ic;
3427 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3428 vim_regfree(regmatch.regprog);
3429 }
3430 p_cpo = save_cpo;
3431 return matches;
3432}
3433
3434/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003436 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3438 */
3439
3440/*
3441 * Handle zero level expression.
3442 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003443 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003444 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 * Return OK or FAIL.
3446 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003447 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003448eval0(
3449 char_u *arg,
3450 typval_T *rettv,
3451 char_u **nextcmd,
3452 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453{
3454 int ret;
3455 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003456 int did_emsg_before = did_emsg;
3457 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458
3459 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003460 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 if (ret == FAIL || !ends_excmd(*p))
3462 {
3463 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003464 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 /*
3466 * Report the invalid expression unless the expression evaluation has
3467 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003468 * exception, or we already gave a more specific error.
3469 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003471 if (!aborting() && did_emsg == did_emsg_before
3472 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003473 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 ret = FAIL;
3475 }
3476 if (nextcmd != NULL)
3477 *nextcmd = check_nextcmd(p);
3478
3479 return ret;
3480}
3481
3482/*
3483 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003484 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 *
3486 * "arg" must point to the first non-white of the expression.
3487 * "arg" is advanced to the next non-white after the recognized expression.
3488 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003489 * Note: "rettv.v_lock" is not set.
3490 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 * Return OK or FAIL.
3492 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003493 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003494eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495{
3496 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003497 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498
3499 /*
3500 * Get the first variable.
3501 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003502 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 return FAIL;
3504
3505 if ((*arg)[0] == '?')
3506 {
3507 result = FALSE;
3508 if (evaluate)
3509 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003510 int error = FALSE;
3511
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003512 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003514 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003515 if (error)
3516 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 }
3518
3519 /*
3520 * Get the second variable.
3521 */
3522 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003523 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 return FAIL;
3525
3526 /*
3527 * Check for the ":".
3528 */
3529 if ((*arg)[0] != ':')
3530 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003531 emsg(_("E109: Missing ':' after '?'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003533 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 return FAIL;
3535 }
3536
3537 /*
3538 * Get the third variable.
3539 */
3540 *arg = skipwhite(*arg + 1);
3541 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3542 {
3543 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003544 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 return FAIL;
3546 }
3547 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003548 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 }
3550
3551 return OK;
3552}
3553
3554/*
3555 * Handle first level expression:
3556 * expr2 || expr2 || expr2 logical OR
3557 *
3558 * "arg" must point to the first non-white of the expression.
3559 * "arg" is advanced to the next non-white after the recognized expression.
3560 *
3561 * Return OK or FAIL.
3562 */
3563 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003564eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565{
Bram Moolenaar33570922005-01-25 22:26:29 +00003566 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 long result;
3568 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003569 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570
3571 /*
3572 * Get the first variable.
3573 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003574 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 return FAIL;
3576
3577 /*
3578 * Repeat until there is no following "||".
3579 */
3580 first = TRUE;
3581 result = FALSE;
3582 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3583 {
3584 if (evaluate && first)
3585 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003586 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003588 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003589 if (error)
3590 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 first = FALSE;
3592 }
3593
3594 /*
3595 * Get the second variable.
3596 */
3597 *arg = skipwhite(*arg + 2);
3598 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3599 return FAIL;
3600
3601 /*
3602 * Compute the result.
3603 */
3604 if (evaluate && !result)
3605 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003606 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003608 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003609 if (error)
3610 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 }
3612 if (evaluate)
3613 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003614 rettv->v_type = VAR_NUMBER;
3615 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 }
3617 }
3618
3619 return OK;
3620}
3621
3622/*
3623 * Handle second level expression:
3624 * expr3 && expr3 && expr3 logical AND
3625 *
3626 * "arg" must point to the first non-white of the expression.
3627 * "arg" is advanced to the next non-white after the recognized expression.
3628 *
3629 * Return OK or FAIL.
3630 */
3631 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003632eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633{
Bram Moolenaar33570922005-01-25 22:26:29 +00003634 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 long result;
3636 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003637 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638
3639 /*
3640 * Get the first variable.
3641 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003642 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 return FAIL;
3644
3645 /*
3646 * Repeat until there is no following "&&".
3647 */
3648 first = TRUE;
3649 result = TRUE;
3650 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3651 {
3652 if (evaluate && first)
3653 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003654 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003656 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003657 if (error)
3658 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 first = FALSE;
3660 }
3661
3662 /*
3663 * Get the second variable.
3664 */
3665 *arg = skipwhite(*arg + 2);
3666 if (eval4(arg, &var2, evaluate && result) == FAIL)
3667 return FAIL;
3668
3669 /*
3670 * Compute the result.
3671 */
3672 if (evaluate && result)
3673 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003674 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003676 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003677 if (error)
3678 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 }
3680 if (evaluate)
3681 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003682 rettv->v_type = VAR_NUMBER;
3683 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 }
3685 }
3686
3687 return OK;
3688}
3689
3690/*
3691 * Handle third level expression:
3692 * var1 == var2
3693 * var1 =~ var2
3694 * var1 != var2
3695 * var1 !~ var2
3696 * var1 > var2
3697 * var1 >= var2
3698 * var1 < var2
3699 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003700 * var1 is var2
3701 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 *
3703 * "arg" must point to the first non-white of the expression.
3704 * "arg" is advanced to the next non-white after the recognized expression.
3705 *
3706 * Return OK or FAIL.
3707 */
3708 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003709eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710{
Bram Moolenaar33570922005-01-25 22:26:29 +00003711 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 char_u *p;
3713 int i;
3714 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003715 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718
3719 /*
3720 * Get the first variable.
3721 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003722 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 return FAIL;
3724
3725 p = *arg;
3726 switch (p[0])
3727 {
3728 case '=': if (p[1] == '=')
3729 type = TYPE_EQUAL;
3730 else if (p[1] == '~')
3731 type = TYPE_MATCH;
3732 break;
3733 case '!': if (p[1] == '=')
3734 type = TYPE_NEQUAL;
3735 else if (p[1] == '~')
3736 type = TYPE_NOMATCH;
3737 break;
3738 case '>': if (p[1] != '=')
3739 {
3740 type = TYPE_GREATER;
3741 len = 1;
3742 }
3743 else
3744 type = TYPE_GEQUAL;
3745 break;
3746 case '<': if (p[1] != '=')
3747 {
3748 type = TYPE_SMALLER;
3749 len = 1;
3750 }
3751 else
3752 type = TYPE_SEQUAL;
3753 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003754 case 'i': if (p[1] == 's')
3755 {
3756 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3757 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003758 i = p[len];
3759 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003760 {
3761 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3762 type_is = TRUE;
3763 }
3764 }
3765 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 }
3767
3768 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003769 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 */
3771 if (type != TYPE_UNKNOWN)
3772 {
3773 /* extra question mark appended: ignore case */
3774 if (p[len] == '?')
3775 {
3776 ic = TRUE;
3777 ++len;
3778 }
3779 /* extra '#' appended: match case */
3780 else if (p[len] == '#')
3781 {
3782 ic = FALSE;
3783 ++len;
3784 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003785 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 else
3787 ic = p_ic;
3788
3789 /*
3790 * Get the second variable.
3791 */
3792 *arg = skipwhite(p + len);
3793 if (eval5(arg, &var2, evaluate) == FAIL)
3794 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003795 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 return FAIL;
3797 }
Bram Moolenaar31988702018-02-13 12:57:42 +01003798 if (evaluate)
3799 {
3800 int ret = typval_compare(rettv, &var2, type, type_is, ic);
3801
3802 clear_tv(&var2);
3803 return ret;
3804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 }
3806
3807 return OK;
3808}
3809
3810/*
3811 * Handle fourth level expression:
3812 * + number addition
3813 * - number subtraction
3814 * . string concatenation
3815 *
3816 * "arg" must point to the first non-white of the expression.
3817 * "arg" is advanced to the next non-white after the recognized expression.
3818 *
3819 * Return OK or FAIL.
3820 */
3821 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003822eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823{
Bram Moolenaar33570922005-01-25 22:26:29 +00003824 typval_T var2;
3825 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003827 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003828#ifdef FEAT_FLOAT
3829 float_T f1 = 0, f2 = 0;
3830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 char_u *s1, *s2;
3832 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3833 char_u *p;
3834
3835 /*
3836 * Get the first variable.
3837 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003838 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 return FAIL;
3840
3841 /*
3842 * Repeat computing, until no '+', '-' or '.' is following.
3843 */
3844 for (;;)
3845 {
3846 op = **arg;
3847 if (op != '+' && op != '-' && op != '.')
3848 break;
3849
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003850 if ((op != '+' || (rettv->v_type != VAR_LIST
3851 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003852#ifdef FEAT_FLOAT
3853 && (op == '.' || rettv->v_type != VAR_FLOAT)
3854#endif
3855 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003856 {
3857 /* For "list + ...", an illegal use of the first operand as
3858 * a number cannot be determined before evaluating the 2nd
3859 * operand: if this is also a list, all is ok.
3860 * For "something . ...", "something - ..." or "non-list + ...",
3861 * we know that the first operand needs to be a string or number
3862 * without evaluating the 2nd operand. So check before to avoid
3863 * side effects after an error. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003864 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003865 {
3866 clear_tv(rettv);
3867 return FAIL;
3868 }
3869 }
3870
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 /*
3872 * Get the second variable.
3873 */
3874 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003875 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003877 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 return FAIL;
3879 }
3880
3881 if (evaluate)
3882 {
3883 /*
3884 * Compute the result.
3885 */
3886 if (op == '.')
3887 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003888 s1 = tv_get_string_buf(rettv, buf1); /* already checked */
3889 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003890 if (s2 == NULL) /* type error ? */
3891 {
3892 clear_tv(rettv);
3893 clear_tv(&var2);
3894 return FAIL;
3895 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003896 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003897 clear_tv(rettv);
3898 rettv->v_type = VAR_STRING;
3899 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003901 else if (op == '+' && rettv->v_type == VAR_BLOB
3902 && var2.v_type == VAR_BLOB)
3903 {
3904 blob_T *b1 = rettv->vval.v_blob;
3905 blob_T *b2 = var2.vval.v_blob;
3906 blob_T *b = blob_alloc();
3907 int i;
3908
3909 if (b != NULL)
3910 {
3911 for (i = 0; i < blob_len(b1); i++)
3912 ga_append(&b->bv_ga, blob_get(b1, i));
3913 for (i = 0; i < blob_len(b2); i++)
3914 ga_append(&b->bv_ga, blob_get(b2, i));
3915
3916 clear_tv(rettv);
3917 rettv_blob_set(rettv, b);
3918 }
3919 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003920 else if (op == '+' && rettv->v_type == VAR_LIST
3921 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003922 {
3923 /* concatenate Lists */
3924 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3925 &var3) == FAIL)
3926 {
3927 clear_tv(rettv);
3928 clear_tv(&var2);
3929 return FAIL;
3930 }
3931 clear_tv(rettv);
3932 *rettv = var3;
3933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 else
3935 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003936 int error = FALSE;
3937
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003938#ifdef FEAT_FLOAT
3939 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003940 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003941 f1 = rettv->vval.v_float;
3942 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003943 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003944 else
3945#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003946 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003947 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003948 if (error)
3949 {
3950 /* This can only happen for "list + non-list". For
3951 * "non-list + ..." or "something - ...", we returned
3952 * before evaluating the 2nd operand. */
3953 clear_tv(rettv);
3954 return FAIL;
3955 }
3956#ifdef FEAT_FLOAT
3957 if (var2.v_type == VAR_FLOAT)
3958 f1 = n1;
3959#endif
3960 }
3961#ifdef FEAT_FLOAT
3962 if (var2.v_type == VAR_FLOAT)
3963 {
3964 f2 = var2.vval.v_float;
3965 n2 = 0;
3966 }
3967 else
3968#endif
3969 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003970 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003971 if (error)
3972 {
3973 clear_tv(rettv);
3974 clear_tv(&var2);
3975 return FAIL;
3976 }
3977#ifdef FEAT_FLOAT
3978 if (rettv->v_type == VAR_FLOAT)
3979 f2 = n2;
3980#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003981 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003982 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003983
3984#ifdef FEAT_FLOAT
3985 /* If there is a float on either side the result is a float. */
3986 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3987 {
3988 if (op == '+')
3989 f1 = f1 + f2;
3990 else
3991 f1 = f1 - f2;
3992 rettv->v_type = VAR_FLOAT;
3993 rettv->vval.v_float = f1;
3994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003996#endif
3997 {
3998 if (op == '+')
3999 n1 = n1 + n2;
4000 else
4001 n1 = n1 - n2;
4002 rettv->v_type = VAR_NUMBER;
4003 rettv->vval.v_number = n1;
4004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004006 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 }
4008 }
4009 return OK;
4010}
4011
4012/*
4013 * Handle fifth level expression:
4014 * * number multiplication
4015 * / number division
4016 * % number modulo
4017 *
4018 * "arg" must point to the first non-white of the expression.
4019 * "arg" is advanced to the next non-white after the recognized expression.
4020 *
4021 * Return OK or FAIL.
4022 */
4023 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004024eval6(
4025 char_u **arg,
4026 typval_T *rettv,
4027 int evaluate,
4028 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029{
Bram Moolenaar33570922005-01-25 22:26:29 +00004030 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004032 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004033#ifdef FEAT_FLOAT
4034 int use_float = FALSE;
4035 float_T f1 = 0, f2;
4036#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004037 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038
4039 /*
4040 * Get the first variable.
4041 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004042 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 return FAIL;
4044
4045 /*
4046 * Repeat computing, until no '*', '/' or '%' is following.
4047 */
4048 for (;;)
4049 {
4050 op = **arg;
4051 if (op != '*' && op != '/' && op != '%')
4052 break;
4053
4054 if (evaluate)
4055 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004056#ifdef FEAT_FLOAT
4057 if (rettv->v_type == VAR_FLOAT)
4058 {
4059 f1 = rettv->vval.v_float;
4060 use_float = TRUE;
4061 n1 = 0;
4062 }
4063 else
4064#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004065 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004066 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004067 if (error)
4068 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 }
4070 else
4071 n1 = 0;
4072
4073 /*
4074 * Get the second variable.
4075 */
4076 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004077 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 return FAIL;
4079
4080 if (evaluate)
4081 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004082#ifdef FEAT_FLOAT
4083 if (var2.v_type == VAR_FLOAT)
4084 {
4085 if (!use_float)
4086 {
4087 f1 = n1;
4088 use_float = TRUE;
4089 }
4090 f2 = var2.vval.v_float;
4091 n2 = 0;
4092 }
4093 else
4094#endif
4095 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004096 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004097 clear_tv(&var2);
4098 if (error)
4099 return FAIL;
4100#ifdef FEAT_FLOAT
4101 if (use_float)
4102 f2 = n2;
4103#endif
4104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105
4106 /*
4107 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004108 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004110#ifdef FEAT_FLOAT
4111 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004113 if (op == '*')
4114 f1 = f1 * f2;
4115 else if (op == '/')
4116 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004117# ifdef VMS
4118 /* VMS crashes on divide by zero, work around it */
4119 if (f2 == 0.0)
4120 {
4121 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004122 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004123 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004124 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004125 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004126 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004127 }
4128 else
4129 f1 = f1 / f2;
4130# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004131 /* We rely on the floating point library to handle divide
4132 * by zero to result in "inf" and not a crash. */
4133 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004134# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004137 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004138 emsg(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004139 return FAIL;
4140 }
4141 rettv->v_type = VAR_FLOAT;
4142 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 }
4144 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004147 if (op == '*')
4148 n1 = n1 * n2;
4149 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01004150 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01004152 n1 = num_modulus(n1, n2);
4153
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004154 rettv->v_type = VAR_NUMBER;
4155 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 }
4158 }
4159
4160 return OK;
4161}
4162
4163/*
4164 * Handle sixth level expression:
4165 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004166 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004167 * "string" string constant
4168 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 * &option-name option value
4170 * @r register contents
4171 * identifier variable value
4172 * function() function call
4173 * $VAR environment variable
4174 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004175 * [expr, expr] List
4176 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 *
4178 * Also handle:
4179 * ! in front logical NOT
4180 * - in front unary minus
4181 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004182 * trailing [] subscript in String or List
4183 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 *
4185 * "arg" must point to the first non-white of the expression.
4186 * "arg" is advanced to the next non-white after the recognized expression.
4187 *
4188 * Return OK or FAIL.
4189 */
4190 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004191eval7(
4192 char_u **arg,
4193 typval_T *rettv,
4194 int evaluate,
4195 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004197 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 int len;
4199 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 char_u *start_leader, *end_leader;
4201 int ret = OK;
4202 char_u *alias;
4203
4204 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004205 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004206 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004208 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209
4210 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004211 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 */
4213 start_leader = *arg;
4214 while (**arg == '!' || **arg == '-' || **arg == '+')
4215 *arg = skipwhite(*arg + 1);
4216 end_leader = *arg;
4217
4218 switch (**arg)
4219 {
4220 /*
4221 * Number constant.
4222 */
4223 case '0':
4224 case '1':
4225 case '2':
4226 case '3':
4227 case '4':
4228 case '5':
4229 case '6':
4230 case '7':
4231 case '8':
4232 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004233 {
4234#ifdef FEAT_FLOAT
4235 char_u *p = skipdigits(*arg + 1);
4236 int get_float = FALSE;
4237
4238 /* We accept a float when the format matches
4239 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004240 * strict to avoid backwards compatibility problems.
4241 * Don't look for a float after the "." operator, so that
4242 * ":let vers = 1.2.3" doesn't fail. */
4243 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004245 get_float = TRUE;
4246 p = skipdigits(p + 2);
4247 if (*p == 'e' || *p == 'E')
4248 {
4249 ++p;
4250 if (*p == '-' || *p == '+')
4251 ++p;
4252 if (!vim_isdigit(*p))
4253 get_float = FALSE;
4254 else
4255 p = skipdigits(p + 1);
4256 }
4257 if (ASCII_ISALPHA(*p) || *p == '.')
4258 get_float = FALSE;
4259 }
4260 if (get_float)
4261 {
4262 float_T f;
4263
4264 *arg += string2float(*arg, &f);
4265 if (evaluate)
4266 {
4267 rettv->v_type = VAR_FLOAT;
4268 rettv->vval.v_float = f;
4269 }
4270 }
4271 else
4272#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004273 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004274 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004275 char_u *bp;
Bram Moolenaare519dfd2019-01-13 15:42:02 +01004276 blob_T *blob = NULL; // init for gcc
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004277
4278 // Blob constant: 0z0123456789abcdef
4279 if (evaluate)
4280 blob = blob_alloc();
4281 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
4282 {
4283 if (!vim_isxdigit(bp[1]))
4284 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004285 if (blob != NULL)
4286 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004287 emsg(_("E973: Blob literal should have an even number of hex characters"));
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004288 ga_clear(&blob->bv_ga);
4289 VIM_CLEAR(blob);
4290 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004291 ret = FAIL;
4292 break;
4293 }
4294 if (blob != NULL)
4295 ga_append(&blob->bv_ga,
4296 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
Bram Moolenaar4131fd52019-01-17 16:32:53 +01004297 if (bp[2] == '.' && vim_isxdigit(bp[3]))
4298 ++bp;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004299 }
4300 if (blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004301 rettv_blob_set(rettv, blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004302 *arg = bp;
4303 }
4304 else
4305 {
4306 // decimal, hex or octal number
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01004307 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004308 *arg += len;
4309 if (evaluate)
4310 {
4311 rettv->v_type = VAR_NUMBER;
4312 rettv->vval.v_number = n;
4313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 }
4315 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317
4318 /*
4319 * String constant: "string".
4320 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004321 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 break;
4323
4324 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004325 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004327 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004328 break;
4329
4330 /*
4331 * List: [expr, expr]
4332 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004333 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 break;
4335
4336 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004337 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004338 * Dictionary: {key: val, key: val}
4339 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004340 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4341 if (ret == NOTDONE)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004342 ret = dict_get_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004343 break;
4344
4345 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004346 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004348 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 break;
4350
4351 /*
4352 * Environment variable: $VAR.
4353 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004354 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 break;
4356
4357 /*
4358 * Register contents: @r.
4359 */
4360 case '@': ++*arg;
4361 if (evaluate)
4362 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004363 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004364 rettv->vval.v_string = get_reg_contents(**arg,
4365 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 }
4367 if (**arg != NUL)
4368 ++*arg;
4369 break;
4370
4371 /*
4372 * nested expression: (expression).
4373 */
4374 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004375 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 if (**arg == ')')
4377 ++*arg;
4378 else if (ret == OK)
4379 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004380 emsg(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004381 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 ret = FAIL;
4383 }
4384 break;
4385
Bram Moolenaar8c711452005-01-14 21:53:12 +00004386 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 break;
4388 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004389
4390 if (ret == NOTDONE)
4391 {
4392 /*
4393 * Must be a variable or function name.
4394 * Can also be a curly-braces kind of name: {expr}.
4395 */
4396 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004397 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004398 if (alias != NULL)
4399 s = alias;
4400
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004401 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004402 ret = FAIL;
4403 else
4404 {
4405 if (**arg == '(') /* recursive! */
4406 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004407 partial_T *partial;
4408
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004409 if (!evaluate)
4410 check_vars(s, len);
4411
Bram Moolenaar8c711452005-01-14 21:53:12 +00004412 /* If "s" is the name of a variable of type VAR_FUNC
4413 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004414 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004415
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004416 /* Need to make a copy, in case evaluating the arguments makes
4417 * the name invalid. */
4418 s = vim_strsave(s);
4419 if (s == NULL)
4420 ret = FAIL;
4421 else
4422 /* Invoke the function. */
4423 ret = get_func_tv(s, len, rettv, arg,
4424 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4425 &len, evaluate, partial, NULL);
4426 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004427
4428 /* If evaluate is FALSE rettv->v_type was not set in
4429 * get_func_tv, but it's needed in handle_subscript() to parse
4430 * what follows. So set it here. */
4431 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4432 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004433 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004434 rettv->v_type = VAR_FUNC;
4435 }
4436
Bram Moolenaar8c711452005-01-14 21:53:12 +00004437 /* Stop the expression evaluation when immediately
4438 * aborting on error, or when an interrupt occurred or
4439 * an exception was thrown but not caught. */
4440 if (aborting())
4441 {
4442 if (ret == OK)
4443 clear_tv(rettv);
4444 ret = FAIL;
4445 }
4446 }
4447 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004448 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004449 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004450 {
4451 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004452 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004453 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004454 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004455 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004456 }
4457
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 *arg = skipwhite(*arg);
4459
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004460 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4461 * expr(expr). */
4462 if (ret == OK)
4463 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464
4465 /*
4466 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4467 */
4468 if (ret == OK && evaluate && end_leader > start_leader)
4469 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004470 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004471 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004472#ifdef FEAT_FLOAT
4473 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004474
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004475 if (rettv->v_type == VAR_FLOAT)
4476 f = rettv->vval.v_float;
4477 else
4478#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004479 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004480 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004482 clear_tv(rettv);
4483 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004485 else
4486 {
4487 while (end_leader > start_leader)
4488 {
4489 --end_leader;
4490 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004491 {
4492#ifdef FEAT_FLOAT
4493 if (rettv->v_type == VAR_FLOAT)
4494 f = !f;
4495 else
4496#endif
4497 val = !val;
4498 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004499 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004500 {
4501#ifdef FEAT_FLOAT
4502 if (rettv->v_type == VAR_FLOAT)
4503 f = -f;
4504 else
4505#endif
4506 val = -val;
4507 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004508 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004509#ifdef FEAT_FLOAT
4510 if (rettv->v_type == VAR_FLOAT)
4511 {
4512 clear_tv(rettv);
4513 rettv->vval.v_float = f;
4514 }
4515 else
4516#endif
4517 {
4518 clear_tv(rettv);
4519 rettv->v_type = VAR_NUMBER;
4520 rettv->vval.v_number = val;
4521 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 }
4524
4525 return ret;
4526}
4527
4528/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004529 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4530 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004531 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4532 */
4533 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004534eval_index(
4535 char_u **arg,
4536 typval_T *rettv,
4537 int evaluate,
4538 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004539{
4540 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004541 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004542 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004543 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004544 long len = -1;
4545 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004546 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004547 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004548
Bram Moolenaara03f2332016-02-06 18:09:59 +01004549 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004550 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004551 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004552 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004553 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004554 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004555 return FAIL;
4556 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004557#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004558 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004559 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004560 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004561#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004562 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004563 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004564 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004565 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004566 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004567 return FAIL;
4568 case VAR_UNKNOWN:
4569 if (evaluate)
4570 return FAIL;
4571 /* FALLTHROUGH */
4572
4573 case VAR_STRING:
4574 case VAR_NUMBER:
4575 case VAR_LIST:
4576 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004577 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004578 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004579 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004580
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004581 init_tv(&var1);
4582 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004583 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004584 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004585 /*
4586 * dict.name
4587 */
4588 key = *arg + 1;
4589 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4590 ;
4591 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004592 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004593 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004594 }
4595 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004596 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004597 /*
4598 * something[idx]
4599 *
4600 * Get the (first) variable from inside the [].
4601 */
4602 *arg = skipwhite(*arg + 1);
4603 if (**arg == ':')
4604 empty1 = TRUE;
4605 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4606 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004607 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004608 {
4609 /* not a number or string */
4610 clear_tv(&var1);
4611 return FAIL;
4612 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004613
4614 /*
4615 * Get the second variable from inside the [:].
4616 */
4617 if (**arg == ':')
4618 {
4619 range = TRUE;
4620 *arg = skipwhite(*arg + 1);
4621 if (**arg == ']')
4622 empty2 = TRUE;
4623 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4624 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004625 if (!empty1)
4626 clear_tv(&var1);
4627 return FAIL;
4628 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004629 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004630 {
4631 /* not a number or string */
4632 if (!empty1)
4633 clear_tv(&var1);
4634 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004635 return FAIL;
4636 }
4637 }
4638
4639 /* Check for the ']'. */
4640 if (**arg != ']')
4641 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004642 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004643 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004644 clear_tv(&var1);
4645 if (range)
4646 clear_tv(&var2);
4647 return FAIL;
4648 }
4649 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004650 }
4651
4652 if (evaluate)
4653 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004654 n1 = 0;
4655 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004656 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004657 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004658 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004659 }
4660 if (range)
4661 {
4662 if (empty2)
4663 n2 = -1;
4664 else
4665 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004666 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004667 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004668 }
4669 }
4670
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004671 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004672 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004673 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004674 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004675 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004676 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004677 case VAR_SPECIAL:
4678 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004679 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004680 break; /* not evaluating, skipping over subscript */
4681
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004682 case VAR_NUMBER:
4683 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004684 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004685 len = (long)STRLEN(s);
4686 if (range)
4687 {
4688 /* The resulting variable is a substring. If the indexes
4689 * are out of range the result is empty. */
4690 if (n1 < 0)
4691 {
4692 n1 = len + n1;
4693 if (n1 < 0)
4694 n1 = 0;
4695 }
4696 if (n2 < 0)
4697 n2 = len + n2;
4698 else if (n2 >= len)
4699 n2 = len;
4700 if (n1 >= len || n2 < 0 || n1 > n2)
4701 s = NULL;
4702 else
4703 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4704 }
4705 else
4706 {
4707 /* The resulting variable is a string of a single
4708 * character. If the index is too big or negative the
4709 * result is empty. */
4710 if (n1 >= len || n1 < 0)
4711 s = NULL;
4712 else
4713 s = vim_strnsave(s + n1, 1);
4714 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004715 clear_tv(rettv);
4716 rettv->v_type = VAR_STRING;
4717 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004718 break;
4719
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004720 case VAR_BLOB:
4721 len = blob_len(rettv->vval.v_blob);
4722 if (range)
4723 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01004724 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004725 // are out of range the result is empty.
4726 if (n1 < 0)
4727 {
4728 n1 = len + n1;
4729 if (n1 < 0)
4730 n1 = 0;
4731 }
4732 if (n2 < 0)
4733 n2 = len + n2;
4734 else if (n2 >= len)
4735 n2 = len - 1;
4736 if (n1 >= len || n2 < 0 || n1 > n2)
4737 {
4738 clear_tv(rettv);
4739 rettv->v_type = VAR_BLOB;
4740 rettv->vval.v_blob = NULL;
4741 }
4742 else
4743 {
4744 blob_T *blob = blob_alloc();
4745
4746 if (blob != NULL)
4747 {
4748 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
4749 {
4750 blob_free(blob);
4751 return FAIL;
4752 }
4753 blob->bv_ga.ga_len = n2 - n1 + 1;
4754 for (i = n1; i <= n2; i++)
4755 blob_set(blob, i - n1,
4756 blob_get(rettv->vval.v_blob, i));
4757
4758 clear_tv(rettv);
4759 rettv_blob_set(rettv, blob);
4760 }
4761 }
4762 }
4763 else
4764 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01004765 // The resulting variable is a byte value.
4766 // If the index is too big or negative that is an error.
4767 if (n1 < 0)
4768 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004769 if (n1 < len && n1 >= 0)
4770 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01004771 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004772
4773 clear_tv(rettv);
4774 rettv->v_type = VAR_NUMBER;
4775 rettv->vval.v_number = v;
4776 }
4777 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004778 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004779 }
4780 break;
4781
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004782 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004783 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004784 if (n1 < 0)
4785 n1 = len + n1;
4786 if (!empty1 && (n1 < 0 || n1 >= len))
4787 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004788 /* For a range we allow invalid values and return an empty
4789 * list. A list index out of range is an error. */
4790 if (!range)
4791 {
4792 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004793 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004794 return FAIL;
4795 }
4796 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004797 }
4798 if (range)
4799 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004800 list_T *l;
4801 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004802
4803 if (n2 < 0)
4804 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004805 else if (n2 >= len)
4806 n2 = len - 1;
4807 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004808 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004809 l = list_alloc();
4810 if (l == NULL)
4811 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004812 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004813 n1 <= n2; ++n1)
4814 {
4815 if (list_append_tv(l, &item->li_tv) == FAIL)
4816 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004817 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004818 return FAIL;
4819 }
4820 item = item->li_next;
4821 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004822 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004823 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004824 }
4825 else
4826 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004827 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004828 clear_tv(rettv);
4829 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004830 }
4831 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004832
4833 case VAR_DICT:
4834 if (range)
4835 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004836 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004837 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004838 if (len == -1)
4839 clear_tv(&var1);
4840 return FAIL;
4841 }
4842 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004843 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004844
4845 if (len == -1)
4846 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004847 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02004848 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004849 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004850 clear_tv(&var1);
4851 return FAIL;
4852 }
4853 }
4854
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004855 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004856
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004857 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004858 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004859 if (len == -1)
4860 clear_tv(&var1);
4861 if (item == NULL)
4862 return FAIL;
4863
4864 copy_tv(&item->di_tv, &var1);
4865 clear_tv(rettv);
4866 *rettv = var1;
4867 }
4868 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004869 }
4870 }
4871
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004872 return OK;
4873}
4874
4875/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 * Get an option value.
4877 * "arg" points to the '&' or '+' before the option name.
4878 * "arg" is advanced to character after the option name.
4879 * Return OK or FAIL.
4880 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004881 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004882get_option_tv(
4883 char_u **arg,
4884 typval_T *rettv, /* when NULL, only check if option exists */
4885 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886{
4887 char_u *option_end;
4888 long numval;
4889 char_u *stringval;
4890 int opt_type;
4891 int c;
4892 int working = (**arg == '+'); /* has("+option") */
4893 int ret = OK;
4894 int opt_flags;
4895
4896 /*
4897 * Isolate the option name and find its value.
4898 */
4899 option_end = find_option_end(arg, &opt_flags);
4900 if (option_end == NULL)
4901 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004902 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004903 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 return FAIL;
4905 }
4906
4907 if (!evaluate)
4908 {
4909 *arg = option_end;
4910 return OK;
4911 }
4912
4913 c = *option_end;
4914 *option_end = NUL;
4915 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004916 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917
4918 if (opt_type == -3) /* invalid name */
4919 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004920 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004921 semsg(_("E113: Unknown option: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 ret = FAIL;
4923 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004924 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 {
4926 if (opt_type == -2) /* hidden string option */
4927 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004928 rettv->v_type = VAR_STRING;
4929 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 }
4931 else if (opt_type == -1) /* hidden number option */
4932 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004933 rettv->v_type = VAR_NUMBER;
4934 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 }
4936 else if (opt_type == 1) /* number option */
4937 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004938 rettv->v_type = VAR_NUMBER;
4939 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 }
4941 else /* string option */
4942 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004943 rettv->v_type = VAR_STRING;
4944 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 }
4946 }
4947 else if (working && (opt_type == -2 || opt_type == -1))
4948 ret = FAIL;
4949
4950 *option_end = c; /* put back for error messages */
4951 *arg = option_end;
4952
4953 return ret;
4954}
4955
4956/*
4957 * Allocate a variable for a string constant.
4958 * Return OK or FAIL.
4959 */
4960 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004961get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962{
4963 char_u *p;
4964 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 int extra = 0;
4966
4967 /*
4968 * Find the end of the string, skipping backslashed characters.
4969 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004970 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 {
4972 if (*p == '\\' && p[1] != NUL)
4973 {
4974 ++p;
4975 /* A "\<x>" form occupies at least 4 characters, and produces up
4976 * to 6 characters: reserve space for 2 extra */
4977 if (*p == '<')
4978 extra += 2;
4979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 }
4981
4982 if (*p != '"')
4983 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004984 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 return FAIL;
4986 }
4987
4988 /* If only parsing, set *arg and return here */
4989 if (!evaluate)
4990 {
4991 *arg = p + 1;
4992 return OK;
4993 }
4994
4995 /*
4996 * Copy the string into allocated memory, handling backslashed
4997 * characters.
4998 */
4999 name = alloc((unsigned)(p - *arg + extra));
5000 if (name == NULL)
5001 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005002 rettv->v_type = VAR_STRING;
5003 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004
Bram Moolenaar8c711452005-01-14 21:53:12 +00005005 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 {
5007 if (*p == '\\')
5008 {
5009 switch (*++p)
5010 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005011 case 'b': *name++ = BS; ++p; break;
5012 case 'e': *name++ = ESC; ++p; break;
5013 case 'f': *name++ = FF; ++p; break;
5014 case 'n': *name++ = NL; ++p; break;
5015 case 'r': *name++ = CAR; ++p; break;
5016 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017
5018 case 'X': /* hex: "\x1", "\x12" */
5019 case 'x':
5020 case 'u': /* Unicode: "\u0023" */
5021 case 'U':
5022 if (vim_isxdigit(p[1]))
5023 {
5024 int n, nr;
5025 int c = toupper(*p);
5026
5027 if (c == 'X')
5028 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005029 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005031 else
5032 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 nr = 0;
5034 while (--n >= 0 && vim_isxdigit(p[1]))
5035 {
5036 ++p;
5037 nr = (nr << 4) + hex2nr(*p);
5038 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005039 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 /* For "\u" store the number according to
5041 * 'encoding'. */
5042 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005043 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005045 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 break;
5048
5049 /* octal: "\1", "\12", "\123" */
5050 case '0':
5051 case '1':
5052 case '2':
5053 case '3':
5054 case '4':
5055 case '5':
5056 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005057 case '7': *name = *p++ - '0';
5058 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005060 *name = (*name << 3) + *p++ - '0';
5061 if (*p >= '0' && *p <= '7')
5062 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005064 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 break;
5066
5067 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005068 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 if (extra != 0)
5070 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005071 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 break;
5073 }
5074 /* FALLTHROUGH */
5075
Bram Moolenaar8c711452005-01-14 21:53:12 +00005076 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 break;
5078 }
5079 }
5080 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005081 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005084 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02005085 if (*p != NUL) /* just in case */
5086 ++p;
5087 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 return OK;
5090}
5091
5092/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005093 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 * Return OK or FAIL.
5095 */
5096 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005097get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098{
5099 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005100 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005101 int reduce = 0;
5102
5103 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005104 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005105 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005106 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005107 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005108 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005109 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005110 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005111 break;
5112 ++reduce;
5113 ++p;
5114 }
5115 }
5116
Bram Moolenaar8c711452005-01-14 21:53:12 +00005117 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005118 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005119 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005120 return FAIL;
5121 }
5122
Bram Moolenaar8c711452005-01-14 21:53:12 +00005123 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005124 if (!evaluate)
5125 {
5126 *arg = p + 1;
5127 return OK;
5128 }
5129
5130 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005131 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005132 */
5133 str = alloc((unsigned)((p - *arg) - reduce));
5134 if (str == NULL)
5135 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005136 rettv->v_type = VAR_STRING;
5137 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005138
Bram Moolenaar8c711452005-01-14 21:53:12 +00005139 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005140 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005141 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005142 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005143 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005144 break;
5145 ++p;
5146 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005147 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005148 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005149 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005150 *arg = p + 1;
5151
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005152 return OK;
5153}
5154
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005155/*
5156 * Return the function name of the partial.
5157 */
5158 char_u *
5159partial_name(partial_T *pt)
5160{
5161 if (pt->pt_name != NULL)
5162 return pt->pt_name;
5163 return pt->pt_func->uf_name;
5164}
5165
Bram Moolenaarddecc252016-04-06 22:59:37 +02005166 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005167partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005168{
5169 int i;
5170
5171 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005172 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005173 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005174 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005175 if (pt->pt_name != NULL)
5176 {
5177 func_unref(pt->pt_name);
5178 vim_free(pt->pt_name);
5179 }
5180 else
5181 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005182 vim_free(pt);
5183}
5184
5185/*
5186 * Unreference a closure: decrement the reference count and free it when it
5187 * becomes zero.
5188 */
5189 void
5190partial_unref(partial_T *pt)
5191{
5192 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005193 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005194}
5195
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005196static int tv_equal_recurse_limit;
5197
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005198 static int
5199func_equal(
5200 typval_T *tv1,
5201 typval_T *tv2,
5202 int ic) /* ignore case */
5203{
5204 char_u *s1, *s2;
5205 dict_T *d1, *d2;
5206 int a1, a2;
5207 int i;
5208
5209 /* empty and NULL function name considered the same */
5210 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005211 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005212 if (s1 != NULL && *s1 == NUL)
5213 s1 = NULL;
5214 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005215 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005216 if (s2 != NULL && *s2 == NUL)
5217 s2 = NULL;
5218 if (s1 == NULL || s2 == NULL)
5219 {
5220 if (s1 != s2)
5221 return FALSE;
5222 }
5223 else if (STRCMP(s1, s2) != 0)
5224 return FALSE;
5225
5226 /* empty dict and NULL dict is different */
5227 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5228 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5229 if (d1 == NULL || d2 == NULL)
5230 {
5231 if (d1 != d2)
5232 return FALSE;
5233 }
5234 else if (!dict_equal(d1, d2, ic, TRUE))
5235 return FALSE;
5236
5237 /* empty list and no list considered the same */
5238 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5239 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5240 if (a1 != a2)
5241 return FALSE;
5242 for (i = 0; i < a1; ++i)
5243 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5244 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5245 return FALSE;
5246
5247 return TRUE;
5248}
5249
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005250/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005251 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005252 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005253 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005254 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005255 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005256tv_equal(
5257 typval_T *tv1,
5258 typval_T *tv2,
5259 int ic, /* ignore case */
5260 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005261{
5262 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005263 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005264 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005265 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005266
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005267 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005268 * recursiveness to a limit. We guess they are equal then.
5269 * A fixed limit has the problem of still taking an awful long time.
5270 * Reduce the limit every time running into it. That should work fine for
5271 * deeply linked structures that are not recursively linked and catch
5272 * recursiveness quickly. */
5273 if (!recursive)
5274 tv_equal_recurse_limit = 1000;
5275 if (recursive_cnt >= tv_equal_recurse_limit)
5276 {
5277 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005278 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005279 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005280
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005281 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5282 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005283 if ((tv1->v_type == VAR_FUNC
5284 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5285 && (tv2->v_type == VAR_FUNC
5286 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5287 {
5288 ++recursive_cnt;
5289 r = func_equal(tv1, tv2, ic);
5290 --recursive_cnt;
5291 return r;
5292 }
5293
5294 if (tv1->v_type != tv2->v_type)
5295 return FALSE;
5296
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005297 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005298 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005299 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005300 ++recursive_cnt;
5301 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5302 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005303 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005304
5305 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005306 ++recursive_cnt;
5307 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5308 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005309 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005310
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005311 case VAR_BLOB:
5312 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
5313
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005314 case VAR_NUMBER:
5315 return tv1->vval.v_number == tv2->vval.v_number;
5316
5317 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005318 s1 = tv_get_string_buf(tv1, buf1);
5319 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005320 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005321
5322 case VAR_SPECIAL:
5323 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005324
5325 case VAR_FLOAT:
5326#ifdef FEAT_FLOAT
5327 return tv1->vval.v_float == tv2->vval.v_float;
5328#endif
5329 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005330#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005331 return tv1->vval.v_job == tv2->vval.v_job;
5332#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005333 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005334#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005335 return tv1->vval.v_channel == tv2->vval.v_channel;
5336#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005337 case VAR_FUNC:
5338 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005339 case VAR_UNKNOWN:
5340 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005341 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005342
Bram Moolenaara03f2332016-02-06 18:09:59 +01005343 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5344 * does not equal anything, not even itself. */
5345 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005346}
5347
5348/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005349 * Return the next (unique) copy ID.
5350 * Used for serializing nested structures.
5351 */
5352 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005353get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005354{
5355 current_copyID += COPYID_INC;
5356 return current_copyID;
5357}
5358
5359/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005360 * Garbage collection for lists and dictionaries.
5361 *
5362 * We use reference counts to be able to free most items right away when they
5363 * are no longer used. But for composite items it's possible that it becomes
5364 * unused while the reference count is > 0: When there is a recursive
5365 * reference. Example:
5366 * :let l = [1, 2, 3]
5367 * :let d = {9: l}
5368 * :let l[1] = d
5369 *
5370 * Since this is quite unusual we handle this with garbage collection: every
5371 * once in a while find out which lists and dicts are not referenced from any
5372 * variable.
5373 *
5374 * Here is a good reference text about garbage collection (refers to Python
5375 * but it applies to all reference-counting mechanisms):
5376 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005377 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005378
5379/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005380 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005381 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005382 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005383 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005384 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005385garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005386{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005387 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005388 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005389 buf_T *buf;
5390 win_T *wp;
5391 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005392 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005393 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005394
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005395 if (!testing)
5396 {
5397 /* Only do this once. */
5398 want_garbage_collect = FALSE;
5399 may_garbage_collect = FALSE;
5400 garbage_collect_at_exit = FALSE;
5401 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005402
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005403 /* We advance by two because we add one for items referenced through
5404 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005405 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005406
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005407 /*
5408 * 1. Go through all accessible variables and mark all lists and dicts
5409 * with copyID.
5410 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005411
5412 /* Don't free variables in the previous_funccal list unless they are only
5413 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005414 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005415 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005416
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005417 /* script-local variables */
5418 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005419 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005420
5421 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005422 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005423 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5424 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005425
5426 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005427 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005428 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5429 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005430 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005431 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5432 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005433
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005434 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005435 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005436 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5437 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005438 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005439 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005440
5441 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005442 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005443
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005444 /* named functions (matters for closures) */
5445 abort = abort || set_ref_in_functions(copyID);
5446
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005447 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005448 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005449
Bram Moolenaard812df62008-11-09 12:46:09 +00005450 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005451 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005452
Bram Moolenaar1dced572012-04-05 16:54:08 +02005453#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005454 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005455#endif
5456
Bram Moolenaardb913952012-06-29 12:54:53 +02005457#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005458 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005459#endif
5460
5461#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005462 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005463#endif
5464
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005465#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005466 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005467 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005468#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005469#ifdef FEAT_NETBEANS_INTG
5470 abort = abort || set_ref_in_nb_channel(copyID);
5471#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005472
Bram Moolenaare3188e22016-05-31 21:13:04 +02005473#ifdef FEAT_TIMERS
5474 abort = abort || set_ref_in_timer(copyID);
5475#endif
5476
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005477#ifdef FEAT_QUICKFIX
5478 abort = abort || set_ref_in_quickfix(copyID);
5479#endif
5480
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005481#ifdef FEAT_TERMINAL
5482 abort = abort || set_ref_in_term(copyID);
5483#endif
5484
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005485 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005486 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005487 /*
5488 * 2. Free lists and dictionaries that are not referenced.
5489 */
5490 did_free = free_unref_items(copyID);
5491
5492 /*
5493 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005494 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005495 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005496 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005497 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005498 else if (p_verbose > 0)
5499 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005500 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005501 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005502
5503 return did_free;
5504}
5505
5506/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005507 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005508 */
5509 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005510free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005511{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005512 int did_free = FALSE;
5513
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005514 /* Let all "free" functions know that we are here. This means no
5515 * dictionaries, lists, channels or jobs are to be freed, because we will
5516 * do that here. */
5517 in_free_unref_items = TRUE;
5518
5519 /*
5520 * PASS 1: free the contents of the items. We don't free the items
5521 * themselves yet, so that it is possible to decrement refcount counters
5522 */
5523
Bram Moolenaarcd524592016-07-17 14:57:05 +02005524 /* Go through the list of dicts and free items without the copyID. */
5525 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005526
Bram Moolenaarda861d62016-07-17 15:46:27 +02005527 /* Go through the list of lists and free items without the copyID. */
5528 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005529
5530#ifdef FEAT_JOB_CHANNEL
5531 /* Go through the list of jobs and free items without the copyID. This
5532 * must happen before doing channels, because jobs refer to channels, but
5533 * the reference from the channel to the job isn't tracked. */
5534 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5535
5536 /* Go through the list of channels and free items without the copyID. */
5537 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5538#endif
5539
5540 /*
5541 * PASS 2: free the items themselves.
5542 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005543 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005544 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005545
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005546#ifdef FEAT_JOB_CHANNEL
5547 /* Go through the list of jobs and free items without the copyID. This
5548 * must happen before doing channels, because jobs refer to channels, but
5549 * the reference from the channel to the job isn't tracked. */
5550 free_unused_jobs(copyID, COPYID_MASK);
5551
5552 /* Go through the list of channels and free items without the copyID. */
5553 free_unused_channels(copyID, COPYID_MASK);
5554#endif
5555
5556 in_free_unref_items = FALSE;
5557
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005558 return did_free;
5559}
5560
5561/*
5562 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005563 * "list_stack" is used to add lists to be marked. Can be NULL.
5564 *
5565 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005566 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005567 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005568set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005569{
5570 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005571 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005572 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005573 hashtab_T *cur_ht;
5574 ht_stack_T *ht_stack = NULL;
5575 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005576
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005577 cur_ht = ht;
5578 for (;;)
5579 {
5580 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005581 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005582 /* Mark each item in the hashtab. If the item contains a hashtab
5583 * it is added to ht_stack, if it contains a list it is added to
5584 * list_stack. */
5585 todo = (int)cur_ht->ht_used;
5586 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5587 if (!HASHITEM_EMPTY(hi))
5588 {
5589 --todo;
5590 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5591 &ht_stack, list_stack);
5592 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005593 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005594
5595 if (ht_stack == NULL)
5596 break;
5597
5598 /* take an item from the stack */
5599 cur_ht = ht_stack->ht;
5600 tempitem = ht_stack;
5601 ht_stack = ht_stack->prev;
5602 free(tempitem);
5603 }
5604
5605 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005606}
5607
5608/*
5609 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005610 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5611 *
5612 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005613 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005614 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005615set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005616{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005617 listitem_T *li;
5618 int abort = FALSE;
5619 list_T *cur_l;
5620 list_stack_T *list_stack = NULL;
5621 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005622
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005623 cur_l = l;
5624 for (;;)
5625 {
5626 if (!abort)
5627 /* Mark each item in the list. If the item contains a hashtab
5628 * it is added to ht_stack, if it contains a list it is added to
5629 * list_stack. */
5630 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5631 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5632 ht_stack, &list_stack);
5633 if (list_stack == NULL)
5634 break;
5635
5636 /* take an item from the stack */
5637 cur_l = list_stack->list;
5638 tempitem = list_stack;
5639 list_stack = list_stack->prev;
5640 free(tempitem);
5641 }
5642
5643 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005644}
5645
5646/*
5647 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005648 * "list_stack" is used to add lists to be marked. Can be NULL.
5649 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5650 *
5651 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005652 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005653 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005654set_ref_in_item(
5655 typval_T *tv,
5656 int copyID,
5657 ht_stack_T **ht_stack,
5658 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005659{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005660 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005661
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005662 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005663 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005664 dict_T *dd = tv->vval.v_dict;
5665
Bram Moolenaara03f2332016-02-06 18:09:59 +01005666 if (dd != NULL && dd->dv_copyID != copyID)
5667 {
5668 /* Didn't see this dict yet. */
5669 dd->dv_copyID = copyID;
5670 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005671 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005672 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5673 }
5674 else
5675 {
5676 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5677 if (newitem == NULL)
5678 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005679 else
5680 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005681 newitem->ht = &dd->dv_hashtab;
5682 newitem->prev = *ht_stack;
5683 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005684 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005685 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005686 }
5687 }
5688 else if (tv->v_type == VAR_LIST)
5689 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005690 list_T *ll = tv->vval.v_list;
5691
Bram Moolenaara03f2332016-02-06 18:09:59 +01005692 if (ll != NULL && ll->lv_copyID != copyID)
5693 {
5694 /* Didn't see this list yet. */
5695 ll->lv_copyID = copyID;
5696 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005697 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005698 abort = set_ref_in_list(ll, copyID, ht_stack);
5699 }
5700 else
5701 {
5702 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005703 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005704 if (newitem == NULL)
5705 abort = TRUE;
5706 else
5707 {
5708 newitem->list = ll;
5709 newitem->prev = *list_stack;
5710 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005711 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005712 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005713 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005714 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005715 else if (tv->v_type == VAR_FUNC)
5716 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005717 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005718 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005719 else if (tv->v_type == VAR_PARTIAL)
5720 {
5721 partial_T *pt = tv->vval.v_partial;
5722 int i;
5723
5724 /* A partial does not have a copyID, because it cannot contain itself.
5725 */
5726 if (pt != NULL)
5727 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005728 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005729
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005730 if (pt->pt_dict != NULL)
5731 {
5732 typval_T dtv;
5733
5734 dtv.v_type = VAR_DICT;
5735 dtv.vval.v_dict = pt->pt_dict;
5736 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5737 }
5738
5739 for (i = 0; i < pt->pt_argc; ++i)
5740 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5741 ht_stack, list_stack);
5742 }
5743 }
5744#ifdef FEAT_JOB_CHANNEL
5745 else if (tv->v_type == VAR_JOB)
5746 {
5747 job_T *job = tv->vval.v_job;
5748 typval_T dtv;
5749
5750 if (job != NULL && job->jv_copyID != copyID)
5751 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005752 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005753 if (job->jv_channel != NULL)
5754 {
5755 dtv.v_type = VAR_CHANNEL;
5756 dtv.vval.v_channel = job->jv_channel;
5757 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5758 }
5759 if (job->jv_exit_partial != NULL)
5760 {
5761 dtv.v_type = VAR_PARTIAL;
5762 dtv.vval.v_partial = job->jv_exit_partial;
5763 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5764 }
5765 }
5766 }
5767 else if (tv->v_type == VAR_CHANNEL)
5768 {
5769 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005770 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005771 typval_T dtv;
5772 jsonq_T *jq;
5773 cbq_T *cq;
5774
5775 if (ch != NULL && ch->ch_copyID != copyID)
5776 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005777 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005778 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005779 {
5780 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5781 jq = jq->jq_next)
5782 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5783 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5784 cq = cq->cq_next)
5785 if (cq->cq_partial != NULL)
5786 {
5787 dtv.v_type = VAR_PARTIAL;
5788 dtv.vval.v_partial = cq->cq_partial;
5789 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5790 }
5791 if (ch->ch_part[part].ch_partial != NULL)
5792 {
5793 dtv.v_type = VAR_PARTIAL;
5794 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5795 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5796 }
5797 }
5798 if (ch->ch_partial != NULL)
5799 {
5800 dtv.v_type = VAR_PARTIAL;
5801 dtv.vval.v_partial = ch->ch_partial;
5802 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5803 }
5804 if (ch->ch_close_partial != NULL)
5805 {
5806 dtv.v_type = VAR_PARTIAL;
5807 dtv.vval.v_partial = ch->ch_close_partial;
5808 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5809 }
5810 }
5811 }
5812#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005813 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005814}
5815
Bram Moolenaar17a13432016-01-24 14:22:10 +01005816 static char *
5817get_var_special_name(int nr)
5818{
5819 switch (nr)
5820 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005821 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005822 case VVAL_TRUE: return "v:true";
5823 case VVAL_NONE: return "v:none";
5824 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005825 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01005826 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01005827 return "42";
5828}
5829
Bram Moolenaar8c711452005-01-14 21:53:12 +00005830/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005831 * Return a string with the string representation of a variable.
5832 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005833 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005834 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005835 * When both "echo_style" and "composite_val" are FALSE, put quotes around
5836 * stings as "string()", otherwise does not put quotes around strings, as
5837 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005838 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5839 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005840 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005841 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005842 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005843echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005844 typval_T *tv,
5845 char_u **tofree,
5846 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005847 int copyID,
5848 int echo_style,
5849 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005850 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005851{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005852 static int recurse = 0;
5853 char_u *r = NULL;
5854
Bram Moolenaar33570922005-01-25 22:26:29 +00005855 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005856 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005857 if (!did_echo_string_emsg)
5858 {
5859 /* Only give this message once for a recursive call to avoid
5860 * flooding the user with errors. And stop iterating over lists
5861 * and dicts. */
5862 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005863 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005864 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005865 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005866 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005867 }
5868 ++recurse;
5869
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005870 switch (tv->v_type)
5871 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005872 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005873 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005874 {
5875 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005876 r = tv->vval.v_string;
5877 if (r == NULL)
5878 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005879 }
5880 else
5881 {
5882 *tofree = string_quote(tv->vval.v_string, FALSE);
5883 r = *tofree;
5884 }
5885 break;
5886
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005887 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005888 if (echo_style)
5889 {
5890 *tofree = NULL;
5891 r = tv->vval.v_string;
5892 }
5893 else
5894 {
5895 *tofree = string_quote(tv->vval.v_string, TRUE);
5896 r = *tofree;
5897 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005898 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005899
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005900 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005901 {
5902 partial_T *pt = tv->vval.v_partial;
5903 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005904 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005905 garray_T ga;
5906 int i;
5907 char_u *tf;
5908
5909 ga_init2(&ga, 1, 100);
5910 ga_concat(&ga, (char_u *)"function(");
5911 if (fname != NULL)
5912 {
5913 ga_concat(&ga, fname);
5914 vim_free(fname);
5915 }
5916 if (pt != NULL && pt->pt_argc > 0)
5917 {
5918 ga_concat(&ga, (char_u *)", [");
5919 for (i = 0; i < pt->pt_argc; ++i)
5920 {
5921 if (i > 0)
5922 ga_concat(&ga, (char_u *)", ");
5923 ga_concat(&ga,
5924 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5925 vim_free(tf);
5926 }
5927 ga_concat(&ga, (char_u *)"]");
5928 }
5929 if (pt != NULL && pt->pt_dict != NULL)
5930 {
5931 typval_T dtv;
5932
5933 ga_concat(&ga, (char_u *)", ");
5934 dtv.v_type = VAR_DICT;
5935 dtv.vval.v_dict = pt->pt_dict;
5936 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5937 vim_free(tf);
5938 }
5939 ga_concat(&ga, (char_u *)")");
5940
5941 *tofree = ga.ga_data;
5942 r = *tofree;
5943 break;
5944 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005945
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005946 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005947 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005948 break;
5949
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005950 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005951 if (tv->vval.v_list == NULL)
5952 {
5953 *tofree = NULL;
5954 r = NULL;
5955 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005956 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5957 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005958 {
5959 *tofree = NULL;
5960 r = (char_u *)"[...]";
5961 }
5962 else
5963 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005964 int old_copyID = tv->vval.v_list->lv_copyID;
5965
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005966 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005967 *tofree = list2string(tv, copyID, restore_copyID);
5968 if (restore_copyID)
5969 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005970 r = *tofree;
5971 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005972 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005973
Bram Moolenaar8c711452005-01-14 21:53:12 +00005974 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005975 if (tv->vval.v_dict == NULL)
5976 {
5977 *tofree = NULL;
5978 r = NULL;
5979 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005980 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5981 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005982 {
5983 *tofree = NULL;
5984 r = (char_u *)"{...}";
5985 }
5986 else
5987 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005988 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005989 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005990 *tofree = dict2string(tv, copyID, restore_copyID);
5991 if (restore_copyID)
5992 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005993 r = *tofree;
5994 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005995 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005996
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005997 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005998 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005999 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006000 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006001 break;
6002
Bram Moolenaar835dc632016-02-07 14:27:38 +01006003 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006004 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006005 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006006 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006007 if (composite_val)
6008 {
6009 *tofree = string_quote(r, FALSE);
6010 r = *tofree;
6011 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006013
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006014 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006015#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006016 *tofree = NULL;
6017 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6018 r = numbuf;
6019 break;
6020#endif
6021
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006022 case VAR_SPECIAL:
6023 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006024 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006025 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006026 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006027
Bram Moolenaar8502c702014-06-17 12:51:16 +02006028 if (--recurse == 0)
6029 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006030 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006031}
6032
6033/*
6034 * Return a string with the string representation of a variable.
6035 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6036 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006037 * Does not put quotes around strings, as ":echo" displays values.
6038 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6039 * May return NULL.
6040 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006041 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006042echo_string(
6043 typval_T *tv,
6044 char_u **tofree,
6045 char_u *numbuf,
6046 int copyID)
6047{
6048 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6049}
6050
6051/*
6052 * Return a string with the string representation of a variable.
6053 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6054 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006055 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006056 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006057 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02006058 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006059tv2string(
6060 typval_T *tv,
6061 char_u **tofree,
6062 char_u *numbuf,
6063 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006064{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006065 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006066}
6067
6068/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006069 * Return string "str" in ' quotes, doubling ' characters.
6070 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006071 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006072 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006073 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006074string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006075{
Bram Moolenaar33570922005-01-25 22:26:29 +00006076 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006077 char_u *p, *r, *s;
6078
Bram Moolenaar33570922005-01-25 22:26:29 +00006079 len = (function ? 13 : 3);
6080 if (str != NULL)
6081 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006082 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006083 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00006084 if (*p == '\'')
6085 ++len;
6086 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006087 s = r = alloc(len);
6088 if (r != NULL)
6089 {
6090 if (function)
6091 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006092 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006093 r += 10;
6094 }
6095 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006096 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006097 if (str != NULL)
6098 for (p = str; *p != NUL; )
6099 {
6100 if (*p == '\'')
6101 *r++ = '\'';
6102 MB_COPY_CHAR(p, r);
6103 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006104 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006105 if (function)
6106 *r++ = ')';
6107 *r++ = NUL;
6108 }
6109 return s;
6110}
6111
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006112#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006113/*
6114 * Convert the string "text" to a floating point number.
6115 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
6116 * this always uses a decimal point.
6117 * Returns the length of the text that was consumed.
6118 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006119 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006120string2float(
6121 char_u *text,
6122 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006123{
6124 char *s = (char *)text;
6125 float_T f;
6126
Bram Moolenaar62473612017-01-08 19:25:40 +01006127 /* MS-Windows does not deal with "inf" and "nan" properly. */
6128 if (STRNICMP(text, "inf", 3) == 0)
6129 {
6130 *value = INFINITY;
6131 return 3;
6132 }
6133 if (STRNICMP(text, "-inf", 3) == 0)
6134 {
6135 *value = -INFINITY;
6136 return 4;
6137 }
6138 if (STRNICMP(text, "nan", 3) == 0)
6139 {
6140 *value = NAN;
6141 return 3;
6142 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006143 f = strtod(s, &s);
6144 *value = f;
6145 return (int)((char_u *)s - text);
6146}
6147#endif
6148
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006149/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 * Get the value of an environment variable.
6151 * "arg" is pointing to the '$'. It is advanced to after the name.
6152 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006153 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 */
6155 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006156get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157{
6158 char_u *string = NULL;
6159 int len;
6160 int cc;
6161 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006162 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163
6164 ++*arg;
6165 name = *arg;
6166 len = get_env_len(arg);
6167 if (evaluate)
6168 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006169 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006170 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006171
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006172 cc = name[len];
6173 name[len] = NUL;
6174 /* first try vim_getenv(), fast for normal environment vars */
6175 string = vim_getenv(name, &mustfree);
6176 if (string != NULL && *string != NUL)
6177 {
6178 if (!mustfree)
6179 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006181 else
6182 {
6183 if (mustfree)
6184 vim_free(string);
6185
6186 /* next try expanding things like $VIM and ${HOME} */
6187 string = expand_env_save(name - 1);
6188 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01006189 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006190 }
6191 name[len] = cc;
6192
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006193 rettv->v_type = VAR_STRING;
6194 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 }
6196
6197 return OK;
6198}
6199
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006200
6201
6202/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006204 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006206 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006207var2fpos(
6208 typval_T *varp,
6209 int dollar_lnum, /* TRUE when $ is last line */
6210 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006212 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006214 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215
Bram Moolenaara5525202006-03-02 22:52:09 +00006216 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006217 if (varp->v_type == VAR_LIST)
6218 {
6219 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006220 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006221 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006222 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006223
6224 l = varp->vval.v_list;
6225 if (l == NULL)
6226 return NULL;
6227
6228 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006229 pos.lnum = list_find_nr(l, 0L, &error);
6230 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006231 return NULL; /* invalid line number */
6232
6233 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006234 pos.col = list_find_nr(l, 1L, &error);
6235 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006236 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006237 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006238
6239 /* We accept "$" for the column number: last column. */
6240 li = list_find(l, 1L);
6241 if (li != NULL && li->li_tv.v_type == VAR_STRING
6242 && li->li_tv.vval.v_string != NULL
6243 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6244 pos.col = len + 1;
6245
Bram Moolenaara5525202006-03-02 22:52:09 +00006246 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006247 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006248 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006249 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006250
Bram Moolenaara5525202006-03-02 22:52:09 +00006251 /* Get the virtual offset. Defaults to zero. */
6252 pos.coladd = list_find_nr(l, 2L, &error);
6253 if (error)
6254 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006255
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006256 return &pos;
6257 }
6258
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006259 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006260 if (name == NULL)
6261 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006262 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006264 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6265 {
6266 if (VIsual_active)
6267 return &VIsual;
6268 return &curwin->w_cursor;
6269 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006270 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006272 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6274 return NULL;
6275 return pp;
6276 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006277
Bram Moolenaara5525202006-03-02 22:52:09 +00006278 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006279
Bram Moolenaar477933c2007-07-17 14:32:23 +00006280 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006281 {
6282 pos.col = 0;
6283 if (name[1] == '0') /* "w0": first visible line */
6284 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006285 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006286 /* In silent Ex mode topline is zero, but that's not a valid line
6287 * number; use one instead. */
6288 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006289 return &pos;
6290 }
6291 else if (name[1] == '$') /* "w$": last visible line */
6292 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006293 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006294 /* In silent Ex mode botline is zero, return zero then. */
6295 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006296 return &pos;
6297 }
6298 }
6299 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006301 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 {
6303 pos.lnum = curbuf->b_ml.ml_line_count;
6304 pos.col = 0;
6305 }
6306 else
6307 {
6308 pos.lnum = curwin->w_cursor.lnum;
6309 pos.col = (colnr_T)STRLEN(ml_get_curline());
6310 }
6311 return &pos;
6312 }
6313 return NULL;
6314}
6315
6316/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006317 * Convert list in "arg" into a position and optional file number.
6318 * When "fnump" is NULL there is no file number, only 3 items.
6319 * Note that the column is passed on as-is, the caller may want to decrement
6320 * it to use 1 for the first column.
6321 * Return FAIL when conversion is not possible, doesn't check the position for
6322 * validity.
6323 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006324 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006325list2fpos(
6326 typval_T *arg,
6327 pos_T *posp,
6328 int *fnump,
6329 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006330{
6331 list_T *l = arg->vval.v_list;
6332 long i = 0;
6333 long n;
6334
Bram Moolenaar493c1782014-05-28 14:34:46 +02006335 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6336 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006337 if (arg->v_type != VAR_LIST
6338 || l == NULL
6339 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006340 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006341 return FAIL;
6342
6343 if (fnump != NULL)
6344 {
6345 n = list_find_nr(l, i++, NULL); /* fnum */
6346 if (n < 0)
6347 return FAIL;
6348 if (n == 0)
6349 n = curbuf->b_fnum; /* current buffer */
6350 *fnump = n;
6351 }
6352
6353 n = list_find_nr(l, i++, NULL); /* lnum */
6354 if (n < 0)
6355 return FAIL;
6356 posp->lnum = n;
6357
6358 n = list_find_nr(l, i++, NULL); /* col */
6359 if (n < 0)
6360 return FAIL;
6361 posp->col = n;
6362
Bram Moolenaar493c1782014-05-28 14:34:46 +02006363 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006364 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006365 posp->coladd = 0;
6366 else
6367 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006368
Bram Moolenaar493c1782014-05-28 14:34:46 +02006369 if (curswantp != NULL)
6370 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6371
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006372 return OK;
6373}
6374
6375/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 * Get the length of an environment variable name.
6377 * Advance "arg" to the first character after the name.
6378 * Return 0 for error.
6379 */
6380 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006381get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382{
6383 char_u *p;
6384 int len;
6385
6386 for (p = *arg; vim_isIDc(*p); ++p)
6387 ;
6388 if (p == *arg) /* no name found */
6389 return 0;
6390
6391 len = (int)(p - *arg);
6392 *arg = p;
6393 return len;
6394}
6395
6396/*
6397 * Get the length of the name of a function or internal variable.
6398 * "arg" is advanced to the first non-white character after the name.
6399 * Return 0 if something is wrong.
6400 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006401 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006402get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403{
6404 char_u *p;
6405 int len;
6406
6407 /* Find the end of the name. */
6408 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006409 {
6410 if (*p == ':')
6411 {
6412 /* "s:" is start of "s:var", but "n:" is not and can be used in
6413 * slice "[n:]". Also "xx:" is not a namespace. */
6414 len = (int)(p - *arg);
6415 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6416 || len > 1)
6417 break;
6418 }
6419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 if (p == *arg) /* no name found */
6421 return 0;
6422
6423 len = (int)(p - *arg);
6424 *arg = skipwhite(p);
6425
6426 return len;
6427}
6428
6429/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006430 * Get the length of the name of a variable or function.
6431 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006433 * Return -1 if curly braces expansion failed.
6434 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 * If the name contains 'magic' {}'s, expand them and return the
6436 * expanded name in an allocated string via 'alias' - caller must free.
6437 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006438 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006439get_name_len(
6440 char_u **arg,
6441 char_u **alias,
6442 int evaluate,
6443 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444{
6445 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 char_u *p;
6447 char_u *expr_start;
6448 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449
6450 *alias = NULL; /* default to no alias */
6451
6452 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6453 && (*arg)[2] == (int)KE_SNR)
6454 {
6455 /* hard coded <SNR>, already translated */
6456 *arg += 3;
6457 return get_id_len(arg) + 3;
6458 }
6459 len = eval_fname_script(*arg);
6460 if (len > 0)
6461 {
6462 /* literal "<SID>", "s:" or "<SNR>" */
6463 *arg += len;
6464 }
6465
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006467 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006469 p = find_name_end(*arg, &expr_start, &expr_end,
6470 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 if (expr_start != NULL)
6472 {
6473 char_u *temp_string;
6474
6475 if (!evaluate)
6476 {
6477 len += (int)(p - *arg);
6478 *arg = skipwhite(p);
6479 return len;
6480 }
6481
6482 /*
6483 * Include any <SID> etc in the expanded string:
6484 * Thus the -len here.
6485 */
6486 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6487 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006488 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 *alias = temp_string;
6490 *arg = skipwhite(p);
6491 return (int)STRLEN(temp_string);
6492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493
6494 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006495 // Only give an error when there is something, otherwise it will be
6496 // reported at a higher level.
6497 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006498 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499
6500 return len;
6501}
6502
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006503/*
6504 * Find the end of a variable or function name, taking care of magic braces.
6505 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6506 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006507 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006508 * Return a pointer to just after the name. Equal to "arg" if there is no
6509 * valid name.
6510 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006511 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006512find_name_end(
6513 char_u *arg,
6514 char_u **expr_start,
6515 char_u **expr_end,
6516 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006518 int mb_nest = 0;
6519 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006521 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006523 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006525 *expr_start = NULL;
6526 *expr_end = NULL;
6527 }
6528
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006529 /* Quick check for valid starting character. */
6530 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6531 return arg;
6532
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006533 for (p = arg; *p != NUL
6534 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006535 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006536 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006537 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006538 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006539 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006540 if (*p == '\'')
6541 {
6542 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006543 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006544 ;
6545 if (*p == NUL)
6546 break;
6547 }
6548 else if (*p == '"')
6549 {
6550 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006551 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006552 if (*p == '\\' && p[1] != NUL)
6553 ++p;
6554 if (*p == NUL)
6555 break;
6556 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006557 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6558 {
6559 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006560 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006561 len = (int)(p - arg);
6562 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006563 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006564 break;
6565 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006566
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006567 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006569 if (*p == '[')
6570 ++br_nest;
6571 else if (*p == ']')
6572 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006574
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006575 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006577 if (*p == '{')
6578 {
6579 mb_nest++;
6580 if (expr_start != NULL && *expr_start == NULL)
6581 *expr_start = p;
6582 }
6583 else if (*p == '}')
6584 {
6585 mb_nest--;
6586 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6587 *expr_end = p;
6588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 }
6591
6592 return p;
6593}
6594
6595/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006596 * Expands out the 'magic' {}'s in a variable/function name.
6597 * Note that this can call itself recursively, to deal with
6598 * constructs like foo{bar}{baz}{bam}
6599 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6600 * "in_start" ^
6601 * "expr_start" ^
6602 * "expr_end" ^
6603 * "in_end" ^
6604 *
6605 * Returns a new allocated string, which the caller must free.
6606 * Returns NULL for failure.
6607 */
6608 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006609make_expanded_name(
6610 char_u *in_start,
6611 char_u *expr_start,
6612 char_u *expr_end,
6613 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006614{
6615 char_u c1;
6616 char_u *retval = NULL;
6617 char_u *temp_result;
6618 char_u *nextcmd = NULL;
6619
6620 if (expr_end == NULL || in_end == NULL)
6621 return NULL;
6622 *expr_start = NUL;
6623 *expr_end = NUL;
6624 c1 = *in_end;
6625 *in_end = NUL;
6626
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006627 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006628 if (temp_result != NULL && nextcmd == NULL)
6629 {
6630 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6631 + (in_end - expr_end) + 1));
6632 if (retval != NULL)
6633 {
6634 STRCPY(retval, in_start);
6635 STRCAT(retval, temp_result);
6636 STRCAT(retval, expr_end + 1);
6637 }
6638 }
6639 vim_free(temp_result);
6640
6641 *in_end = c1; /* put char back for error messages */
6642 *expr_start = '{';
6643 *expr_end = '}';
6644
6645 if (retval != NULL)
6646 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006647 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006648 if (expr_start != NULL)
6649 {
6650 /* Further expansion! */
6651 temp_result = make_expanded_name(retval, expr_start,
6652 expr_end, temp_result);
6653 vim_free(retval);
6654 retval = temp_result;
6655 }
6656 }
6657
6658 return retval;
6659}
6660
6661/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006663 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006665 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006666eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006668 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6669}
6670
6671/*
6672 * Return TRUE if character "c" can be used as the first character in a
6673 * variable or function name (excluding '{' and '}').
6674 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006675 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006676eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006677{
6678 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679}
6680
6681/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 * Set number v: variable to "val".
6683 */
6684 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006685set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006687 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688}
6689
6690/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006691 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006693 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006694get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006696 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697}
6698
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006699/*
6700 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01006701 * If the String variable has never been set, return an empty string.
6702 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006703 */
6704 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006705get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006706{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006707 return tv_get_string(&vimvars[idx].vv_tv);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006708}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006709
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006711 * Get List v: variable value. Caller must take care of reference count when
6712 * needed.
6713 */
6714 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006715get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006716{
6717 return vimvars[idx].vv_list;
6718}
6719
6720/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006721 * Get Dict v: variable value. Caller must take care of reference count when
6722 * needed.
6723 */
6724 dict_T *
6725get_vim_var_dict(int idx)
6726{
6727 return vimvars[idx].vv_dict;
6728}
6729
6730/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006731 * Set v:char to character "c".
6732 */
6733 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006734set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006735{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006736 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006737
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006738 if (has_mbyte)
6739 buf[(*mb_char2bytes)(c, buf)] = NUL;
6740 else
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006741 {
6742 buf[0] = c;
6743 buf[1] = NUL;
6744 }
6745 set_vim_var_string(VV_CHAR, buf, -1);
6746}
6747
6748/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006749 * Set v:count to "count" and v:count1 to "count1".
6750 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 */
6752 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006753set_vcount(
6754 long count,
6755 long count1,
6756 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006758 if (set_prevcount)
6759 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006760 vimvars[VV_COUNT].vv_nr = count;
6761 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762}
6763
6764/*
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02006765 * Save variables that might be changed as a side effect. Used when executing
6766 * a timer callback.
6767 */
6768 void
6769save_vimvars(vimvars_save_T *vvsave)
6770{
6771 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
6772 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
6773 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
6774}
6775
6776/*
6777 * Restore variables saved by save_vimvars().
6778 */
6779 void
6780restore_vimvars(vimvars_save_T *vvsave)
6781{
6782 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
6783 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
6784 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
6785}
6786
6787/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 * Set string v: variable to a copy of "val".
6789 */
6790 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006791set_vim_var_string(
6792 int idx,
6793 char_u *val,
6794 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795{
Bram Moolenaara542c682016-01-31 16:28:04 +01006796 clear_tv(&vimvars[idx].vv_di.di_tv);
6797 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006799 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006801 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006803 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804}
6805
6806/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006807 * Set List v: variable to "val".
6808 */
6809 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006810set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006811{
Bram Moolenaara542c682016-01-31 16:28:04 +01006812 clear_tv(&vimvars[idx].vv_di.di_tv);
6813 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006814 vimvars[idx].vv_list = val;
6815 if (val != NULL)
6816 ++val->lv_refcount;
6817}
6818
6819/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006820 * Set Dictionary v: variable to "val".
6821 */
6822 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006823set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006824{
Bram Moolenaara542c682016-01-31 16:28:04 +01006825 clear_tv(&vimvars[idx].vv_di.di_tv);
6826 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006827 vimvars[idx].vv_dict = val;
6828 if (val != NULL)
6829 {
6830 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006831 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02006832 }
6833}
6834
6835/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 * Set v:register if needed.
6837 */
6838 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006839set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840{
6841 char_u regname;
6842
6843 if (c == 0 || c == ' ')
6844 regname = '"';
6845 else
6846 regname = c;
6847 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006848 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 set_vim_var_string(VV_REG, &regname, 1);
6850}
6851
6852/*
6853 * Get or set v:exception. If "oldval" == NULL, return the current value.
6854 * Otherwise, restore the value to "oldval" and return NULL.
6855 * Must always be called in pairs to save and restore v:exception! Does not
6856 * take care of memory allocations.
6857 */
6858 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006859v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860{
6861 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006862 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863
Bram Moolenaare9a41262005-01-15 22:18:47 +00006864 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 return NULL;
6866}
6867
6868/*
6869 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
6870 * Otherwise, restore the value to "oldval" and return NULL.
6871 * Must always be called in pairs to save and restore v:throwpoint! Does not
6872 * take care of memory allocations.
6873 */
6874 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006875v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876{
6877 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006878 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879
Bram Moolenaare9a41262005-01-15 22:18:47 +00006880 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 return NULL;
6882}
6883
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884/*
6885 * Set v:cmdarg.
6886 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6887 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6888 * Must always be called in pairs!
6889 */
6890 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006891set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892{
6893 char_u *oldval;
6894 char_u *newval;
6895 unsigned len;
6896
Bram Moolenaare9a41262005-01-15 22:18:47 +00006897 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006898 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006900 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006901 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006902 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 }
6904
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006905 if (eap->force_bin == FORCE_BIN)
6906 len = 6;
6907 else if (eap->force_bin == FORCE_NOBIN)
6908 len = 8;
6909 else
6910 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006911
6912 if (eap->read_edit)
6913 len += 7;
6914
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006915 if (eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006916 len += 10; /* " ++ff=unix" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006917 if (eap->force_enc != 0)
6918 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006919 if (eap->bad_char != 0)
6920 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006921
6922 newval = alloc(len + 1);
6923 if (newval == NULL)
6924 return NULL;
6925
6926 if (eap->force_bin == FORCE_BIN)
6927 sprintf((char *)newval, " ++bin");
6928 else if (eap->force_bin == FORCE_NOBIN)
6929 sprintf((char *)newval, " ++nobin");
6930 else
6931 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006932
6933 if (eap->read_edit)
6934 STRCAT(newval, " ++edit");
6935
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006936 if (eap->force_ff != 0)
6937 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006938 eap->force_ff == 'u' ? "unix"
6939 : eap->force_ff == 'd' ? "dos"
6940 : "mac");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006941 if (eap->force_enc != 0)
6942 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6943 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006944 if (eap->bad_char == BAD_KEEP)
6945 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6946 else if (eap->bad_char == BAD_DROP)
6947 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6948 else if (eap->bad_char != 0)
6949 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006950 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006951 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953
6954/*
6955 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006956 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006958 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006959get_var_tv(
6960 char_u *name,
6961 int len, /* length of "name" */
6962 typval_T *rettv, /* NULL when only checking existence */
6963 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
6964 int verbose, /* may give error message */
6965 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966{
6967 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006968 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006969 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971
6972 /* truncate the name, so that we can use strcmp() */
6973 cc = name[len];
6974 name[len] = NUL;
6975
6976 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 * Check for user-defined variables.
6978 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01006979 v = find_var(name, NULL, no_autoload);
6980 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006982 tv = &v->di_tv;
6983 if (dip != NULL)
6984 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 }
6986
Bram Moolenaare9a41262005-01-15 22:18:47 +00006987 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006989 if (rettv != NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006990 semsg(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 ret = FAIL;
6992 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006993 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006994 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995
6996 name[len] = cc;
6997
6998 return ret;
6999}
7000
7001/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007002 * Check if variable "name[len]" is a local variable or an argument.
7003 * If so, "*eval_lavars_used" is set to TRUE.
7004 */
7005 static void
7006check_vars(char_u *name, int len)
7007{
7008 int cc;
7009 char_u *varname;
7010 hashtab_T *ht;
7011
7012 if (eval_lavars_used == NULL)
7013 return;
7014
7015 /* truncate the name, so that we can use strcmp() */
7016 cc = name[len];
7017 name[len] = NUL;
7018
7019 ht = find_var_ht(name, &varname);
7020 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
7021 {
7022 if (find_var(name, NULL, TRUE) != NULL)
7023 *eval_lavars_used = TRUE;
7024 }
7025
7026 name[len] = cc;
7027}
7028
7029/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007030 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
7031 * Also handle function call with Funcref variable: func(expr)
7032 * Can all be combined: dict.func(expr)[idx]['func'](expr)
7033 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007034 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007035handle_subscript(
7036 char_u **arg,
7037 typval_T *rettv,
7038 int evaluate, /* do more than finding the end */
7039 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007040{
7041 int ret = OK;
7042 dict_T *selfdict = NULL;
7043 char_u *s;
7044 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007045 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007046
7047 while (ret == OK
7048 && (**arg == '['
7049 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007050 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7051 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01007052 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007053 {
7054 if (**arg == '(')
7055 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007056 partial_T *pt = NULL;
7057
Bram Moolenaard9fba312005-06-26 22:34:35 +00007058 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007059 if (evaluate)
7060 {
7061 functv = *rettv;
7062 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007063
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007064 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007065 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007066 {
7067 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007068 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007069 }
7070 else
7071 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007072 }
7073 else
7074 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007075 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00007076 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007077 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007078
7079 /* Clear the funcref afterwards, so that deleting it while
7080 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007081 if (evaluate)
7082 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007083
7084 /* Stop the expression evaluation when immediately aborting on
7085 * error, or when an interrupt occurred or an exception was thrown
7086 * but not caught. */
7087 if (aborting())
7088 {
7089 if (ret == OK)
7090 clear_tv(rettv);
7091 ret = FAIL;
7092 }
7093 dict_unref(selfdict);
7094 selfdict = NULL;
7095 }
7096 else /* **arg == '[' || **arg == '.' */
7097 {
7098 dict_unref(selfdict);
7099 if (rettv->v_type == VAR_DICT)
7100 {
7101 selfdict = rettv->vval.v_dict;
7102 if (selfdict != NULL)
7103 ++selfdict->dv_refcount;
7104 }
7105 else
7106 selfdict = NULL;
7107 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
7108 {
7109 clear_tv(rettv);
7110 ret = FAIL;
7111 }
7112 }
7113 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007114
Bram Moolenaar1d429612016-05-24 15:44:17 +02007115 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
7116 * Don't do this when "Func" is already a partial that was bound
7117 * explicitly (pt_auto is FALSE). */
7118 if (selfdict != NULL
7119 && (rettv->v_type == VAR_FUNC
7120 || (rettv->v_type == VAR_PARTIAL
7121 && (rettv->vval.v_partial->pt_auto
7122 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007123 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007124
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007125 dict_unref(selfdict);
7126 return ret;
7127}
7128
7129/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007130 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007131 * value).
7132 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01007133 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007134alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007135{
Bram Moolenaar33570922005-01-25 22:26:29 +00007136 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007137}
7138
7139/*
7140 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 * The string "s" must have been allocated, it is consumed.
7142 * Return NULL for out of memory, the variable otherwise.
7143 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007144 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007145alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146{
Bram Moolenaar33570922005-01-25 22:26:29 +00007147 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007149 rettv = alloc_tv();
7150 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007152 rettv->v_type = VAR_STRING;
7153 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 }
7155 else
7156 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007157 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158}
7159
7160/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007161 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007163 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007164free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165{
7166 if (varp != NULL)
7167 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007168 switch (varp->v_type)
7169 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007170 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007171 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007172 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007173 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007174 vim_free(varp->vval.v_string);
7175 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007176 case VAR_PARTIAL:
7177 partial_unref(varp->vval.v_partial);
7178 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007179 case VAR_BLOB:
7180 blob_unref(varp->vval.v_blob);
7181 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007182 case VAR_LIST:
7183 list_unref(varp->vval.v_list);
7184 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007185 case VAR_DICT:
7186 dict_unref(varp->vval.v_dict);
7187 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007188 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007189#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007190 job_unref(varp->vval.v_job);
7191 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007192#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007193 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007194#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007195 channel_unref(varp->vval.v_channel);
7196 break;
7197#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007198 case VAR_NUMBER:
7199 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007200 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007201 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007202 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 vim_free(varp);
7205 }
7206}
7207
7208/*
7209 * Free the memory for a variable value and set the value to NULL or 0.
7210 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007211 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007212clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213{
7214 if (varp != NULL)
7215 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007216 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007218 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007219 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007220 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007221 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007222 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007223 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007224 case VAR_PARTIAL:
7225 partial_unref(varp->vval.v_partial);
7226 varp->vval.v_partial = NULL;
7227 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007228 case VAR_BLOB:
7229 blob_unref(varp->vval.v_blob);
7230 varp->vval.v_blob = NULL;
7231 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007232 case VAR_LIST:
7233 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007234 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007235 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007236 case VAR_DICT:
7237 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007238 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007239 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007240 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007241 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007242 varp->vval.v_number = 0;
7243 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007244 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007245#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007246 varp->vval.v_float = 0.0;
7247 break;
7248#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007249 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007250#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007251 job_unref(varp->vval.v_job);
7252 varp->vval.v_job = NULL;
7253#endif
7254 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007255 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007256#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007257 channel_unref(varp->vval.v_channel);
7258 varp->vval.v_channel = NULL;
7259#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007260 case VAR_UNKNOWN:
7261 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007263 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 }
7265}
7266
7267/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007268 * Set the value of a variable to NULL without freeing items.
7269 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007270 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007271init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007272{
7273 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007274 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007275}
7276
7277/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 * Get the number value of a variable.
7279 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007280 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007281 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007282 * caller of incompatible types: it sets *denote to TRUE if "denote"
7283 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007285 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007286tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007288 int error = FALSE;
7289
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007290 return tv_get_number_chk(varp, &error); /* return 0L on error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007291}
7292
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007293 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007294tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007295{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007296 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007298 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007300 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007301 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007302 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007303#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007304 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007305 break;
7306#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007307 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007308 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007309 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007310 break;
7311 case VAR_STRING:
7312 if (varp->vval.v_string != NULL)
7313 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01007314 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007315 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007316 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007317 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007318 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007319 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007320 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007321 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007322 case VAR_SPECIAL:
7323 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7324 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007325 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007326#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007327 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007328 break;
7329#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007330 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007331#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007332 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007333 break;
7334#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007335 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007336 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007337 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007338 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007339 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007340 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007342 if (denote == NULL) /* useful for values that must be unsigned */
7343 n = -1;
7344 else
7345 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007346 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347}
7348
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007349#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007350 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007351tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007352{
7353 switch (varp->v_type)
7354 {
7355 case VAR_NUMBER:
7356 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007357 case VAR_FLOAT:
7358 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007359 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007360 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007361 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007362 break;
7363 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007364 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007365 break;
7366 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007367 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007368 break;
7369 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007370 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007371 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007372 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007373 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007374 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007375 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007376# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007377 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007378 break;
7379# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007380 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007381# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007382 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007383 break;
7384# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007385 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007386 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007387 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007388 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007389 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007390 break;
7391 }
7392 return 0;
7393}
7394#endif
7395
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 * Get the string value of a variable.
7398 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007399 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7400 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 * If the String variable has never been set, return an empty string.
7402 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007403 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007404 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007406 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007407tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408{
7409 static char_u mybuf[NUMBUFLEN];
7410
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007411 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412}
7413
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007414 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007415tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007417 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007418
7419 return res != NULL ? res : (char_u *)"";
7420}
7421
Bram Moolenaar7d647822014-04-05 21:28:56 +02007422/*
7423 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7424 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007425 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007426tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007427{
7428 static char_u mybuf[NUMBUFLEN];
7429
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007430 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007431}
7432
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007433 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007434tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007435{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007436 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007438 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007439 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007440 (long_long_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007441 return buf;
7442 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007443 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007444 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007445 break;
7446 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007447 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007448 break;
7449 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007450 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007451 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007452 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007453#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007454 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007455 break;
7456#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007457 case VAR_STRING:
7458 if (varp->vval.v_string != NULL)
7459 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007460 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007461 case VAR_SPECIAL:
7462 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7463 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007464 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007465 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007466 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007467 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007468#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007469 {
7470 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007471 char *status;
7472
7473 if (job == NULL)
7474 return (char_u *)"no process";
7475 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007476 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007477 : "run";
7478# ifdef UNIX
7479 vim_snprintf((char *)buf, NUMBUFLEN,
7480 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01007481# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007482 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007483 "process %ld %s",
7484 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007485 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007486# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007487 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007488 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7489# endif
7490 return buf;
7491 }
7492#endif
7493 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007494 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007495#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007496 {
7497 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007498 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007499
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007500 if (channel == NULL)
7501 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7502 else
7503 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007504 "channel %d %s", channel->ch_id, status);
7505 return buf;
7506 }
7507#endif
7508 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007509 case VAR_UNKNOWN:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007510 emsg(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007511 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007513 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514}
7515
7516/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007517 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
7518 * string() on Dict, List, etc.
7519 */
7520 char_u *
7521tv_stringify(typval_T *varp, char_u *buf)
7522{
7523 if (varp->v_type == VAR_LIST
7524 || varp->v_type == VAR_DICT
7525 || varp->v_type == VAR_FUNC
7526 || varp->v_type == VAR_PARTIAL
7527 || varp->v_type == VAR_FLOAT)
7528 {
7529 typval_T tmp;
7530
7531 f_string(varp, &tmp);
7532 tv_get_string_buf(&tmp, buf);
7533 clear_tv(varp);
7534 *varp = tmp;
7535 return tmp.vval.v_string;
7536 }
7537 return tv_get_string_buf(varp, buf);
7538}
7539
7540/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 * Find variable "name" in the list of variables.
7542 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007543 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007544 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007545 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007547 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007548find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007551 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007552 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553
Bram Moolenaara7043832005-01-21 11:56:39 +00007554 ht = find_var_ht(name, &varname);
7555 if (htp != NULL)
7556 *htp = ht;
7557 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007559 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7560 if (ret != NULL)
7561 return ret;
7562
7563 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007564 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565}
7566
7567/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007568 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007569 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007571 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007572find_var_in_ht(
7573 hashtab_T *ht,
7574 int htname,
7575 char_u *varname,
7576 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007577{
Bram Moolenaar33570922005-01-25 22:26:29 +00007578 hashitem_T *hi;
7579
7580 if (*varname == NUL)
7581 {
7582 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007583 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007584 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007585 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007586 case 'g': return &globvars_var;
7587 case 'v': return &vimvars_var;
7588 case 'b': return &curbuf->b_bufvar;
7589 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007590 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007591 case 'l': return get_funccal_local_var();
7592 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007593 }
7594 return NULL;
7595 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007596
7597 hi = hash_find(ht, varname);
7598 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007599 {
7600 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007601 * worked find the variable again. Don't auto-load a script if it was
7602 * loaded already, otherwise it would be loaded every time when
7603 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007604 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007605 {
7606 /* Note: script_autoload() may make "hi" invalid. It must either
7607 * be obtained again or not used. */
7608 if (!script_autoload(varname, FALSE) || aborting())
7609 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007610 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007611 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007612 if (HASHITEM_EMPTY(hi))
7613 return NULL;
7614 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007615 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007616}
7617
7618/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007619 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007620 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007621 * Set "varname" to the start of name without ':'.
7622 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007623 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007624find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007626 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007627 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007628
Bram Moolenaar73627d02015-08-11 15:46:09 +02007629 if (name[0] == NUL)
7630 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 if (name[1] != ':')
7632 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007633 /* The name must not start with a colon or #. */
7634 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 return NULL;
7636 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007637
7638 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007639 hi = hash_find(&compat_hashtab, name);
7640 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +00007641 return &compat_hashtab;
7642
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007643 ht = get_funccal_local_ht();
7644 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007645 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007646 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 }
7648 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007649 if (*name == 'g') /* global variable */
7650 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007651 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7652 */
7653 if (vim_strchr(name + 2, ':') != NULL
7654 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007655 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007657 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007659 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007660 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007661 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007662 if (*name == 'v') /* v: variable */
7663 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007664 if (*name == 'a') /* a: function argument */
7665 return get_funccal_args_ht();
7666 if (*name == 'l') /* l: local function variable */
7667 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 if (*name == 's' /* script variable */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007669 && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
7670 return &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 return NULL;
7672}
7673
7674/*
7675 * Get the string value of a (global/local) variable.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007676 * Note: see tv_get_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 * Returns NULL when it doesn't exist.
7678 */
7679 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007680get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681{
Bram Moolenaar33570922005-01-25 22:26:29 +00007682 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007684 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 if (v == NULL)
7686 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007687 return tv_get_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688}
7689
7690/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007691 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 * sourcing this script and when executing functions defined in the script.
7693 */
7694 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007695new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696{
Bram Moolenaara7043832005-01-21 11:56:39 +00007697 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007698 hashtab_T *ht;
7699 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007700
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7702 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007703 /* Re-allocating ga_data means that an ht_array pointing to
7704 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007705 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007706 for (i = 1; i <= ga_scripts.ga_len; ++i)
7707 {
7708 ht = &SCRIPT_VARS(i);
7709 if (ht->ht_mask == HT_INIT_SIZE - 1)
7710 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007711 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007712 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007713 }
7714
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 while (ga_scripts.ga_len < id)
7716 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007717 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007718 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007719 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 }
7722 }
7723}
7724
7725/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007726 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7727 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 */
7729 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007730init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731{
Bram Moolenaar33570922005-01-25 22:26:29 +00007732 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007733 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007734 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007735 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007736 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007737 dict_var->di_tv.vval.v_dict = dict;
7738 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007739 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007740 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7741 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742}
7743
7744/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007745 * Unreference a dictionary initialized by init_var_dict().
7746 */
7747 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007748unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007749{
7750 /* Now the dict needs to be freed if no one else is using it, go back to
7751 * normal reference counting. */
7752 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7753 dict_unref(dict);
7754}
7755
7756/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007758 * Frees all allocated variables and the value they contain.
7759 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 */
7761 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007762vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007763{
7764 vars_clear_ext(ht, TRUE);
7765}
7766
7767/*
7768 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7769 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007770 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007771vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772{
Bram Moolenaara7043832005-01-21 11:56:39 +00007773 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007774 hashitem_T *hi;
7775 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776
Bram Moolenaar33570922005-01-25 22:26:29 +00007777 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007778 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007779 for (hi = ht->ht_array; todo > 0; ++hi)
7780 {
7781 if (!HASHITEM_EMPTY(hi))
7782 {
7783 --todo;
7784
Bram Moolenaar33570922005-01-25 22:26:29 +00007785 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007786 * ht_array might change then. hash_clear() takes care of it
7787 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007788 v = HI2DI(hi);
7789 if (free_val)
7790 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007791 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007792 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007793 }
7794 }
7795 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007796 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797}
7798
Bram Moolenaara7043832005-01-21 11:56:39 +00007799/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007800 * Delete a variable from hashtab "ht" at item "hi".
7801 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007802 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007804delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805{
Bram Moolenaar33570922005-01-25 22:26:29 +00007806 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007807
7808 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007809 clear_tv(&di->di_tv);
7810 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811}
7812
7813/*
7814 * List the value of one internal variable.
7815 */
7816 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01007817list_one_var(dictitem_T *v, char *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007819 char_u *tofree;
7820 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007821 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007822
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007823 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007824 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007825 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007826 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827}
7828
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007830list_one_var_a(
Bram Moolenaar32526b32019-01-19 17:43:09 +01007831 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007832 char_u *name,
7833 int type,
7834 char_u *string,
7835 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836{
Bram Moolenaar31859182007-08-14 20:41:13 +00007837 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7838 msg_start();
7839 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 if (name != NULL) /* "a:" vars don't have a name stored */
Bram Moolenaar32526b32019-01-19 17:43:09 +01007841 msg_puts((char *)name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 msg_putchar(' ');
7843 msg_advance(22);
7844 if (type == VAR_NUMBER)
7845 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007846 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007847 msg_putchar('*');
7848 else if (type == VAR_LIST)
7849 {
7850 msg_putchar('[');
7851 if (*string == '[')
7852 ++string;
7853 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007854 else if (type == VAR_DICT)
7855 {
7856 msg_putchar('{');
7857 if (*string == '{')
7858 ++string;
7859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 else
7861 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007862
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007864
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007865 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007866 msg_puts("()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007867 if (*first)
7868 {
7869 msg_clr_eos();
7870 *first = FALSE;
7871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872}
7873
7874/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007875 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 * If the variable already exists, the value is updated.
7877 * Otherwise the variable is created.
7878 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007879 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007880set_var(
7881 char_u *name,
7882 typval_T *tv,
7883 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884{
Bram Moolenaar33570922005-01-25 22:26:29 +00007885 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007887 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007889 ht = find_var_ht(name, &varname);
7890 if (ht == NULL || *varname == NUL)
7891 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007892 semsg(_(e_illvar), name);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007893 return;
7894 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007895 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007896
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007897 /* Search in parent scope which is possible to reference from lambda */
7898 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007899 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007900
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007901 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7902 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007903 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007904
Bram Moolenaar33570922005-01-25 22:26:29 +00007905 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007907 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007908 if (var_check_ro(v->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007909 || var_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007910 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007911
7912 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007913 * Handle setting internal v: variables separately where needed to
7914 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007915 */
7916 if (ht == &vimvarht)
7917 {
7918 if (v->di_tv.v_type == VAR_STRING)
7919 {
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01007920 VIM_CLEAR(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007921 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01007922 {
7923 char_u *val = tv_get_string(tv);
7924
7925 // Careful: when assigning to v:errmsg and tv_get_string()
7926 // causes an error message the variable will alrady be set.
7927 if (v->di_tv.vval.v_string == NULL)
7928 v->di_tv.vval.v_string = vim_strsave(val);
7929 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007930 else
7931 {
7932 /* Take over the string to avoid an extra alloc/free. */
7933 v->di_tv.vval.v_string = tv->vval.v_string;
7934 tv->vval.v_string = NULL;
7935 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007936 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007937 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007938 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007939 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007940 v->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007941 if (STRCMP(varname, "searchforward") == 0)
7942 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007943#ifdef FEAT_SEARCH_EXTRA
7944 else if (STRCMP(varname, "hlsearch") == 0)
7945 {
7946 no_hlsearch = !v->di_tv.vval.v_number;
7947 redraw_all_later(SOME_VALID);
7948 }
7949#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007950 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007951 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007952 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01007953 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007954 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01007955 return;
7956 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007957 }
7958
7959 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 }
7961 else /* add a new variable */
7962 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01007963 // Can't add "v:" or "a:" variable.
7964 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007965 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007966 semsg(_(e_illvar), name);
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007967 return;
7968 }
7969
Bram Moolenaar31b81602019-02-10 22:14:27 +01007970 // Make sure the variable name is valid.
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007971 if (!valid_varname(varname))
7972 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00007973
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007974 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7975 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00007976 if (v == NULL)
7977 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007978 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00007979 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007981 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 return;
7983 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007984 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007986
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007987 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00007988 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007989 else
7990 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007991 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007992 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007993 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995}
7996
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007997/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007998 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00007999 * Also give an error message.
8000 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008001 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008002var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00008003{
8004 if (flags & DI_FLAGS_RO)
8005 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008006 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008007 return TRUE;
8008 }
8009 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
8010 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008011 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008012 return TRUE;
8013 }
8014 return FALSE;
8015}
8016
8017/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008018 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
8019 * Also give an error message.
8020 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008021 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008022var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008023{
8024 if (flags & DI_FLAGS_FIX)
8025 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008026 semsg(_("E795: Cannot delete variable %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008027 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008028 return TRUE;
8029 }
8030 return FALSE;
8031}
8032
8033/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008034 * Check if a funcref is assigned to a valid variable name.
8035 * Return TRUE and give an error if not.
8036 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008037 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008038var_check_func_name(
8039 char_u *name, /* points to start of variable name */
8040 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008041{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02008042 /* Allow for w: b: s: and t:. */
8043 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008044 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
8045 ? name[2] : name[0]))
8046 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008047 semsg(_("E704: Funcref variable name must start with a capital: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008048 name);
8049 return TRUE;
8050 }
8051 /* Don't allow hiding a function. When "v" is not NULL we might be
8052 * assigning another function to the same var, the type is checked
8053 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02008054 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008055 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008056 semsg(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008057 name);
8058 return TRUE;
8059 }
8060 return FALSE;
8061}
8062
8063/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008064 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02008065 * Also give an error message, using "name" or _("name") when use_gettext is
8066 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008067 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008068 int
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008069var_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008070{
8071 if (lock & VAR_LOCKED)
8072 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008073 semsg(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008074 name == NULL ? (char_u *)_("Unknown")
8075 : use_gettext ? (char_u *)_(name)
8076 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008077 return TRUE;
8078 }
8079 if (lock & VAR_FIXED)
8080 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008081 semsg(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008082 name == NULL ? (char_u *)_("Unknown")
8083 : use_gettext ? (char_u *)_(name)
8084 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008085 return TRUE;
8086 }
8087 return FALSE;
8088}
8089
8090/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008091 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
8092 * Also give an error message, using "name" or _("name") when use_gettext is
8093 * TRUE.
8094 */
8095 static int
8096tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
8097{
8098 int lock = 0;
8099
8100 switch (tv->v_type)
8101 {
8102 case VAR_BLOB:
8103 if (tv->vval.v_blob != NULL)
8104 lock = tv->vval.v_blob->bv_lock;
8105 break;
8106 case VAR_LIST:
8107 if (tv->vval.v_list != NULL)
8108 lock = tv->vval.v_list->lv_lock;
8109 break;
8110 case VAR_DICT:
8111 if (tv->vval.v_dict != NULL)
8112 lock = tv->vval.v_dict->dv_lock;
8113 break;
8114 default:
8115 break;
8116 }
8117 return var_check_lock(tv->v_lock, name, use_gettext)
8118 || (lock != 0 && var_check_lock(lock, name, use_gettext));
8119}
8120
8121/*
8122 * Check if a variable name is valid.
8123 * Return FALSE and give an error if not.
8124 */
8125 int
8126valid_varname(char_u *varname)
8127{
8128 char_u *p;
8129
8130 for (p = varname; *p != NUL; ++p)
8131 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
8132 && *p != AUTOLOAD_CHAR)
8133 {
8134 semsg(_(e_illvar), varname);
8135 return FALSE;
8136 }
8137 return TRUE;
8138}
8139
8140/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008141 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008142 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008143 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008144 * It is OK for "from" and "to" to point to the same item. This is used to
8145 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008146 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008147 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008148copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008150 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008151 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008152 switch (from->v_type)
8153 {
8154 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008155 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008156 to->vval.v_number = from->vval.v_number;
8157 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008158 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008159#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008160 to->vval.v_float = from->vval.v_float;
8161 break;
8162#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008163 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008164#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01008165 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01008166 if (to->vval.v_job != NULL)
8167 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01008168 break;
8169#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01008170 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008171#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01008172 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01008173 if (to->vval.v_channel != NULL)
8174 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01008175 break;
8176#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008177 case VAR_STRING:
8178 case VAR_FUNC:
8179 if (from->vval.v_string == NULL)
8180 to->vval.v_string = NULL;
8181 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008182 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008183 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008184 if (from->v_type == VAR_FUNC)
8185 func_ref(to->vval.v_string);
8186 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008187 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008188 case VAR_PARTIAL:
8189 if (from->vval.v_partial == NULL)
8190 to->vval.v_partial = NULL;
8191 else
8192 {
8193 to->vval.v_partial = from->vval.v_partial;
8194 ++to->vval.v_partial->pt_refcount;
8195 }
8196 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008197 case VAR_BLOB:
8198 if (from->vval.v_blob == NULL)
8199 to->vval.v_blob = NULL;
8200 else
8201 {
8202 to->vval.v_blob = from->vval.v_blob;
8203 ++to->vval.v_blob->bv_refcount;
8204 }
8205 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008206 case VAR_LIST:
8207 if (from->vval.v_list == NULL)
8208 to->vval.v_list = NULL;
8209 else
8210 {
8211 to->vval.v_list = from->vval.v_list;
8212 ++to->vval.v_list->lv_refcount;
8213 }
8214 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008215 case VAR_DICT:
8216 if (from->vval.v_dict == NULL)
8217 to->vval.v_dict = NULL;
8218 else
8219 {
8220 to->vval.v_dict = from->vval.v_dict;
8221 ++to->vval.v_dict->dv_refcount;
8222 }
8223 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008224 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008225 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008226 break;
8227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228}
8229
8230/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008231 * Make a copy of an item.
8232 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008233 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
8234 * reference to an already copied list/dict can be used.
8235 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008236 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008237 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008238item_copy(
8239 typval_T *from,
8240 typval_T *to,
8241 int deep,
8242 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008243{
8244 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008245 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008246
Bram Moolenaar33570922005-01-25 22:26:29 +00008247 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008248 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008249 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008250 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008251 }
8252 ++recurse;
8253
8254 switch (from->v_type)
8255 {
8256 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008257 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008258 case VAR_STRING:
8259 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008260 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008261 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008262 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008263 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008264 copy_tv(from, to);
8265 break;
8266 case VAR_LIST:
8267 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008268 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008269 if (from->vval.v_list == NULL)
8270 to->vval.v_list = NULL;
8271 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8272 {
8273 /* use the copy made earlier */
8274 to->vval.v_list = from->vval.v_list->lv_copylist;
8275 ++to->vval.v_list->lv_refcount;
8276 }
8277 else
8278 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8279 if (to->vval.v_list == NULL)
8280 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008281 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008282 case VAR_BLOB:
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008283 ret = blob_copy(from, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008284 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008285 case VAR_DICT:
8286 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008287 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008288 if (from->vval.v_dict == NULL)
8289 to->vval.v_dict = NULL;
8290 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8291 {
8292 /* use the copy made earlier */
8293 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8294 ++to->vval.v_dict->dv_refcount;
8295 }
8296 else
8297 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8298 if (to->vval.v_dict == NULL)
8299 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008300 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008301 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008302 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008303 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008304 }
8305 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008306 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008307}
8308
8309/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008310 * This function is used by f_input() and f_inputdialog() functions. The third
8311 * argument to f_input() specifies the type of completion to use at the
8312 * prompt. The third argument to f_inputdialog() specifies the value to return
8313 * when the user cancels the prompt.
8314 */
8315 void
8316get_user_input(
8317 typval_T *argvars,
8318 typval_T *rettv,
8319 int inputdialog,
8320 int secret)
8321{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008322 char_u *prompt = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008323 char_u *p = NULL;
8324 int c;
8325 char_u buf[NUMBUFLEN];
8326 int cmd_silent_save = cmd_silent;
8327 char_u *defstr = (char_u *)"";
8328 int xp_type = EXPAND_NOTHING;
8329 char_u *xp_arg = NULL;
8330
8331 rettv->v_type = VAR_STRING;
8332 rettv->vval.v_string = NULL;
8333
8334#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008335 /* While starting up, there is no place to enter text. When running tests
8336 * with --not-a-term we assume feedkeys() will be used. */
8337 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008338 return;
8339#endif
8340
8341 cmd_silent = FALSE; /* Want to see the prompt. */
8342 if (prompt != NULL)
8343 {
8344 /* Only the part of the message after the last NL is considered as
8345 * prompt for the command line */
8346 p = vim_strrchr(prompt, '\n');
8347 if (p == NULL)
8348 p = prompt;
8349 else
8350 {
8351 ++p;
8352 c = *p;
8353 *p = NUL;
8354 msg_start();
8355 msg_clr_eos();
Bram Moolenaar32526b32019-01-19 17:43:09 +01008356 msg_puts_attr((char *)prompt, echo_attr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008357 msg_didout = FALSE;
8358 msg_starthere();
8359 *p = c;
8360 }
8361 cmdline_row = msg_row;
8362
8363 if (argvars[1].v_type != VAR_UNKNOWN)
8364 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008365 defstr = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008366 if (defstr != NULL)
8367 stuffReadbuffSpec(defstr);
8368
8369 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8370 {
8371 char_u *xp_name;
8372 int xp_namelen;
8373 long argt;
8374
8375 /* input() with a third argument: completion */
8376 rettv->vval.v_string = NULL;
8377
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008378 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008379 if (xp_name == NULL)
8380 return;
8381
8382 xp_namelen = (int)STRLEN(xp_name);
8383
8384 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8385 &xp_arg) == FAIL)
8386 return;
8387 }
8388 }
8389
8390 if (defstr != NULL)
8391 {
8392 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02008393
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008394 ex_normal_busy = 0;
8395 rettv->vval.v_string =
8396 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8397 xp_type, xp_arg);
8398 ex_normal_busy = save_ex_normal_busy;
8399 }
8400 if (inputdialog && rettv->vval.v_string == NULL
8401 && argvars[1].v_type != VAR_UNKNOWN
8402 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008403 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008404 &argvars[2], buf));
8405
8406 vim_free(xp_arg);
8407
8408 /* since the user typed this, no need to wait for return */
8409 need_wait_return = FALSE;
8410 msg_didout = FALSE;
8411 }
8412 cmd_silent = cmd_silent_save;
8413}
8414
8415/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 * ":echo expr1 ..." print each argument separated with a space, add a
8417 * newline at the end.
8418 * ":echon expr1 ..." print each argument plain.
8419 */
8420 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008421ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422{
8423 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008424 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008425 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 char_u *p;
8427 int needclr = TRUE;
8428 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008429 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01008430 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008431 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432
8433 if (eap->skip)
8434 ++emsg_skip;
8435 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8436 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008437 /* If eval1() causes an error message the text from the command may
8438 * still need to be cleared. E.g., "echo 22,44". */
8439 need_clr_eos = needclr;
8440
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008442 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 {
8444 /*
8445 * Report the invalid expression unless the expression evaluation
8446 * has been cancelled due to an aborting error, an interrupt, or an
8447 * exception.
8448 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008449 if (!aborting() && did_emsg == did_emsg_before
8450 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008451 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008452 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 break;
8454 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008455 need_clr_eos = FALSE;
8456
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 if (!eap->skip)
8458 {
8459 if (atstart)
8460 {
8461 atstart = FALSE;
8462 /* Call msg_start() after eval1(), evaluating the expression
8463 * may cause a message to appear. */
8464 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008465 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008466 /* Mark the saved text as finishing the line, so that what
8467 * follows is displayed on a new line when scrolling back
8468 * at the more prompt. */
8469 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 }
8473 else if (eap->cmdidx == CMD_echo)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008474 msg_puts_attr(" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008475 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008476 if (p != NULL)
8477 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008479 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008481 if (*p != TAB && needclr)
8482 {
8483 /* remove any text still there from the command */
8484 msg_clr_eos();
8485 needclr = FALSE;
8486 }
8487 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 }
8489 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008490 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008491 if (has_mbyte)
8492 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008493 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008494
8495 (void)msg_outtrans_len_attr(p, i, echo_attr);
8496 p += i - 1;
8497 }
8498 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008499 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008502 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008504 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 arg = skipwhite(arg);
8506 }
8507 eap->nextcmd = check_nextcmd(arg);
8508
8509 if (eap->skip)
8510 --emsg_skip;
8511 else
8512 {
8513 /* remove text that may still be there from the command */
8514 if (needclr)
8515 msg_clr_eos();
8516 if (eap->cmdidx == CMD_echo)
8517 msg_end();
8518 }
8519}
8520
8521/*
8522 * ":echohl {name}".
8523 */
8524 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008525ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008527 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528}
8529
8530/*
8531 * ":execute expr1 ..." execute the result of an expression.
8532 * ":echomsg expr1 ..." Print a message
8533 * ":echoerr expr1 ..." Print an error
8534 * Each gets spaces around each argument and a newline at the end for
8535 * echo commands
8536 */
8537 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008538ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539{
8540 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008541 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 int ret = OK;
8543 char_u *p;
8544 garray_T ga;
8545 int len;
Bram Moolenaar8ff5af92018-11-28 21:20:38 +01008546 int save_did_emsg = did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547
8548 ga_init2(&ga, 1, 80);
8549
8550 if (eap->skip)
8551 ++emsg_skip;
8552 while (*arg != NUL && *arg != '|' && *arg != '\n')
8553 {
8554 p = arg;
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 Moolenaar53744302015-07-17 17:38:22 +02009222/* reset v:option_new, v:option_old and v:option_type */
9223 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009224reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02009225{
9226 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
9227 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
9228 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
9229}
9230
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009231/*
9232 * Prepare "gap" for an assert error and add the sourcing position.
9233 */
9234 void
9235prepare_assert_error(garray_T *gap)
9236{
9237 char buf[NUMBUFLEN];
9238
9239 ga_init2(gap, 1, 100);
9240 if (sourcing_name != NULL)
9241 {
9242 ga_concat(gap, sourcing_name);
9243 if (sourcing_lnum > 0)
9244 ga_concat(gap, (char_u *)" ");
9245 }
9246 if (sourcing_lnum > 0)
9247 {
9248 sprintf(buf, "line %ld", (long)sourcing_lnum);
9249 ga_concat(gap, (char_u *)buf);
9250 }
9251 if (sourcing_name != NULL || sourcing_lnum > 0)
9252 ga_concat(gap, (char_u *)": ");
9253}
9254
9255/*
9256 * Add an assert error to v:errors.
9257 */
9258 void
9259assert_error(garray_T *gap)
9260{
9261 struct vimvar *vp = &vimvars[VV_ERRORS];
9262
9263 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9264 /* Make sure v:errors is a list. */
9265 set_vim_var_list(VV_ERRORS, list_alloc());
9266 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9267}
9268
Bram Moolenaar65a54642018-04-28 16:56:53 +02009269 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009270assert_equal_common(typval_T *argvars, assert_type_T atype)
9271{
9272 garray_T ga;
9273
9274 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9275 != (atype == ASSERT_EQUAL))
9276 {
9277 prepare_assert_error(&ga);
9278 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9279 atype);
9280 assert_error(&ga);
9281 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009282 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009283 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009284 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009285}
9286
Bram Moolenaar65a54642018-04-28 16:56:53 +02009287 int
Bram Moolenaard96ff162018-02-18 22:13:29 +01009288assert_equalfile(typval_T *argvars)
9289{
9290 char_u buf1[NUMBUFLEN];
9291 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009292 char_u *fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
9293 char_u *fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009294 garray_T ga;
9295 FILE *fd1;
9296 FILE *fd2;
9297
9298 if (fname1 == NULL || fname2 == NULL)
Bram Moolenaar65a54642018-04-28 16:56:53 +02009299 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009300
9301 IObuff[0] = NUL;
9302 fd1 = mch_fopen((char *)fname1, READBIN);
9303 if (fd1 == NULL)
9304 {
9305 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname1);
9306 }
9307 else
9308 {
9309 fd2 = mch_fopen((char *)fname2, READBIN);
9310 if (fd2 == NULL)
9311 {
9312 fclose(fd1);
9313 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname2);
9314 }
9315 else
9316 {
9317 int c1, c2;
9318 long count = 0;
9319
9320 for (;;)
9321 {
9322 c1 = fgetc(fd1);
9323 c2 = fgetc(fd2);
9324 if (c1 == EOF)
9325 {
9326 if (c2 != EOF)
9327 STRCPY(IObuff, "first file is shorter");
9328 break;
9329 }
9330 else if (c2 == EOF)
9331 {
9332 STRCPY(IObuff, "second file is shorter");
9333 break;
9334 }
9335 else if (c1 != c2)
9336 {
9337 vim_snprintf((char *)IObuff, IOSIZE,
9338 "difference at byte %ld", count);
9339 break;
9340 }
9341 ++count;
9342 }
Bram Moolenaar30494182018-02-20 21:46:05 +01009343 fclose(fd1);
9344 fclose(fd2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009345 }
9346 }
9347 if (IObuff[0] != NUL)
9348 {
9349 prepare_assert_error(&ga);
9350 ga_concat(&ga, IObuff);
9351 assert_error(&ga);
9352 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009353 return 1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009354 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009355 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009356}
9357
Bram Moolenaar65a54642018-04-28 16:56:53 +02009358 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009359assert_match_common(typval_T *argvars, assert_type_T atype)
9360{
9361 garray_T ga;
9362 char_u buf1[NUMBUFLEN];
9363 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009364 char_u *pat = tv_get_string_buf_chk(&argvars[0], buf1);
9365 char_u *text = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009366
9367 if (pat == NULL || text == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009368 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009369 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
9370 {
9371 prepare_assert_error(&ga);
9372 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9373 atype);
9374 assert_error(&ga);
9375 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009376 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009377 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009378 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009379}
9380
Bram Moolenaar65a54642018-04-28 16:56:53 +02009381 int
Bram Moolenaar61c04492016-07-23 15:35:35 +02009382assert_inrange(typval_T *argvars)
9383{
9384 garray_T ga;
9385 int error = FALSE;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009386 char_u *tofree;
9387 char msg[200];
9388 char_u numbuf[NUMBUFLEN];
9389
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009390#ifdef FEAT_FLOAT
9391 if (argvars[0].v_type == VAR_FLOAT
9392 || argvars[1].v_type == VAR_FLOAT
9393 || argvars[2].v_type == VAR_FLOAT)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009394 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009395 float_T flower = tv_get_float(&argvars[0]);
9396 float_T fupper = tv_get_float(&argvars[1]);
9397 float_T factual = tv_get_float(&argvars[2]);
9398
9399 if (factual < flower || factual > fupper)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009400 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009401 prepare_assert_error(&ga);
9402 if (argvars[3].v_type != VAR_UNKNOWN)
9403 {
9404 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9405 vim_free(tofree);
9406 }
9407 else
9408 {
9409 vim_snprintf(msg, 200, "Expected range %g - %g, but got %g",
9410 flower, fupper, factual);
9411 ga_concat(&ga, (char_u *)msg);
9412 }
9413 assert_error(&ga);
9414 ga_clear(&ga);
9415 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009416 }
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009417 }
9418 else
9419#endif
9420 {
9421 varnumber_T lower = tv_get_number_chk(&argvars[0], &error);
9422 varnumber_T upper = tv_get_number_chk(&argvars[1], &error);
9423 varnumber_T actual = tv_get_number_chk(&argvars[2], &error);
9424
9425 if (error)
9426 return 0;
9427 if (actual < lower || actual > upper)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009428 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009429 prepare_assert_error(&ga);
9430 if (argvars[3].v_type != VAR_UNKNOWN)
9431 {
9432 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9433 vim_free(tofree);
9434 }
9435 else
9436 {
9437 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
Bram Moolenaar61c04492016-07-23 15:35:35 +02009438 (long)lower, (long)upper, (long)actual);
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009439 ga_concat(&ga, (char_u *)msg);
9440 }
9441 assert_error(&ga);
9442 ga_clear(&ga);
9443 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009444 }
Bram Moolenaar61c04492016-07-23 15:35:35 +02009445 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009446 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009447}
9448
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009449/*
9450 * Common for assert_true() and assert_false().
Bram Moolenaar65a54642018-04-28 16:56:53 +02009451 * Return non-zero for failure.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009452 */
Bram Moolenaar65a54642018-04-28 16:56:53 +02009453 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009454assert_bool(typval_T *argvars, int isTrue)
9455{
9456 int error = FALSE;
9457 garray_T ga;
9458
9459 if (argvars[0].v_type == VAR_SPECIAL
9460 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar65a54642018-04-28 16:56:53 +02009461 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009462 if (argvars[0].v_type != VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009463 || (tv_get_number_chk(&argvars[0], &error) == 0) == isTrue
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009464 || error)
9465 {
9466 prepare_assert_error(&ga);
9467 fill_assert_error(&ga, &argvars[1],
9468 (char_u *)(isTrue ? "True" : "False"),
9469 NULL, &argvars[0], ASSERT_OTHER);
9470 assert_error(&ga);
9471 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009472 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009473 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009474 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009475}
9476
Bram Moolenaar65a54642018-04-28 16:56:53 +02009477 int
Bram Moolenaar42205552017-03-18 19:42:22 +01009478assert_report(typval_T *argvars)
9479{
9480 garray_T ga;
9481
9482 prepare_assert_error(&ga);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009483 ga_concat(&ga, tv_get_string(&argvars[0]));
Bram Moolenaar42205552017-03-18 19:42:22 +01009484 assert_error(&ga);
9485 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009486 return 1;
Bram Moolenaar42205552017-03-18 19:42:22 +01009487}
9488
Bram Moolenaar65a54642018-04-28 16:56:53 +02009489 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009490assert_exception(typval_T *argvars)
9491{
9492 garray_T ga;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009493 char_u *error = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009494
9495 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9496 {
9497 prepare_assert_error(&ga);
9498 ga_concat(&ga, (char_u *)"v:exception is not set");
9499 assert_error(&ga);
9500 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009501 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009502 }
9503 else if (error != NULL
9504 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9505 {
9506 prepare_assert_error(&ga);
9507 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9508 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9509 assert_error(&ga);
9510 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009511 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009512 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009513 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009514}
9515
Bram Moolenaar65a54642018-04-28 16:56:53 +02009516 int
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009517assert_beeps(typval_T *argvars)
9518{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009519 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009520 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009521 int ret = 0;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009522
9523 called_vim_beep = FALSE;
9524 suppress_errthrow = TRUE;
9525 emsg_silent = FALSE;
9526 do_cmdline_cmd(cmd);
9527 if (!called_vim_beep)
9528 {
9529 prepare_assert_error(&ga);
9530 ga_concat(&ga, (char_u *)"command did not beep: ");
9531 ga_concat(&ga, cmd);
9532 assert_error(&ga);
9533 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009534 ret = 1;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009535 }
9536
9537 suppress_errthrow = FALSE;
9538 emsg_on_display = FALSE;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009539 return ret;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009540}
9541
Bram Moolenaar65a54642018-04-28 16:56:53 +02009542 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009543assert_fails(typval_T *argvars)
9544{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009545 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009546 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009547 int ret = 0;
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02009548 char_u numbuf[NUMBUFLEN];
9549 char_u *tofree;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009550
9551 called_emsg = FALSE;
9552 suppress_errthrow = TRUE;
9553 emsg_silent = TRUE;
9554 do_cmdline_cmd(cmd);
9555 if (!called_emsg)
9556 {
9557 prepare_assert_error(&ga);
9558 ga_concat(&ga, (char_u *)"command did not fail: ");
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02009559 if (argvars[1].v_type != VAR_UNKNOWN
9560 && argvars[2].v_type != VAR_UNKNOWN)
9561 {
9562 ga_concat(&ga, echo_string(&argvars[2], &tofree, numbuf, 0));
9563 vim_free(tofree);
9564 }
9565 else
9566 ga_concat(&ga, cmd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009567 assert_error(&ga);
9568 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009569 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009570 }
9571 else if (argvars[1].v_type != VAR_UNKNOWN)
9572 {
9573 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009574 char *error = (char *)tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009575
9576 if (error == NULL
9577 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9578 {
9579 prepare_assert_error(&ga);
9580 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9581 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
9582 assert_error(&ga);
9583 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009584 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009585 }
9586 }
9587
9588 called_emsg = FALSE;
9589 suppress_errthrow = FALSE;
9590 emsg_silent = FALSE;
9591 emsg_on_display = FALSE;
9592 set_vim_var_string(VV_ERRMSG, NULL, 0);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009593 return ret;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009594}
9595
9596/*
Bram Moolenaar86576712019-01-25 20:48:33 +01009597 * Append "p[clen]" to "gap", escaping unprintable characters.
9598 * Changes NL to \n, CR to \r, etc.
9599 */
9600 static void
9601ga_concat_esc(garray_T *gap, char_u *p, int clen)
9602{
9603 char_u buf[NUMBUFLEN];
9604
9605 if (clen > 1)
9606 {
9607 mch_memmove(buf, p, clen);
9608 buf[clen] = NUL;
9609 ga_concat(gap, buf);
9610 }
9611 else switch (*p)
9612 {
9613 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9614 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9615 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9616 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9617 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9618 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9619 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9620 default:
9621 if (*p < ' ')
9622 {
9623 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9624 ga_concat(gap, buf);
9625 }
9626 else
9627 ga_append(gap, *p);
9628 break;
9629 }
9630}
9631
9632/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009633 * Append "str" to "gap", escaping unprintable characters.
9634 * Changes NL to \n, CR to \r, etc.
9635 */
9636 static void
Bram Moolenaar86576712019-01-25 20:48:33 +01009637ga_concat_shorten_esc(garray_T *gap, char_u *str)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009638{
9639 char_u *p;
Bram Moolenaar86576712019-01-25 20:48:33 +01009640 char_u *s;
9641 int c;
9642 int clen;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009643 char_u buf[NUMBUFLEN];
Bram Moolenaar86576712019-01-25 20:48:33 +01009644 int same_len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009645
9646 if (str == NULL)
9647 {
9648 ga_concat(gap, (char_u *)"NULL");
9649 return;
9650 }
9651
9652 for (p = str; *p != NUL; ++p)
Bram Moolenaar86576712019-01-25 20:48:33 +01009653 {
9654 same_len = 1;
9655 s = p;
9656 c = mb_ptr2char_adv(&s);
9657 clen = s - p;
9658 while (*s != NUL && c == mb_ptr2char(s))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009659 {
Bram Moolenaar86576712019-01-25 20:48:33 +01009660 ++same_len;
9661 s += clen;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009662 }
Bram Moolenaar86576712019-01-25 20:48:33 +01009663 if (same_len > 20)
9664 {
9665 ga_concat(gap, (char_u *)"\\[");
9666 ga_concat_esc(gap, p, clen);
9667 ga_concat(gap, (char_u *)" occurs ");
9668 vim_snprintf((char *)buf, NUMBUFLEN, "%d", same_len);
9669 ga_concat(gap, buf);
9670 ga_concat(gap, (char_u *)" times]");
9671 p = s - 1;
9672 }
9673 else
9674 ga_concat_esc(gap, p, clen);
9675 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009676}
9677
9678/*
9679 * Fill "gap" with information about an assert error.
9680 */
9681 void
9682fill_assert_error(
9683 garray_T *gap,
9684 typval_T *opt_msg_tv,
9685 char_u *exp_str,
9686 typval_T *exp_tv,
9687 typval_T *got_tv,
9688 assert_type_T atype)
9689{
9690 char_u numbuf[NUMBUFLEN];
9691 char_u *tofree;
9692
9693 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9694 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009695 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9696 vim_free(tofree);
9697 ga_concat(gap, (char_u *)": ");
9698 }
9699
9700 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9701 ga_concat(gap, (char_u *)"Pattern ");
9702 else if (atype == ASSERT_NOTEQUAL)
9703 ga_concat(gap, (char_u *)"Expected not equal to ");
9704 else
9705 ga_concat(gap, (char_u *)"Expected ");
9706 if (exp_str == NULL)
9707 {
Bram Moolenaar86576712019-01-25 20:48:33 +01009708 ga_concat_shorten_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009709 vim_free(tofree);
9710 }
9711 else
Bram Moolenaar86576712019-01-25 20:48:33 +01009712 ga_concat_shorten_esc(gap, exp_str);
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009713 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009714 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009715 if (atype == ASSERT_MATCH)
9716 ga_concat(gap, (char_u *)" does not match ");
9717 else if (atype == ASSERT_NOTMATCH)
9718 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009719 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009720 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar86576712019-01-25 20:48:33 +01009721 ga_concat_shorten_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009722 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009723 }
9724}
9725
Bram Moolenaar31988702018-02-13 12:57:42 +01009726/*
9727 * Compare "typ1" and "typ2". Put the result in "typ1".
9728 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009729 int
9730typval_compare(
9731 typval_T *typ1, /* first operand */
9732 typval_T *typ2, /* second operand */
9733 exptype_T type, /* operator */
9734 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +01009735 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009736{
9737 int i;
9738 varnumber_T n1, n2;
9739 char_u *s1, *s2;
9740 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
9741
Bram Moolenaar31988702018-02-13 12:57:42 +01009742 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009743 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009744 /* For "is" a different type always means FALSE, for "notis"
9745 * it means TRUE. */
9746 n1 = (type == TYPE_NEQUAL);
9747 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009748 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
9749 {
9750 if (type_is)
9751 {
9752 n1 = (typ1->v_type == typ2->v_type
9753 && typ1->vval.v_blob == typ2->vval.v_blob);
9754 if (type == TYPE_NEQUAL)
9755 n1 = !n1;
9756 }
9757 else if (typ1->v_type != typ2->v_type
9758 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9759 {
9760 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009761 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009762 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009763 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009764 clear_tv(typ1);
9765 return FAIL;
9766 }
9767 else
9768 {
9769 // Compare two Blobs for being equal or unequal.
9770 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
9771 if (type == TYPE_NEQUAL)
9772 n1 = !n1;
9773 }
9774 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009775 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
9776 {
9777 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009778 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009779 n1 = (typ1->v_type == typ2->v_type
9780 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009781 if (type == TYPE_NEQUAL)
9782 n1 = !n1;
9783 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009784 else if (typ1->v_type != typ2->v_type
9785 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009786 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009787 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009788 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009789 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009790 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009791 clear_tv(typ1);
9792 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009793 }
9794 else
9795 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009796 /* Compare two Lists for being equal or unequal. */
9797 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
9798 ic, FALSE);
9799 if (type == TYPE_NEQUAL)
9800 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009801 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009802 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009803
9804 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
9805 {
9806 if (type_is)
9807 {
9808 n1 = (typ1->v_type == typ2->v_type
9809 && typ1->vval.v_dict == typ2->vval.v_dict);
9810 if (type == TYPE_NEQUAL)
9811 n1 = !n1;
9812 }
9813 else if (typ1->v_type != typ2->v_type
9814 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9815 {
9816 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009817 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009818 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009819 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009820 clear_tv(typ1);
9821 return FAIL;
9822 }
9823 else
9824 {
9825 /* Compare two Dictionaries for being equal or unequal. */
9826 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
9827 ic, FALSE);
9828 if (type == TYPE_NEQUAL)
9829 n1 = !n1;
9830 }
9831 }
9832
9833 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
9834 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
9835 {
9836 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
9837 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009838 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009839 clear_tv(typ1);
9840 return FAIL;
9841 }
9842 if ((typ1->v_type == VAR_PARTIAL
9843 && typ1->vval.v_partial == NULL)
9844 || (typ2->v_type == VAR_PARTIAL
9845 && typ2->vval.v_partial == NULL))
9846 /* when a partial is NULL assume not equal */
9847 n1 = FALSE;
9848 else if (type_is)
9849 {
9850 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
9851 /* strings are considered the same if their value is
9852 * the same */
9853 n1 = tv_equal(typ1, typ2, ic, FALSE);
9854 else if (typ1->v_type == VAR_PARTIAL
9855 && typ2->v_type == VAR_PARTIAL)
9856 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
9857 else
9858 n1 = FALSE;
9859 }
9860 else
9861 n1 = tv_equal(typ1, typ2, ic, FALSE);
9862 if (type == TYPE_NEQUAL)
9863 n1 = !n1;
9864 }
9865
9866#ifdef FEAT_FLOAT
9867 /*
9868 * If one of the two variables is a float, compare as a float.
9869 * When using "=~" or "!~", always compare as string.
9870 */
9871 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
9872 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9873 {
9874 float_T f1, f2;
9875
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009876 f1 = tv_get_float(typ1);
9877 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009878 n1 = FALSE;
9879 switch (type)
9880 {
9881 case TYPE_EQUAL: n1 = (f1 == f2); break;
9882 case TYPE_NEQUAL: n1 = (f1 != f2); break;
9883 case TYPE_GREATER: n1 = (f1 > f2); break;
9884 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
9885 case TYPE_SMALLER: n1 = (f1 < f2); break;
9886 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
9887 case TYPE_UNKNOWN:
9888 case TYPE_MATCH:
9889 case TYPE_NOMATCH: break; /* avoid gcc warning */
9890 }
9891 }
9892#endif
9893
9894 /*
9895 * If one of the two variables is a number, compare as a number.
9896 * When using "=~" or "!~", always compare as string.
9897 */
9898 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
9899 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9900 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009901 n1 = tv_get_number(typ1);
9902 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009903 switch (type)
9904 {
9905 case TYPE_EQUAL: n1 = (n1 == n2); break;
9906 case TYPE_NEQUAL: n1 = (n1 != n2); break;
9907 case TYPE_GREATER: n1 = (n1 > n2); break;
9908 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
9909 case TYPE_SMALLER: n1 = (n1 < n2); break;
9910 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
9911 case TYPE_UNKNOWN:
9912 case TYPE_MATCH:
9913 case TYPE_NOMATCH: break; /* avoid gcc warning */
9914 }
9915 }
9916 else
9917 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009918 s1 = tv_get_string_buf(typ1, buf1);
9919 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009920 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
9921 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
9922 else
9923 i = 0;
9924 n1 = FALSE;
9925 switch (type)
9926 {
9927 case TYPE_EQUAL: n1 = (i == 0); break;
9928 case TYPE_NEQUAL: n1 = (i != 0); break;
9929 case TYPE_GREATER: n1 = (i > 0); break;
9930 case TYPE_GEQUAL: n1 = (i >= 0); break;
9931 case TYPE_SMALLER: n1 = (i < 0); break;
9932 case TYPE_SEQUAL: n1 = (i <= 0); break;
9933
9934 case TYPE_MATCH:
9935 case TYPE_NOMATCH:
9936 n1 = pattern_match(s2, s1, ic);
9937 if (type == TYPE_NOMATCH)
9938 n1 = !n1;
9939 break;
9940
9941 case TYPE_UNKNOWN: break; /* avoid gcc warning */
9942 }
9943 }
9944 clear_tv(typ1);
9945 typ1->v_type = VAR_NUMBER;
9946 typ1->vval.v_number = n1;
9947
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009948 return OK;
9949}
9950
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009951 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02009952typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009953{
9954 char_u *tofree;
9955 char_u numbuf[NUMBUFLEN];
9956 char_u *ret = NULL;
9957
9958 if (arg == NULL)
9959 return vim_strsave((char_u *)"(does not exist)");
9960 ret = tv2string(arg, &tofree, numbuf, 0);
9961 /* Make a copy if we have a value but it's not in allocated memory. */
9962 if (ret != NULL && tofree == NULL)
9963 ret = vim_strsave(ret);
9964 return ret;
9965}
9966
9967 int
9968var_exists(char_u *var)
9969{
9970 char_u *name;
9971 char_u *tofree;
9972 typval_T tv;
9973 int len = 0;
9974 int n = FALSE;
9975
9976 /* get_name_len() takes care of expanding curly braces */
9977 name = var;
9978 len = get_name_len(&var, &tofree, TRUE, FALSE);
9979 if (len > 0)
9980 {
9981 if (tofree != NULL)
9982 name = tofree;
9983 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
9984 if (n)
9985 {
9986 /* handle d.key, l[idx], f(expr) */
9987 n = (handle_subscript(&var, &tv, TRUE, FALSE) == OK);
9988 if (n)
9989 clear_tv(&tv);
9990 }
9991 }
9992 if (*var != NUL)
9993 n = FALSE;
9994
9995 vim_free(tofree);
9996 return n;
9997}
9998
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999#endif /* FEAT_EVAL */
10000
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010002#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003
Bram Moolenaar4f974752019-02-17 17:44:42 +010010004#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005/*
10006 * Functions for ":8" filename modifier: get 8.3 version of a filename.
10007 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008
10009/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010010 * Get the short path (8.3) for the filename in "fnamep".
10011 * Only works for a valid file name.
10012 * When the path gets longer "fnamep" is changed and the allocated buffer
10013 * is put in "bufp".
10014 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
10015 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016 */
10017 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010018get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010020 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021 char_u *newbuf;
10022
10023 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010024 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025 if (l > len - 1)
10026 {
10027 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010028 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 newbuf = vim_strnsave(*fnamep, l);
10030 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010031 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032
10033 vim_free(*bufp);
10034 *fnamep = *bufp = newbuf;
10035
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010036 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010037 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038 }
10039
10040 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010041 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042}
10043
10044/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010045 * Get the short path (8.3) for the filename in "fname". The converted
10046 * path is returned in "bufp".
10047 *
10048 * Some of the directories specified in "fname" may not exist. This function
10049 * will shorten the existing directories at the beginning of the path and then
10050 * append the remaining non-existing path.
10051 *
10052 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020010053 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010054 * bufp - Pointer to an allocated buffer for the filename.
10055 * fnamelen - Length of the filename pointed to by fname
10056 *
10057 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058 */
10059 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010060shortpath_for_invalid_fname(
10061 char_u **fname,
10062 char_u **bufp,
10063 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010065 char_u *short_fname, *save_fname, *pbuf_unused;
10066 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010068 int old_len, len;
10069 int new_len, sfx_len;
10070 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071
10072 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010073 old_len = *fnamelen;
10074 save_fname = vim_strnsave(*fname, old_len);
10075 pbuf_unused = NULL;
10076 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010078 endp = save_fname + old_len - 1; /* Find the end of the copy */
10079 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010081 /*
10082 * Try shortening the supplied path till it succeeds by removing one
10083 * directory at a time from the tail of the path.
10084 */
10085 len = 0;
10086 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010088 /* go back one path-separator */
10089 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
10090 --endp;
10091 if (endp <= save_fname)
10092 break; /* processed the complete path */
10093
10094 /*
10095 * Replace the path separator with a NUL and try to shorten the
10096 * resulting path.
10097 */
10098 ch = *endp;
10099 *endp = 0;
10100 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010101 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010102 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
10103 {
10104 retval = FAIL;
10105 goto theend;
10106 }
10107 *endp = ch; /* preserve the string */
10108
10109 if (len > 0)
10110 break; /* successfully shortened the path */
10111
10112 /* failed to shorten the path. Skip the path separator */
10113 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114 }
10115
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010116 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010118 /*
10119 * Succeeded in shortening the path. Now concatenate the shortened
10120 * path with the remaining path at the tail.
10121 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010123 /* Compute the length of the new path. */
10124 sfx_len = (int)(save_endp - endp) + 1;
10125 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010127 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010129 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010131 /* There is not enough space in the currently allocated string,
10132 * copy it to a buffer big enough. */
10133 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010135 {
10136 retval = FAIL;
10137 goto theend;
10138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 }
10140 else
10141 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010142 /* Transfer short_fname to the main buffer (it's big enough),
10143 * unless get_short_pathname() did its work in-place. */
10144 *fname = *bufp = save_fname;
10145 if (short_fname != save_fname)
10146 vim_strncpy(save_fname, short_fname, len);
10147 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010149
10150 /* concat the not-shortened part of the path */
10151 vim_strncpy(*fname + len, endp, sfx_len);
10152 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010154
10155theend:
10156 vim_free(pbuf_unused);
10157 vim_free(save_fname);
10158
10159 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160}
10161
10162/*
10163 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010164 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165 */
10166 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010167shortpath_for_partial(
10168 char_u **fnamep,
10169 char_u **bufp,
10170 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171{
10172 int sepcount, len, tflen;
10173 char_u *p;
10174 char_u *pbuf, *tfname;
10175 int hasTilde;
10176
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010177 /* Count up the path separators from the RHS.. so we know which part
10178 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010180 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181 if (vim_ispathsep(*p))
10182 ++sepcount;
10183
10184 /* Need full path first (use expand_env() to remove a "~/") */
10185 hasTilde = (**fnamep == '~');
10186 if (hasTilde)
10187 pbuf = tfname = expand_env_save(*fnamep);
10188 else
10189 pbuf = tfname = FullName_save(*fnamep, FALSE);
10190
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010191 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010193 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
10194 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195
10196 if (len == 0)
10197 {
10198 /* Don't have a valid filename, so shorten the rest of the
10199 * path if we can. This CAN give us invalid 8.3 filenames, but
10200 * there's not a lot of point in guessing what it might be.
10201 */
10202 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010203 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
10204 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205 }
10206
10207 /* Count the paths backward to find the beginning of the desired string. */
10208 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010209 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010210 if (has_mbyte)
10211 p -= mb_head_off(tfname, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212 if (vim_ispathsep(*p))
10213 {
10214 if (sepcount == 0 || (hasTilde && sepcount == 1))
10215 break;
10216 else
10217 sepcount --;
10218 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220 if (hasTilde)
10221 {
10222 --p;
10223 if (p >= tfname)
10224 *p = '~';
10225 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010226 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227 }
10228 else
10229 ++p;
10230
10231 /* Copy in the string - p indexes into tfname - allocated at pbuf */
10232 vim_free(*bufp);
10233 *fnamelen = (int)STRLEN(p);
10234 *bufp = pbuf;
10235 *fnamep = p;
10236
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010237 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238}
Bram Moolenaar4f974752019-02-17 17:44:42 +010010239#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240
10241/*
10242 * Adjust a filename, according to a string of modifiers.
10243 * *fnamep must be NUL terminated when called. When returning, the length is
10244 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010245 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246 * When there is an error, *fnamep is set to NULL.
10247 */
10248 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010249modify_fname(
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010250 char_u *src, // string with modifiers
10251 int tilde_file, // "~" is a file name, not $HOME
10252 int *usedlen, // characters after src that are used
10253 char_u **fnamep, // file name so far
10254 char_u **bufp, // buffer for allocated file name or NULL
10255 int *fnamelen) // length of fnamep
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256{
10257 int valid = 0;
10258 char_u *tail;
10259 char_u *s, *p, *pbuf;
10260 char_u dirname[MAXPATHL];
10261 int c;
10262 int has_fullname = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010263#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010264 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265 int has_shortname = 0;
10266#endif
10267
10268repeat:
10269 /* ":p" - full path/file_name */
10270 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
10271 {
10272 has_fullname = 1;
10273
10274 valid |= VALID_PATH;
10275 *usedlen += 2;
10276
10277 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
10278 if ((*fnamep)[0] == '~'
10279#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
10280 && ((*fnamep)[1] == '/'
10281# ifdef BACKSLASH_IN_FILENAME
10282 || (*fnamep)[1] == '\\'
10283# endif
10284 || (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010286 && !(tilde_file && (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010287 )
10288 {
10289 *fnamep = expand_env_save(*fnamep);
10290 vim_free(*bufp); /* free any allocated file name */
10291 *bufp = *fnamep;
10292 if (*fnamep == NULL)
10293 return -1;
10294 }
10295
10296 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010297 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298 {
10299 if (vim_ispathsep(*p)
10300 && p[1] == '.'
10301 && (p[2] == NUL
10302 || vim_ispathsep(p[2])
10303 || (p[2] == '.'
10304 && (p[3] == NUL || vim_ispathsep(p[3])))))
10305 break;
10306 }
10307
10308 /* FullName_save() is slow, don't use it when not needed. */
10309 if (*p != NUL || !vim_isAbsName(*fnamep))
10310 {
10311 *fnamep = FullName_save(*fnamep, *p != NUL);
10312 vim_free(*bufp); /* free any allocated file name */
10313 *bufp = *fnamep;
10314 if (*fnamep == NULL)
10315 return -1;
10316 }
10317
Bram Moolenaar4f974752019-02-17 17:44:42 +010010318#ifdef MSWIN
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010319# if _WIN32_WINNT >= 0x0500
10320 if (vim_strchr(*fnamep, '~') != NULL)
10321 {
10322 /* Expand 8.3 filename to full path. Needed to make sure the same
10323 * file does not have two different names.
10324 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
10325 p = alloc(_MAX_PATH + 1);
10326 if (p != NULL)
10327 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010328 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010329 {
10330 vim_free(*bufp);
10331 *bufp = *fnamep = p;
10332 }
10333 else
10334 vim_free(p);
10335 }
10336 }
10337# endif
10338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339 /* Append a path separator to a directory. */
10340 if (mch_isdir(*fnamep))
10341 {
10342 /* Make room for one or two extra characters. */
10343 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
10344 vim_free(*bufp); /* free any allocated file name */
10345 *bufp = *fnamep;
10346 if (*fnamep == NULL)
10347 return -1;
10348 add_pathsep(*fnamep);
10349 }
10350 }
10351
10352 /* ":." - path relative to the current directory */
10353 /* ":~" - path relative to the home directory */
10354 /* ":8" - shortname path - postponed till after */
10355 while (src[*usedlen] == ':'
10356 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
10357 {
10358 *usedlen += 2;
10359 if (c == '8')
10360 {
Bram Moolenaar4f974752019-02-17 17:44:42 +010010361#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362 has_shortname = 1; /* Postpone this. */
10363#endif
10364 continue;
10365 }
10366 pbuf = NULL;
10367 /* Need full path first (use expand_env() to remove a "~/") */
10368 if (!has_fullname)
10369 {
10370 if (c == '.' && **fnamep == '~')
10371 p = pbuf = expand_env_save(*fnamep);
10372 else
10373 p = pbuf = FullName_save(*fnamep, FALSE);
10374 }
10375 else
10376 p = *fnamep;
10377
10378 has_fullname = 0;
10379
10380 if (p != NULL)
10381 {
10382 if (c == '.')
10383 {
10384 mch_dirname(dirname, MAXPATHL);
10385 s = shorten_fname(p, dirname);
10386 if (s != NULL)
10387 {
10388 *fnamep = s;
10389 if (pbuf != NULL)
10390 {
10391 vim_free(*bufp); /* free any allocated file name */
10392 *bufp = pbuf;
10393 pbuf = NULL;
10394 }
10395 }
10396 }
10397 else
10398 {
10399 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
10400 /* Only replace it when it starts with '~' */
10401 if (*dirname == '~')
10402 {
10403 s = vim_strsave(dirname);
10404 if (s != NULL)
10405 {
10406 *fnamep = s;
10407 vim_free(*bufp);
10408 *bufp = s;
10409 }
10410 }
10411 }
10412 vim_free(pbuf);
10413 }
10414 }
10415
10416 tail = gettail(*fnamep);
10417 *fnamelen = (int)STRLEN(*fnamep);
10418
10419 /* ":h" - head, remove "/file_name", can be repeated */
10420 /* Don't remove the first "/" or "c:\" */
10421 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
10422 {
10423 valid |= VALID_HEAD;
10424 *usedlen += 2;
10425 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010426 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010427 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428 *fnamelen = (int)(tail - *fnamep);
10429#ifdef VMS
10430 if (*fnamelen > 0)
10431 *fnamelen += 1; /* the path separator is part of the path */
10432#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010433 if (*fnamelen == 0)
10434 {
10435 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
10436 p = vim_strsave((char_u *)".");
10437 if (p == NULL)
10438 return -1;
10439 vim_free(*bufp);
10440 *bufp = *fnamep = tail = p;
10441 *fnamelen = 1;
10442 }
10443 else
10444 {
10445 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010446 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448 }
10449
10450 /* ":8" - shortname */
10451 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
10452 {
10453 *usedlen += 2;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010454#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455 has_shortname = 1;
10456#endif
10457 }
10458
Bram Moolenaar4f974752019-02-17 17:44:42 +010010459#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010460 /*
10461 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462 */
10463 if (has_shortname)
10464 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010465 /* Copy the string if it is shortened by :h and when it wasn't copied
10466 * yet, because we are going to change it in place. Avoids changing
10467 * the buffer name for "%:8". */
10468 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469 {
10470 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020010471 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472 return -1;
10473 vim_free(*bufp);
10474 *bufp = *fnamep = p;
10475 }
10476
10477 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020010478 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479 if (!has_fullname && !vim_isAbsName(*fnamep))
10480 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010481 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482 return -1;
10483 }
10484 else
10485 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010486 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487
Bram Moolenaardc935552011-08-17 15:23:23 +020010488 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010490 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010491 return -1;
10492
10493 if (l == 0)
10494 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010495 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010497 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498 return -1;
10499 }
10500 *fnamelen = l;
10501 }
10502 }
Bram Moolenaar4f974752019-02-17 17:44:42 +010010503#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504
10505 /* ":t" - tail, just the basename */
10506 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
10507 {
10508 *usedlen += 2;
10509 *fnamelen -= (int)(tail - *fnamep);
10510 *fnamep = tail;
10511 }
10512
10513 /* ":e" - extension, can be repeated */
10514 /* ":r" - root, without extension, can be repeated */
10515 while (src[*usedlen] == ':'
10516 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
10517 {
10518 /* find a '.' in the tail:
10519 * - for second :e: before the current fname
10520 * - otherwise: The last '.'
10521 */
10522 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
10523 s = *fnamep - 2;
10524 else
10525 s = *fnamep + *fnamelen - 1;
10526 for ( ; s > tail; --s)
10527 if (s[0] == '.')
10528 break;
10529 if (src[*usedlen + 1] == 'e') /* :e */
10530 {
10531 if (s > tail)
10532 {
10533 *fnamelen += (int)(*fnamep - (s + 1));
10534 *fnamep = s + 1;
10535#ifdef VMS
10536 /* cut version from the extension */
10537 s = *fnamep + *fnamelen - 1;
10538 for ( ; s > *fnamep; --s)
10539 if (s[0] == ';')
10540 break;
10541 if (s > *fnamep)
10542 *fnamelen = s - *fnamep;
10543#endif
10544 }
10545 else if (*fnamep <= tail)
10546 *fnamelen = 0;
10547 }
10548 else /* :r */
10549 {
10550 if (s > tail) /* remove one extension */
10551 *fnamelen = (int)(s - *fnamep);
10552 }
10553 *usedlen += 2;
10554 }
10555
10556 /* ":s?pat?foo?" - substitute */
10557 /* ":gs?pat?foo?" - global substitute */
10558 if (src[*usedlen] == ':'
10559 && (src[*usedlen + 1] == 's'
10560 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
10561 {
10562 char_u *str;
10563 char_u *pat;
10564 char_u *sub;
10565 int sep;
10566 char_u *flags;
10567 int didit = FALSE;
10568
10569 flags = (char_u *)"";
10570 s = src + *usedlen + 2;
10571 if (src[*usedlen + 1] == 'g')
10572 {
10573 flags = (char_u *)"g";
10574 ++s;
10575 }
10576
10577 sep = *s++;
10578 if (sep)
10579 {
10580 /* find end of pattern */
10581 p = vim_strchr(s, sep);
10582 if (p != NULL)
10583 {
10584 pat = vim_strnsave(s, (int)(p - s));
10585 if (pat != NULL)
10586 {
10587 s = p + 1;
10588 /* find end of substitution */
10589 p = vim_strchr(s, sep);
10590 if (p != NULL)
10591 {
10592 sub = vim_strnsave(s, (int)(p - s));
10593 str = vim_strnsave(*fnamep, *fnamelen);
10594 if (sub != NULL && str != NULL)
10595 {
10596 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010597 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598 if (s != NULL)
10599 {
10600 *fnamep = s;
10601 *fnamelen = (int)STRLEN(s);
10602 vim_free(*bufp);
10603 *bufp = s;
10604 didit = TRUE;
10605 }
10606 }
10607 vim_free(sub);
10608 vim_free(str);
10609 }
10610 vim_free(pat);
10611 }
10612 }
10613 /* after using ":s", repeat all the modifiers */
10614 if (didit)
10615 goto repeat;
10616 }
10617 }
10618
Bram Moolenaar26df0922014-02-23 23:39:13 +010010619 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10620 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010621 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010622 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010623 if (c != NUL)
10624 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010625 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010626 if (c != NUL)
10627 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010628 if (p == NULL)
10629 return -1;
10630 vim_free(*bufp);
10631 *bufp = *fnamep = p;
10632 *fnamelen = (int)STRLEN(p);
10633 *usedlen += 2;
10634 }
10635
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636 return valid;
10637}
10638
10639/*
10640 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010641 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642 * "flags" can be "g" to do a global substitute.
10643 * Returns an allocated string, NULL for error.
10644 */
10645 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010646do_string_sub(
10647 char_u *str,
10648 char_u *pat,
10649 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010650 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010651 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652{
10653 int sublen;
10654 regmatch_T regmatch;
10655 int i;
10656 int do_all;
10657 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010658 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659 garray_T ga;
10660 char_u *ret;
10661 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010662 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663
10664 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
10665 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010666 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010667
10668 ga_init2(&ga, 1, 200);
10669
10670 do_all = (flags[0] == 'g');
10671
10672 regmatch.rm_ic = p_ic;
10673 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10674 if (regmatch.regprog != NULL)
10675 {
10676 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010677 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
10679 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010010680 /* Skip empty match except for first match. */
10681 if (regmatch.startp[0] == regmatch.endp[0])
10682 {
10683 if (zero_width == regmatch.startp[0])
10684 {
10685 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020010686 i = MB_PTR2LEN(tail);
10687 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
10688 (size_t)i);
10689 ga.ga_len += i;
10690 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010691 continue;
10692 }
10693 zero_width = regmatch.startp[0];
10694 }
10695
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696 /*
10697 * Get some space for a temporary buffer to do the substitution
10698 * into. It will contain:
10699 * - The text up to where the match is.
10700 * - The substituted text.
10701 * - The text after the match.
10702 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010703 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010010704 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
10706 {
10707 ga_clear(&ga);
10708 break;
10709 }
10710
10711 /* copy the text up to where the match is */
10712 i = (int)(regmatch.startp[0] - tail);
10713 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10714 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010715 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010716 + ga.ga_len + i, TRUE, TRUE, FALSE);
10717 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010718 tail = regmatch.endp[0];
10719 if (*tail == NUL)
10720 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721 if (!do_all)
10722 break;
10723 }
10724
10725 if (ga.ga_data != NULL)
10726 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10727
Bram Moolenaar473de612013-06-08 18:19:48 +020010728 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010729 }
10730
10731 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10732 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010733 if (p_cpo == empty_option)
10734 p_cpo = save_cpo;
10735 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010736 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010737 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010738
10739 return ret;
10740}
10741
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010742 static int
10743filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10744{
10745 typval_T rettv;
10746 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010747 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010748
10749 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10750 argv[0] = vimvars[VV_KEY].vv_tv;
10751 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010752 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10753 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010754 if (map)
10755 {
10756 /* map(): replace the list item value */
10757 clear_tv(tv);
10758 rettv.v_lock = 0;
10759 *tv = rettv;
10760 }
10761 else
10762 {
10763 int error = FALSE;
10764
10765 /* filter(): when expr is zero remove the item */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010766 *remp = (tv_get_number_chk(&rettv, &error) == 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010767 clear_tv(&rettv);
10768 /* On type error, nothing has been removed; return FAIL to stop the
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010769 * loop. The error message was given by tv_get_number_chk(). */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010770 if (error)
10771 goto theend;
10772 }
10773 retval = OK;
10774theend:
10775 clear_tv(&vimvars[VV_VAL].vv_tv);
10776 return retval;
10777}
10778
10779
10780/*
10781 * Implementation of map() and filter().
10782 */
10783 void
10784filter_map(typval_T *argvars, typval_T *rettv, int map)
10785{
10786 typval_T *expr;
10787 listitem_T *li, *nli;
10788 list_T *l = NULL;
10789 dictitem_T *di;
10790 hashtab_T *ht;
10791 hashitem_T *hi;
10792 dict_T *d = NULL;
10793 typval_T save_val;
10794 typval_T save_key;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010795 blob_T *b = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010796 int rem;
10797 int todo;
10798 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10799 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10800 : N_("filter() argument"));
10801 int save_did_emsg;
10802 int idx = 0;
10803
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010804 if (argvars[0].v_type == VAR_BLOB)
10805 {
10806 if ((b = argvars[0].vval.v_blob) == NULL)
10807 return;
10808 }
10809 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010810 {
10811 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010812 || (!map && var_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010813 return;
10814 }
10815 else if (argvars[0].v_type == VAR_DICT)
10816 {
10817 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010818 || (!map && var_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010819 return;
10820 }
10821 else
10822 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010823 semsg(_(e_listdictarg), ermsg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010824 return;
10825 }
10826
10827 expr = &argvars[1];
10828 /* On type errors, the preceding call has already displayed an error
10829 * message. Avoid a misleading error message for an empty string that
10830 * was not passed as argument. */
10831 if (expr->v_type != VAR_UNKNOWN)
10832 {
10833 prepare_vimvar(VV_VAL, &save_val);
10834
10835 /* We reset "did_emsg" to be able to detect whether an error
10836 * occurred during evaluation of the expression. */
10837 save_did_emsg = did_emsg;
10838 did_emsg = FALSE;
10839
10840 prepare_vimvar(VV_KEY, &save_key);
10841 if (argvars[0].v_type == VAR_DICT)
10842 {
10843 vimvars[VV_KEY].vv_type = VAR_STRING;
10844
10845 ht = &d->dv_hashtab;
10846 hash_lock(ht);
10847 todo = (int)ht->ht_used;
10848 for (hi = ht->ht_array; todo > 0; ++hi)
10849 {
10850 if (!HASHITEM_EMPTY(hi))
10851 {
10852 int r;
10853
10854 --todo;
10855 di = HI2DI(hi);
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010856 if (map && (var_check_lock(di->di_tv.v_lock,
10857 arg_errmsg, TRUE)
10858 || var_check_ro(di->di_flags,
10859 arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010860 break;
10861 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10862 r = filter_map_one(&di->di_tv, expr, map, &rem);
10863 clear_tv(&vimvars[VV_KEY].vv_tv);
10864 if (r == FAIL || did_emsg)
10865 break;
10866 if (!map && rem)
10867 {
10868 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10869 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10870 break;
10871 dictitem_remove(d, di);
10872 }
10873 }
10874 }
10875 hash_unlock(ht);
10876 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010877 else if (argvars[0].v_type == VAR_BLOB)
10878 {
10879 int i;
10880 typval_T tv;
10881
10882 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10883 for (i = 0; i < b->bv_ga.ga_len; i++)
10884 {
10885 tv.v_type = VAR_NUMBER;
10886 tv.vval.v_number = blob_get(b, i);
10887 vimvars[VV_KEY].vv_nr = idx;
10888 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg)
10889 break;
10890 if (tv.v_type != VAR_NUMBER)
10891 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010892 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010893 return;
10894 }
10895 tv.v_type = VAR_NUMBER;
10896 blob_set(b, i, tv.vval.v_number);
10897 if (!map && rem)
10898 {
10899 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
10900
10901 mch_memmove(p + idx, p + i + 1,
10902 (size_t)b->bv_ga.ga_len - i - 1);
10903 --b->bv_ga.ga_len;
10904 --i;
10905 }
10906 }
10907 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010908 else
10909 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +010010910 // argvars[0].v_type == VAR_LIST
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010911 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10912
10913 for (li = l->lv_first; li != NULL; li = nli)
10914 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010915 if (map && var_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010916 break;
10917 nli = li->li_next;
10918 vimvars[VV_KEY].vv_nr = idx;
10919 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10920 || did_emsg)
10921 break;
10922 if (!map && rem)
10923 listitem_remove(l, li);
10924 ++idx;
10925 }
10926 }
10927
10928 restore_vimvar(VV_KEY, &save_key);
10929 restore_vimvar(VV_VAL, &save_val);
10930
10931 did_emsg |= save_did_emsg;
10932 }
10933
10934 copy_tv(&argvars[0], rettv);
10935}
10936
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */