blob: 9e1ac6d99363d8e6ba95162862eecbeb8c81cb2d [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 Moolenaar4228bec2011-03-27 16:03:15 +02002141 return NULL;
2142 }
2143
Bram Moolenaar31b81602019-02-10 22:14:27 +01002144 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002145 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002146 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002147 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002148 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002149 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002150 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002151 }
2152 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002153 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002154 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002155 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002156 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002157 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002158 p = NULL;
2159 break;
2160 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002161 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002162 else if ((flags & GLV_READ_ONLY) == 0
2163 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002164 {
2165 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002166 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002167 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002168
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002169 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002170 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002171 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002172 else if (lp->ll_tv->v_type == VAR_BLOB)
2173 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002174 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
2175
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002176 /*
2177 * Get the number and item for the only or first index of the List.
2178 */
2179 if (empty1)
2180 lp->ll_n1 = 0;
2181 else
2182 // is number or string
2183 lp->ll_n1 = (long)tv_get_number(&var1);
2184 clear_tv(&var1);
2185
2186 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002187 || lp->ll_n1 > bloblen
2188 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002189 {
2190 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002191 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002192 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002193 return NULL;
2194 }
2195 if (lp->ll_range && !lp->ll_empty2)
2196 {
2197 lp->ll_n2 = (long)tv_get_number(&var2);
2198 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002199 if (lp->ll_n2 < 0
2200 || lp->ll_n2 >= bloblen
2201 || lp->ll_n2 < lp->ll_n1)
2202 {
2203 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002204 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002205 return NULL;
2206 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002207 }
2208 lp->ll_blob = lp->ll_tv->vval.v_blob;
2209 lp->ll_tv = NULL;
2210 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002211 else
2212 {
2213 /*
2214 * Get the number and item for the only or first index of the List.
2215 */
2216 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002217 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002218 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002219 /* is number or string */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002220 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002221 clear_tv(&var1);
2222
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002223 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002224 lp->ll_list = lp->ll_tv->vval.v_list;
2225 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2226 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002227 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002228 if (lp->ll_n1 < 0)
2229 {
2230 lp->ll_n1 = 0;
2231 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2232 }
2233 }
2234 if (lp->ll_li == NULL)
2235 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002236 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002237 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002238 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002239 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002240 }
2241
2242 /*
2243 * May need to find the item or absolute index for the second
2244 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002245 * When no index given: "lp->ll_empty2" is TRUE.
2246 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002247 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002248 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002249 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002250 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002251 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002252 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002253 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002254 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002255 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002256 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002257 {
2258 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002259 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002260 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002261 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002262 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002263 }
2264
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002265 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2266 if (lp->ll_n1 < 0)
2267 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2268 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002269 {
2270 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002271 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002272 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002273 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002274 }
2275
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002276 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002277 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002278 }
2279
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002280 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002281 return p;
2282}
2283
2284/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002285 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002286 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002287 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002288clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002289{
2290 vim_free(lp->ll_exp_name);
2291 vim_free(lp->ll_newkey);
2292}
2293
2294/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002295 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002296 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01002297 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
2298 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002299 */
2300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002301set_var_lval(
2302 lval_T *lp,
2303 char_u *endp,
2304 typval_T *rettv,
2305 int copy,
2306 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002307{
2308 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002309 listitem_T *ri;
2310 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002311
2312 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002313 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002314 cc = *endp;
2315 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002316 if (lp->ll_blob != NULL)
2317 {
2318 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002319
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002320 if (op != NULL && *op != '=')
2321 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002322 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002323 return;
2324 }
2325
2326 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2327 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002328 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002329
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002330 if (lp->ll_empty2)
2331 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
2332
2333 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002334 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002335 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002336 return;
2337 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002338 if (lp->ll_empty2)
2339 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002340
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002341 ir = 0;
2342 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
2343 blob_set(lp->ll_blob, il,
2344 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002345 }
2346 else
2347 {
2348 val = (int)tv_get_number_chk(rettv, &error);
2349 if (!error)
2350 {
2351 garray_T *gap = &lp->ll_blob->bv_ga;
2352
2353 // Allow for appending a byte. Setting a byte beyond
2354 // the end is an error otherwise.
2355 if (lp->ll_n1 < gap->ga_len
2356 || (lp->ll_n1 == gap->ga_len
2357 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
2358 {
2359 blob_set(lp->ll_blob, lp->ll_n1, val);
2360 if (lp->ll_n1 == gap->ga_len)
2361 ++gap->ga_len;
2362 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002363 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002364 }
2365 }
2366 }
2367 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002368 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002369 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002370
Bram Moolenaarff697e62019-02-12 22:28:33 +01002371 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01002372 di = NULL;
2373 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2374 &tv, &di, TRUE, FALSE) == OK)
2375 {
2376 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002377 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2378 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01002379 && tv_op(&tv, rettv, op) == OK)
2380 set_var(lp->ll_name, &tv, FALSE);
2381 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002382 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002383 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002384 else
2385 set_var(lp->ll_name, rettv, copy);
2386 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002387 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002388 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002389 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002390 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002391 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002392 else if (lp->ll_range)
2393 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002394 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002395 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002396
2397 /*
2398 * Check whether any of the list items is locked
2399 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002400 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002401 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002402 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002403 return;
2404 ri = ri->li_next;
2405 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2406 break;
2407 ll_li = ll_li->li_next;
2408 ++ll_n1;
2409 }
2410
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002411 /*
2412 * Assign the List values to the list items.
2413 */
2414 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002415 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002416 if (op != NULL && *op != '=')
2417 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2418 else
2419 {
2420 clear_tv(&lp->ll_li->li_tv);
2421 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2422 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002423 ri = ri->li_next;
2424 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2425 break;
2426 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002427 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002428 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002429 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002430 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002431 ri = NULL;
2432 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002433 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002434 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002435 lp->ll_li = lp->ll_li->li_next;
2436 ++lp->ll_n1;
2437 }
2438 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002439 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002440 else if (lp->ll_empty2
2441 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002442 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002443 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002444 }
2445 else
2446 {
2447 /*
2448 * Assign to a List or Dictionary item.
2449 */
2450 if (lp->ll_newkey != NULL)
2451 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002452 if (op != NULL && *op != '=')
2453 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002454 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002455 return;
2456 }
2457
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002458 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002459 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002460 if (di == NULL)
2461 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002462 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2463 {
2464 vim_free(di);
2465 return;
2466 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002467 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002468 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002469 else if (op != NULL && *op != '=')
2470 {
2471 tv_op(lp->ll_tv, rettv, op);
2472 return;
2473 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002474 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002475 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002476
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002477 /*
2478 * Assign the value to the variable or list item.
2479 */
2480 if (copy)
2481 copy_tv(rettv, lp->ll_tv);
2482 else
2483 {
2484 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002485 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002486 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002487 }
2488 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002489}
2490
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002491/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01002492 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2493 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002494 * Returns OK or FAIL.
2495 */
2496 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002497tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002498{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002499 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002500 char_u numbuf[NUMBUFLEN];
2501 char_u *s;
2502
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002503 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2504 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2505 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002506 {
2507 switch (tv1->v_type)
2508 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002509 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002510 case VAR_DICT:
2511 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002512 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002513 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002514 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002515 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002516 break;
2517
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002518 case VAR_BLOB:
2519 if (*op != '+' || tv2->v_type != VAR_BLOB)
2520 break;
2521 // BLOB += BLOB
2522 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2523 {
2524 blob_T *b1 = tv1->vval.v_blob;
2525 blob_T *b2 = tv2->vval.v_blob;
2526 int i, len = blob_len(b2);
2527 for (i = 0; i < len; i++)
2528 ga_append(&b1->bv_ga, blob_get(b2, i));
2529 }
2530 return OK;
2531
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002532 case VAR_LIST:
2533 if (*op != '+' || tv2->v_type != VAR_LIST)
2534 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002535 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002536 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2537 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2538 return OK;
2539
2540 case VAR_NUMBER:
2541 case VAR_STRING:
2542 if (tv2->v_type == VAR_LIST)
2543 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002544 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002545 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002546 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002547 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002548#ifdef FEAT_FLOAT
2549 if (tv2->v_type == VAR_FLOAT)
2550 {
2551 float_T f = n;
2552
Bram Moolenaarff697e62019-02-12 22:28:33 +01002553 if (*op == '%')
2554 break;
2555 switch (*op)
2556 {
2557 case '+': f += tv2->vval.v_float; break;
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 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002562 clear_tv(tv1);
2563 tv1->v_type = VAR_FLOAT;
2564 tv1->vval.v_float = f;
2565 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002566 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002567#endif
2568 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002569 switch (*op)
2570 {
2571 case '+': n += tv_get_number(tv2); break;
2572 case '-': n -= tv_get_number(tv2); break;
2573 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01002574 case '/': n = num_divide(n, tv_get_number(tv2)); break;
2575 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002576 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002577 clear_tv(tv1);
2578 tv1->v_type = VAR_NUMBER;
2579 tv1->vval.v_number = n;
2580 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002581 }
2582 else
2583 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002584 if (tv2->v_type == VAR_FLOAT)
2585 break;
2586
Bram Moolenaarff697e62019-02-12 22:28:33 +01002587 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002588 s = tv_get_string(tv1);
2589 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002590 clear_tv(tv1);
2591 tv1->v_type = VAR_STRING;
2592 tv1->vval.v_string = s;
2593 }
2594 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002595
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002596 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002597#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002598 {
2599 float_T f;
2600
Bram Moolenaarff697e62019-02-12 22:28:33 +01002601 if (*op == '%' || *op == '.'
2602 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002603 && tv2->v_type != VAR_NUMBER
2604 && tv2->v_type != VAR_STRING))
2605 break;
2606 if (tv2->v_type == VAR_FLOAT)
2607 f = tv2->vval.v_float;
2608 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002609 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01002610 switch (*op)
2611 {
2612 case '+': tv1->vval.v_float += f; break;
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 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002617 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002618#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002619 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002620 }
2621 }
2622
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002623 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002624 return FAIL;
2625}
2626
2627/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002628 * Evaluate the expression used in a ":for var in expr" command.
2629 * "arg" points to "var".
2630 * Set "*errp" to TRUE for an error, FALSE otherwise;
2631 * Return a pointer that holds the info. Null when there is an error.
2632 */
2633 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002634eval_for_line(
2635 char_u *arg,
2636 int *errp,
2637 char_u **nextcmdp,
2638 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002639{
Bram Moolenaar33570922005-01-25 22:26:29 +00002640 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002641 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002642 typval_T tv;
2643 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002644
2645 *errp = TRUE; /* default: there is an error */
2646
Bram Moolenaar33570922005-01-25 22:26:29 +00002647 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002648 if (fi == NULL)
2649 return NULL;
2650
2651 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2652 if (expr == NULL)
2653 return fi;
2654
2655 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002656 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002657 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002658 emsg(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002659 return fi;
2660 }
2661
2662 if (skip)
2663 ++emsg_skip;
2664 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2665 {
2666 *errp = FALSE;
2667 if (!skip)
2668 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002669 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002670 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002671 l = tv.vval.v_list;
2672 if (l == NULL)
2673 {
2674 // a null list is like an empty list: do nothing
2675 clear_tv(&tv);
2676 }
2677 else
2678 {
2679 // No need to increment the refcount, it's already set for
2680 // the list being used in "tv".
2681 fi->fi_list = l;
2682 list_add_watch(l, &fi->fi_lw);
2683 fi->fi_lw.lw_item = l->lv_first;
2684 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002685 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002686 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002687 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002688 fi->fi_bi = 0;
2689 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002690 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002691 typval_T btv;
2692
2693 // Make a copy, so that the iteration still works when the
2694 // blob is changed.
2695 blob_copy(&tv, &btv);
2696 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002697 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002698 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002699 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002700 else
2701 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002702 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002703 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002704 }
2705 }
2706 }
2707 if (skip)
2708 --emsg_skip;
2709
2710 return fi;
2711}
2712
2713/*
2714 * Use the first item in a ":for" list. Advance to the next.
2715 * Assign the values to the variable (list). "arg" points to the first one.
2716 * Return TRUE when a valid item was found, FALSE when at end of list or
2717 * something wrong.
2718 */
2719 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002720next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002721{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002722 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002723 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002724 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002725
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002726 if (fi->fi_blob != NULL)
2727 {
2728 typval_T tv;
2729
2730 if (fi->fi_bi >= blob_len(fi->fi_blob))
2731 return FALSE;
2732 tv.v_type = VAR_NUMBER;
2733 tv.v_lock = VAR_FIXED;
2734 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2735 ++fi->fi_bi;
2736 return ex_let_vars(arg, &tv, TRUE,
2737 fi->fi_semicolon, fi->fi_varcount, NULL) == OK;
2738 }
2739
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002740 item = fi->fi_lw.lw_item;
2741 if (item == NULL)
2742 result = FALSE;
2743 else
2744 {
2745 fi->fi_lw.lw_item = item->li_next;
2746 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2747 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2748 }
2749 return result;
2750}
2751
2752/*
2753 * Free the structure used to store info used by ":for".
2754 */
2755 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002756free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002757{
Bram Moolenaar33570922005-01-25 22:26:29 +00002758 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002759
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002760 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002761 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002762 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002763 list_unref(fi->fi_list);
2764 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002765 if (fi != NULL && fi->fi_blob != NULL)
2766 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002767 vim_free(fi);
2768}
2769
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2771
2772 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002773set_context_for_expression(
2774 expand_T *xp,
2775 char_u *arg,
2776 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777{
2778 int got_eq = FALSE;
2779 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002780 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002782 if (cmdidx == CMD_let)
2783 {
2784 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002785 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002786 {
2787 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002788 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002789 {
2790 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002791 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002792 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002793 break;
2794 }
2795 return;
2796 }
2797 }
2798 else
2799 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2800 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 while ((xp->xp_pattern = vim_strpbrk(arg,
2802 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2803 {
2804 c = *xp->xp_pattern;
2805 if (c == '&')
2806 {
2807 c = xp->xp_pattern[1];
2808 if (c == '&')
2809 {
2810 ++xp->xp_pattern;
2811 xp->xp_context = cmdidx != CMD_let || got_eq
2812 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2813 }
2814 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002815 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002817 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2818 xp->xp_pattern += 2;
2819
2820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 }
2822 else if (c == '$')
2823 {
2824 /* environment variable */
2825 xp->xp_context = EXPAND_ENV_VARS;
2826 }
2827 else if (c == '=')
2828 {
2829 got_eq = TRUE;
2830 xp->xp_context = EXPAND_EXPRESSION;
2831 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002832 else if (c == '#'
2833 && xp->xp_context == EXPAND_EXPRESSION)
2834 {
2835 /* Autoload function/variable contains '#'. */
2836 break;
2837 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002838 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 && xp->xp_context == EXPAND_FUNCTIONS
2840 && vim_strchr(xp->xp_pattern, '(') == NULL)
2841 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002842 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 break;
2844 }
2845 else if (cmdidx != CMD_let || got_eq)
2846 {
2847 if (c == '"') /* string */
2848 {
2849 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2850 if (c == '\\' && xp->xp_pattern[1] != NUL)
2851 ++xp->xp_pattern;
2852 xp->xp_context = EXPAND_NOTHING;
2853 }
2854 else if (c == '\'') /* literal string */
2855 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002856 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2858 /* skip */ ;
2859 xp->xp_context = EXPAND_NOTHING;
2860 }
2861 else if (c == '|')
2862 {
2863 if (xp->xp_pattern[1] == '|')
2864 {
2865 ++xp->xp_pattern;
2866 xp->xp_context = EXPAND_EXPRESSION;
2867 }
2868 else
2869 xp->xp_context = EXPAND_COMMANDS;
2870 }
2871 else
2872 xp->xp_context = EXPAND_EXPRESSION;
2873 }
2874 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002875 /* Doesn't look like something valid, expand as an expression
2876 * anyway. */
2877 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 arg = xp->xp_pattern;
2879 if (*arg != NUL)
2880 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2881 /* skip */ ;
2882 }
2883 xp->xp_pattern = arg;
2884}
2885
2886#endif /* FEAT_CMDL_COMPL */
2887
2888/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 * ":unlet[!] var1 ... " command.
2890 */
2891 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002892ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002894 ex_unletlock(eap, eap->arg, 0);
2895}
2896
2897/*
2898 * ":lockvar" and ":unlockvar" commands
2899 */
2900 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002901ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002902{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002904 int deep = 2;
2905
2906 if (eap->forceit)
2907 deep = -1;
2908 else if (vim_isdigit(*arg))
2909 {
2910 deep = getdigits(&arg);
2911 arg = skipwhite(arg);
2912 }
2913
2914 ex_unletlock(eap, arg, deep);
2915}
2916
2917/*
2918 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2919 */
2920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002921ex_unletlock(
2922 exarg_T *eap,
2923 char_u *argstart,
2924 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002925{
2926 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002929 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930
2931 do
2932 {
Bram Moolenaar137374f2018-05-13 15:59:50 +02002933 if (*arg == '$')
2934 {
2935 char_u *name = ++arg;
2936
2937 if (get_env_len(&arg) == 0)
2938 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002939 semsg(_(e_invarg2), name - 1);
Bram Moolenaar137374f2018-05-13 15:59:50 +02002940 return;
2941 }
2942 vim_unsetenv(name);
2943 arg = skipwhite(arg);
2944 continue;
2945 }
2946
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002947 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002948 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002949 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002950 if (lv.ll_name == NULL)
2951 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002952 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002953 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002955 if (name_end != NULL)
2956 {
2957 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002958 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002959 }
2960 if (!(eap->skip || error))
2961 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 break;
2963 }
2964
2965 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002966 {
2967 if (eap->cmdidx == CMD_unlet)
2968 {
2969 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2970 error = TRUE;
2971 }
2972 else
2973 {
2974 if (do_lock_var(&lv, name_end, deep,
2975 eap->cmdidx == CMD_lockvar) == FAIL)
2976 error = TRUE;
2977 }
2978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002980 if (!eap->skip)
2981 clear_lval(&lv);
2982
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 arg = skipwhite(name_end);
2984 } while (!ends_excmd(*arg));
2985
2986 eap->nextcmd = check_nextcmd(arg);
2987}
2988
Bram Moolenaar8c711452005-01-14 21:53:12 +00002989 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002990do_unlet_var(
2991 lval_T *lp,
2992 char_u *name_end,
2993 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002994{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002995 int ret = OK;
2996 int cc;
2997
2998 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002999 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003000 cc = *name_end;
3001 *name_end = NUL;
3002
3003 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003004 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003005 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003006 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003007 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003008 else if ((lp->ll_list != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003009 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003010 || (lp->ll_dict != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003011 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003012 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003013 else if (lp->ll_range)
3014 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003015 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003016 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003017 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003018
3019 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3020 {
3021 li = ll_li->li_next;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003022 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003023 return FAIL;
3024 ll_li = li;
3025 ++ll_n1;
3026 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003027
3028 /* Delete a range of List items. */
3029 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3030 {
3031 li = lp->ll_li->li_next;
3032 listitem_remove(lp->ll_list, lp->ll_li);
3033 lp->ll_li = li;
3034 ++lp->ll_n1;
3035 }
3036 }
3037 else
3038 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003039 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003040 /* unlet a List item. */
3041 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003042 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003043 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003044 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003045 }
3046
3047 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003048}
3049
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050/*
3051 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003052 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 */
3054 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003055do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056{
Bram Moolenaar33570922005-01-25 22:26:29 +00003057 hashtab_T *ht;
3058 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003059 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003060 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003061 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062
Bram Moolenaar33570922005-01-25 22:26:29 +00003063 ht = find_var_ht(name, &varname);
3064 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003066 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003067 if (d == NULL)
3068 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003069 if (ht == &globvarht)
3070 d = &globvardict;
3071 else if (ht == &compat_hashtab)
3072 d = &vimvardict;
3073 else
3074 {
3075 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3076 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3077 }
3078 if (d == NULL)
3079 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003080 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003081 return FAIL;
3082 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003083 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003084 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003085 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003086 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003087 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003088 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003089 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003090 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003091 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003092 || var_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003093 return FAIL;
3094
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003095 delete_var(ht, hi);
3096 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003099 if (forceit)
3100 return OK;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003101 semsg(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 return FAIL;
3103}
3104
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003105/*
3106 * Lock or unlock variable indicated by "lp".
3107 * "deep" is the levels to go (-1 for unlimited);
3108 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3109 */
3110 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003111do_lock_var(
3112 lval_T *lp,
3113 char_u *name_end,
3114 int deep,
3115 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003116{
3117 int ret = OK;
3118 int cc;
3119 dictitem_T *di;
3120
3121 if (deep == 0) /* nothing to do */
3122 return OK;
3123
3124 if (lp->ll_tv == NULL)
3125 {
3126 cc = *name_end;
3127 *name_end = NUL;
3128
3129 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003130 di = find_var(lp->ll_name, NULL, TRUE);
3131 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003132 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01003133 else if ((di->di_flags & DI_FLAGS_FIX)
3134 && di->di_tv.v_type != VAR_DICT
3135 && di->di_tv.v_type != VAR_LIST)
3136 /* For historic reasons this error is not given for a list or dict.
3137 * E.g., the b: dict could be locked/unlocked. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003138 semsg(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003139 else
3140 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01003141 if (lock)
3142 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003143 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01003144 di->di_flags &= ~DI_FLAGS_LOCK;
3145 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003146 }
3147 *name_end = cc;
3148 }
3149 else if (lp->ll_range)
3150 {
3151 listitem_T *li = lp->ll_li;
3152
3153 /* (un)lock a range of List items. */
3154 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3155 {
3156 item_lock(&li->li_tv, deep, lock);
3157 li = li->li_next;
3158 ++lp->ll_n1;
3159 }
3160 }
3161 else if (lp->ll_list != NULL)
3162 /* (un)lock a List item. */
3163 item_lock(&lp->ll_li->li_tv, deep, lock);
3164 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003165 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003166 item_lock(&lp->ll_di->di_tv, deep, lock);
3167
3168 return ret;
3169}
3170
3171/*
3172 * Lock or unlock an item. "deep" is nr of levels to go.
3173 */
3174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003175item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003176{
3177 static int recurse = 0;
3178 list_T *l;
3179 listitem_T *li;
3180 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003181 blob_T *b;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003182 hashitem_T *hi;
3183 int todo;
3184
3185 if (recurse >= DICT_MAXNEST)
3186 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003187 emsg(_("E743: variable nested too deep for (un)lock"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003188 return;
3189 }
3190 if (deep == 0)
3191 return;
3192 ++recurse;
3193
3194 /* lock/unlock the item itself */
3195 if (lock)
3196 tv->v_lock |= VAR_LOCKED;
3197 else
3198 tv->v_lock &= ~VAR_LOCKED;
3199
3200 switch (tv->v_type)
3201 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003202 case VAR_UNKNOWN:
3203 case VAR_NUMBER:
3204 case VAR_STRING:
3205 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003206 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003207 case VAR_FLOAT:
3208 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003209 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003210 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003211 break;
3212
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003213 case VAR_BLOB:
3214 if ((b = tv->vval.v_blob) != NULL)
3215 {
3216 if (lock)
3217 b->bv_lock |= VAR_LOCKED;
3218 else
3219 b->bv_lock &= ~VAR_LOCKED;
3220 }
3221 break;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003222 case VAR_LIST:
3223 if ((l = tv->vval.v_list) != NULL)
3224 {
3225 if (lock)
3226 l->lv_lock |= VAR_LOCKED;
3227 else
3228 l->lv_lock &= ~VAR_LOCKED;
3229 if (deep < 0 || deep > 1)
3230 /* recursive: lock/unlock the items the List contains */
3231 for (li = l->lv_first; li != NULL; li = li->li_next)
3232 item_lock(&li->li_tv, deep - 1, lock);
3233 }
3234 break;
3235 case VAR_DICT:
3236 if ((d = tv->vval.v_dict) != NULL)
3237 {
3238 if (lock)
3239 d->dv_lock |= VAR_LOCKED;
3240 else
3241 d->dv_lock &= ~VAR_LOCKED;
3242 if (deep < 0 || deep > 1)
3243 {
3244 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003245 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003246 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3247 {
3248 if (!HASHITEM_EMPTY(hi))
3249 {
3250 --todo;
3251 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3252 }
3253 }
3254 }
3255 }
3256 }
3257 --recurse;
3258}
3259
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3261/*
3262 * Delete all "menutrans_" variables.
3263 */
3264 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003265del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266{
Bram Moolenaar33570922005-01-25 22:26:29 +00003267 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003268 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269
Bram Moolenaar33570922005-01-25 22:26:29 +00003270 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003271 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003272 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003273 {
3274 if (!HASHITEM_EMPTY(hi))
3275 {
3276 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003277 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3278 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003279 }
3280 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003281 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282}
3283#endif
3284
3285#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3286
3287/*
3288 * Local string buffer for the next two functions to store a variable name
3289 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3290 * get_user_var_name().
3291 */
3292
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293static char_u *varnamebuf = NULL;
3294static int varnamebuflen = 0;
3295
3296/*
3297 * Function to concatenate a prefix and a variable name.
3298 */
3299 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003300cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301{
3302 int len;
3303
3304 len = (int)STRLEN(name) + 3;
3305 if (len > varnamebuflen)
3306 {
3307 vim_free(varnamebuf);
3308 len += 10; /* some additional space */
3309 varnamebuf = alloc(len);
3310 if (varnamebuf == NULL)
3311 {
3312 varnamebuflen = 0;
3313 return NULL;
3314 }
3315 varnamebuflen = len;
3316 }
3317 *varnamebuf = prefix;
3318 varnamebuf[1] = ':';
3319 STRCPY(varnamebuf + 2, name);
3320 return varnamebuf;
3321}
3322
3323/*
3324 * Function given to ExpandGeneric() to obtain the list of user defined
3325 * (global/buffer/window/built-in) variable names.
3326 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003328get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003330 static long_u gdone;
3331 static long_u bdone;
3332 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003333 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003334 static int vidx;
3335 static hashitem_T *hi;
3336 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337
3338 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003339 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003340 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003341 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003342 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003343
3344 /* Global variables */
3345 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003347 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003348 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003349 else
3350 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003351 while (HASHITEM_EMPTY(hi))
3352 ++hi;
3353 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3354 return cat_prefix_varname('g', hi->hi_key);
3355 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003357
3358 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003359 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003360 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003362 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003363 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003364 else
3365 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003366 while (HASHITEM_EMPTY(hi))
3367 ++hi;
3368 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003370
3371 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003372 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003373 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003375 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003376 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003377 else
3378 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003379 while (HASHITEM_EMPTY(hi))
3380 ++hi;
3381 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003383
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003384 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003385 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003386 if (tdone < ht->ht_used)
3387 {
3388 if (tdone++ == 0)
3389 hi = ht->ht_array;
3390 else
3391 ++hi;
3392 while (HASHITEM_EMPTY(hi))
3393 ++hi;
3394 return cat_prefix_varname('t', hi->hi_key);
3395 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003396
Bram Moolenaar33570922005-01-25 22:26:29 +00003397 /* v: variables */
3398 if (vidx < VV_LEN)
3399 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400
Bram Moolenaard23a8232018-02-10 18:45:26 +01003401 VIM_CLEAR(varnamebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 varnamebuflen = 0;
3403 return NULL;
3404}
3405
3406#endif /* FEAT_CMDL_COMPL */
3407
3408/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003409 * Return TRUE if "pat" matches "text".
3410 * Does not use 'cpo' and always uses 'magic'.
3411 */
3412 static int
3413pattern_match(char_u *pat, char_u *text, int ic)
3414{
3415 int matches = FALSE;
3416 char_u *save_cpo;
3417 regmatch_T regmatch;
3418
3419 /* avoid 'l' flag in 'cpoptions' */
3420 save_cpo = p_cpo;
3421 p_cpo = (char_u *)"";
3422 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3423 if (regmatch.regprog != NULL)
3424 {
3425 regmatch.rm_ic = ic;
3426 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3427 vim_regfree(regmatch.regprog);
3428 }
3429 p_cpo = save_cpo;
3430 return matches;
3431}
3432
3433/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003435 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3437 */
3438
3439/*
3440 * Handle zero level expression.
3441 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003442 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003443 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 * Return OK or FAIL.
3445 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003446 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003447eval0(
3448 char_u *arg,
3449 typval_T *rettv,
3450 char_u **nextcmd,
3451 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452{
3453 int ret;
3454 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003455 int did_emsg_before = did_emsg;
3456 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457
3458 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003459 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 if (ret == FAIL || !ends_excmd(*p))
3461 {
3462 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003463 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 /*
3465 * Report the invalid expression unless the expression evaluation has
3466 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003467 * exception, or we already gave a more specific error.
3468 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003470 if (!aborting() && did_emsg == did_emsg_before
3471 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003472 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 ret = FAIL;
3474 }
3475 if (nextcmd != NULL)
3476 *nextcmd = check_nextcmd(p);
3477
3478 return ret;
3479}
3480
3481/*
3482 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003483 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 *
3485 * "arg" must point to the first non-white of the expression.
3486 * "arg" is advanced to the next non-white after the recognized expression.
3487 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003488 * Note: "rettv.v_lock" is not set.
3489 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 * Return OK or FAIL.
3491 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003492 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003493eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494{
3495 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003496 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497
3498 /*
3499 * Get the first variable.
3500 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003501 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 return FAIL;
3503
3504 if ((*arg)[0] == '?')
3505 {
3506 result = FALSE;
3507 if (evaluate)
3508 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003509 int error = FALSE;
3510
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003511 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003513 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003514 if (error)
3515 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 }
3517
3518 /*
3519 * Get the second variable.
3520 */
3521 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003522 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 return FAIL;
3524
3525 /*
3526 * Check for the ":".
3527 */
3528 if ((*arg)[0] != ':')
3529 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003530 emsg(_("E109: Missing ':' after '?'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003532 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 return FAIL;
3534 }
3535
3536 /*
3537 * Get the third variable.
3538 */
3539 *arg = skipwhite(*arg + 1);
3540 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3541 {
3542 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003543 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 return FAIL;
3545 }
3546 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003547 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 }
3549
3550 return OK;
3551}
3552
3553/*
3554 * Handle first level expression:
3555 * expr2 || expr2 || expr2 logical OR
3556 *
3557 * "arg" must point to the first non-white of the expression.
3558 * "arg" is advanced to the next non-white after the recognized expression.
3559 *
3560 * Return OK or FAIL.
3561 */
3562 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003563eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564{
Bram Moolenaar33570922005-01-25 22:26:29 +00003565 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 long result;
3567 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003568 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569
3570 /*
3571 * Get the first variable.
3572 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003573 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 return FAIL;
3575
3576 /*
3577 * Repeat until there is no following "||".
3578 */
3579 first = TRUE;
3580 result = FALSE;
3581 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3582 {
3583 if (evaluate && first)
3584 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003585 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003587 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003588 if (error)
3589 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 first = FALSE;
3591 }
3592
3593 /*
3594 * Get the second variable.
3595 */
3596 *arg = skipwhite(*arg + 2);
3597 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3598 return FAIL;
3599
3600 /*
3601 * Compute the result.
3602 */
3603 if (evaluate && !result)
3604 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003605 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003607 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003608 if (error)
3609 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 }
3611 if (evaluate)
3612 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003613 rettv->v_type = VAR_NUMBER;
3614 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 }
3616 }
3617
3618 return OK;
3619}
3620
3621/*
3622 * Handle second level expression:
3623 * expr3 && expr3 && expr3 logical AND
3624 *
3625 * "arg" must point to the first non-white of the expression.
3626 * "arg" is advanced to the next non-white after the recognized expression.
3627 *
3628 * Return OK or FAIL.
3629 */
3630 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003631eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632{
Bram Moolenaar33570922005-01-25 22:26:29 +00003633 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 long result;
3635 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003636 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637
3638 /*
3639 * Get the first variable.
3640 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003641 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 return FAIL;
3643
3644 /*
3645 * Repeat until there is no following "&&".
3646 */
3647 first = TRUE;
3648 result = TRUE;
3649 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3650 {
3651 if (evaluate && first)
3652 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003653 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003655 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003656 if (error)
3657 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 first = FALSE;
3659 }
3660
3661 /*
3662 * Get the second variable.
3663 */
3664 *arg = skipwhite(*arg + 2);
3665 if (eval4(arg, &var2, evaluate && result) == FAIL)
3666 return FAIL;
3667
3668 /*
3669 * Compute the result.
3670 */
3671 if (evaluate && result)
3672 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003673 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003675 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003676 if (error)
3677 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 }
3679 if (evaluate)
3680 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003681 rettv->v_type = VAR_NUMBER;
3682 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 }
3684 }
3685
3686 return OK;
3687}
3688
3689/*
3690 * Handle third level expression:
3691 * var1 == var2
3692 * var1 =~ var2
3693 * var1 != var2
3694 * var1 !~ var2
3695 * var1 > var2
3696 * var1 >= var2
3697 * var1 < var2
3698 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003699 * var1 is var2
3700 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 *
3702 * "arg" must point to the first non-white of the expression.
3703 * "arg" is advanced to the next non-white after the recognized expression.
3704 *
3705 * Return OK or FAIL.
3706 */
3707 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003708eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709{
Bram Moolenaar33570922005-01-25 22:26:29 +00003710 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 char_u *p;
3712 int i;
3713 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003714 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717
3718 /*
3719 * Get the first variable.
3720 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003721 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 return FAIL;
3723
3724 p = *arg;
3725 switch (p[0])
3726 {
3727 case '=': if (p[1] == '=')
3728 type = TYPE_EQUAL;
3729 else if (p[1] == '~')
3730 type = TYPE_MATCH;
3731 break;
3732 case '!': if (p[1] == '=')
3733 type = TYPE_NEQUAL;
3734 else if (p[1] == '~')
3735 type = TYPE_NOMATCH;
3736 break;
3737 case '>': if (p[1] != '=')
3738 {
3739 type = TYPE_GREATER;
3740 len = 1;
3741 }
3742 else
3743 type = TYPE_GEQUAL;
3744 break;
3745 case '<': if (p[1] != '=')
3746 {
3747 type = TYPE_SMALLER;
3748 len = 1;
3749 }
3750 else
3751 type = TYPE_SEQUAL;
3752 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003753 case 'i': if (p[1] == 's')
3754 {
3755 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3756 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003757 i = p[len];
3758 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003759 {
3760 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3761 type_is = TRUE;
3762 }
3763 }
3764 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 }
3766
3767 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003768 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 */
3770 if (type != TYPE_UNKNOWN)
3771 {
3772 /* extra question mark appended: ignore case */
3773 if (p[len] == '?')
3774 {
3775 ic = TRUE;
3776 ++len;
3777 }
3778 /* extra '#' appended: match case */
3779 else if (p[len] == '#')
3780 {
3781 ic = FALSE;
3782 ++len;
3783 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003784 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 else
3786 ic = p_ic;
3787
3788 /*
3789 * Get the second variable.
3790 */
3791 *arg = skipwhite(p + len);
3792 if (eval5(arg, &var2, evaluate) == FAIL)
3793 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003794 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 return FAIL;
3796 }
Bram Moolenaar31988702018-02-13 12:57:42 +01003797 if (evaluate)
3798 {
3799 int ret = typval_compare(rettv, &var2, type, type_is, ic);
3800
3801 clear_tv(&var2);
3802 return ret;
3803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 }
3805
3806 return OK;
3807}
3808
3809/*
3810 * Handle fourth level expression:
3811 * + number addition
3812 * - number subtraction
3813 * . string concatenation
3814 *
3815 * "arg" must point to the first non-white of the expression.
3816 * "arg" is advanced to the next non-white after the recognized expression.
3817 *
3818 * Return OK or FAIL.
3819 */
3820 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003821eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822{
Bram Moolenaar33570922005-01-25 22:26:29 +00003823 typval_T var2;
3824 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003826 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003827#ifdef FEAT_FLOAT
3828 float_T f1 = 0, f2 = 0;
3829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 char_u *s1, *s2;
3831 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3832 char_u *p;
3833
3834 /*
3835 * Get the first variable.
3836 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003837 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 return FAIL;
3839
3840 /*
3841 * Repeat computing, until no '+', '-' or '.' is following.
3842 */
3843 for (;;)
3844 {
3845 op = **arg;
3846 if (op != '+' && op != '-' && op != '.')
3847 break;
3848
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003849 if ((op != '+' || (rettv->v_type != VAR_LIST
3850 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003851#ifdef FEAT_FLOAT
3852 && (op == '.' || rettv->v_type != VAR_FLOAT)
3853#endif
3854 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003855 {
3856 /* For "list + ...", an illegal use of the first operand as
3857 * a number cannot be determined before evaluating the 2nd
3858 * operand: if this is also a list, all is ok.
3859 * For "something . ...", "something - ..." or "non-list + ...",
3860 * we know that the first operand needs to be a string or number
3861 * without evaluating the 2nd operand. So check before to avoid
3862 * side effects after an error. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003863 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003864 {
3865 clear_tv(rettv);
3866 return FAIL;
3867 }
3868 }
3869
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 /*
3871 * Get the second variable.
3872 */
3873 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003874 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003876 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 return FAIL;
3878 }
3879
3880 if (evaluate)
3881 {
3882 /*
3883 * Compute the result.
3884 */
3885 if (op == '.')
3886 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003887 s1 = tv_get_string_buf(rettv, buf1); /* already checked */
3888 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003889 if (s2 == NULL) /* type error ? */
3890 {
3891 clear_tv(rettv);
3892 clear_tv(&var2);
3893 return FAIL;
3894 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003895 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003896 clear_tv(rettv);
3897 rettv->v_type = VAR_STRING;
3898 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003900 else if (op == '+' && rettv->v_type == VAR_BLOB
3901 && var2.v_type == VAR_BLOB)
3902 {
3903 blob_T *b1 = rettv->vval.v_blob;
3904 blob_T *b2 = var2.vval.v_blob;
3905 blob_T *b = blob_alloc();
3906 int i;
3907
3908 if (b != NULL)
3909 {
3910 for (i = 0; i < blob_len(b1); i++)
3911 ga_append(&b->bv_ga, blob_get(b1, i));
3912 for (i = 0; i < blob_len(b2); i++)
3913 ga_append(&b->bv_ga, blob_get(b2, i));
3914
3915 clear_tv(rettv);
3916 rettv_blob_set(rettv, b);
3917 }
3918 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003919 else if (op == '+' && rettv->v_type == VAR_LIST
3920 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003921 {
3922 /* concatenate Lists */
3923 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3924 &var3) == FAIL)
3925 {
3926 clear_tv(rettv);
3927 clear_tv(&var2);
3928 return FAIL;
3929 }
3930 clear_tv(rettv);
3931 *rettv = var3;
3932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 else
3934 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003935 int error = FALSE;
3936
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003937#ifdef FEAT_FLOAT
3938 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003939 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003940 f1 = rettv->vval.v_float;
3941 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003942 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003943 else
3944#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003945 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003946 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003947 if (error)
3948 {
3949 /* This can only happen for "list + non-list". For
3950 * "non-list + ..." or "something - ...", we returned
3951 * before evaluating the 2nd operand. */
3952 clear_tv(rettv);
3953 return FAIL;
3954 }
3955#ifdef FEAT_FLOAT
3956 if (var2.v_type == VAR_FLOAT)
3957 f1 = n1;
3958#endif
3959 }
3960#ifdef FEAT_FLOAT
3961 if (var2.v_type == VAR_FLOAT)
3962 {
3963 f2 = var2.vval.v_float;
3964 n2 = 0;
3965 }
3966 else
3967#endif
3968 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003969 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003970 if (error)
3971 {
3972 clear_tv(rettv);
3973 clear_tv(&var2);
3974 return FAIL;
3975 }
3976#ifdef FEAT_FLOAT
3977 if (rettv->v_type == VAR_FLOAT)
3978 f2 = n2;
3979#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003980 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003981 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003982
3983#ifdef FEAT_FLOAT
3984 /* If there is a float on either side the result is a float. */
3985 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3986 {
3987 if (op == '+')
3988 f1 = f1 + f2;
3989 else
3990 f1 = f1 - f2;
3991 rettv->v_type = VAR_FLOAT;
3992 rettv->vval.v_float = f1;
3993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003995#endif
3996 {
3997 if (op == '+')
3998 n1 = n1 + n2;
3999 else
4000 n1 = n1 - n2;
4001 rettv->v_type = VAR_NUMBER;
4002 rettv->vval.v_number = n1;
4003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004005 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 }
4007 }
4008 return OK;
4009}
4010
4011/*
4012 * Handle fifth level expression:
4013 * * number multiplication
4014 * / number division
4015 * % number modulo
4016 *
4017 * "arg" must point to the first non-white of the expression.
4018 * "arg" is advanced to the next non-white after the recognized expression.
4019 *
4020 * Return OK or FAIL.
4021 */
4022 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004023eval6(
4024 char_u **arg,
4025 typval_T *rettv,
4026 int evaluate,
4027 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028{
Bram Moolenaar33570922005-01-25 22:26:29 +00004029 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004031 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004032#ifdef FEAT_FLOAT
4033 int use_float = FALSE;
4034 float_T f1 = 0, f2;
4035#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004036 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037
4038 /*
4039 * Get the first variable.
4040 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004041 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 return FAIL;
4043
4044 /*
4045 * Repeat computing, until no '*', '/' or '%' is following.
4046 */
4047 for (;;)
4048 {
4049 op = **arg;
4050 if (op != '*' && op != '/' && op != '%')
4051 break;
4052
4053 if (evaluate)
4054 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004055#ifdef FEAT_FLOAT
4056 if (rettv->v_type == VAR_FLOAT)
4057 {
4058 f1 = rettv->vval.v_float;
4059 use_float = TRUE;
4060 n1 = 0;
4061 }
4062 else
4063#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004064 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004065 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004066 if (error)
4067 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 }
4069 else
4070 n1 = 0;
4071
4072 /*
4073 * Get the second variable.
4074 */
4075 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004076 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 return FAIL;
4078
4079 if (evaluate)
4080 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004081#ifdef FEAT_FLOAT
4082 if (var2.v_type == VAR_FLOAT)
4083 {
4084 if (!use_float)
4085 {
4086 f1 = n1;
4087 use_float = TRUE;
4088 }
4089 f2 = var2.vval.v_float;
4090 n2 = 0;
4091 }
4092 else
4093#endif
4094 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004095 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004096 clear_tv(&var2);
4097 if (error)
4098 return FAIL;
4099#ifdef FEAT_FLOAT
4100 if (use_float)
4101 f2 = n2;
4102#endif
4103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104
4105 /*
4106 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004107 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004109#ifdef FEAT_FLOAT
4110 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004112 if (op == '*')
4113 f1 = f1 * f2;
4114 else if (op == '/')
4115 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004116# ifdef VMS
4117 /* VMS crashes on divide by zero, work around it */
4118 if (f2 == 0.0)
4119 {
4120 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004121 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004122 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004123 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004124 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004125 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004126 }
4127 else
4128 f1 = f1 / f2;
4129# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004130 /* We rely on the floating point library to handle divide
4131 * by zero to result in "inf" and not a crash. */
4132 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004133# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004136 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004137 emsg(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004138 return FAIL;
4139 }
4140 rettv->v_type = VAR_FLOAT;
4141 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 }
4143 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004146 if (op == '*')
4147 n1 = n1 * n2;
4148 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01004149 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01004151 n1 = num_modulus(n1, n2);
4152
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004153 rettv->v_type = VAR_NUMBER;
4154 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 }
4157 }
4158
4159 return OK;
4160}
4161
4162/*
4163 * Handle sixth level expression:
4164 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004165 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004166 * "string" string constant
4167 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 * &option-name option value
4169 * @r register contents
4170 * identifier variable value
4171 * function() function call
4172 * $VAR environment variable
4173 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004174 * [expr, expr] List
4175 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 *
4177 * Also handle:
4178 * ! in front logical NOT
4179 * - in front unary minus
4180 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004181 * trailing [] subscript in String or List
4182 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 *
4184 * "arg" must point to the first non-white of the expression.
4185 * "arg" is advanced to the next non-white after the recognized expression.
4186 *
4187 * Return OK or FAIL.
4188 */
4189 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004190eval7(
4191 char_u **arg,
4192 typval_T *rettv,
4193 int evaluate,
4194 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004196 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 int len;
4198 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 char_u *start_leader, *end_leader;
4200 int ret = OK;
4201 char_u *alias;
4202
4203 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004204 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004205 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004207 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208
4209 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004210 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 */
4212 start_leader = *arg;
4213 while (**arg == '!' || **arg == '-' || **arg == '+')
4214 *arg = skipwhite(*arg + 1);
4215 end_leader = *arg;
4216
4217 switch (**arg)
4218 {
4219 /*
4220 * Number constant.
4221 */
4222 case '0':
4223 case '1':
4224 case '2':
4225 case '3':
4226 case '4':
4227 case '5':
4228 case '6':
4229 case '7':
4230 case '8':
4231 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004232 {
4233#ifdef FEAT_FLOAT
4234 char_u *p = skipdigits(*arg + 1);
4235 int get_float = FALSE;
4236
4237 /* We accept a float when the format matches
4238 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004239 * strict to avoid backwards compatibility problems.
4240 * Don't look for a float after the "." operator, so that
4241 * ":let vers = 1.2.3" doesn't fail. */
4242 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004244 get_float = TRUE;
4245 p = skipdigits(p + 2);
4246 if (*p == 'e' || *p == 'E')
4247 {
4248 ++p;
4249 if (*p == '-' || *p == '+')
4250 ++p;
4251 if (!vim_isdigit(*p))
4252 get_float = FALSE;
4253 else
4254 p = skipdigits(p + 1);
4255 }
4256 if (ASCII_ISALPHA(*p) || *p == '.')
4257 get_float = FALSE;
4258 }
4259 if (get_float)
4260 {
4261 float_T f;
4262
4263 *arg += string2float(*arg, &f);
4264 if (evaluate)
4265 {
4266 rettv->v_type = VAR_FLOAT;
4267 rettv->vval.v_float = f;
4268 }
4269 }
4270 else
4271#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004272 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004273 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004274 char_u *bp;
Bram Moolenaare519dfd2019-01-13 15:42:02 +01004275 blob_T *blob = NULL; // init for gcc
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004276
4277 // Blob constant: 0z0123456789abcdef
4278 if (evaluate)
4279 blob = blob_alloc();
4280 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
4281 {
4282 if (!vim_isxdigit(bp[1]))
4283 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004284 if (blob != NULL)
4285 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004286 emsg(_("E973: Blob literal should have an even number of hex characters"));
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004287 ga_clear(&blob->bv_ga);
4288 VIM_CLEAR(blob);
4289 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004290 ret = FAIL;
4291 break;
4292 }
4293 if (blob != NULL)
4294 ga_append(&blob->bv_ga,
4295 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
Bram Moolenaar4131fd52019-01-17 16:32:53 +01004296 if (bp[2] == '.' && vim_isxdigit(bp[3]))
4297 ++bp;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004298 }
4299 if (blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004300 rettv_blob_set(rettv, blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004301 *arg = bp;
4302 }
4303 else
4304 {
4305 // decimal, hex or octal number
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01004306 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004307 *arg += len;
4308 if (evaluate)
4309 {
4310 rettv->v_type = VAR_NUMBER;
4311 rettv->vval.v_number = n;
4312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 }
4314 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316
4317 /*
4318 * String constant: "string".
4319 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004320 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 break;
4322
4323 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004324 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004326 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004327 break;
4328
4329 /*
4330 * List: [expr, expr]
4331 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004332 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 break;
4334
4335 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004336 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004337 * Dictionary: {key: val, key: val}
4338 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004339 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4340 if (ret == NOTDONE)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004341 ret = dict_get_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004342 break;
4343
4344 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004345 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004347 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 break;
4349
4350 /*
4351 * Environment variable: $VAR.
4352 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004353 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 break;
4355
4356 /*
4357 * Register contents: @r.
4358 */
4359 case '@': ++*arg;
4360 if (evaluate)
4361 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004362 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004363 rettv->vval.v_string = get_reg_contents(**arg,
4364 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 }
4366 if (**arg != NUL)
4367 ++*arg;
4368 break;
4369
4370 /*
4371 * nested expression: (expression).
4372 */
4373 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004374 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 if (**arg == ')')
4376 ++*arg;
4377 else if (ret == OK)
4378 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004379 emsg(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004380 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 ret = FAIL;
4382 }
4383 break;
4384
Bram Moolenaar8c711452005-01-14 21:53:12 +00004385 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 break;
4387 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004388
4389 if (ret == NOTDONE)
4390 {
4391 /*
4392 * Must be a variable or function name.
4393 * Can also be a curly-braces kind of name: {expr}.
4394 */
4395 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004396 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004397 if (alias != NULL)
4398 s = alias;
4399
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004400 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004401 ret = FAIL;
4402 else
4403 {
4404 if (**arg == '(') /* recursive! */
4405 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004406 partial_T *partial;
4407
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004408 if (!evaluate)
4409 check_vars(s, len);
4410
Bram Moolenaar8c711452005-01-14 21:53:12 +00004411 /* If "s" is the name of a variable of type VAR_FUNC
4412 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004413 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004414
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004415 /* Need to make a copy, in case evaluating the arguments makes
4416 * the name invalid. */
4417 s = vim_strsave(s);
4418 if (s == NULL)
4419 ret = FAIL;
4420 else
4421 /* Invoke the function. */
4422 ret = get_func_tv(s, len, rettv, arg,
4423 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4424 &len, evaluate, partial, NULL);
4425 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004426
4427 /* If evaluate is FALSE rettv->v_type was not set in
4428 * get_func_tv, but it's needed in handle_subscript() to parse
4429 * what follows. So set it here. */
4430 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4431 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004432 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004433 rettv->v_type = VAR_FUNC;
4434 }
4435
Bram Moolenaar8c711452005-01-14 21:53:12 +00004436 /* Stop the expression evaluation when immediately
4437 * aborting on error, or when an interrupt occurred or
4438 * an exception was thrown but not caught. */
4439 if (aborting())
4440 {
4441 if (ret == OK)
4442 clear_tv(rettv);
4443 ret = FAIL;
4444 }
4445 }
4446 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004447 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004448 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004449 {
4450 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004451 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004452 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004453 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004454 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004455 }
4456
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 *arg = skipwhite(*arg);
4458
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004459 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4460 * expr(expr). */
4461 if (ret == OK)
4462 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463
4464 /*
4465 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4466 */
4467 if (ret == OK && evaluate && end_leader > start_leader)
4468 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004469 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004470 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004471#ifdef FEAT_FLOAT
4472 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004473
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004474 if (rettv->v_type == VAR_FLOAT)
4475 f = rettv->vval.v_float;
4476 else
4477#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004478 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004479 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004481 clear_tv(rettv);
4482 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004484 else
4485 {
4486 while (end_leader > start_leader)
4487 {
4488 --end_leader;
4489 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004490 {
4491#ifdef FEAT_FLOAT
4492 if (rettv->v_type == VAR_FLOAT)
4493 f = !f;
4494 else
4495#endif
4496 val = !val;
4497 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004498 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004499 {
4500#ifdef FEAT_FLOAT
4501 if (rettv->v_type == VAR_FLOAT)
4502 f = -f;
4503 else
4504#endif
4505 val = -val;
4506 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004507 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004508#ifdef FEAT_FLOAT
4509 if (rettv->v_type == VAR_FLOAT)
4510 {
4511 clear_tv(rettv);
4512 rettv->vval.v_float = f;
4513 }
4514 else
4515#endif
4516 {
4517 clear_tv(rettv);
4518 rettv->v_type = VAR_NUMBER;
4519 rettv->vval.v_number = val;
4520 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 }
4523
4524 return ret;
4525}
4526
4527/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004528 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4529 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004530 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4531 */
4532 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004533eval_index(
4534 char_u **arg,
4535 typval_T *rettv,
4536 int evaluate,
4537 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004538{
4539 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004540 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004541 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004542 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004543 long len = -1;
4544 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004545 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004546 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004547
Bram Moolenaara03f2332016-02-06 18:09:59 +01004548 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004549 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004550 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004551 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004552 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004553 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004554 return FAIL;
4555 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004556#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004557 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004558 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004559 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004560#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004561 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004562 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004563 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004564 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004565 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004566 return FAIL;
4567 case VAR_UNKNOWN:
4568 if (evaluate)
4569 return FAIL;
4570 /* FALLTHROUGH */
4571
4572 case VAR_STRING:
4573 case VAR_NUMBER:
4574 case VAR_LIST:
4575 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004576 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004577 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004578 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004579
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004580 init_tv(&var1);
4581 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004582 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004583 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004584 /*
4585 * dict.name
4586 */
4587 key = *arg + 1;
4588 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4589 ;
4590 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004591 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004592 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004593 }
4594 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004595 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004596 /*
4597 * something[idx]
4598 *
4599 * Get the (first) variable from inside the [].
4600 */
4601 *arg = skipwhite(*arg + 1);
4602 if (**arg == ':')
4603 empty1 = TRUE;
4604 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4605 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004606 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004607 {
4608 /* not a number or string */
4609 clear_tv(&var1);
4610 return FAIL;
4611 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004612
4613 /*
4614 * Get the second variable from inside the [:].
4615 */
4616 if (**arg == ':')
4617 {
4618 range = TRUE;
4619 *arg = skipwhite(*arg + 1);
4620 if (**arg == ']')
4621 empty2 = TRUE;
4622 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4623 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004624 if (!empty1)
4625 clear_tv(&var1);
4626 return FAIL;
4627 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004628 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004629 {
4630 /* not a number or string */
4631 if (!empty1)
4632 clear_tv(&var1);
4633 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004634 return FAIL;
4635 }
4636 }
4637
4638 /* Check for the ']'. */
4639 if (**arg != ']')
4640 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004641 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004642 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004643 clear_tv(&var1);
4644 if (range)
4645 clear_tv(&var2);
4646 return FAIL;
4647 }
4648 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004649 }
4650
4651 if (evaluate)
4652 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004653 n1 = 0;
4654 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004655 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004656 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004657 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004658 }
4659 if (range)
4660 {
4661 if (empty2)
4662 n2 = -1;
4663 else
4664 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004665 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004666 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004667 }
4668 }
4669
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004670 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004671 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004672 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004673 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004674 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004675 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004676 case VAR_SPECIAL:
4677 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004678 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004679 break; /* not evaluating, skipping over subscript */
4680
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004681 case VAR_NUMBER:
4682 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004683 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004684 len = (long)STRLEN(s);
4685 if (range)
4686 {
4687 /* The resulting variable is a substring. If the indexes
4688 * are out of range the result is empty. */
4689 if (n1 < 0)
4690 {
4691 n1 = len + n1;
4692 if (n1 < 0)
4693 n1 = 0;
4694 }
4695 if (n2 < 0)
4696 n2 = len + n2;
4697 else if (n2 >= len)
4698 n2 = len;
4699 if (n1 >= len || n2 < 0 || n1 > n2)
4700 s = NULL;
4701 else
4702 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4703 }
4704 else
4705 {
4706 /* The resulting variable is a string of a single
4707 * character. If the index is too big or negative the
4708 * result is empty. */
4709 if (n1 >= len || n1 < 0)
4710 s = NULL;
4711 else
4712 s = vim_strnsave(s + n1, 1);
4713 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004714 clear_tv(rettv);
4715 rettv->v_type = VAR_STRING;
4716 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004717 break;
4718
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004719 case VAR_BLOB:
4720 len = blob_len(rettv->vval.v_blob);
4721 if (range)
4722 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01004723 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004724 // are out of range the result is empty.
4725 if (n1 < 0)
4726 {
4727 n1 = len + n1;
4728 if (n1 < 0)
4729 n1 = 0;
4730 }
4731 if (n2 < 0)
4732 n2 = len + n2;
4733 else if (n2 >= len)
4734 n2 = len - 1;
4735 if (n1 >= len || n2 < 0 || n1 > n2)
4736 {
4737 clear_tv(rettv);
4738 rettv->v_type = VAR_BLOB;
4739 rettv->vval.v_blob = NULL;
4740 }
4741 else
4742 {
4743 blob_T *blob = blob_alloc();
4744
4745 if (blob != NULL)
4746 {
4747 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
4748 {
4749 blob_free(blob);
4750 return FAIL;
4751 }
4752 blob->bv_ga.ga_len = n2 - n1 + 1;
4753 for (i = n1; i <= n2; i++)
4754 blob_set(blob, i - n1,
4755 blob_get(rettv->vval.v_blob, i));
4756
4757 clear_tv(rettv);
4758 rettv_blob_set(rettv, blob);
4759 }
4760 }
4761 }
4762 else
4763 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01004764 // The resulting variable is a byte value.
4765 // If the index is too big or negative that is an error.
4766 if (n1 < 0)
4767 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004768 if (n1 < len && n1 >= 0)
4769 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01004770 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004771
4772 clear_tv(rettv);
4773 rettv->v_type = VAR_NUMBER;
4774 rettv->vval.v_number = v;
4775 }
4776 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004777 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004778 }
4779 break;
4780
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004781 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004782 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004783 if (n1 < 0)
4784 n1 = len + n1;
4785 if (!empty1 && (n1 < 0 || n1 >= len))
4786 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004787 /* For a range we allow invalid values and return an empty
4788 * list. A list index out of range is an error. */
4789 if (!range)
4790 {
4791 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004792 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004793 return FAIL;
4794 }
4795 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004796 }
4797 if (range)
4798 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004799 list_T *l;
4800 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004801
4802 if (n2 < 0)
4803 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004804 else if (n2 >= len)
4805 n2 = len - 1;
4806 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004807 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004808 l = list_alloc();
4809 if (l == NULL)
4810 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004811 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004812 n1 <= n2; ++n1)
4813 {
4814 if (list_append_tv(l, &item->li_tv) == FAIL)
4815 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004816 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004817 return FAIL;
4818 }
4819 item = item->li_next;
4820 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004821 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004822 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004823 }
4824 else
4825 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004826 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004827 clear_tv(rettv);
4828 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004829 }
4830 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004831
4832 case VAR_DICT:
4833 if (range)
4834 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004835 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004836 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004837 if (len == -1)
4838 clear_tv(&var1);
4839 return FAIL;
4840 }
4841 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004842 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004843
4844 if (len == -1)
4845 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004846 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02004847 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004848 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004849 clear_tv(&var1);
4850 return FAIL;
4851 }
4852 }
4853
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004854 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004855
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004856 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004857 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004858 if (len == -1)
4859 clear_tv(&var1);
4860 if (item == NULL)
4861 return FAIL;
4862
4863 copy_tv(&item->di_tv, &var1);
4864 clear_tv(rettv);
4865 *rettv = var1;
4866 }
4867 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004868 }
4869 }
4870
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004871 return OK;
4872}
4873
4874/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 * Get an option value.
4876 * "arg" points to the '&' or '+' before the option name.
4877 * "arg" is advanced to character after the option name.
4878 * Return OK or FAIL.
4879 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004880 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004881get_option_tv(
4882 char_u **arg,
4883 typval_T *rettv, /* when NULL, only check if option exists */
4884 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885{
4886 char_u *option_end;
4887 long numval;
4888 char_u *stringval;
4889 int opt_type;
4890 int c;
4891 int working = (**arg == '+'); /* has("+option") */
4892 int ret = OK;
4893 int opt_flags;
4894
4895 /*
4896 * Isolate the option name and find its value.
4897 */
4898 option_end = find_option_end(arg, &opt_flags);
4899 if (option_end == NULL)
4900 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004901 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004902 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 return FAIL;
4904 }
4905
4906 if (!evaluate)
4907 {
4908 *arg = option_end;
4909 return OK;
4910 }
4911
4912 c = *option_end;
4913 *option_end = NUL;
4914 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004915 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916
4917 if (opt_type == -3) /* invalid name */
4918 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004919 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004920 semsg(_("E113: Unknown option: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 ret = FAIL;
4922 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004923 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 {
4925 if (opt_type == -2) /* hidden string option */
4926 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004927 rettv->v_type = VAR_STRING;
4928 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 }
4930 else if (opt_type == -1) /* hidden number option */
4931 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004932 rettv->v_type = VAR_NUMBER;
4933 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 }
4935 else if (opt_type == 1) /* number option */
4936 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004937 rettv->v_type = VAR_NUMBER;
4938 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 }
4940 else /* string option */
4941 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004942 rettv->v_type = VAR_STRING;
4943 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 }
4945 }
4946 else if (working && (opt_type == -2 || opt_type == -1))
4947 ret = FAIL;
4948
4949 *option_end = c; /* put back for error messages */
4950 *arg = option_end;
4951
4952 return ret;
4953}
4954
4955/*
4956 * Allocate a variable for a string constant.
4957 * Return OK or FAIL.
4958 */
4959 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004960get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961{
4962 char_u *p;
4963 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 int extra = 0;
4965
4966 /*
4967 * Find the end of the string, skipping backslashed characters.
4968 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004969 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 {
4971 if (*p == '\\' && p[1] != NUL)
4972 {
4973 ++p;
4974 /* A "\<x>" form occupies at least 4 characters, and produces up
4975 * to 6 characters: reserve space for 2 extra */
4976 if (*p == '<')
4977 extra += 2;
4978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 }
4980
4981 if (*p != '"')
4982 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004983 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 return FAIL;
4985 }
4986
4987 /* If only parsing, set *arg and return here */
4988 if (!evaluate)
4989 {
4990 *arg = p + 1;
4991 return OK;
4992 }
4993
4994 /*
4995 * Copy the string into allocated memory, handling backslashed
4996 * characters.
4997 */
4998 name = alloc((unsigned)(p - *arg + extra));
4999 if (name == NULL)
5000 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005001 rettv->v_type = VAR_STRING;
5002 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003
Bram Moolenaar8c711452005-01-14 21:53:12 +00005004 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 {
5006 if (*p == '\\')
5007 {
5008 switch (*++p)
5009 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005010 case 'b': *name++ = BS; ++p; break;
5011 case 'e': *name++ = ESC; ++p; break;
5012 case 'f': *name++ = FF; ++p; break;
5013 case 'n': *name++ = NL; ++p; break;
5014 case 'r': *name++ = CAR; ++p; break;
5015 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016
5017 case 'X': /* hex: "\x1", "\x12" */
5018 case 'x':
5019 case 'u': /* Unicode: "\u0023" */
5020 case 'U':
5021 if (vim_isxdigit(p[1]))
5022 {
5023 int n, nr;
5024 int c = toupper(*p);
5025
5026 if (c == 'X')
5027 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005028 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005030 else
5031 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 nr = 0;
5033 while (--n >= 0 && vim_isxdigit(p[1]))
5034 {
5035 ++p;
5036 nr = (nr << 4) + hex2nr(*p);
5037 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005038 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 /* For "\u" store the number according to
5040 * 'encoding'. */
5041 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005042 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005044 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 break;
5047
5048 /* octal: "\1", "\12", "\123" */
5049 case '0':
5050 case '1':
5051 case '2':
5052 case '3':
5053 case '4':
5054 case '5':
5055 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005056 case '7': *name = *p++ - '0';
5057 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005059 *name = (*name << 3) + *p++ - '0';
5060 if (*p >= '0' && *p <= '7')
5061 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005063 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 break;
5065
5066 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005067 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 if (extra != 0)
5069 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005070 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 break;
5072 }
5073 /* FALLTHROUGH */
5074
Bram Moolenaar8c711452005-01-14 21:53:12 +00005075 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 break;
5077 }
5078 }
5079 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005080 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005083 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02005084 if (*p != NUL) /* just in case */
5085 ++p;
5086 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 return OK;
5089}
5090
5091/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005092 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 * Return OK or FAIL.
5094 */
5095 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005096get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097{
5098 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005099 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005100 int reduce = 0;
5101
5102 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005103 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005104 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005105 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005106 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005107 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005108 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005109 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005110 break;
5111 ++reduce;
5112 ++p;
5113 }
5114 }
5115
Bram Moolenaar8c711452005-01-14 21:53:12 +00005116 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005117 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005118 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005119 return FAIL;
5120 }
5121
Bram Moolenaar8c711452005-01-14 21:53:12 +00005122 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005123 if (!evaluate)
5124 {
5125 *arg = p + 1;
5126 return OK;
5127 }
5128
5129 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005130 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005131 */
5132 str = alloc((unsigned)((p - *arg) - reduce));
5133 if (str == NULL)
5134 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005135 rettv->v_type = VAR_STRING;
5136 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005137
Bram Moolenaar8c711452005-01-14 21:53:12 +00005138 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005139 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005140 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005141 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005142 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005143 break;
5144 ++p;
5145 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005146 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005147 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005148 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005149 *arg = p + 1;
5150
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005151 return OK;
5152}
5153
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005154/*
5155 * Return the function name of the partial.
5156 */
5157 char_u *
5158partial_name(partial_T *pt)
5159{
5160 if (pt->pt_name != NULL)
5161 return pt->pt_name;
5162 return pt->pt_func->uf_name;
5163}
5164
Bram Moolenaarddecc252016-04-06 22:59:37 +02005165 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005166partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005167{
5168 int i;
5169
5170 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005171 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005172 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005173 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005174 if (pt->pt_name != NULL)
5175 {
5176 func_unref(pt->pt_name);
5177 vim_free(pt->pt_name);
5178 }
5179 else
5180 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005181 vim_free(pt);
5182}
5183
5184/*
5185 * Unreference a closure: decrement the reference count and free it when it
5186 * becomes zero.
5187 */
5188 void
5189partial_unref(partial_T *pt)
5190{
5191 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005192 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005193}
5194
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005195static int tv_equal_recurse_limit;
5196
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005197 static int
5198func_equal(
5199 typval_T *tv1,
5200 typval_T *tv2,
5201 int ic) /* ignore case */
5202{
5203 char_u *s1, *s2;
5204 dict_T *d1, *d2;
5205 int a1, a2;
5206 int i;
5207
5208 /* empty and NULL function name considered the same */
5209 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005210 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005211 if (s1 != NULL && *s1 == NUL)
5212 s1 = NULL;
5213 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005214 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005215 if (s2 != NULL && *s2 == NUL)
5216 s2 = NULL;
5217 if (s1 == NULL || s2 == NULL)
5218 {
5219 if (s1 != s2)
5220 return FALSE;
5221 }
5222 else if (STRCMP(s1, s2) != 0)
5223 return FALSE;
5224
5225 /* empty dict and NULL dict is different */
5226 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5227 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5228 if (d1 == NULL || d2 == NULL)
5229 {
5230 if (d1 != d2)
5231 return FALSE;
5232 }
5233 else if (!dict_equal(d1, d2, ic, TRUE))
5234 return FALSE;
5235
5236 /* empty list and no list considered the same */
5237 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5238 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5239 if (a1 != a2)
5240 return FALSE;
5241 for (i = 0; i < a1; ++i)
5242 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5243 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5244 return FALSE;
5245
5246 return TRUE;
5247}
5248
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005249/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005250 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005251 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005252 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005253 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005254 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005255tv_equal(
5256 typval_T *tv1,
5257 typval_T *tv2,
5258 int ic, /* ignore case */
5259 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005260{
5261 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005262 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005263 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005264 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005265
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005266 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005267 * recursiveness to a limit. We guess they are equal then.
5268 * A fixed limit has the problem of still taking an awful long time.
5269 * Reduce the limit every time running into it. That should work fine for
5270 * deeply linked structures that are not recursively linked and catch
5271 * recursiveness quickly. */
5272 if (!recursive)
5273 tv_equal_recurse_limit = 1000;
5274 if (recursive_cnt >= tv_equal_recurse_limit)
5275 {
5276 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005277 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005278 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005279
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005280 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5281 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005282 if ((tv1->v_type == VAR_FUNC
5283 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5284 && (tv2->v_type == VAR_FUNC
5285 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5286 {
5287 ++recursive_cnt;
5288 r = func_equal(tv1, tv2, ic);
5289 --recursive_cnt;
5290 return r;
5291 }
5292
5293 if (tv1->v_type != tv2->v_type)
5294 return FALSE;
5295
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005296 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005297 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005298 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005299 ++recursive_cnt;
5300 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5301 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005302 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005303
5304 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005305 ++recursive_cnt;
5306 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5307 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005308 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005309
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005310 case VAR_BLOB:
5311 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
5312
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005313 case VAR_NUMBER:
5314 return tv1->vval.v_number == tv2->vval.v_number;
5315
5316 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005317 s1 = tv_get_string_buf(tv1, buf1);
5318 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005319 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005320
5321 case VAR_SPECIAL:
5322 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005323
5324 case VAR_FLOAT:
5325#ifdef FEAT_FLOAT
5326 return tv1->vval.v_float == tv2->vval.v_float;
5327#endif
5328 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005329#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005330 return tv1->vval.v_job == tv2->vval.v_job;
5331#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005332 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005333#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005334 return tv1->vval.v_channel == tv2->vval.v_channel;
5335#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005336 case VAR_FUNC:
5337 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005338 case VAR_UNKNOWN:
5339 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005340 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005341
Bram Moolenaara03f2332016-02-06 18:09:59 +01005342 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5343 * does not equal anything, not even itself. */
5344 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005345}
5346
5347/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005348 * Return the next (unique) copy ID.
5349 * Used for serializing nested structures.
5350 */
5351 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005352get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005353{
5354 current_copyID += COPYID_INC;
5355 return current_copyID;
5356}
5357
5358/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005359 * Garbage collection for lists and dictionaries.
5360 *
5361 * We use reference counts to be able to free most items right away when they
5362 * are no longer used. But for composite items it's possible that it becomes
5363 * unused while the reference count is > 0: When there is a recursive
5364 * reference. Example:
5365 * :let l = [1, 2, 3]
5366 * :let d = {9: l}
5367 * :let l[1] = d
5368 *
5369 * Since this is quite unusual we handle this with garbage collection: every
5370 * once in a while find out which lists and dicts are not referenced from any
5371 * variable.
5372 *
5373 * Here is a good reference text about garbage collection (refers to Python
5374 * but it applies to all reference-counting mechanisms):
5375 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005376 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005377
5378/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005379 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005380 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005381 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005382 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005383 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005384garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005385{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005386 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005387 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005388 buf_T *buf;
5389 win_T *wp;
5390 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005391 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005392 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005393
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005394 if (!testing)
5395 {
5396 /* Only do this once. */
5397 want_garbage_collect = FALSE;
5398 may_garbage_collect = FALSE;
5399 garbage_collect_at_exit = FALSE;
5400 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005401
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005402 /* We advance by two because we add one for items referenced through
5403 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005404 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005405
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005406 /*
5407 * 1. Go through all accessible variables and mark all lists and dicts
5408 * with copyID.
5409 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005410
5411 /* Don't free variables in the previous_funccal list unless they are only
5412 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005413 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005414 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005415
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005416 /* script-local variables */
5417 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005418 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005419
5420 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005421 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005422 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5423 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005424
5425 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005426 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005427 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5428 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005429 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005430 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5431 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005432
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005433 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005434 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005435 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5436 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005437 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005438 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005439
5440 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005441 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005442
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005443 /* named functions (matters for closures) */
5444 abort = abort || set_ref_in_functions(copyID);
5445
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005446 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005447 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005448
Bram Moolenaard812df62008-11-09 12:46:09 +00005449 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005450 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005451
Bram Moolenaar1dced572012-04-05 16:54:08 +02005452#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005453 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005454#endif
5455
Bram Moolenaardb913952012-06-29 12:54:53 +02005456#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005457 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005458#endif
5459
5460#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005461 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005462#endif
5463
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005464#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005465 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005466 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005467#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005468#ifdef FEAT_NETBEANS_INTG
5469 abort = abort || set_ref_in_nb_channel(copyID);
5470#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005471
Bram Moolenaare3188e22016-05-31 21:13:04 +02005472#ifdef FEAT_TIMERS
5473 abort = abort || set_ref_in_timer(copyID);
5474#endif
5475
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005476#ifdef FEAT_QUICKFIX
5477 abort = abort || set_ref_in_quickfix(copyID);
5478#endif
5479
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005480#ifdef FEAT_TERMINAL
5481 abort = abort || set_ref_in_term(copyID);
5482#endif
5483
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005484 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005485 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005486 /*
5487 * 2. Free lists and dictionaries that are not referenced.
5488 */
5489 did_free = free_unref_items(copyID);
5490
5491 /*
5492 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005493 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005494 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005495 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005496 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005497 else if (p_verbose > 0)
5498 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005499 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005500 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005501
5502 return did_free;
5503}
5504
5505/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005506 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005507 */
5508 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005509free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005510{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005511 int did_free = FALSE;
5512
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005513 /* Let all "free" functions know that we are here. This means no
5514 * dictionaries, lists, channels or jobs are to be freed, because we will
5515 * do that here. */
5516 in_free_unref_items = TRUE;
5517
5518 /*
5519 * PASS 1: free the contents of the items. We don't free the items
5520 * themselves yet, so that it is possible to decrement refcount counters
5521 */
5522
Bram Moolenaarcd524592016-07-17 14:57:05 +02005523 /* Go through the list of dicts and free items without the copyID. */
5524 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005525
Bram Moolenaarda861d62016-07-17 15:46:27 +02005526 /* Go through the list of lists and free items without the copyID. */
5527 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005528
5529#ifdef FEAT_JOB_CHANNEL
5530 /* Go through the list of jobs and free items without the copyID. This
5531 * must happen before doing channels, because jobs refer to channels, but
5532 * the reference from the channel to the job isn't tracked. */
5533 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5534
5535 /* Go through the list of channels and free items without the copyID. */
5536 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5537#endif
5538
5539 /*
5540 * PASS 2: free the items themselves.
5541 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005542 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005543 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005544
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005545#ifdef FEAT_JOB_CHANNEL
5546 /* Go through the list of jobs and free items without the copyID. This
5547 * must happen before doing channels, because jobs refer to channels, but
5548 * the reference from the channel to the job isn't tracked. */
5549 free_unused_jobs(copyID, COPYID_MASK);
5550
5551 /* Go through the list of channels and free items without the copyID. */
5552 free_unused_channels(copyID, COPYID_MASK);
5553#endif
5554
5555 in_free_unref_items = FALSE;
5556
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005557 return did_free;
5558}
5559
5560/*
5561 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005562 * "list_stack" is used to add lists to be marked. Can be NULL.
5563 *
5564 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005565 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005566 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005567set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005568{
5569 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005570 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005571 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005572 hashtab_T *cur_ht;
5573 ht_stack_T *ht_stack = NULL;
5574 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005575
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005576 cur_ht = ht;
5577 for (;;)
5578 {
5579 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005580 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005581 /* Mark each item in the hashtab. If the item contains a hashtab
5582 * it is added to ht_stack, if it contains a list it is added to
5583 * list_stack. */
5584 todo = (int)cur_ht->ht_used;
5585 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5586 if (!HASHITEM_EMPTY(hi))
5587 {
5588 --todo;
5589 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5590 &ht_stack, list_stack);
5591 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005592 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005593
5594 if (ht_stack == NULL)
5595 break;
5596
5597 /* take an item from the stack */
5598 cur_ht = ht_stack->ht;
5599 tempitem = ht_stack;
5600 ht_stack = ht_stack->prev;
5601 free(tempitem);
5602 }
5603
5604 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005605}
5606
5607/*
5608 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005609 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5610 *
5611 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005612 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005613 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005614set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005615{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005616 listitem_T *li;
5617 int abort = FALSE;
5618 list_T *cur_l;
5619 list_stack_T *list_stack = NULL;
5620 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005621
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005622 cur_l = l;
5623 for (;;)
5624 {
5625 if (!abort)
5626 /* Mark each item in the list. If the item contains a hashtab
5627 * it is added to ht_stack, if it contains a list it is added to
5628 * list_stack. */
5629 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5630 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5631 ht_stack, &list_stack);
5632 if (list_stack == NULL)
5633 break;
5634
5635 /* take an item from the stack */
5636 cur_l = list_stack->list;
5637 tempitem = list_stack;
5638 list_stack = list_stack->prev;
5639 free(tempitem);
5640 }
5641
5642 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005643}
5644
5645/*
5646 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005647 * "list_stack" is used to add lists to be marked. Can be NULL.
5648 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5649 *
5650 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005651 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005652 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005653set_ref_in_item(
5654 typval_T *tv,
5655 int copyID,
5656 ht_stack_T **ht_stack,
5657 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005658{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005659 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005660
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005661 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005662 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005663 dict_T *dd = tv->vval.v_dict;
5664
Bram Moolenaara03f2332016-02-06 18:09:59 +01005665 if (dd != NULL && dd->dv_copyID != copyID)
5666 {
5667 /* Didn't see this dict yet. */
5668 dd->dv_copyID = copyID;
5669 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005670 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005671 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5672 }
5673 else
5674 {
5675 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5676 if (newitem == NULL)
5677 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005678 else
5679 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005680 newitem->ht = &dd->dv_hashtab;
5681 newitem->prev = *ht_stack;
5682 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005683 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005684 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005685 }
5686 }
5687 else if (tv->v_type == VAR_LIST)
5688 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005689 list_T *ll = tv->vval.v_list;
5690
Bram Moolenaara03f2332016-02-06 18:09:59 +01005691 if (ll != NULL && ll->lv_copyID != copyID)
5692 {
5693 /* Didn't see this list yet. */
5694 ll->lv_copyID = copyID;
5695 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005696 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005697 abort = set_ref_in_list(ll, copyID, ht_stack);
5698 }
5699 else
5700 {
5701 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005702 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005703 if (newitem == NULL)
5704 abort = TRUE;
5705 else
5706 {
5707 newitem->list = ll;
5708 newitem->prev = *list_stack;
5709 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005710 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005711 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005712 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005713 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005714 else if (tv->v_type == VAR_FUNC)
5715 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005716 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005717 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005718 else if (tv->v_type == VAR_PARTIAL)
5719 {
5720 partial_T *pt = tv->vval.v_partial;
5721 int i;
5722
5723 /* A partial does not have a copyID, because it cannot contain itself.
5724 */
5725 if (pt != NULL)
5726 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005727 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005728
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005729 if (pt->pt_dict != NULL)
5730 {
5731 typval_T dtv;
5732
5733 dtv.v_type = VAR_DICT;
5734 dtv.vval.v_dict = pt->pt_dict;
5735 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5736 }
5737
5738 for (i = 0; i < pt->pt_argc; ++i)
5739 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5740 ht_stack, list_stack);
5741 }
5742 }
5743#ifdef FEAT_JOB_CHANNEL
5744 else if (tv->v_type == VAR_JOB)
5745 {
5746 job_T *job = tv->vval.v_job;
5747 typval_T dtv;
5748
5749 if (job != NULL && job->jv_copyID != copyID)
5750 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005751 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005752 if (job->jv_channel != NULL)
5753 {
5754 dtv.v_type = VAR_CHANNEL;
5755 dtv.vval.v_channel = job->jv_channel;
5756 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5757 }
5758 if (job->jv_exit_partial != NULL)
5759 {
5760 dtv.v_type = VAR_PARTIAL;
5761 dtv.vval.v_partial = job->jv_exit_partial;
5762 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5763 }
5764 }
5765 }
5766 else if (tv->v_type == VAR_CHANNEL)
5767 {
5768 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005769 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005770 typval_T dtv;
5771 jsonq_T *jq;
5772 cbq_T *cq;
5773
5774 if (ch != NULL && ch->ch_copyID != copyID)
5775 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005776 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005777 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005778 {
5779 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5780 jq = jq->jq_next)
5781 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5782 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5783 cq = cq->cq_next)
5784 if (cq->cq_partial != NULL)
5785 {
5786 dtv.v_type = VAR_PARTIAL;
5787 dtv.vval.v_partial = cq->cq_partial;
5788 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5789 }
5790 if (ch->ch_part[part].ch_partial != NULL)
5791 {
5792 dtv.v_type = VAR_PARTIAL;
5793 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5794 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5795 }
5796 }
5797 if (ch->ch_partial != NULL)
5798 {
5799 dtv.v_type = VAR_PARTIAL;
5800 dtv.vval.v_partial = ch->ch_partial;
5801 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5802 }
5803 if (ch->ch_close_partial != NULL)
5804 {
5805 dtv.v_type = VAR_PARTIAL;
5806 dtv.vval.v_partial = ch->ch_close_partial;
5807 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5808 }
5809 }
5810 }
5811#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005812 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005813}
5814
Bram Moolenaar17a13432016-01-24 14:22:10 +01005815 static char *
5816get_var_special_name(int nr)
5817{
5818 switch (nr)
5819 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005820 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005821 case VVAL_TRUE: return "v:true";
5822 case VVAL_NONE: return "v:none";
5823 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005824 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01005825 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01005826 return "42";
5827}
5828
Bram Moolenaar8c711452005-01-14 21:53:12 +00005829/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005830 * Return a string with the string representation of a variable.
5831 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005832 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005833 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005834 * When both "echo_style" and "composite_val" are FALSE, put quotes around
5835 * stings as "string()", otherwise does not put quotes around strings, as
5836 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005837 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5838 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005839 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005840 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005841 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005842echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005843 typval_T *tv,
5844 char_u **tofree,
5845 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005846 int copyID,
5847 int echo_style,
5848 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005849 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005850{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005851 static int recurse = 0;
5852 char_u *r = NULL;
5853
Bram Moolenaar33570922005-01-25 22:26:29 +00005854 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005855 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005856 if (!did_echo_string_emsg)
5857 {
5858 /* Only give this message once for a recursive call to avoid
5859 * flooding the user with errors. And stop iterating over lists
5860 * and dicts. */
5861 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005862 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005863 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005864 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005865 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005866 }
5867 ++recurse;
5868
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005869 switch (tv->v_type)
5870 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005871 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005872 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005873 {
5874 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005875 r = tv->vval.v_string;
5876 if (r == NULL)
5877 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005878 }
5879 else
5880 {
5881 *tofree = string_quote(tv->vval.v_string, FALSE);
5882 r = *tofree;
5883 }
5884 break;
5885
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005886 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005887 if (echo_style)
5888 {
5889 *tofree = NULL;
5890 r = tv->vval.v_string;
5891 }
5892 else
5893 {
5894 *tofree = string_quote(tv->vval.v_string, TRUE);
5895 r = *tofree;
5896 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005897 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005898
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005899 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005900 {
5901 partial_T *pt = tv->vval.v_partial;
5902 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005903 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005904 garray_T ga;
5905 int i;
5906 char_u *tf;
5907
5908 ga_init2(&ga, 1, 100);
5909 ga_concat(&ga, (char_u *)"function(");
5910 if (fname != NULL)
5911 {
5912 ga_concat(&ga, fname);
5913 vim_free(fname);
5914 }
5915 if (pt != NULL && pt->pt_argc > 0)
5916 {
5917 ga_concat(&ga, (char_u *)", [");
5918 for (i = 0; i < pt->pt_argc; ++i)
5919 {
5920 if (i > 0)
5921 ga_concat(&ga, (char_u *)", ");
5922 ga_concat(&ga,
5923 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5924 vim_free(tf);
5925 }
5926 ga_concat(&ga, (char_u *)"]");
5927 }
5928 if (pt != NULL && pt->pt_dict != NULL)
5929 {
5930 typval_T dtv;
5931
5932 ga_concat(&ga, (char_u *)", ");
5933 dtv.v_type = VAR_DICT;
5934 dtv.vval.v_dict = pt->pt_dict;
5935 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5936 vim_free(tf);
5937 }
5938 ga_concat(&ga, (char_u *)")");
5939
5940 *tofree = ga.ga_data;
5941 r = *tofree;
5942 break;
5943 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005944
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005945 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005946 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005947 break;
5948
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005949 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005950 if (tv->vval.v_list == NULL)
5951 {
5952 *tofree = NULL;
5953 r = NULL;
5954 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005955 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5956 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005957 {
5958 *tofree = NULL;
5959 r = (char_u *)"[...]";
5960 }
5961 else
5962 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005963 int old_copyID = tv->vval.v_list->lv_copyID;
5964
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005965 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005966 *tofree = list2string(tv, copyID, restore_copyID);
5967 if (restore_copyID)
5968 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005969 r = *tofree;
5970 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005971 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005972
Bram Moolenaar8c711452005-01-14 21:53:12 +00005973 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005974 if (tv->vval.v_dict == NULL)
5975 {
5976 *tofree = NULL;
5977 r = NULL;
5978 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005979 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5980 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005981 {
5982 *tofree = NULL;
5983 r = (char_u *)"{...}";
5984 }
5985 else
5986 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005987 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005988 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005989 *tofree = dict2string(tv, copyID, restore_copyID);
5990 if (restore_copyID)
5991 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005992 r = *tofree;
5993 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005994 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005995
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005996 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005997 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005998 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005999 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006000 break;
6001
Bram Moolenaar835dc632016-02-07 14:27:38 +01006002 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006003 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006004 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006005 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006006 if (composite_val)
6007 {
6008 *tofree = string_quote(r, FALSE);
6009 r = *tofree;
6010 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006011 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006012
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006013 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006014#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006015 *tofree = NULL;
6016 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6017 r = numbuf;
6018 break;
6019#endif
6020
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006021 case VAR_SPECIAL:
6022 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006023 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006024 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006025 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006026
Bram Moolenaar8502c702014-06-17 12:51:16 +02006027 if (--recurse == 0)
6028 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006029 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006030}
6031
6032/*
6033 * Return a string with the string representation of a variable.
6034 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6035 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006036 * Does not put quotes around strings, as ":echo" displays values.
6037 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6038 * May return NULL.
6039 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006040 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006041echo_string(
6042 typval_T *tv,
6043 char_u **tofree,
6044 char_u *numbuf,
6045 int copyID)
6046{
6047 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6048}
6049
6050/*
6051 * Return a string with the string representation of a variable.
6052 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6053 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006054 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006055 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006056 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02006057 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006058tv2string(
6059 typval_T *tv,
6060 char_u **tofree,
6061 char_u *numbuf,
6062 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006063{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006064 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006065}
6066
6067/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006068 * Return string "str" in ' quotes, doubling ' characters.
6069 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006070 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006071 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006072 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006073string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006074{
Bram Moolenaar33570922005-01-25 22:26:29 +00006075 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006076 char_u *p, *r, *s;
6077
Bram Moolenaar33570922005-01-25 22:26:29 +00006078 len = (function ? 13 : 3);
6079 if (str != NULL)
6080 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006081 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006082 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00006083 if (*p == '\'')
6084 ++len;
6085 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006086 s = r = alloc(len);
6087 if (r != NULL)
6088 {
6089 if (function)
6090 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006091 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006092 r += 10;
6093 }
6094 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006095 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006096 if (str != NULL)
6097 for (p = str; *p != NUL; )
6098 {
6099 if (*p == '\'')
6100 *r++ = '\'';
6101 MB_COPY_CHAR(p, r);
6102 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006103 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006104 if (function)
6105 *r++ = ')';
6106 *r++ = NUL;
6107 }
6108 return s;
6109}
6110
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006111#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006112/*
6113 * Convert the string "text" to a floating point number.
6114 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
6115 * this always uses a decimal point.
6116 * Returns the length of the text that was consumed.
6117 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006118 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006119string2float(
6120 char_u *text,
6121 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006122{
6123 char *s = (char *)text;
6124 float_T f;
6125
Bram Moolenaar62473612017-01-08 19:25:40 +01006126 /* MS-Windows does not deal with "inf" and "nan" properly. */
6127 if (STRNICMP(text, "inf", 3) == 0)
6128 {
6129 *value = INFINITY;
6130 return 3;
6131 }
6132 if (STRNICMP(text, "-inf", 3) == 0)
6133 {
6134 *value = -INFINITY;
6135 return 4;
6136 }
6137 if (STRNICMP(text, "nan", 3) == 0)
6138 {
6139 *value = NAN;
6140 return 3;
6141 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006142 f = strtod(s, &s);
6143 *value = f;
6144 return (int)((char_u *)s - text);
6145}
6146#endif
6147
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006148/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 * Get the value of an environment variable.
6150 * "arg" is pointing to the '$'. It is advanced to after the name.
6151 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006152 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 */
6154 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006155get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156{
6157 char_u *string = NULL;
6158 int len;
6159 int cc;
6160 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006161 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162
6163 ++*arg;
6164 name = *arg;
6165 len = get_env_len(arg);
6166 if (evaluate)
6167 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006168 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006169 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006170
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006171 cc = name[len];
6172 name[len] = NUL;
6173 /* first try vim_getenv(), fast for normal environment vars */
6174 string = vim_getenv(name, &mustfree);
6175 if (string != NULL && *string != NUL)
6176 {
6177 if (!mustfree)
6178 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006180 else
6181 {
6182 if (mustfree)
6183 vim_free(string);
6184
6185 /* next try expanding things like $VIM and ${HOME} */
6186 string = expand_env_save(name - 1);
6187 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01006188 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006189 }
6190 name[len] = cc;
6191
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006192 rettv->v_type = VAR_STRING;
6193 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 }
6195
6196 return OK;
6197}
6198
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006199
6200
6201/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006203 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006205 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006206var2fpos(
6207 typval_T *varp,
6208 int dollar_lnum, /* TRUE when $ is last line */
6209 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006211 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006213 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214
Bram Moolenaara5525202006-03-02 22:52:09 +00006215 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006216 if (varp->v_type == VAR_LIST)
6217 {
6218 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006219 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006220 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006221 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006222
6223 l = varp->vval.v_list;
6224 if (l == NULL)
6225 return NULL;
6226
6227 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006228 pos.lnum = list_find_nr(l, 0L, &error);
6229 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006230 return NULL; /* invalid line number */
6231
6232 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006233 pos.col = list_find_nr(l, 1L, &error);
6234 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006235 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006236 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006237
6238 /* We accept "$" for the column number: last column. */
6239 li = list_find(l, 1L);
6240 if (li != NULL && li->li_tv.v_type == VAR_STRING
6241 && li->li_tv.vval.v_string != NULL
6242 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6243 pos.col = len + 1;
6244
Bram Moolenaara5525202006-03-02 22:52:09 +00006245 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006246 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006247 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006248 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006249
Bram Moolenaara5525202006-03-02 22:52:09 +00006250 /* Get the virtual offset. Defaults to zero. */
6251 pos.coladd = list_find_nr(l, 2L, &error);
6252 if (error)
6253 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006254
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006255 return &pos;
6256 }
6257
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006258 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006259 if (name == NULL)
6260 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006261 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006263 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6264 {
6265 if (VIsual_active)
6266 return &VIsual;
6267 return &curwin->w_cursor;
6268 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006269 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006271 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6273 return NULL;
6274 return pp;
6275 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006276
Bram Moolenaara5525202006-03-02 22:52:09 +00006277 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006278
Bram Moolenaar477933c2007-07-17 14:32:23 +00006279 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006280 {
6281 pos.col = 0;
6282 if (name[1] == '0') /* "w0": first visible line */
6283 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006284 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006285 /* In silent Ex mode topline is zero, but that's not a valid line
6286 * number; use one instead. */
6287 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006288 return &pos;
6289 }
6290 else if (name[1] == '$') /* "w$": last visible line */
6291 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006292 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006293 /* In silent Ex mode botline is zero, return zero then. */
6294 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006295 return &pos;
6296 }
6297 }
6298 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006300 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 {
6302 pos.lnum = curbuf->b_ml.ml_line_count;
6303 pos.col = 0;
6304 }
6305 else
6306 {
6307 pos.lnum = curwin->w_cursor.lnum;
6308 pos.col = (colnr_T)STRLEN(ml_get_curline());
6309 }
6310 return &pos;
6311 }
6312 return NULL;
6313}
6314
6315/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006316 * Convert list in "arg" into a position and optional file number.
6317 * When "fnump" is NULL there is no file number, only 3 items.
6318 * Note that the column is passed on as-is, the caller may want to decrement
6319 * it to use 1 for the first column.
6320 * Return FAIL when conversion is not possible, doesn't check the position for
6321 * validity.
6322 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006323 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006324list2fpos(
6325 typval_T *arg,
6326 pos_T *posp,
6327 int *fnump,
6328 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006329{
6330 list_T *l = arg->vval.v_list;
6331 long i = 0;
6332 long n;
6333
Bram Moolenaar493c1782014-05-28 14:34:46 +02006334 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6335 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006336 if (arg->v_type != VAR_LIST
6337 || l == NULL
6338 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006339 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006340 return FAIL;
6341
6342 if (fnump != NULL)
6343 {
6344 n = list_find_nr(l, i++, NULL); /* fnum */
6345 if (n < 0)
6346 return FAIL;
6347 if (n == 0)
6348 n = curbuf->b_fnum; /* current buffer */
6349 *fnump = n;
6350 }
6351
6352 n = list_find_nr(l, i++, NULL); /* lnum */
6353 if (n < 0)
6354 return FAIL;
6355 posp->lnum = n;
6356
6357 n = list_find_nr(l, i++, NULL); /* col */
6358 if (n < 0)
6359 return FAIL;
6360 posp->col = n;
6361
Bram Moolenaar493c1782014-05-28 14:34:46 +02006362 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006363 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006364 posp->coladd = 0;
6365 else
6366 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006367
Bram Moolenaar493c1782014-05-28 14:34:46 +02006368 if (curswantp != NULL)
6369 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6370
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006371 return OK;
6372}
6373
6374/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 * Get the length of an environment variable name.
6376 * Advance "arg" to the first character after the name.
6377 * Return 0 for error.
6378 */
6379 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006380get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381{
6382 char_u *p;
6383 int len;
6384
6385 for (p = *arg; vim_isIDc(*p); ++p)
6386 ;
6387 if (p == *arg) /* no name found */
6388 return 0;
6389
6390 len = (int)(p - *arg);
6391 *arg = p;
6392 return len;
6393}
6394
6395/*
6396 * Get the length of the name of a function or internal variable.
6397 * "arg" is advanced to the first non-white character after the name.
6398 * Return 0 if something is wrong.
6399 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006400 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006401get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402{
6403 char_u *p;
6404 int len;
6405
6406 /* Find the end of the name. */
6407 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006408 {
6409 if (*p == ':')
6410 {
6411 /* "s:" is start of "s:var", but "n:" is not and can be used in
6412 * slice "[n:]". Also "xx:" is not a namespace. */
6413 len = (int)(p - *arg);
6414 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6415 || len > 1)
6416 break;
6417 }
6418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 if (p == *arg) /* no name found */
6420 return 0;
6421
6422 len = (int)(p - *arg);
6423 *arg = skipwhite(p);
6424
6425 return len;
6426}
6427
6428/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006429 * Get the length of the name of a variable or function.
6430 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006432 * Return -1 if curly braces expansion failed.
6433 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 * If the name contains 'magic' {}'s, expand them and return the
6435 * expanded name in an allocated string via 'alias' - caller must free.
6436 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006437 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006438get_name_len(
6439 char_u **arg,
6440 char_u **alias,
6441 int evaluate,
6442 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443{
6444 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 char_u *p;
6446 char_u *expr_start;
6447 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448
6449 *alias = NULL; /* default to no alias */
6450
6451 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6452 && (*arg)[2] == (int)KE_SNR)
6453 {
6454 /* hard coded <SNR>, already translated */
6455 *arg += 3;
6456 return get_id_len(arg) + 3;
6457 }
6458 len = eval_fname_script(*arg);
6459 if (len > 0)
6460 {
6461 /* literal "<SID>", "s:" or "<SNR>" */
6462 *arg += len;
6463 }
6464
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006466 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006468 p = find_name_end(*arg, &expr_start, &expr_end,
6469 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 if (expr_start != NULL)
6471 {
6472 char_u *temp_string;
6473
6474 if (!evaluate)
6475 {
6476 len += (int)(p - *arg);
6477 *arg = skipwhite(p);
6478 return len;
6479 }
6480
6481 /*
6482 * Include any <SID> etc in the expanded string:
6483 * Thus the -len here.
6484 */
6485 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6486 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006487 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 *alias = temp_string;
6489 *arg = skipwhite(p);
6490 return (int)STRLEN(temp_string);
6491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492
6493 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006494 // Only give an error when there is something, otherwise it will be
6495 // reported at a higher level.
6496 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006497 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498
6499 return len;
6500}
6501
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006502/*
6503 * Find the end of a variable or function name, taking care of magic braces.
6504 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6505 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006506 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006507 * Return a pointer to just after the name. Equal to "arg" if there is no
6508 * valid name.
6509 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006510 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006511find_name_end(
6512 char_u *arg,
6513 char_u **expr_start,
6514 char_u **expr_end,
6515 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006517 int mb_nest = 0;
6518 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006520 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006522 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006524 *expr_start = NULL;
6525 *expr_end = NULL;
6526 }
6527
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006528 /* Quick check for valid starting character. */
6529 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6530 return arg;
6531
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006532 for (p = arg; *p != NUL
6533 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006534 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006535 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006536 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006537 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006538 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006539 if (*p == '\'')
6540 {
6541 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006542 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006543 ;
6544 if (*p == NUL)
6545 break;
6546 }
6547 else if (*p == '"')
6548 {
6549 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006550 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006551 if (*p == '\\' && p[1] != NUL)
6552 ++p;
6553 if (*p == NUL)
6554 break;
6555 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006556 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6557 {
6558 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006559 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006560 len = (int)(p - arg);
6561 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006562 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006563 break;
6564 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006565
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006566 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006568 if (*p == '[')
6569 ++br_nest;
6570 else if (*p == ']')
6571 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006573
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006574 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006576 if (*p == '{')
6577 {
6578 mb_nest++;
6579 if (expr_start != NULL && *expr_start == NULL)
6580 *expr_start = p;
6581 }
6582 else if (*p == '}')
6583 {
6584 mb_nest--;
6585 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6586 *expr_end = p;
6587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 }
6590
6591 return p;
6592}
6593
6594/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006595 * Expands out the 'magic' {}'s in a variable/function name.
6596 * Note that this can call itself recursively, to deal with
6597 * constructs like foo{bar}{baz}{bam}
6598 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6599 * "in_start" ^
6600 * "expr_start" ^
6601 * "expr_end" ^
6602 * "in_end" ^
6603 *
6604 * Returns a new allocated string, which the caller must free.
6605 * Returns NULL for failure.
6606 */
6607 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006608make_expanded_name(
6609 char_u *in_start,
6610 char_u *expr_start,
6611 char_u *expr_end,
6612 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006613{
6614 char_u c1;
6615 char_u *retval = NULL;
6616 char_u *temp_result;
6617 char_u *nextcmd = NULL;
6618
6619 if (expr_end == NULL || in_end == NULL)
6620 return NULL;
6621 *expr_start = NUL;
6622 *expr_end = NUL;
6623 c1 = *in_end;
6624 *in_end = NUL;
6625
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006626 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006627 if (temp_result != NULL && nextcmd == NULL)
6628 {
6629 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6630 + (in_end - expr_end) + 1));
6631 if (retval != NULL)
6632 {
6633 STRCPY(retval, in_start);
6634 STRCAT(retval, temp_result);
6635 STRCAT(retval, expr_end + 1);
6636 }
6637 }
6638 vim_free(temp_result);
6639
6640 *in_end = c1; /* put char back for error messages */
6641 *expr_start = '{';
6642 *expr_end = '}';
6643
6644 if (retval != NULL)
6645 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006646 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006647 if (expr_start != NULL)
6648 {
6649 /* Further expansion! */
6650 temp_result = make_expanded_name(retval, expr_start,
6651 expr_end, temp_result);
6652 vim_free(retval);
6653 retval = temp_result;
6654 }
6655 }
6656
6657 return retval;
6658}
6659
6660/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006662 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006664 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006665eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006667 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6668}
6669
6670/*
6671 * Return TRUE if character "c" can be used as the first character in a
6672 * variable or function name (excluding '{' and '}').
6673 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006674 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006675eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006676{
6677 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678}
6679
6680/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 * Set number v: variable to "val".
6682 */
6683 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006684set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006686 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687}
6688
6689/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006690 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006692 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006693get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006695 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696}
6697
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006698/*
6699 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01006700 * If the String variable has never been set, return an empty string.
6701 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006702 */
6703 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006704get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006705{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006706 return tv_get_string(&vimvars[idx].vv_tv);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006707}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006708
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006710 * Get List v: variable value. Caller must take care of reference count when
6711 * needed.
6712 */
6713 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006714get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006715{
6716 return vimvars[idx].vv_list;
6717}
6718
6719/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006720 * Get Dict v: variable value. Caller must take care of reference count when
6721 * needed.
6722 */
6723 dict_T *
6724get_vim_var_dict(int idx)
6725{
6726 return vimvars[idx].vv_dict;
6727}
6728
6729/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006730 * Set v:char to character "c".
6731 */
6732 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006733set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006734{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006735 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006736
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006737 if (has_mbyte)
6738 buf[(*mb_char2bytes)(c, buf)] = NUL;
6739 else
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006740 {
6741 buf[0] = c;
6742 buf[1] = NUL;
6743 }
6744 set_vim_var_string(VV_CHAR, buf, -1);
6745}
6746
6747/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006748 * Set v:count to "count" and v:count1 to "count1".
6749 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 */
6751 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006752set_vcount(
6753 long count,
6754 long count1,
6755 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006757 if (set_prevcount)
6758 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006759 vimvars[VV_COUNT].vv_nr = count;
6760 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761}
6762
6763/*
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02006764 * Save variables that might be changed as a side effect. Used when executing
6765 * a timer callback.
6766 */
6767 void
6768save_vimvars(vimvars_save_T *vvsave)
6769{
6770 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
6771 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
6772 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
6773}
6774
6775/*
6776 * Restore variables saved by save_vimvars().
6777 */
6778 void
6779restore_vimvars(vimvars_save_T *vvsave)
6780{
6781 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
6782 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
6783 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
6784}
6785
6786/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 * Set string v: variable to a copy of "val".
6788 */
6789 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006790set_vim_var_string(
6791 int idx,
6792 char_u *val,
6793 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794{
Bram Moolenaara542c682016-01-31 16:28:04 +01006795 clear_tv(&vimvars[idx].vv_di.di_tv);
6796 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006798 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006800 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006802 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803}
6804
6805/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006806 * Set List v: variable to "val".
6807 */
6808 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006809set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006810{
Bram Moolenaara542c682016-01-31 16:28:04 +01006811 clear_tv(&vimvars[idx].vv_di.di_tv);
6812 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006813 vimvars[idx].vv_list = val;
6814 if (val != NULL)
6815 ++val->lv_refcount;
6816}
6817
6818/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006819 * Set Dictionary v: variable to "val".
6820 */
6821 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006822set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006823{
Bram Moolenaara542c682016-01-31 16:28:04 +01006824 clear_tv(&vimvars[idx].vv_di.di_tv);
6825 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006826 vimvars[idx].vv_dict = val;
6827 if (val != NULL)
6828 {
6829 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006830 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02006831 }
6832}
6833
6834/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 * Set v:register if needed.
6836 */
6837 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006838set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839{
6840 char_u regname;
6841
6842 if (c == 0 || c == ' ')
6843 regname = '"';
6844 else
6845 regname = c;
6846 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006847 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 set_vim_var_string(VV_REG, &regname, 1);
6849}
6850
6851/*
6852 * Get or set v:exception. If "oldval" == NULL, return the current value.
6853 * Otherwise, restore the value to "oldval" and return NULL.
6854 * Must always be called in pairs to save and restore v:exception! Does not
6855 * take care of memory allocations.
6856 */
6857 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006858v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859{
6860 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006861 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862
Bram Moolenaare9a41262005-01-15 22:18:47 +00006863 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 return NULL;
6865}
6866
6867/*
6868 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
6869 * Otherwise, restore the value to "oldval" and return NULL.
6870 * Must always be called in pairs to save and restore v:throwpoint! Does not
6871 * take care of memory allocations.
6872 */
6873 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006874v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875{
6876 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006877 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878
Bram Moolenaare9a41262005-01-15 22:18:47 +00006879 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 return NULL;
6881}
6882
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883/*
6884 * Set v:cmdarg.
6885 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6886 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6887 * Must always be called in pairs!
6888 */
6889 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006890set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891{
6892 char_u *oldval;
6893 char_u *newval;
6894 unsigned len;
6895
Bram Moolenaare9a41262005-01-15 22:18:47 +00006896 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006897 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006899 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006900 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006901 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 }
6903
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006904 if (eap->force_bin == FORCE_BIN)
6905 len = 6;
6906 else if (eap->force_bin == FORCE_NOBIN)
6907 len = 8;
6908 else
6909 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006910
6911 if (eap->read_edit)
6912 len += 7;
6913
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006914 if (eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006915 len += 10; /* " ++ff=unix" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006916 if (eap->force_enc != 0)
6917 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006918 if (eap->bad_char != 0)
6919 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006920
6921 newval = alloc(len + 1);
6922 if (newval == NULL)
6923 return NULL;
6924
6925 if (eap->force_bin == FORCE_BIN)
6926 sprintf((char *)newval, " ++bin");
6927 else if (eap->force_bin == FORCE_NOBIN)
6928 sprintf((char *)newval, " ++nobin");
6929 else
6930 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006931
6932 if (eap->read_edit)
6933 STRCAT(newval, " ++edit");
6934
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006935 if (eap->force_ff != 0)
6936 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006937 eap->force_ff == 'u' ? "unix"
6938 : eap->force_ff == 'd' ? "dos"
6939 : "mac");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006940 if (eap->force_enc != 0)
6941 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6942 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006943 if (eap->bad_char == BAD_KEEP)
6944 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6945 else if (eap->bad_char == BAD_DROP)
6946 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6947 else if (eap->bad_char != 0)
6948 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006949 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006950 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952
6953/*
6954 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006955 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006957 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006958get_var_tv(
6959 char_u *name,
6960 int len, /* length of "name" */
6961 typval_T *rettv, /* NULL when only checking existence */
6962 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
6963 int verbose, /* may give error message */
6964 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965{
6966 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006967 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006968 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970
6971 /* truncate the name, so that we can use strcmp() */
6972 cc = name[len];
6973 name[len] = NUL;
6974
6975 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 * Check for user-defined variables.
6977 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01006978 v = find_var(name, NULL, no_autoload);
6979 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006981 tv = &v->di_tv;
6982 if (dip != NULL)
6983 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 }
6985
Bram Moolenaare9a41262005-01-15 22:18:47 +00006986 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006988 if (rettv != NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006989 semsg(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 ret = FAIL;
6991 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006992 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006993 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994
6995 name[len] = cc;
6996
6997 return ret;
6998}
6999
7000/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007001 * Check if variable "name[len]" is a local variable or an argument.
7002 * If so, "*eval_lavars_used" is set to TRUE.
7003 */
7004 static void
7005check_vars(char_u *name, int len)
7006{
7007 int cc;
7008 char_u *varname;
7009 hashtab_T *ht;
7010
7011 if (eval_lavars_used == NULL)
7012 return;
7013
7014 /* truncate the name, so that we can use strcmp() */
7015 cc = name[len];
7016 name[len] = NUL;
7017
7018 ht = find_var_ht(name, &varname);
7019 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
7020 {
7021 if (find_var(name, NULL, TRUE) != NULL)
7022 *eval_lavars_used = TRUE;
7023 }
7024
7025 name[len] = cc;
7026}
7027
7028/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007029 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
7030 * Also handle function call with Funcref variable: func(expr)
7031 * Can all be combined: dict.func(expr)[idx]['func'](expr)
7032 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007033 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007034handle_subscript(
7035 char_u **arg,
7036 typval_T *rettv,
7037 int evaluate, /* do more than finding the end */
7038 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007039{
7040 int ret = OK;
7041 dict_T *selfdict = NULL;
7042 char_u *s;
7043 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007044 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007045
7046 while (ret == OK
7047 && (**arg == '['
7048 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007049 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7050 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01007051 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007052 {
7053 if (**arg == '(')
7054 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007055 partial_T *pt = NULL;
7056
Bram Moolenaard9fba312005-06-26 22:34:35 +00007057 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007058 if (evaluate)
7059 {
7060 functv = *rettv;
7061 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007062
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007063 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007064 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007065 {
7066 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007067 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007068 }
7069 else
7070 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007071 }
7072 else
7073 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007074 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00007075 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007076 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007077
7078 /* Clear the funcref afterwards, so that deleting it while
7079 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007080 if (evaluate)
7081 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007082
7083 /* Stop the expression evaluation when immediately aborting on
7084 * error, or when an interrupt occurred or an exception was thrown
7085 * but not caught. */
7086 if (aborting())
7087 {
7088 if (ret == OK)
7089 clear_tv(rettv);
7090 ret = FAIL;
7091 }
7092 dict_unref(selfdict);
7093 selfdict = NULL;
7094 }
7095 else /* **arg == '[' || **arg == '.' */
7096 {
7097 dict_unref(selfdict);
7098 if (rettv->v_type == VAR_DICT)
7099 {
7100 selfdict = rettv->vval.v_dict;
7101 if (selfdict != NULL)
7102 ++selfdict->dv_refcount;
7103 }
7104 else
7105 selfdict = NULL;
7106 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
7107 {
7108 clear_tv(rettv);
7109 ret = FAIL;
7110 }
7111 }
7112 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007113
Bram Moolenaar1d429612016-05-24 15:44:17 +02007114 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
7115 * Don't do this when "Func" is already a partial that was bound
7116 * explicitly (pt_auto is FALSE). */
7117 if (selfdict != NULL
7118 && (rettv->v_type == VAR_FUNC
7119 || (rettv->v_type == VAR_PARTIAL
7120 && (rettv->vval.v_partial->pt_auto
7121 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007122 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007123
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007124 dict_unref(selfdict);
7125 return ret;
7126}
7127
7128/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007129 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007130 * value).
7131 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01007132 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007133alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007134{
Bram Moolenaar33570922005-01-25 22:26:29 +00007135 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007136}
7137
7138/*
7139 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 * The string "s" must have been allocated, it is consumed.
7141 * Return NULL for out of memory, the variable otherwise.
7142 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007143 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007144alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145{
Bram Moolenaar33570922005-01-25 22:26:29 +00007146 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007148 rettv = alloc_tv();
7149 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007151 rettv->v_type = VAR_STRING;
7152 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 }
7154 else
7155 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007156 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157}
7158
7159/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007160 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007162 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007163free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164{
7165 if (varp != NULL)
7166 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007167 switch (varp->v_type)
7168 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007169 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007170 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007171 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007172 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007173 vim_free(varp->vval.v_string);
7174 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007175 case VAR_PARTIAL:
7176 partial_unref(varp->vval.v_partial);
7177 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007178 case VAR_BLOB:
7179 blob_unref(varp->vval.v_blob);
7180 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007181 case VAR_LIST:
7182 list_unref(varp->vval.v_list);
7183 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007184 case VAR_DICT:
7185 dict_unref(varp->vval.v_dict);
7186 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007187 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007188#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007189 job_unref(varp->vval.v_job);
7190 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007191#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007192 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007193#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007194 channel_unref(varp->vval.v_channel);
7195 break;
7196#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007197 case VAR_NUMBER:
7198 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007199 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007200 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007201 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 vim_free(varp);
7204 }
7205}
7206
7207/*
7208 * Free the memory for a variable value and set the value to NULL or 0.
7209 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007210 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007211clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212{
7213 if (varp != NULL)
7214 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007215 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007217 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007218 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007219 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007220 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007221 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007222 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007223 case VAR_PARTIAL:
7224 partial_unref(varp->vval.v_partial);
7225 varp->vval.v_partial = NULL;
7226 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007227 case VAR_BLOB:
7228 blob_unref(varp->vval.v_blob);
7229 varp->vval.v_blob = NULL;
7230 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007231 case VAR_LIST:
7232 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007233 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007234 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007235 case VAR_DICT:
7236 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007237 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007238 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007239 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007240 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007241 varp->vval.v_number = 0;
7242 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007243 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007244#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007245 varp->vval.v_float = 0.0;
7246 break;
7247#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007248 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007249#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007250 job_unref(varp->vval.v_job);
7251 varp->vval.v_job = NULL;
7252#endif
7253 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007254 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007255#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007256 channel_unref(varp->vval.v_channel);
7257 varp->vval.v_channel = NULL;
7258#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007259 case VAR_UNKNOWN:
7260 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007262 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 }
7264}
7265
7266/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007267 * Set the value of a variable to NULL without freeing items.
7268 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007269 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007270init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007271{
7272 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007273 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007274}
7275
7276/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 * Get the number value of a variable.
7278 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007279 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007280 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007281 * caller of incompatible types: it sets *denote to TRUE if "denote"
7282 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007284 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007285tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007287 int error = FALSE;
7288
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007289 return tv_get_number_chk(varp, &error); /* return 0L on error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007290}
7291
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007292 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007293tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007294{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007295 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007297 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007299 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007300 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007301 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007302#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007303 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007304 break;
7305#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007306 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007307 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007308 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007309 break;
7310 case VAR_STRING:
7311 if (varp->vval.v_string != NULL)
7312 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01007313 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007314 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007315 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007316 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007317 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007318 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007319 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007320 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007321 case VAR_SPECIAL:
7322 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7323 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007324 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007325#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007326 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007327 break;
7328#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007329 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007330#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007331 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007332 break;
7333#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007334 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007335 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007336 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007337 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007338 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007339 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007341 if (denote == NULL) /* useful for values that must be unsigned */
7342 n = -1;
7343 else
7344 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007345 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346}
7347
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007348#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007349 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007350tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007351{
7352 switch (varp->v_type)
7353 {
7354 case VAR_NUMBER:
7355 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007356 case VAR_FLOAT:
7357 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007358 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007359 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007360 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007361 break;
7362 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007363 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007364 break;
7365 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007366 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007367 break;
7368 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007369 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007370 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007371 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007372 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007373 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007374 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007375# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007376 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007377 break;
7378# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007379 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007380# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007381 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007382 break;
7383# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007384 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007385 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007386 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007387 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007388 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007389 break;
7390 }
7391 return 0;
7392}
7393#endif
7394
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 * Get the string value of a variable.
7397 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007398 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7399 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 * If the String variable has never been set, return an empty string.
7401 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007402 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007403 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007405 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007406tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407{
7408 static char_u mybuf[NUMBUFLEN];
7409
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007410 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411}
7412
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007413 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007414tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007416 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007417
7418 return res != NULL ? res : (char_u *)"";
7419}
7420
Bram Moolenaar7d647822014-04-05 21:28:56 +02007421/*
7422 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7423 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007424 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007425tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007426{
7427 static char_u mybuf[NUMBUFLEN];
7428
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007429 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007430}
7431
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007432 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007433tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007434{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007435 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007437 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007438 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007439 (long_long_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007440 return buf;
7441 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007442 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007443 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007444 break;
7445 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007446 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007447 break;
7448 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007449 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007450 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007451 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007452#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007453 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007454 break;
7455#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007456 case VAR_STRING:
7457 if (varp->vval.v_string != NULL)
7458 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007459 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007460 case VAR_SPECIAL:
7461 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7462 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007463 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007464 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007465 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007466 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007467#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007468 {
7469 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007470 char *status;
7471
7472 if (job == NULL)
7473 return (char_u *)"no process";
7474 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007475 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007476 : "run";
7477# ifdef UNIX
7478 vim_snprintf((char *)buf, NUMBUFLEN,
7479 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01007480# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007481 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007482 "process %ld %s",
7483 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007484 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007485# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007486 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007487 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7488# endif
7489 return buf;
7490 }
7491#endif
7492 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007493 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007494#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007495 {
7496 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007497 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007498
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007499 if (channel == NULL)
7500 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7501 else
7502 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007503 "channel %d %s", channel->ch_id, status);
7504 return buf;
7505 }
7506#endif
7507 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007508 case VAR_UNKNOWN:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007509 emsg(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007510 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007512 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513}
7514
7515/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007516 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
7517 * string() on Dict, List, etc.
7518 */
7519 char_u *
7520tv_stringify(typval_T *varp, char_u *buf)
7521{
7522 if (varp->v_type == VAR_LIST
7523 || varp->v_type == VAR_DICT
7524 || varp->v_type == VAR_FUNC
7525 || varp->v_type == VAR_PARTIAL
7526 || varp->v_type == VAR_FLOAT)
7527 {
7528 typval_T tmp;
7529
7530 f_string(varp, &tmp);
7531 tv_get_string_buf(&tmp, buf);
7532 clear_tv(varp);
7533 *varp = tmp;
7534 return tmp.vval.v_string;
7535 }
7536 return tv_get_string_buf(varp, buf);
7537}
7538
7539/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 * Find variable "name" in the list of variables.
7541 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007542 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007543 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007544 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007546 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007547find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007550 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007551 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552
Bram Moolenaara7043832005-01-21 11:56:39 +00007553 ht = find_var_ht(name, &varname);
7554 if (htp != NULL)
7555 *htp = ht;
7556 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007558 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7559 if (ret != NULL)
7560 return ret;
7561
7562 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007563 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564}
7565
7566/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007567 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007568 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007570 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007571find_var_in_ht(
7572 hashtab_T *ht,
7573 int htname,
7574 char_u *varname,
7575 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007576{
Bram Moolenaar33570922005-01-25 22:26:29 +00007577 hashitem_T *hi;
7578
7579 if (*varname == NUL)
7580 {
7581 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007582 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007583 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007584 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007585 case 'g': return &globvars_var;
7586 case 'v': return &vimvars_var;
7587 case 'b': return &curbuf->b_bufvar;
7588 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007589 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007590 case 'l': return get_funccal_local_var();
7591 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007592 }
7593 return NULL;
7594 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007595
7596 hi = hash_find(ht, varname);
7597 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007598 {
7599 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007600 * worked find the variable again. Don't auto-load a script if it was
7601 * loaded already, otherwise it would be loaded every time when
7602 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007603 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007604 {
7605 /* Note: script_autoload() may make "hi" invalid. It must either
7606 * be obtained again or not used. */
7607 if (!script_autoload(varname, FALSE) || aborting())
7608 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007609 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007610 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007611 if (HASHITEM_EMPTY(hi))
7612 return NULL;
7613 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007614 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007615}
7616
7617/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007618 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007619 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007620 * Set "varname" to the start of name without ':'.
7621 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007622 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007623find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007625 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007626 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007627
Bram Moolenaar73627d02015-08-11 15:46:09 +02007628 if (name[0] == NUL)
7629 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 if (name[1] != ':')
7631 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007632 /* The name must not start with a colon or #. */
7633 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 return NULL;
7635 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007636
7637 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007638 hi = hash_find(&compat_hashtab, name);
7639 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +00007640 return &compat_hashtab;
7641
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007642 ht = get_funccal_local_ht();
7643 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007644 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007645 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 }
7647 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007648 if (*name == 'g') /* global variable */
7649 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007650 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7651 */
7652 if (vim_strchr(name + 2, ':') != NULL
7653 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007654 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007656 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007658 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007659 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007660 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007661 if (*name == 'v') /* v: variable */
7662 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007663 if (*name == 'a') /* a: function argument */
7664 return get_funccal_args_ht();
7665 if (*name == 'l') /* l: local function variable */
7666 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 if (*name == 's' /* script variable */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007668 && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
7669 return &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 return NULL;
7671}
7672
7673/*
7674 * Get the string value of a (global/local) variable.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007675 * Note: see tv_get_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 * Returns NULL when it doesn't exist.
7677 */
7678 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007679get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680{
Bram Moolenaar33570922005-01-25 22:26:29 +00007681 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007683 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 if (v == NULL)
7685 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007686 return tv_get_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687}
7688
7689/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007690 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 * sourcing this script and when executing functions defined in the script.
7692 */
7693 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007694new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695{
Bram Moolenaara7043832005-01-21 11:56:39 +00007696 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007697 hashtab_T *ht;
7698 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007699
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7701 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007702 /* Re-allocating ga_data means that an ht_array pointing to
7703 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007704 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007705 for (i = 1; i <= ga_scripts.ga_len; ++i)
7706 {
7707 ht = &SCRIPT_VARS(i);
7708 if (ht->ht_mask == HT_INIT_SIZE - 1)
7709 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007710 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007711 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007712 }
7713
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 while (ga_scripts.ga_len < id)
7715 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007716 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007717 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007718 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 }
7721 }
7722}
7723
7724/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007725 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7726 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 */
7728 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007729init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730{
Bram Moolenaar33570922005-01-25 22:26:29 +00007731 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007732 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007733 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007734 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007735 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007736 dict_var->di_tv.vval.v_dict = dict;
7737 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007738 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007739 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7740 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741}
7742
7743/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007744 * Unreference a dictionary initialized by init_var_dict().
7745 */
7746 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007747unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007748{
7749 /* Now the dict needs to be freed if no one else is using it, go back to
7750 * normal reference counting. */
7751 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7752 dict_unref(dict);
7753}
7754
7755/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007757 * Frees all allocated variables and the value they contain.
7758 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 */
7760 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007761vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007762{
7763 vars_clear_ext(ht, TRUE);
7764}
7765
7766/*
7767 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7768 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007769 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007770vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771{
Bram Moolenaara7043832005-01-21 11:56:39 +00007772 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007773 hashitem_T *hi;
7774 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775
Bram Moolenaar33570922005-01-25 22:26:29 +00007776 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007777 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007778 for (hi = ht->ht_array; todo > 0; ++hi)
7779 {
7780 if (!HASHITEM_EMPTY(hi))
7781 {
7782 --todo;
7783
Bram Moolenaar33570922005-01-25 22:26:29 +00007784 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007785 * ht_array might change then. hash_clear() takes care of it
7786 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007787 v = HI2DI(hi);
7788 if (free_val)
7789 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007790 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007791 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007792 }
7793 }
7794 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007795 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796}
7797
Bram Moolenaara7043832005-01-21 11:56:39 +00007798/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007799 * Delete a variable from hashtab "ht" at item "hi".
7800 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007801 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007803delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804{
Bram Moolenaar33570922005-01-25 22:26:29 +00007805 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007806
7807 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007808 clear_tv(&di->di_tv);
7809 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810}
7811
7812/*
7813 * List the value of one internal variable.
7814 */
7815 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01007816list_one_var(dictitem_T *v, char *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007818 char_u *tofree;
7819 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007820 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007821
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007822 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007823 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007824 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007825 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826}
7827
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007829list_one_var_a(
Bram Moolenaar32526b32019-01-19 17:43:09 +01007830 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007831 char_u *name,
7832 int type,
7833 char_u *string,
7834 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835{
Bram Moolenaar31859182007-08-14 20:41:13 +00007836 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7837 msg_start();
7838 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 if (name != NULL) /* "a:" vars don't have a name stored */
Bram Moolenaar32526b32019-01-19 17:43:09 +01007840 msg_puts((char *)name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 msg_putchar(' ');
7842 msg_advance(22);
7843 if (type == VAR_NUMBER)
7844 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007845 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007846 msg_putchar('*');
7847 else if (type == VAR_LIST)
7848 {
7849 msg_putchar('[');
7850 if (*string == '[')
7851 ++string;
7852 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007853 else if (type == VAR_DICT)
7854 {
7855 msg_putchar('{');
7856 if (*string == '{')
7857 ++string;
7858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 else
7860 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007861
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007863
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007864 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007865 msg_puts("()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007866 if (*first)
7867 {
7868 msg_clr_eos();
7869 *first = FALSE;
7870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871}
7872
7873/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007874 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 * If the variable already exists, the value is updated.
7876 * Otherwise the variable is created.
7877 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007878 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007879set_var(
7880 char_u *name,
7881 typval_T *tv,
7882 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883{
Bram Moolenaar33570922005-01-25 22:26:29 +00007884 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007886 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007888 ht = find_var_ht(name, &varname);
7889 if (ht == NULL || *varname == NUL)
7890 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007891 semsg(_(e_illvar), name);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007892 return;
7893 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007894 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007895
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007896 /* Search in parent scope which is possible to reference from lambda */
7897 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007898 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007899
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007900 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7901 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007902 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007903
Bram Moolenaar33570922005-01-25 22:26:29 +00007904 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007906 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007907 if (var_check_ro(v->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007908 || var_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007909 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007910
7911 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007912 * Handle setting internal v: variables separately where needed to
7913 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007914 */
7915 if (ht == &vimvarht)
7916 {
7917 if (v->di_tv.v_type == VAR_STRING)
7918 {
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01007919 VIM_CLEAR(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007920 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01007921 {
7922 char_u *val = tv_get_string(tv);
7923
7924 // Careful: when assigning to v:errmsg and tv_get_string()
7925 // causes an error message the variable will alrady be set.
7926 if (v->di_tv.vval.v_string == NULL)
7927 v->di_tv.vval.v_string = vim_strsave(val);
7928 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007929 else
7930 {
7931 /* Take over the string to avoid an extra alloc/free. */
7932 v->di_tv.vval.v_string = tv->vval.v_string;
7933 tv->vval.v_string = NULL;
7934 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007935 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007936 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007937 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007938 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007939 v->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007940 if (STRCMP(varname, "searchforward") == 0)
7941 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007942#ifdef FEAT_SEARCH_EXTRA
7943 else if (STRCMP(varname, "hlsearch") == 0)
7944 {
7945 no_hlsearch = !v->di_tv.vval.v_number;
7946 redraw_all_later(SOME_VALID);
7947 }
7948#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007949 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007950 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007951 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01007952 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007953 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01007954 return;
7955 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007956 }
7957
7958 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 }
7960 else /* add a new variable */
7961 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01007962 // Can't add "v:" or "a:" variable.
7963 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007964 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007965 semsg(_(e_illvar), name);
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007966 return;
7967 }
7968
Bram Moolenaar31b81602019-02-10 22:14:27 +01007969 // Make sure the variable name is valid.
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007970 if (!valid_varname(varname))
7971 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00007972
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007973 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7974 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00007975 if (v == NULL)
7976 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007977 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00007978 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007980 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 return;
7982 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007983 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007985
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007986 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00007987 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007988 else
7989 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007990 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007991 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007992 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994}
7995
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007996/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007997 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00007998 * Also give an error message.
7999 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008000 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008001var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00008002{
8003 if (flags & DI_FLAGS_RO)
8004 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008005 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008006 return TRUE;
8007 }
8008 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
8009 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008010 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008011 return TRUE;
8012 }
8013 return FALSE;
8014}
8015
8016/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008017 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
8018 * Also give an error message.
8019 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008020 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008021var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008022{
8023 if (flags & DI_FLAGS_FIX)
8024 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008025 semsg(_("E795: Cannot delete variable %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008026 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008027 return TRUE;
8028 }
8029 return FALSE;
8030}
8031
8032/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008033 * Check if a funcref is assigned to a valid variable name.
8034 * Return TRUE and give an error if not.
8035 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008036 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008037var_check_func_name(
8038 char_u *name, /* points to start of variable name */
8039 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008040{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02008041 /* Allow for w: b: s: and t:. */
8042 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008043 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
8044 ? name[2] : name[0]))
8045 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008046 semsg(_("E704: Funcref variable name must start with a capital: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008047 name);
8048 return TRUE;
8049 }
8050 /* Don't allow hiding a function. When "v" is not NULL we might be
8051 * assigning another function to the same var, the type is checked
8052 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02008053 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008054 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008055 semsg(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008056 name);
8057 return TRUE;
8058 }
8059 return FALSE;
8060}
8061
8062/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008063 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02008064 * Also give an error message, using "name" or _("name") when use_gettext is
8065 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008066 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008067 int
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008068var_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008069{
8070 if (lock & VAR_LOCKED)
8071 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008072 semsg(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008073 name == NULL ? (char_u *)_("Unknown")
8074 : use_gettext ? (char_u *)_(name)
8075 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008076 return TRUE;
8077 }
8078 if (lock & VAR_FIXED)
8079 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008080 semsg(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008081 name == NULL ? (char_u *)_("Unknown")
8082 : use_gettext ? (char_u *)_(name)
8083 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008084 return TRUE;
8085 }
8086 return FALSE;
8087}
8088
8089/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008090 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
8091 * Also give an error message, using "name" or _("name") when use_gettext is
8092 * TRUE.
8093 */
8094 static int
8095tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
8096{
8097 int lock = 0;
8098
8099 switch (tv->v_type)
8100 {
8101 case VAR_BLOB:
8102 if (tv->vval.v_blob != NULL)
8103 lock = tv->vval.v_blob->bv_lock;
8104 break;
8105 case VAR_LIST:
8106 if (tv->vval.v_list != NULL)
8107 lock = tv->vval.v_list->lv_lock;
8108 break;
8109 case VAR_DICT:
8110 if (tv->vval.v_dict != NULL)
8111 lock = tv->vval.v_dict->dv_lock;
8112 break;
8113 default:
8114 break;
8115 }
8116 return var_check_lock(tv->v_lock, name, use_gettext)
8117 || (lock != 0 && var_check_lock(lock, name, use_gettext));
8118}
8119
8120/*
8121 * Check if a variable name is valid.
8122 * Return FALSE and give an error if not.
8123 */
8124 int
8125valid_varname(char_u *varname)
8126{
8127 char_u *p;
8128
8129 for (p = varname; *p != NUL; ++p)
8130 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
8131 && *p != AUTOLOAD_CHAR)
8132 {
8133 semsg(_(e_illvar), varname);
8134 return FALSE;
8135 }
8136 return TRUE;
8137}
8138
8139/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008140 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008141 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008142 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008143 * It is OK for "from" and "to" to point to the same item. This is used to
8144 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008145 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008146 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008147copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008149 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008150 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008151 switch (from->v_type)
8152 {
8153 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008154 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008155 to->vval.v_number = from->vval.v_number;
8156 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008157 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008158#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008159 to->vval.v_float = from->vval.v_float;
8160 break;
8161#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008162 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008163#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01008164 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01008165 if (to->vval.v_job != NULL)
8166 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01008167 break;
8168#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01008169 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008170#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01008171 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01008172 if (to->vval.v_channel != NULL)
8173 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01008174 break;
8175#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008176 case VAR_STRING:
8177 case VAR_FUNC:
8178 if (from->vval.v_string == NULL)
8179 to->vval.v_string = NULL;
8180 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008181 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008182 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008183 if (from->v_type == VAR_FUNC)
8184 func_ref(to->vval.v_string);
8185 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008186 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008187 case VAR_PARTIAL:
8188 if (from->vval.v_partial == NULL)
8189 to->vval.v_partial = NULL;
8190 else
8191 {
8192 to->vval.v_partial = from->vval.v_partial;
8193 ++to->vval.v_partial->pt_refcount;
8194 }
8195 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008196 case VAR_BLOB:
8197 if (from->vval.v_blob == NULL)
8198 to->vval.v_blob = NULL;
8199 else
8200 {
8201 to->vval.v_blob = from->vval.v_blob;
8202 ++to->vval.v_blob->bv_refcount;
8203 }
8204 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008205 case VAR_LIST:
8206 if (from->vval.v_list == NULL)
8207 to->vval.v_list = NULL;
8208 else
8209 {
8210 to->vval.v_list = from->vval.v_list;
8211 ++to->vval.v_list->lv_refcount;
8212 }
8213 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008214 case VAR_DICT:
8215 if (from->vval.v_dict == NULL)
8216 to->vval.v_dict = NULL;
8217 else
8218 {
8219 to->vval.v_dict = from->vval.v_dict;
8220 ++to->vval.v_dict->dv_refcount;
8221 }
8222 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008223 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008224 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008225 break;
8226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227}
8228
8229/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008230 * Make a copy of an item.
8231 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008232 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
8233 * reference to an already copied list/dict can be used.
8234 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008235 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008236 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008237item_copy(
8238 typval_T *from,
8239 typval_T *to,
8240 int deep,
8241 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008242{
8243 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008244 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008245
Bram Moolenaar33570922005-01-25 22:26:29 +00008246 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008247 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008248 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008249 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008250 }
8251 ++recurse;
8252
8253 switch (from->v_type)
8254 {
8255 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008256 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008257 case VAR_STRING:
8258 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008259 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008260 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008261 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008262 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008263 copy_tv(from, to);
8264 break;
8265 case VAR_LIST:
8266 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008267 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008268 if (from->vval.v_list == NULL)
8269 to->vval.v_list = NULL;
8270 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8271 {
8272 /* use the copy made earlier */
8273 to->vval.v_list = from->vval.v_list->lv_copylist;
8274 ++to->vval.v_list->lv_refcount;
8275 }
8276 else
8277 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8278 if (to->vval.v_list == NULL)
8279 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008280 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008281 case VAR_BLOB:
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008282 ret = blob_copy(from, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008283 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008284 case VAR_DICT:
8285 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008286 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008287 if (from->vval.v_dict == NULL)
8288 to->vval.v_dict = NULL;
8289 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8290 {
8291 /* use the copy made earlier */
8292 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8293 ++to->vval.v_dict->dv_refcount;
8294 }
8295 else
8296 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8297 if (to->vval.v_dict == NULL)
8298 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008299 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008300 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008301 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008302 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008303 }
8304 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008305 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008306}
8307
8308/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008309 * This function is used by f_input() and f_inputdialog() functions. The third
8310 * argument to f_input() specifies the type of completion to use at the
8311 * prompt. The third argument to f_inputdialog() specifies the value to return
8312 * when the user cancels the prompt.
8313 */
8314 void
8315get_user_input(
8316 typval_T *argvars,
8317 typval_T *rettv,
8318 int inputdialog,
8319 int secret)
8320{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008321 char_u *prompt = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008322 char_u *p = NULL;
8323 int c;
8324 char_u buf[NUMBUFLEN];
8325 int cmd_silent_save = cmd_silent;
8326 char_u *defstr = (char_u *)"";
8327 int xp_type = EXPAND_NOTHING;
8328 char_u *xp_arg = NULL;
8329
8330 rettv->v_type = VAR_STRING;
8331 rettv->vval.v_string = NULL;
8332
8333#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008334 /* While starting up, there is no place to enter text. When running tests
8335 * with --not-a-term we assume feedkeys() will be used. */
8336 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008337 return;
8338#endif
8339
8340 cmd_silent = FALSE; /* Want to see the prompt. */
8341 if (prompt != NULL)
8342 {
8343 /* Only the part of the message after the last NL is considered as
8344 * prompt for the command line */
8345 p = vim_strrchr(prompt, '\n');
8346 if (p == NULL)
8347 p = prompt;
8348 else
8349 {
8350 ++p;
8351 c = *p;
8352 *p = NUL;
8353 msg_start();
8354 msg_clr_eos();
Bram Moolenaar32526b32019-01-19 17:43:09 +01008355 msg_puts_attr((char *)prompt, echo_attr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008356 msg_didout = FALSE;
8357 msg_starthere();
8358 *p = c;
8359 }
8360 cmdline_row = msg_row;
8361
8362 if (argvars[1].v_type != VAR_UNKNOWN)
8363 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008364 defstr = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008365 if (defstr != NULL)
8366 stuffReadbuffSpec(defstr);
8367
8368 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8369 {
8370 char_u *xp_name;
8371 int xp_namelen;
8372 long argt;
8373
8374 /* input() with a third argument: completion */
8375 rettv->vval.v_string = NULL;
8376
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008377 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008378 if (xp_name == NULL)
8379 return;
8380
8381 xp_namelen = (int)STRLEN(xp_name);
8382
8383 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8384 &xp_arg) == FAIL)
8385 return;
8386 }
8387 }
8388
8389 if (defstr != NULL)
8390 {
8391 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02008392
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008393 ex_normal_busy = 0;
8394 rettv->vval.v_string =
8395 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8396 xp_type, xp_arg);
8397 ex_normal_busy = save_ex_normal_busy;
8398 }
8399 if (inputdialog && rettv->vval.v_string == NULL
8400 && argvars[1].v_type != VAR_UNKNOWN
8401 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008402 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008403 &argvars[2], buf));
8404
8405 vim_free(xp_arg);
8406
8407 /* since the user typed this, no need to wait for return */
8408 need_wait_return = FALSE;
8409 msg_didout = FALSE;
8410 }
8411 cmd_silent = cmd_silent_save;
8412}
8413
8414/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 * ":echo expr1 ..." print each argument separated with a space, add a
8416 * newline at the end.
8417 * ":echon expr1 ..." print each argument plain.
8418 */
8419 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008420ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421{
8422 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008423 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008424 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 char_u *p;
8426 int needclr = TRUE;
8427 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008428 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01008429 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008430 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431
8432 if (eap->skip)
8433 ++emsg_skip;
8434 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8435 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008436 /* If eval1() causes an error message the text from the command may
8437 * still need to be cleared. E.g., "echo 22,44". */
8438 need_clr_eos = needclr;
8439
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008441 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 {
8443 /*
8444 * Report the invalid expression unless the expression evaluation
8445 * has been cancelled due to an aborting error, an interrupt, or an
8446 * exception.
8447 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008448 if (!aborting() && did_emsg == did_emsg_before
8449 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008450 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008451 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 break;
8453 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008454 need_clr_eos = FALSE;
8455
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 if (!eap->skip)
8457 {
8458 if (atstart)
8459 {
8460 atstart = FALSE;
8461 /* Call msg_start() after eval1(), evaluating the expression
8462 * may cause a message to appear. */
8463 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008464 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008465 /* Mark the saved text as finishing the line, so that what
8466 * follows is displayed on a new line when scrolling back
8467 * at the more prompt. */
8468 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 }
8472 else if (eap->cmdidx == CMD_echo)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008473 msg_puts_attr(" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008474 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008475 if (p != NULL)
8476 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008478 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008480 if (*p != TAB && needclr)
8481 {
8482 /* remove any text still there from the command */
8483 msg_clr_eos();
8484 needclr = FALSE;
8485 }
8486 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 }
8488 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008489 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008490 if (has_mbyte)
8491 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008492 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008493
8494 (void)msg_outtrans_len_attr(p, i, echo_attr);
8495 p += i - 1;
8496 }
8497 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008498 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008501 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008503 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 arg = skipwhite(arg);
8505 }
8506 eap->nextcmd = check_nextcmd(arg);
8507
8508 if (eap->skip)
8509 --emsg_skip;
8510 else
8511 {
8512 /* remove text that may still be there from the command */
8513 if (needclr)
8514 msg_clr_eos();
8515 if (eap->cmdidx == CMD_echo)
8516 msg_end();
8517 }
8518}
8519
8520/*
8521 * ":echohl {name}".
8522 */
8523 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008524ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008526 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527}
8528
8529/*
8530 * ":execute expr1 ..." execute the result of an expression.
8531 * ":echomsg expr1 ..." Print a message
8532 * ":echoerr expr1 ..." Print an error
8533 * Each gets spaces around each argument and a newline at the end for
8534 * echo commands
8535 */
8536 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008537ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538{
8539 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008540 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 int ret = OK;
8542 char_u *p;
8543 garray_T ga;
8544 int len;
Bram Moolenaar8ff5af92018-11-28 21:20:38 +01008545 int save_did_emsg = did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546
8547 ga_init2(&ga, 1, 80);
8548
8549 if (eap->skip)
8550 ++emsg_skip;
8551 while (*arg != NUL && *arg != '|' && *arg != '\n')
8552 {
8553 p = arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01008554 ret = eval1_emsg(&arg, &rettv, !eap->skip);
8555 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557
8558 if (!eap->skip)
8559 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008560 char_u buf[NUMBUFLEN];
8561
8562 if (eap->cmdidx == CMD_execute)
8563 p = tv_get_string_buf(&rettv, buf);
8564 else
8565 p = tv_stringify(&rettv, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 len = (int)STRLEN(p);
8567 if (ga_grow(&ga, len + 2) == FAIL)
8568 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008569 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 ret = FAIL;
8571 break;
8572 }
8573 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 ga.ga_len += len;
8577 }
8578
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008579 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 arg = skipwhite(arg);
8581 }
8582
8583 if (ret != FAIL && ga.ga_data != NULL)
8584 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008585 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8586 {
8587 /* Mark the already saved text as finishing the line, so that what
8588 * follows is displayed on a new line when scrolling back at the
8589 * more prompt. */
8590 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008591 }
8592
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008594 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01008595 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008596 out_flush();
8597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598 else if (eap->cmdidx == CMD_echoerr)
8599 {
8600 /* We don't want to abort following commands, restore did_emsg. */
8601 save_did_emsg = did_emsg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008602 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603 if (!force_abort)
8604 did_emsg = save_did_emsg;
8605 }
8606 else if (eap->cmdidx == CMD_execute)
8607 do_cmdline((char_u *)ga.ga_data,
8608 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8609 }
8610
8611 ga_clear(&ga);
8612
8613 if (eap->skip)
8614 --emsg_skip;
8615
8616 eap->nextcmd = check_nextcmd(arg);
8617}
8618
8619/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008620 * Find window specified by "vp" in tabpage "tp".
8621 */
8622 win_T *
8623find_win_by_nr(
8624 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01008625 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008626{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008627 win_T *wp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008628 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008629
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008630 if (nr < 0)
8631 return NULL;
8632 if (nr == 0)
8633 return curwin;
8634
Bram Moolenaar29323592016-07-24 22:04:11 +02008635 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8636 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008637 if (nr >= LOWEST_WIN_ID)
8638 {
8639 if (wp->w_id == nr)
8640 return wp;
8641 }
8642 else if (--nr <= 0)
8643 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008644 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008645 if (nr >= LOWEST_WIN_ID)
8646 return NULL;
8647 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008648}
8649
8650/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02008651 * Find a window: When using a Window ID in any tab page, when using a number
8652 * in the current tab page.
8653 */
8654 win_T *
8655find_win_by_nr_or_id(typval_T *vp)
8656{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008657 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02008658
8659 if (nr >= LOWEST_WIN_ID)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01008660 return win_id2wp(tv_get_number(vp));
Bram Moolenaare6e39892018-10-25 12:32:11 +02008661 return find_win_by_nr(vp, NULL);
8662}
8663
8664/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008665 * Find window specified by "wvp" in tabpage "tvp".
8666 */
8667 win_T *
8668find_tabwin(
8669 typval_T *wvp, /* VAR_UNKNOWN for current window */
8670 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
8671{
8672 win_T *wp = NULL;
8673 tabpage_T *tp = NULL;
8674 long n;
8675
8676 if (wvp->v_type != VAR_UNKNOWN)
8677 {
8678 if (tvp->v_type != VAR_UNKNOWN)
8679 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008680 n = (long)tv_get_number(tvp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008681 if (n >= 0)
8682 tp = find_tabpage(n);
8683 }
8684 else
8685 tp = curtab;
8686
8687 if (tp != NULL)
8688 wp = find_win_by_nr(wvp, tp);
8689 }
8690 else
8691 wp = curwin;
8692
8693 return wp;
8694}
8695
8696/*
8697 * getwinvar() and gettabwinvar()
8698 */
8699 void
8700getwinvar(
8701 typval_T *argvars,
8702 typval_T *rettv,
8703 int off) /* 1 for gettabwinvar() */
8704{
8705 win_T *win;
8706 char_u *varname;
8707 dictitem_T *v;
8708 tabpage_T *tp = NULL;
8709 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008710 win_T *oldcurwin;
8711 tabpage_T *oldtabpage;
8712 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008713
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008714 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008715 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008716 else
8717 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008718 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008719 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008720 ++emsg_off;
8721
8722 rettv->v_type = VAR_STRING;
8723 rettv->vval.v_string = NULL;
8724
8725 if (win != NULL && varname != NULL)
8726 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008727 /* Set curwin to be our win, temporarily. Also set the tabpage,
8728 * otherwise the window is not valid. Only do this when needed,
8729 * autocommands get blocked. */
8730 need_switch_win = !(tp == curtab && win == curwin);
8731 if (!need_switch_win
8732 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008733 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008734 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008735 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008736 if (varname[1] == NUL)
8737 {
8738 /* get all window-local options in a dict */
8739 dict_T *opts = get_winbuf_options(FALSE);
8740
8741 if (opts != NULL)
8742 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008743 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02008744 done = TRUE;
8745 }
8746 }
8747 else if (get_option_tv(&varname, rettv, 1) == OK)
8748 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008749 done = TRUE;
8750 }
8751 else
8752 {
8753 /* Look up the variable. */
8754 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8755 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8756 varname, FALSE);
8757 if (v != NULL)
8758 {
8759 copy_tv(&v->di_tv, rettv);
8760 done = TRUE;
8761 }
8762 }
8763 }
8764
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008765 if (need_switch_win)
8766 /* restore previous notion of curwin */
8767 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008768 }
8769
8770 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8771 /* use the default return value */
8772 copy_tv(&argvars[off + 2], rettv);
8773
8774 --emsg_off;
8775}
8776
8777/*
8778 * "setwinvar()" and "settabwinvar()" functions
8779 */
8780 void
8781setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8782{
8783 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008784 win_T *save_curwin;
8785 tabpage_T *save_curtab;
8786 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008787 char_u *varname, *winvarname;
8788 typval_T *varp;
8789 char_u nbuf[NUMBUFLEN];
8790 tabpage_T *tp = NULL;
8791
Bram Moolenaare0fb7d12019-02-12 20:48:10 +01008792 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008793 return;
8794
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008795 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008796 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008797 else
8798 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008799 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008800 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008801 varp = &argvars[off + 2];
8802
8803 if (win != NULL && varname != NULL && varp != NULL)
8804 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008805 need_switch_win = !(tp == curtab && win == curwin);
8806 if (!need_switch_win
8807 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008808 {
8809 if (*varname == '&')
8810 {
8811 long numval;
8812 char_u *strval;
8813 int error = FALSE;
8814
8815 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008816 numval = (long)tv_get_number_chk(varp, &error);
8817 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008818 if (!error && strval != NULL)
8819 set_option_value(varname, numval, strval, OPT_LOCAL);
8820 }
8821 else
8822 {
8823 winvarname = alloc((unsigned)STRLEN(varname) + 3);
8824 if (winvarname != NULL)
8825 {
8826 STRCPY(winvarname, "w:");
8827 STRCPY(winvarname + 2, varname);
8828 set_var(winvarname, varp, TRUE);
8829 vim_free(winvarname);
8830 }
8831 }
8832 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008833 if (need_switch_win)
8834 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008835 }
8836}
8837
8838/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8840 * "arg" points to the "&" or '+' when called, to "option" when returning.
8841 * Returns NULL when no option name found. Otherwise pointer to the char
8842 * after the option name.
8843 */
8844 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008845find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846{
8847 char_u *p = *arg;
8848
8849 ++p;
8850 if (*p == 'g' && p[1] == ':')
8851 {
8852 *opt_flags = OPT_GLOBAL;
8853 p += 2;
8854 }
8855 else if (*p == 'l' && p[1] == ':')
8856 {
8857 *opt_flags = OPT_LOCAL;
8858 p += 2;
8859 }
8860 else
8861 *opt_flags = 0;
8862
8863 if (!ASCII_ISALPHA(*p))
8864 return NULL;
8865 *arg = p;
8866
8867 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
8868 p += 4; /* termcap option */
8869 else
8870 while (ASCII_ISALPHA(*p))
8871 ++p;
8872 return p;
8873}
8874
8875/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008876 * Return the autoload script name for a function or variable name.
8877 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02008879 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008880autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02008881{
Bram Moolenaara1544c02013-05-30 12:35:52 +02008882 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008883 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02008884
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008885 /* Get the script file name: replace '#' with '/', append ".vim". */
8886 scriptname = alloc((unsigned)(STRLEN(name) + 14));
8887 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008888 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008889 STRCPY(scriptname, "autoload/");
8890 STRCAT(scriptname, name);
8891 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
8892 STRCAT(scriptname, ".vim");
8893 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
8894 *p = '/';
8895 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008896}
8897
8898/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008899 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008900 * Return TRUE if a package was loaded.
8901 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008902 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008903script_autoload(
8904 char_u *name,
8905 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008906{
8907 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008908 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008909 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008910 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008911
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008912 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008913 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008914 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008915 return FALSE;
8916
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008917 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008918
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008919 /* Find the name in the list of previously loaded package names. Skip
8920 * "autoload/", it's always the same. */
8921 for (i = 0; i < ga_loaded.ga_len; ++i)
8922 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
8923 break;
8924 if (!reload && i < ga_loaded.ga_len)
8925 ret = FALSE; /* was loaded already */
8926 else
8927 {
8928 /* Remember the name if it wasn't loaded already. */
8929 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
8930 {
8931 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
8932 tofree = NULL;
8933 }
8934
8935 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008936 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008937 ret = TRUE;
8938 }
8939
8940 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008941 return ret;
8942}
8943
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
8945typedef enum
8946{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008947 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
8948 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
8949 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950} var_flavour_T;
8951
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01008953var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954{
8955 char_u *p = varname;
8956
8957 if (ASCII_ISUPPER(*p))
8958 {
8959 while (*(++p))
8960 if (ASCII_ISLOWER(*p))
8961 return VAR_FLAVOUR_SESSION;
8962 return VAR_FLAVOUR_VIMINFO;
8963 }
8964 else
8965 return VAR_FLAVOUR_DEFAULT;
8966}
8967#endif
8968
8969#if defined(FEAT_VIMINFO) || defined(PROTO)
8970/*
8971 * Restore global vars that start with a capital from the viminfo file
8972 */
8973 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008974read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975{
8976 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008977 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00008978 typval_T tv;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02008979 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980
8981 if (!writing && (find_viminfo_parameter('!') != NULL))
8982 {
8983 tab = vim_strchr(virp->vir_line + 1, '\t');
8984 if (tab != NULL)
8985 {
8986 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008987 switch (*tab)
8988 {
8989 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008990#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008991 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008992#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008993 case 'D': type = VAR_DICT; break;
8994 case 'L': type = VAR_LIST; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008995 case 'B': type = VAR_BLOB; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008996 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998
8999 tab = vim_strchr(tab, '\t');
9000 if (tab != NULL)
9001 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009002 tv.v_type = type;
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009003 if (type == VAR_STRING || type == VAR_DICT
9004 || type == VAR_LIST || type == VAR_BLOB)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009005 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009007#ifdef FEAT_FLOAT
9008 else if (type == VAR_FLOAT)
9009 (void)string2float(tab + 1, &tv.vval.v_float);
9010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009012 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009013 if (type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009014 {
9015 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
9016
9017 if (etv == NULL)
9018 /* Failed to parse back the dict or list, use it as a
9019 * string. */
9020 tv.v_type = VAR_STRING;
9021 else
9022 {
9023 vim_free(tv.vval.v_string);
9024 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01009025 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009026 }
9027 }
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009028 else if (type == VAR_BLOB)
9029 {
9030 blob_T *blob = string2blob(tv.vval.v_string);
9031
9032 if (blob == NULL)
9033 // Failed to parse back the blob, use it as a string.
9034 tv.v_type = VAR_STRING;
9035 else
9036 {
9037 vim_free(tv.vval.v_string);
9038 tv.v_type = VAR_BLOB;
9039 tv.vval.v_blob = blob;
9040 }
9041 }
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009042
Bram Moolenaarb20e3342016-01-18 23:29:01 +01009043 /* when in a function use global variables */
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009044 save_funccal(&funccal_entry);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009045 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009046 restore_funccal();
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009047
9048 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009049 vim_free(tv.vval.v_string);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009050 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST ||
9051 tv.v_type == VAR_BLOB)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009052 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053 }
9054 }
9055 }
9056
9057 return viminfo_readline(virp);
9058}
9059
9060/*
9061 * Write global vars that start with a capital to the viminfo file
9062 */
9063 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009064write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065{
Bram Moolenaar33570922005-01-25 22:26:29 +00009066 hashitem_T *hi;
9067 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009068 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01009069 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009070 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009071 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009072 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073
9074 if (find_viminfo_parameter('!') == NULL)
9075 return;
9076
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02009077 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00009078
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009079 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009080 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009082 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009084 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009085 this_var = HI2DI(hi);
9086 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009087 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009088 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00009089 {
9090 case VAR_STRING: s = "STR"; break;
9091 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009092 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009093 case VAR_DICT: s = "DIC"; break;
9094 case VAR_LIST: s = "LIS"; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009095 case VAR_BLOB: s = "BLO"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009096 case VAR_SPECIAL: s = "XPL"; break;
9097
9098 case VAR_UNKNOWN:
9099 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009100 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01009101 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01009102 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01009103 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00009104 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009105 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009106 if (this_var->di_tv.v_type == VAR_SPECIAL)
9107 {
9108 sprintf((char *)numbuf, "%ld",
9109 (long)this_var->di_tv.vval.v_number);
9110 p = numbuf;
9111 tofree = NULL;
9112 }
9113 else
9114 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009115 if (p != NULL)
9116 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00009117 vim_free(tofree);
9118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 }
9120 }
9121}
9122#endif
9123
9124#if defined(FEAT_SESSION) || defined(PROTO)
9125 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009126store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127{
Bram Moolenaar33570922005-01-25 22:26:29 +00009128 hashitem_T *hi;
9129 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009130 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 char_u *p, *t;
9132
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009133 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009134 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009136 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009138 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009139 this_var = HI2DI(hi);
9140 if ((this_var->di_tv.v_type == VAR_NUMBER
9141 || this_var->di_tv.v_type == VAR_STRING)
9142 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009143 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009144 /* Escape special characters with a backslash. Turn a LF and
9145 * CR into \n and \r. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009146 p = vim_strsave_escaped(tv_get_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00009147 (char_u *)"\\\"\n\r");
9148 if (p == NULL) /* out of memory */
9149 break;
9150 for (t = p; *t != NUL; ++t)
9151 if (*t == '\n')
9152 *t = 'n';
9153 else if (*t == '\r')
9154 *t = 'r';
9155 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00009156 this_var->di_key,
9157 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9158 : ' ',
9159 p,
9160 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9161 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00009162 || put_eol(fd) == FAIL)
9163 {
9164 vim_free(p);
9165 return FAIL;
9166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 vim_free(p);
9168 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009169#ifdef FEAT_FLOAT
9170 else if (this_var->di_tv.v_type == VAR_FLOAT
9171 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
9172 {
9173 float_T f = this_var->di_tv.vval.v_float;
9174 int sign = ' ';
9175
9176 if (f < 0)
9177 {
9178 f = -f;
9179 sign = '-';
9180 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01009181 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009182 this_var->di_key, sign, f) < 0)
9183 || put_eol(fd) == FAIL)
9184 return FAIL;
9185 }
9186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187 }
9188 }
9189 return OK;
9190}
9191#endif
9192
Bram Moolenaar661b1822005-07-28 22:36:45 +00009193/*
9194 * Display script name where an item was last set.
9195 * Should only be invoked when 'verbose' is non-zero.
9196 */
9197 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009198last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009199{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009200 char_u *p;
9201
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009202 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009203 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009204 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009205 if (p != NULL)
9206 {
9207 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01009208 msg_puts(_("\n\tLast set from "));
9209 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009210 if (script_ctx.sc_lnum > 0)
9211 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01009212 msg_puts(_(" line "));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009213 msg_outnum((long)script_ctx.sc_lnum);
9214 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009215 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009216 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009217 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00009218 }
9219}
9220
Bram Moolenaar53744302015-07-17 17:38:22 +02009221/* reset v:option_new, v:option_old and v:option_type */
9222 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009223reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02009224{
9225 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
9226 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
9227 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
9228}
9229
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009230/*
9231 * Prepare "gap" for an assert error and add the sourcing position.
9232 */
9233 void
9234prepare_assert_error(garray_T *gap)
9235{
9236 char buf[NUMBUFLEN];
9237
9238 ga_init2(gap, 1, 100);
9239 if (sourcing_name != NULL)
9240 {
9241 ga_concat(gap, sourcing_name);
9242 if (sourcing_lnum > 0)
9243 ga_concat(gap, (char_u *)" ");
9244 }
9245 if (sourcing_lnum > 0)
9246 {
9247 sprintf(buf, "line %ld", (long)sourcing_lnum);
9248 ga_concat(gap, (char_u *)buf);
9249 }
9250 if (sourcing_name != NULL || sourcing_lnum > 0)
9251 ga_concat(gap, (char_u *)": ");
9252}
9253
9254/*
9255 * Add an assert error to v:errors.
9256 */
9257 void
9258assert_error(garray_T *gap)
9259{
9260 struct vimvar *vp = &vimvars[VV_ERRORS];
9261
9262 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9263 /* Make sure v:errors is a list. */
9264 set_vim_var_list(VV_ERRORS, list_alloc());
9265 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9266}
9267
Bram Moolenaar65a54642018-04-28 16:56:53 +02009268 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009269assert_equal_common(typval_T *argvars, assert_type_T atype)
9270{
9271 garray_T ga;
9272
9273 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9274 != (atype == ASSERT_EQUAL))
9275 {
9276 prepare_assert_error(&ga);
9277 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9278 atype);
9279 assert_error(&ga);
9280 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009281 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009282 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009283 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009284}
9285
Bram Moolenaar65a54642018-04-28 16:56:53 +02009286 int
Bram Moolenaard96ff162018-02-18 22:13:29 +01009287assert_equalfile(typval_T *argvars)
9288{
9289 char_u buf1[NUMBUFLEN];
9290 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009291 char_u *fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
9292 char_u *fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009293 garray_T ga;
9294 FILE *fd1;
9295 FILE *fd2;
9296
9297 if (fname1 == NULL || fname2 == NULL)
Bram Moolenaar65a54642018-04-28 16:56:53 +02009298 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009299
9300 IObuff[0] = NUL;
9301 fd1 = mch_fopen((char *)fname1, READBIN);
9302 if (fd1 == NULL)
9303 {
9304 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname1);
9305 }
9306 else
9307 {
9308 fd2 = mch_fopen((char *)fname2, READBIN);
9309 if (fd2 == NULL)
9310 {
9311 fclose(fd1);
9312 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname2);
9313 }
9314 else
9315 {
9316 int c1, c2;
9317 long count = 0;
9318
9319 for (;;)
9320 {
9321 c1 = fgetc(fd1);
9322 c2 = fgetc(fd2);
9323 if (c1 == EOF)
9324 {
9325 if (c2 != EOF)
9326 STRCPY(IObuff, "first file is shorter");
9327 break;
9328 }
9329 else if (c2 == EOF)
9330 {
9331 STRCPY(IObuff, "second file is shorter");
9332 break;
9333 }
9334 else if (c1 != c2)
9335 {
9336 vim_snprintf((char *)IObuff, IOSIZE,
9337 "difference at byte %ld", count);
9338 break;
9339 }
9340 ++count;
9341 }
Bram Moolenaar30494182018-02-20 21:46:05 +01009342 fclose(fd1);
9343 fclose(fd2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009344 }
9345 }
9346 if (IObuff[0] != NUL)
9347 {
9348 prepare_assert_error(&ga);
9349 ga_concat(&ga, IObuff);
9350 assert_error(&ga);
9351 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009352 return 1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009353 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009354 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009355}
9356
Bram Moolenaar65a54642018-04-28 16:56:53 +02009357 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009358assert_match_common(typval_T *argvars, assert_type_T atype)
9359{
9360 garray_T ga;
9361 char_u buf1[NUMBUFLEN];
9362 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009363 char_u *pat = tv_get_string_buf_chk(&argvars[0], buf1);
9364 char_u *text = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009365
9366 if (pat == NULL || text == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009367 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009368 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
9369 {
9370 prepare_assert_error(&ga);
9371 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9372 atype);
9373 assert_error(&ga);
9374 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009375 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009376 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009377 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009378}
9379
Bram Moolenaar65a54642018-04-28 16:56:53 +02009380 int
Bram Moolenaar61c04492016-07-23 15:35:35 +02009381assert_inrange(typval_T *argvars)
9382{
9383 garray_T ga;
9384 int error = FALSE;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009385 char_u *tofree;
9386 char msg[200];
9387 char_u numbuf[NUMBUFLEN];
9388
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009389#ifdef FEAT_FLOAT
9390 if (argvars[0].v_type == VAR_FLOAT
9391 || argvars[1].v_type == VAR_FLOAT
9392 || argvars[2].v_type == VAR_FLOAT)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009393 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009394 float_T flower = tv_get_float(&argvars[0]);
9395 float_T fupper = tv_get_float(&argvars[1]);
9396 float_T factual = tv_get_float(&argvars[2]);
9397
9398 if (factual < flower || factual > fupper)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009399 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009400 prepare_assert_error(&ga);
9401 if (argvars[3].v_type != VAR_UNKNOWN)
9402 {
9403 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9404 vim_free(tofree);
9405 }
9406 else
9407 {
9408 vim_snprintf(msg, 200, "Expected range %g - %g, but got %g",
9409 flower, fupper, factual);
9410 ga_concat(&ga, (char_u *)msg);
9411 }
9412 assert_error(&ga);
9413 ga_clear(&ga);
9414 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009415 }
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009416 }
9417 else
9418#endif
9419 {
9420 varnumber_T lower = tv_get_number_chk(&argvars[0], &error);
9421 varnumber_T upper = tv_get_number_chk(&argvars[1], &error);
9422 varnumber_T actual = tv_get_number_chk(&argvars[2], &error);
9423
9424 if (error)
9425 return 0;
9426 if (actual < lower || actual > upper)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009427 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009428 prepare_assert_error(&ga);
9429 if (argvars[3].v_type != VAR_UNKNOWN)
9430 {
9431 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9432 vim_free(tofree);
9433 }
9434 else
9435 {
9436 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
Bram Moolenaar61c04492016-07-23 15:35:35 +02009437 (long)lower, (long)upper, (long)actual);
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009438 ga_concat(&ga, (char_u *)msg);
9439 }
9440 assert_error(&ga);
9441 ga_clear(&ga);
9442 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009443 }
Bram Moolenaar61c04492016-07-23 15:35:35 +02009444 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009445 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009446}
9447
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009448/*
9449 * Common for assert_true() and assert_false().
Bram Moolenaar65a54642018-04-28 16:56:53 +02009450 * Return non-zero for failure.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009451 */
Bram Moolenaar65a54642018-04-28 16:56:53 +02009452 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009453assert_bool(typval_T *argvars, int isTrue)
9454{
9455 int error = FALSE;
9456 garray_T ga;
9457
9458 if (argvars[0].v_type == VAR_SPECIAL
9459 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar65a54642018-04-28 16:56:53 +02009460 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009461 if (argvars[0].v_type != VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009462 || (tv_get_number_chk(&argvars[0], &error) == 0) == isTrue
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009463 || error)
9464 {
9465 prepare_assert_error(&ga);
9466 fill_assert_error(&ga, &argvars[1],
9467 (char_u *)(isTrue ? "True" : "False"),
9468 NULL, &argvars[0], ASSERT_OTHER);
9469 assert_error(&ga);
9470 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009471 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009472 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009473 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009474}
9475
Bram Moolenaar65a54642018-04-28 16:56:53 +02009476 int
Bram Moolenaar42205552017-03-18 19:42:22 +01009477assert_report(typval_T *argvars)
9478{
9479 garray_T ga;
9480
9481 prepare_assert_error(&ga);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009482 ga_concat(&ga, tv_get_string(&argvars[0]));
Bram Moolenaar42205552017-03-18 19:42:22 +01009483 assert_error(&ga);
9484 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009485 return 1;
Bram Moolenaar42205552017-03-18 19:42:22 +01009486}
9487
Bram Moolenaar65a54642018-04-28 16:56:53 +02009488 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009489assert_exception(typval_T *argvars)
9490{
9491 garray_T ga;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009492 char_u *error = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009493
9494 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9495 {
9496 prepare_assert_error(&ga);
9497 ga_concat(&ga, (char_u *)"v:exception is not set");
9498 assert_error(&ga);
9499 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009500 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009501 }
9502 else if (error != NULL
9503 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9504 {
9505 prepare_assert_error(&ga);
9506 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9507 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9508 assert_error(&ga);
9509 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009510 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009511 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009512 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009513}
9514
Bram Moolenaar65a54642018-04-28 16:56:53 +02009515 int
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009516assert_beeps(typval_T *argvars)
9517{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009518 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009519 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009520 int ret = 0;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009521
9522 called_vim_beep = FALSE;
9523 suppress_errthrow = TRUE;
9524 emsg_silent = FALSE;
9525 do_cmdline_cmd(cmd);
9526 if (!called_vim_beep)
9527 {
9528 prepare_assert_error(&ga);
9529 ga_concat(&ga, (char_u *)"command did not beep: ");
9530 ga_concat(&ga, cmd);
9531 assert_error(&ga);
9532 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009533 ret = 1;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009534 }
9535
9536 suppress_errthrow = FALSE;
9537 emsg_on_display = FALSE;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009538 return ret;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009539}
9540
Bram Moolenaar65a54642018-04-28 16:56:53 +02009541 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009542assert_fails(typval_T *argvars)
9543{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009544 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009545 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009546 int ret = 0;
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02009547 char_u numbuf[NUMBUFLEN];
9548 char_u *tofree;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009549
9550 called_emsg = FALSE;
9551 suppress_errthrow = TRUE;
9552 emsg_silent = TRUE;
9553 do_cmdline_cmd(cmd);
9554 if (!called_emsg)
9555 {
9556 prepare_assert_error(&ga);
9557 ga_concat(&ga, (char_u *)"command did not fail: ");
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02009558 if (argvars[1].v_type != VAR_UNKNOWN
9559 && argvars[2].v_type != VAR_UNKNOWN)
9560 {
9561 ga_concat(&ga, echo_string(&argvars[2], &tofree, numbuf, 0));
9562 vim_free(tofree);
9563 }
9564 else
9565 ga_concat(&ga, cmd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009566 assert_error(&ga);
9567 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009568 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009569 }
9570 else if (argvars[1].v_type != VAR_UNKNOWN)
9571 {
9572 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009573 char *error = (char *)tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009574
9575 if (error == NULL
9576 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9577 {
9578 prepare_assert_error(&ga);
9579 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9580 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
9581 assert_error(&ga);
9582 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009583 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009584 }
9585 }
9586
9587 called_emsg = FALSE;
9588 suppress_errthrow = FALSE;
9589 emsg_silent = FALSE;
9590 emsg_on_display = FALSE;
9591 set_vim_var_string(VV_ERRMSG, NULL, 0);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009592 return ret;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009593}
9594
9595/*
Bram Moolenaar86576712019-01-25 20:48:33 +01009596 * Append "p[clen]" to "gap", escaping unprintable characters.
9597 * Changes NL to \n, CR to \r, etc.
9598 */
9599 static void
9600ga_concat_esc(garray_T *gap, char_u *p, int clen)
9601{
9602 char_u buf[NUMBUFLEN];
9603
9604 if (clen > 1)
9605 {
9606 mch_memmove(buf, p, clen);
9607 buf[clen] = NUL;
9608 ga_concat(gap, buf);
9609 }
9610 else switch (*p)
9611 {
9612 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9613 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9614 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9615 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9616 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9617 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9618 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9619 default:
9620 if (*p < ' ')
9621 {
9622 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9623 ga_concat(gap, buf);
9624 }
9625 else
9626 ga_append(gap, *p);
9627 break;
9628 }
9629}
9630
9631/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009632 * Append "str" to "gap", escaping unprintable characters.
9633 * Changes NL to \n, CR to \r, etc.
9634 */
9635 static void
Bram Moolenaar86576712019-01-25 20:48:33 +01009636ga_concat_shorten_esc(garray_T *gap, char_u *str)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009637{
9638 char_u *p;
Bram Moolenaar86576712019-01-25 20:48:33 +01009639 char_u *s;
9640 int c;
9641 int clen;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009642 char_u buf[NUMBUFLEN];
Bram Moolenaar86576712019-01-25 20:48:33 +01009643 int same_len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009644
9645 if (str == NULL)
9646 {
9647 ga_concat(gap, (char_u *)"NULL");
9648 return;
9649 }
9650
9651 for (p = str; *p != NUL; ++p)
Bram Moolenaar86576712019-01-25 20:48:33 +01009652 {
9653 same_len = 1;
9654 s = p;
9655 c = mb_ptr2char_adv(&s);
9656 clen = s - p;
9657 while (*s != NUL && c == mb_ptr2char(s))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009658 {
Bram Moolenaar86576712019-01-25 20:48:33 +01009659 ++same_len;
9660 s += clen;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009661 }
Bram Moolenaar86576712019-01-25 20:48:33 +01009662 if (same_len > 20)
9663 {
9664 ga_concat(gap, (char_u *)"\\[");
9665 ga_concat_esc(gap, p, clen);
9666 ga_concat(gap, (char_u *)" occurs ");
9667 vim_snprintf((char *)buf, NUMBUFLEN, "%d", same_len);
9668 ga_concat(gap, buf);
9669 ga_concat(gap, (char_u *)" times]");
9670 p = s - 1;
9671 }
9672 else
9673 ga_concat_esc(gap, p, clen);
9674 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009675}
9676
9677/*
9678 * Fill "gap" with information about an assert error.
9679 */
9680 void
9681fill_assert_error(
9682 garray_T *gap,
9683 typval_T *opt_msg_tv,
9684 char_u *exp_str,
9685 typval_T *exp_tv,
9686 typval_T *got_tv,
9687 assert_type_T atype)
9688{
9689 char_u numbuf[NUMBUFLEN];
9690 char_u *tofree;
9691
9692 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9693 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009694 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9695 vim_free(tofree);
9696 ga_concat(gap, (char_u *)": ");
9697 }
9698
9699 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9700 ga_concat(gap, (char_u *)"Pattern ");
9701 else if (atype == ASSERT_NOTEQUAL)
9702 ga_concat(gap, (char_u *)"Expected not equal to ");
9703 else
9704 ga_concat(gap, (char_u *)"Expected ");
9705 if (exp_str == NULL)
9706 {
Bram Moolenaar86576712019-01-25 20:48:33 +01009707 ga_concat_shorten_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009708 vim_free(tofree);
9709 }
9710 else
Bram Moolenaar86576712019-01-25 20:48:33 +01009711 ga_concat_shorten_esc(gap, exp_str);
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009712 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009713 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009714 if (atype == ASSERT_MATCH)
9715 ga_concat(gap, (char_u *)" does not match ");
9716 else if (atype == ASSERT_NOTMATCH)
9717 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009718 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009719 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar86576712019-01-25 20:48:33 +01009720 ga_concat_shorten_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009721 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009722 }
9723}
9724
Bram Moolenaar31988702018-02-13 12:57:42 +01009725/*
9726 * Compare "typ1" and "typ2". Put the result in "typ1".
9727 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009728 int
9729typval_compare(
9730 typval_T *typ1, /* first operand */
9731 typval_T *typ2, /* second operand */
9732 exptype_T type, /* operator */
9733 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +01009734 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009735{
9736 int i;
9737 varnumber_T n1, n2;
9738 char_u *s1, *s2;
9739 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
9740
Bram Moolenaar31988702018-02-13 12:57:42 +01009741 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009742 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009743 /* For "is" a different type always means FALSE, for "notis"
9744 * it means TRUE. */
9745 n1 = (type == TYPE_NEQUAL);
9746 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009747 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
9748 {
9749 if (type_is)
9750 {
9751 n1 = (typ1->v_type == typ2->v_type
9752 && typ1->vval.v_blob == typ2->vval.v_blob);
9753 if (type == TYPE_NEQUAL)
9754 n1 = !n1;
9755 }
9756 else if (typ1->v_type != typ2->v_type
9757 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9758 {
9759 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009760 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009761 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009762 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009763 clear_tv(typ1);
9764 return FAIL;
9765 }
9766 else
9767 {
9768 // Compare two Blobs for being equal or unequal.
9769 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
9770 if (type == TYPE_NEQUAL)
9771 n1 = !n1;
9772 }
9773 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009774 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
9775 {
9776 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009777 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009778 n1 = (typ1->v_type == typ2->v_type
9779 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009780 if (type == TYPE_NEQUAL)
9781 n1 = !n1;
9782 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009783 else if (typ1->v_type != typ2->v_type
9784 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009785 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009786 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009787 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009788 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009789 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009790 clear_tv(typ1);
9791 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009792 }
9793 else
9794 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009795 /* Compare two Lists for being equal or unequal. */
9796 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
9797 ic, FALSE);
9798 if (type == TYPE_NEQUAL)
9799 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009800 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009801 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009802
9803 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
9804 {
9805 if (type_is)
9806 {
9807 n1 = (typ1->v_type == typ2->v_type
9808 && typ1->vval.v_dict == typ2->vval.v_dict);
9809 if (type == TYPE_NEQUAL)
9810 n1 = !n1;
9811 }
9812 else if (typ1->v_type != typ2->v_type
9813 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9814 {
9815 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009816 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009817 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009818 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009819 clear_tv(typ1);
9820 return FAIL;
9821 }
9822 else
9823 {
9824 /* Compare two Dictionaries for being equal or unequal. */
9825 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
9826 ic, FALSE);
9827 if (type == TYPE_NEQUAL)
9828 n1 = !n1;
9829 }
9830 }
9831
9832 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
9833 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
9834 {
9835 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
9836 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009837 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009838 clear_tv(typ1);
9839 return FAIL;
9840 }
9841 if ((typ1->v_type == VAR_PARTIAL
9842 && typ1->vval.v_partial == NULL)
9843 || (typ2->v_type == VAR_PARTIAL
9844 && typ2->vval.v_partial == NULL))
9845 /* when a partial is NULL assume not equal */
9846 n1 = FALSE;
9847 else if (type_is)
9848 {
9849 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
9850 /* strings are considered the same if their value is
9851 * the same */
9852 n1 = tv_equal(typ1, typ2, ic, FALSE);
9853 else if (typ1->v_type == VAR_PARTIAL
9854 && typ2->v_type == VAR_PARTIAL)
9855 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
9856 else
9857 n1 = FALSE;
9858 }
9859 else
9860 n1 = tv_equal(typ1, typ2, ic, FALSE);
9861 if (type == TYPE_NEQUAL)
9862 n1 = !n1;
9863 }
9864
9865#ifdef FEAT_FLOAT
9866 /*
9867 * If one of the two variables is a float, compare as a float.
9868 * When using "=~" or "!~", always compare as string.
9869 */
9870 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
9871 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9872 {
9873 float_T f1, f2;
9874
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009875 f1 = tv_get_float(typ1);
9876 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009877 n1 = FALSE;
9878 switch (type)
9879 {
9880 case TYPE_EQUAL: n1 = (f1 == f2); break;
9881 case TYPE_NEQUAL: n1 = (f1 != f2); break;
9882 case TYPE_GREATER: n1 = (f1 > f2); break;
9883 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
9884 case TYPE_SMALLER: n1 = (f1 < f2); break;
9885 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
9886 case TYPE_UNKNOWN:
9887 case TYPE_MATCH:
9888 case TYPE_NOMATCH: break; /* avoid gcc warning */
9889 }
9890 }
9891#endif
9892
9893 /*
9894 * If one of the two variables is a number, compare as a number.
9895 * When using "=~" or "!~", always compare as string.
9896 */
9897 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
9898 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9899 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009900 n1 = tv_get_number(typ1);
9901 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009902 switch (type)
9903 {
9904 case TYPE_EQUAL: n1 = (n1 == n2); break;
9905 case TYPE_NEQUAL: n1 = (n1 != n2); break;
9906 case TYPE_GREATER: n1 = (n1 > n2); break;
9907 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
9908 case TYPE_SMALLER: n1 = (n1 < n2); break;
9909 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
9910 case TYPE_UNKNOWN:
9911 case TYPE_MATCH:
9912 case TYPE_NOMATCH: break; /* avoid gcc warning */
9913 }
9914 }
9915 else
9916 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009917 s1 = tv_get_string_buf(typ1, buf1);
9918 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009919 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
9920 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
9921 else
9922 i = 0;
9923 n1 = FALSE;
9924 switch (type)
9925 {
9926 case TYPE_EQUAL: n1 = (i == 0); break;
9927 case TYPE_NEQUAL: n1 = (i != 0); break;
9928 case TYPE_GREATER: n1 = (i > 0); break;
9929 case TYPE_GEQUAL: n1 = (i >= 0); break;
9930 case TYPE_SMALLER: n1 = (i < 0); break;
9931 case TYPE_SEQUAL: n1 = (i <= 0); break;
9932
9933 case TYPE_MATCH:
9934 case TYPE_NOMATCH:
9935 n1 = pattern_match(s2, s1, ic);
9936 if (type == TYPE_NOMATCH)
9937 n1 = !n1;
9938 break;
9939
9940 case TYPE_UNKNOWN: break; /* avoid gcc warning */
9941 }
9942 }
9943 clear_tv(typ1);
9944 typ1->v_type = VAR_NUMBER;
9945 typ1->vval.v_number = n1;
9946
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009947 return OK;
9948}
9949
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009950 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02009951typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009952{
9953 char_u *tofree;
9954 char_u numbuf[NUMBUFLEN];
9955 char_u *ret = NULL;
9956
9957 if (arg == NULL)
9958 return vim_strsave((char_u *)"(does not exist)");
9959 ret = tv2string(arg, &tofree, numbuf, 0);
9960 /* Make a copy if we have a value but it's not in allocated memory. */
9961 if (ret != NULL && tofree == NULL)
9962 ret = vim_strsave(ret);
9963 return ret;
9964}
9965
9966 int
9967var_exists(char_u *var)
9968{
9969 char_u *name;
9970 char_u *tofree;
9971 typval_T tv;
9972 int len = 0;
9973 int n = FALSE;
9974
9975 /* get_name_len() takes care of expanding curly braces */
9976 name = var;
9977 len = get_name_len(&var, &tofree, TRUE, FALSE);
9978 if (len > 0)
9979 {
9980 if (tofree != NULL)
9981 name = tofree;
9982 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
9983 if (n)
9984 {
9985 /* handle d.key, l[idx], f(expr) */
9986 n = (handle_subscript(&var, &tv, TRUE, FALSE) == OK);
9987 if (n)
9988 clear_tv(&tv);
9989 }
9990 }
9991 if (*var != NUL)
9992 n = FALSE;
9993
9994 vim_free(tofree);
9995 return n;
9996}
9997
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998#endif /* FEAT_EVAL */
9999
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010001#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002
Bram Moolenaar4f974752019-02-17 17:44:42 +010010003#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004/*
10005 * Functions for ":8" filename modifier: get 8.3 version of a filename.
10006 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007
10008/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010009 * Get the short path (8.3) for the filename in "fnamep".
10010 * Only works for a valid file name.
10011 * When the path gets longer "fnamep" is changed and the allocated buffer
10012 * is put in "bufp".
10013 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
10014 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015 */
10016 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010017get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010019 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 char_u *newbuf;
10021
10022 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010023 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 if (l > len - 1)
10025 {
10026 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010027 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028 newbuf = vim_strnsave(*fnamep, l);
10029 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010030 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031
10032 vim_free(*bufp);
10033 *fnamep = *bufp = newbuf;
10034
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010035 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010036 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037 }
10038
10039 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010040 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041}
10042
10043/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010044 * Get the short path (8.3) for the filename in "fname". The converted
10045 * path is returned in "bufp".
10046 *
10047 * Some of the directories specified in "fname" may not exist. This function
10048 * will shorten the existing directories at the beginning of the path and then
10049 * append the remaining non-existing path.
10050 *
10051 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020010052 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010053 * bufp - Pointer to an allocated buffer for the filename.
10054 * fnamelen - Length of the filename pointed to by fname
10055 *
10056 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 */
10058 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010059shortpath_for_invalid_fname(
10060 char_u **fname,
10061 char_u **bufp,
10062 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010064 char_u *short_fname, *save_fname, *pbuf_unused;
10065 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010067 int old_len, len;
10068 int new_len, sfx_len;
10069 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070
10071 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010072 old_len = *fnamelen;
10073 save_fname = vim_strnsave(*fname, old_len);
10074 pbuf_unused = NULL;
10075 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010077 endp = save_fname + old_len - 1; /* Find the end of the copy */
10078 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010080 /*
10081 * Try shortening the supplied path till it succeeds by removing one
10082 * directory at a time from the tail of the path.
10083 */
10084 len = 0;
10085 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010087 /* go back one path-separator */
10088 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
10089 --endp;
10090 if (endp <= save_fname)
10091 break; /* processed the complete path */
10092
10093 /*
10094 * Replace the path separator with a NUL and try to shorten the
10095 * resulting path.
10096 */
10097 ch = *endp;
10098 *endp = 0;
10099 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010100 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010101 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
10102 {
10103 retval = FAIL;
10104 goto theend;
10105 }
10106 *endp = ch; /* preserve the string */
10107
10108 if (len > 0)
10109 break; /* successfully shortened the path */
10110
10111 /* failed to shorten the path. Skip the path separator */
10112 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113 }
10114
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010115 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010117 /*
10118 * Succeeded in shortening the path. Now concatenate the shortened
10119 * path with the remaining path at the tail.
10120 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010122 /* Compute the length of the new path. */
10123 sfx_len = (int)(save_endp - endp) + 1;
10124 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010126 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010128 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010130 /* There is not enough space in the currently allocated string,
10131 * copy it to a buffer big enough. */
10132 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010134 {
10135 retval = FAIL;
10136 goto theend;
10137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138 }
10139 else
10140 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010141 /* Transfer short_fname to the main buffer (it's big enough),
10142 * unless get_short_pathname() did its work in-place. */
10143 *fname = *bufp = save_fname;
10144 if (short_fname != save_fname)
10145 vim_strncpy(save_fname, short_fname, len);
10146 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010148
10149 /* concat the not-shortened part of the path */
10150 vim_strncpy(*fname + len, endp, sfx_len);
10151 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010153
10154theend:
10155 vim_free(pbuf_unused);
10156 vim_free(save_fname);
10157
10158 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159}
10160
10161/*
10162 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010163 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164 */
10165 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010166shortpath_for_partial(
10167 char_u **fnamep,
10168 char_u **bufp,
10169 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170{
10171 int sepcount, len, tflen;
10172 char_u *p;
10173 char_u *pbuf, *tfname;
10174 int hasTilde;
10175
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010176 /* Count up the path separators from the RHS.. so we know which part
10177 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010179 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180 if (vim_ispathsep(*p))
10181 ++sepcount;
10182
10183 /* Need full path first (use expand_env() to remove a "~/") */
10184 hasTilde = (**fnamep == '~');
10185 if (hasTilde)
10186 pbuf = tfname = expand_env_save(*fnamep);
10187 else
10188 pbuf = tfname = FullName_save(*fnamep, FALSE);
10189
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010190 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010192 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
10193 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194
10195 if (len == 0)
10196 {
10197 /* Don't have a valid filename, so shorten the rest of the
10198 * path if we can. This CAN give us invalid 8.3 filenames, but
10199 * there's not a lot of point in guessing what it might be.
10200 */
10201 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010202 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
10203 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204 }
10205
10206 /* Count the paths backward to find the beginning of the desired string. */
10207 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010208 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010209 if (has_mbyte)
10210 p -= mb_head_off(tfname, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211 if (vim_ispathsep(*p))
10212 {
10213 if (sepcount == 0 || (hasTilde && sepcount == 1))
10214 break;
10215 else
10216 sepcount --;
10217 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219 if (hasTilde)
10220 {
10221 --p;
10222 if (p >= tfname)
10223 *p = '~';
10224 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010225 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226 }
10227 else
10228 ++p;
10229
10230 /* Copy in the string - p indexes into tfname - allocated at pbuf */
10231 vim_free(*bufp);
10232 *fnamelen = (int)STRLEN(p);
10233 *bufp = pbuf;
10234 *fnamep = p;
10235
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010236 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237}
Bram Moolenaar4f974752019-02-17 17:44:42 +010010238#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239
10240/*
10241 * Adjust a filename, according to a string of modifiers.
10242 * *fnamep must be NUL terminated when called. When returning, the length is
10243 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010244 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245 * When there is an error, *fnamep is set to NULL.
10246 */
10247 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010248modify_fname(
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010249 char_u *src, // string with modifiers
10250 int tilde_file, // "~" is a file name, not $HOME
10251 int *usedlen, // characters after src that are used
10252 char_u **fnamep, // file name so far
10253 char_u **bufp, // buffer for allocated file name or NULL
10254 int *fnamelen) // length of fnamep
Bram Moolenaar071d4272004-06-13 20:20:40 +000010255{
10256 int valid = 0;
10257 char_u *tail;
10258 char_u *s, *p, *pbuf;
10259 char_u dirname[MAXPATHL];
10260 int c;
10261 int has_fullname = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010262#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010263 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264 int has_shortname = 0;
10265#endif
10266
10267repeat:
10268 /* ":p" - full path/file_name */
10269 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
10270 {
10271 has_fullname = 1;
10272
10273 valid |= VALID_PATH;
10274 *usedlen += 2;
10275
10276 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
10277 if ((*fnamep)[0] == '~'
10278#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
10279 && ((*fnamep)[1] == '/'
10280# ifdef BACKSLASH_IN_FILENAME
10281 || (*fnamep)[1] == '\\'
10282# endif
10283 || (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010285 && !(tilde_file && (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010286 )
10287 {
10288 *fnamep = expand_env_save(*fnamep);
10289 vim_free(*bufp); /* free any allocated file name */
10290 *bufp = *fnamep;
10291 if (*fnamep == NULL)
10292 return -1;
10293 }
10294
10295 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010296 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297 {
10298 if (vim_ispathsep(*p)
10299 && p[1] == '.'
10300 && (p[2] == NUL
10301 || vim_ispathsep(p[2])
10302 || (p[2] == '.'
10303 && (p[3] == NUL || vim_ispathsep(p[3])))))
10304 break;
10305 }
10306
10307 /* FullName_save() is slow, don't use it when not needed. */
10308 if (*p != NUL || !vim_isAbsName(*fnamep))
10309 {
10310 *fnamep = FullName_save(*fnamep, *p != NUL);
10311 vim_free(*bufp); /* free any allocated file name */
10312 *bufp = *fnamep;
10313 if (*fnamep == NULL)
10314 return -1;
10315 }
10316
Bram Moolenaar4f974752019-02-17 17:44:42 +010010317#ifdef MSWIN
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010318# if _WIN32_WINNT >= 0x0500
10319 if (vim_strchr(*fnamep, '~') != NULL)
10320 {
10321 /* Expand 8.3 filename to full path. Needed to make sure the same
10322 * file does not have two different names.
10323 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
10324 p = alloc(_MAX_PATH + 1);
10325 if (p != NULL)
10326 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010327 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010328 {
10329 vim_free(*bufp);
10330 *bufp = *fnamep = p;
10331 }
10332 else
10333 vim_free(p);
10334 }
10335 }
10336# endif
10337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338 /* Append a path separator to a directory. */
10339 if (mch_isdir(*fnamep))
10340 {
10341 /* Make room for one or two extra characters. */
10342 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
10343 vim_free(*bufp); /* free any allocated file name */
10344 *bufp = *fnamep;
10345 if (*fnamep == NULL)
10346 return -1;
10347 add_pathsep(*fnamep);
10348 }
10349 }
10350
10351 /* ":." - path relative to the current directory */
10352 /* ":~" - path relative to the home directory */
10353 /* ":8" - shortname path - postponed till after */
10354 while (src[*usedlen] == ':'
10355 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
10356 {
10357 *usedlen += 2;
10358 if (c == '8')
10359 {
Bram Moolenaar4f974752019-02-17 17:44:42 +010010360#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361 has_shortname = 1; /* Postpone this. */
10362#endif
10363 continue;
10364 }
10365 pbuf = NULL;
10366 /* Need full path first (use expand_env() to remove a "~/") */
10367 if (!has_fullname)
10368 {
10369 if (c == '.' && **fnamep == '~')
10370 p = pbuf = expand_env_save(*fnamep);
10371 else
10372 p = pbuf = FullName_save(*fnamep, FALSE);
10373 }
10374 else
10375 p = *fnamep;
10376
10377 has_fullname = 0;
10378
10379 if (p != NULL)
10380 {
10381 if (c == '.')
10382 {
10383 mch_dirname(dirname, MAXPATHL);
10384 s = shorten_fname(p, dirname);
10385 if (s != NULL)
10386 {
10387 *fnamep = s;
10388 if (pbuf != NULL)
10389 {
10390 vim_free(*bufp); /* free any allocated file name */
10391 *bufp = pbuf;
10392 pbuf = NULL;
10393 }
10394 }
10395 }
10396 else
10397 {
10398 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
10399 /* Only replace it when it starts with '~' */
10400 if (*dirname == '~')
10401 {
10402 s = vim_strsave(dirname);
10403 if (s != NULL)
10404 {
10405 *fnamep = s;
10406 vim_free(*bufp);
10407 *bufp = s;
10408 }
10409 }
10410 }
10411 vim_free(pbuf);
10412 }
10413 }
10414
10415 tail = gettail(*fnamep);
10416 *fnamelen = (int)STRLEN(*fnamep);
10417
10418 /* ":h" - head, remove "/file_name", can be repeated */
10419 /* Don't remove the first "/" or "c:\" */
10420 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
10421 {
10422 valid |= VALID_HEAD;
10423 *usedlen += 2;
10424 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010425 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010426 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427 *fnamelen = (int)(tail - *fnamep);
10428#ifdef VMS
10429 if (*fnamelen > 0)
10430 *fnamelen += 1; /* the path separator is part of the path */
10431#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010432 if (*fnamelen == 0)
10433 {
10434 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
10435 p = vim_strsave((char_u *)".");
10436 if (p == NULL)
10437 return -1;
10438 vim_free(*bufp);
10439 *bufp = *fnamep = tail = p;
10440 *fnamelen = 1;
10441 }
10442 else
10443 {
10444 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010445 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447 }
10448
10449 /* ":8" - shortname */
10450 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
10451 {
10452 *usedlen += 2;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010453#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454 has_shortname = 1;
10455#endif
10456 }
10457
Bram Moolenaar4f974752019-02-17 17:44:42 +010010458#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010459 /*
10460 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461 */
10462 if (has_shortname)
10463 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010464 /* Copy the string if it is shortened by :h and when it wasn't copied
10465 * yet, because we are going to change it in place. Avoids changing
10466 * the buffer name for "%:8". */
10467 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468 {
10469 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020010470 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471 return -1;
10472 vim_free(*bufp);
10473 *bufp = *fnamep = p;
10474 }
10475
10476 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020010477 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478 if (!has_fullname && !vim_isAbsName(*fnamep))
10479 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010480 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481 return -1;
10482 }
10483 else
10484 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010485 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486
Bram Moolenaardc935552011-08-17 15:23:23 +020010487 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010488 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010489 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490 return -1;
10491
10492 if (l == 0)
10493 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010494 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010496 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497 return -1;
10498 }
10499 *fnamelen = l;
10500 }
10501 }
Bram Moolenaar4f974752019-02-17 17:44:42 +010010502#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503
10504 /* ":t" - tail, just the basename */
10505 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
10506 {
10507 *usedlen += 2;
10508 *fnamelen -= (int)(tail - *fnamep);
10509 *fnamep = tail;
10510 }
10511
10512 /* ":e" - extension, can be repeated */
10513 /* ":r" - root, without extension, can be repeated */
10514 while (src[*usedlen] == ':'
10515 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
10516 {
10517 /* find a '.' in the tail:
10518 * - for second :e: before the current fname
10519 * - otherwise: The last '.'
10520 */
10521 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
10522 s = *fnamep - 2;
10523 else
10524 s = *fnamep + *fnamelen - 1;
10525 for ( ; s > tail; --s)
10526 if (s[0] == '.')
10527 break;
10528 if (src[*usedlen + 1] == 'e') /* :e */
10529 {
10530 if (s > tail)
10531 {
10532 *fnamelen += (int)(*fnamep - (s + 1));
10533 *fnamep = s + 1;
10534#ifdef VMS
10535 /* cut version from the extension */
10536 s = *fnamep + *fnamelen - 1;
10537 for ( ; s > *fnamep; --s)
10538 if (s[0] == ';')
10539 break;
10540 if (s > *fnamep)
10541 *fnamelen = s - *fnamep;
10542#endif
10543 }
10544 else if (*fnamep <= tail)
10545 *fnamelen = 0;
10546 }
10547 else /* :r */
10548 {
10549 if (s > tail) /* remove one extension */
10550 *fnamelen = (int)(s - *fnamep);
10551 }
10552 *usedlen += 2;
10553 }
10554
10555 /* ":s?pat?foo?" - substitute */
10556 /* ":gs?pat?foo?" - global substitute */
10557 if (src[*usedlen] == ':'
10558 && (src[*usedlen + 1] == 's'
10559 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
10560 {
10561 char_u *str;
10562 char_u *pat;
10563 char_u *sub;
10564 int sep;
10565 char_u *flags;
10566 int didit = FALSE;
10567
10568 flags = (char_u *)"";
10569 s = src + *usedlen + 2;
10570 if (src[*usedlen + 1] == 'g')
10571 {
10572 flags = (char_u *)"g";
10573 ++s;
10574 }
10575
10576 sep = *s++;
10577 if (sep)
10578 {
10579 /* find end of pattern */
10580 p = vim_strchr(s, sep);
10581 if (p != NULL)
10582 {
10583 pat = vim_strnsave(s, (int)(p - s));
10584 if (pat != NULL)
10585 {
10586 s = p + 1;
10587 /* find end of substitution */
10588 p = vim_strchr(s, sep);
10589 if (p != NULL)
10590 {
10591 sub = vim_strnsave(s, (int)(p - s));
10592 str = vim_strnsave(*fnamep, *fnamelen);
10593 if (sub != NULL && str != NULL)
10594 {
10595 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010596 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010597 if (s != NULL)
10598 {
10599 *fnamep = s;
10600 *fnamelen = (int)STRLEN(s);
10601 vim_free(*bufp);
10602 *bufp = s;
10603 didit = TRUE;
10604 }
10605 }
10606 vim_free(sub);
10607 vim_free(str);
10608 }
10609 vim_free(pat);
10610 }
10611 }
10612 /* after using ":s", repeat all the modifiers */
10613 if (didit)
10614 goto repeat;
10615 }
10616 }
10617
Bram Moolenaar26df0922014-02-23 23:39:13 +010010618 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10619 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010620 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010621 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010622 if (c != NUL)
10623 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010624 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010625 if (c != NUL)
10626 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010627 if (p == NULL)
10628 return -1;
10629 vim_free(*bufp);
10630 *bufp = *fnamep = p;
10631 *fnamelen = (int)STRLEN(p);
10632 *usedlen += 2;
10633 }
10634
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635 return valid;
10636}
10637
10638/*
10639 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010640 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641 * "flags" can be "g" to do a global substitute.
10642 * Returns an allocated string, NULL for error.
10643 */
10644 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010645do_string_sub(
10646 char_u *str,
10647 char_u *pat,
10648 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010649 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010650 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010651{
10652 int sublen;
10653 regmatch_T regmatch;
10654 int i;
10655 int do_all;
10656 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010657 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658 garray_T ga;
10659 char_u *ret;
10660 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010661 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662
10663 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
10664 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010665 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666
10667 ga_init2(&ga, 1, 200);
10668
10669 do_all = (flags[0] == 'g');
10670
10671 regmatch.rm_ic = p_ic;
10672 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10673 if (regmatch.regprog != NULL)
10674 {
10675 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010676 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
10678 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010010679 /* Skip empty match except for first match. */
10680 if (regmatch.startp[0] == regmatch.endp[0])
10681 {
10682 if (zero_width == regmatch.startp[0])
10683 {
10684 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020010685 i = MB_PTR2LEN(tail);
10686 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
10687 (size_t)i);
10688 ga.ga_len += i;
10689 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010690 continue;
10691 }
10692 zero_width = regmatch.startp[0];
10693 }
10694
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695 /*
10696 * Get some space for a temporary buffer to do the substitution
10697 * into. It will contain:
10698 * - The text up to where the match is.
10699 * - The substituted text.
10700 * - The text after the match.
10701 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010702 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010010703 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
10705 {
10706 ga_clear(&ga);
10707 break;
10708 }
10709
10710 /* copy the text up to where the match is */
10711 i = (int)(regmatch.startp[0] - tail);
10712 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10713 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010714 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715 + ga.ga_len + i, TRUE, TRUE, FALSE);
10716 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010717 tail = regmatch.endp[0];
10718 if (*tail == NUL)
10719 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720 if (!do_all)
10721 break;
10722 }
10723
10724 if (ga.ga_data != NULL)
10725 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10726
Bram Moolenaar473de612013-06-08 18:19:48 +020010727 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728 }
10729
10730 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10731 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010732 if (p_cpo == empty_option)
10733 p_cpo = save_cpo;
10734 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010735 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010736 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737
10738 return ret;
10739}
10740
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010741 static int
10742filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10743{
10744 typval_T rettv;
10745 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010746 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010747
10748 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10749 argv[0] = vimvars[VV_KEY].vv_tv;
10750 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010751 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10752 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010753 if (map)
10754 {
10755 /* map(): replace the list item value */
10756 clear_tv(tv);
10757 rettv.v_lock = 0;
10758 *tv = rettv;
10759 }
10760 else
10761 {
10762 int error = FALSE;
10763
10764 /* filter(): when expr is zero remove the item */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010765 *remp = (tv_get_number_chk(&rettv, &error) == 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010766 clear_tv(&rettv);
10767 /* On type error, nothing has been removed; return FAIL to stop the
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010768 * loop. The error message was given by tv_get_number_chk(). */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010769 if (error)
10770 goto theend;
10771 }
10772 retval = OK;
10773theend:
10774 clear_tv(&vimvars[VV_VAL].vv_tv);
10775 return retval;
10776}
10777
10778
10779/*
10780 * Implementation of map() and filter().
10781 */
10782 void
10783filter_map(typval_T *argvars, typval_T *rettv, int map)
10784{
10785 typval_T *expr;
10786 listitem_T *li, *nli;
10787 list_T *l = NULL;
10788 dictitem_T *di;
10789 hashtab_T *ht;
10790 hashitem_T *hi;
10791 dict_T *d = NULL;
10792 typval_T save_val;
10793 typval_T save_key;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010794 blob_T *b = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010795 int rem;
10796 int todo;
10797 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10798 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10799 : N_("filter() argument"));
10800 int save_did_emsg;
10801 int idx = 0;
10802
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010803 if (argvars[0].v_type == VAR_BLOB)
10804 {
10805 if ((b = argvars[0].vval.v_blob) == NULL)
10806 return;
10807 }
10808 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010809 {
10810 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010811 || (!map && var_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010812 return;
10813 }
10814 else if (argvars[0].v_type == VAR_DICT)
10815 {
10816 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010817 || (!map && var_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010818 return;
10819 }
10820 else
10821 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010822 semsg(_(e_listdictarg), ermsg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010823 return;
10824 }
10825
10826 expr = &argvars[1];
10827 /* On type errors, the preceding call has already displayed an error
10828 * message. Avoid a misleading error message for an empty string that
10829 * was not passed as argument. */
10830 if (expr->v_type != VAR_UNKNOWN)
10831 {
10832 prepare_vimvar(VV_VAL, &save_val);
10833
10834 /* We reset "did_emsg" to be able to detect whether an error
10835 * occurred during evaluation of the expression. */
10836 save_did_emsg = did_emsg;
10837 did_emsg = FALSE;
10838
10839 prepare_vimvar(VV_KEY, &save_key);
10840 if (argvars[0].v_type == VAR_DICT)
10841 {
10842 vimvars[VV_KEY].vv_type = VAR_STRING;
10843
10844 ht = &d->dv_hashtab;
10845 hash_lock(ht);
10846 todo = (int)ht->ht_used;
10847 for (hi = ht->ht_array; todo > 0; ++hi)
10848 {
10849 if (!HASHITEM_EMPTY(hi))
10850 {
10851 int r;
10852
10853 --todo;
10854 di = HI2DI(hi);
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010855 if (map && (var_check_lock(di->di_tv.v_lock,
10856 arg_errmsg, TRUE)
10857 || var_check_ro(di->di_flags,
10858 arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010859 break;
10860 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10861 r = filter_map_one(&di->di_tv, expr, map, &rem);
10862 clear_tv(&vimvars[VV_KEY].vv_tv);
10863 if (r == FAIL || did_emsg)
10864 break;
10865 if (!map && rem)
10866 {
10867 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10868 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10869 break;
10870 dictitem_remove(d, di);
10871 }
10872 }
10873 }
10874 hash_unlock(ht);
10875 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010876 else if (argvars[0].v_type == VAR_BLOB)
10877 {
10878 int i;
10879 typval_T tv;
10880
10881 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10882 for (i = 0; i < b->bv_ga.ga_len; i++)
10883 {
10884 tv.v_type = VAR_NUMBER;
10885 tv.vval.v_number = blob_get(b, i);
10886 vimvars[VV_KEY].vv_nr = idx;
10887 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg)
10888 break;
10889 if (tv.v_type != VAR_NUMBER)
10890 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010891 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010892 return;
10893 }
10894 tv.v_type = VAR_NUMBER;
10895 blob_set(b, i, tv.vval.v_number);
10896 if (!map && rem)
10897 {
10898 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
10899
10900 mch_memmove(p + idx, p + i + 1,
10901 (size_t)b->bv_ga.ga_len - i - 1);
10902 --b->bv_ga.ga_len;
10903 --i;
10904 }
10905 }
10906 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010907 else
10908 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +010010909 // argvars[0].v_type == VAR_LIST
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010910 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10911
10912 for (li = l->lv_first; li != NULL; li = nli)
10913 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010914 if (map && var_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010915 break;
10916 nli = li->li_next;
10917 vimvars[VV_KEY].vv_nr = idx;
10918 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10919 || did_emsg)
10920 break;
10921 if (!map && rem)
10922 listitem_remove(l, li);
10923 ++idx;
10924 }
10925 }
10926
10927 restore_vimvar(VV_KEY, &save_key);
10928 restore_vimvar(VV_VAL, &save_val);
10929
10930 did_emsg |= save_did_emsg;
10931 }
10932
10933 copy_tv(&argvars[0], rettv);
10934}
10935
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */