blob: 78e2c60adde18ab9473cee9752ca5f5a68f126e6 [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 Moolenaar0f248b02019-04-04 15:36:05 +02001237 * ":let var ..= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001238 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 */
1240 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001241ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242{
1243 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001244 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001245 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001247 int var_count = 0;
1248 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001249 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001250 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001251 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252
Bram Moolenaardb552d602006-03-23 22:59:57 +00001253 argend = skip_var_list(arg, &var_count, &semicolon);
1254 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001255 return;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001256 if (argend > arg && argend[-1] == '.') // for var.='str'
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001257 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001258 expr = skipwhite(argend);
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001259 if (*expr != '=' && !((vim_strchr((char_u *)"+-*/%.", *expr) != NULL
1260 && expr[1] == '=') || STRNCMP(expr, "..=", 3) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001262 /*
1263 * ":let" without "=": list variables
1264 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001265 if (*arg == '[')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001266 emsg(_(e_invarg));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001267 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001268 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001269 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001270 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001271 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001272 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001273 list_glob_vars(&first);
1274 list_buf_vars(&first);
1275 list_win_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001276 list_tab_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001277 list_script_vars(&first);
1278 list_func_vars(&first);
1279 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 eap->nextcmd = check_nextcmd(arg);
1282 }
1283 else
1284 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001285 op[0] = '=';
1286 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001287 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001288 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001289 if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001290 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001291 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001292 if (expr[0] == '.' && expr[1] == '.') // ..=
1293 ++expr;
1294 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001295 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001296 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001297 else
1298 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001299
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 if (eap->skip)
1301 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001302 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 if (eap->skip)
1304 {
1305 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001306 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 --emsg_skip;
1308 }
1309 else if (i != FAIL)
1310 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001311 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001312 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001313 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 }
1315 }
1316}
1317
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001318/*
1319 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1320 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001321 * When "nextchars" is not NULL it points to a string with characters that
1322 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1323 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001324 * Returns OK or FAIL;
1325 */
1326 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001327ex_let_vars(
1328 char_u *arg_start,
1329 typval_T *tv,
1330 int copy, /* copy values from "tv", don't move */
1331 int semicolon, /* from skip_var_list() */
1332 int var_count, /* from skip_var_list() */
1333 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001334{
1335 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001336 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001337 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001338 listitem_T *item;
1339 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001340
1341 if (*arg != '[')
1342 {
1343 /*
1344 * ":let var = expr" or ":for var in list"
1345 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001346 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001347 return FAIL;
1348 return OK;
1349 }
1350
1351 /*
1352 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1353 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001354 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001355 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001356 emsg(_(e_listreq));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001357 return FAIL;
1358 }
1359
1360 i = list_len(l);
1361 if (semicolon == 0 && var_count < i)
1362 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001363 emsg(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001364 return FAIL;
1365 }
1366 if (var_count - semicolon > i)
1367 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001368 emsg(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001369 return FAIL;
1370 }
1371
1372 item = l->lv_first;
1373 while (*arg != ']')
1374 {
1375 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001376 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001377 item = item->li_next;
1378 if (arg == NULL)
1379 return FAIL;
1380
1381 arg = skipwhite(arg);
1382 if (*arg == ';')
1383 {
1384 /* Put the rest of the list (may be empty) in the var after ';'.
1385 * Create a new list for this. */
1386 l = list_alloc();
1387 if (l == NULL)
1388 return FAIL;
1389 while (item != NULL)
1390 {
1391 list_append_tv(l, &item->li_tv);
1392 item = item->li_next;
1393 }
1394
1395 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001396 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001397 ltv.vval.v_list = l;
1398 l->lv_refcount = 1;
1399
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001400 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1401 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001402 clear_tv(&ltv);
1403 if (arg == NULL)
1404 return FAIL;
1405 break;
1406 }
1407 else if (*arg != ',' && *arg != ']')
1408 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001409 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001410 return FAIL;
1411 }
1412 }
1413
1414 return OK;
1415}
1416
1417/*
1418 * Skip over assignable variable "var" or list of variables "[var, var]".
1419 * Used for ":let varvar = expr" and ":for varvar in expr".
1420 * For "[var, var]" increment "*var_count" for each variable.
1421 * for "[var, var; var]" set "semicolon".
1422 * Return NULL for an error.
1423 */
1424 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001425skip_var_list(
1426 char_u *arg,
1427 int *var_count,
1428 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001429{
1430 char_u *p, *s;
1431
1432 if (*arg == '[')
1433 {
1434 /* "[var, var]": find the matching ']'. */
1435 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001436 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001437 {
1438 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1439 s = skip_var_one(p);
1440 if (s == p)
1441 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001442 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001443 return NULL;
1444 }
1445 ++*var_count;
1446
1447 p = skipwhite(s);
1448 if (*p == ']')
1449 break;
1450 else if (*p == ';')
1451 {
1452 if (*semicolon == 1)
1453 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001454 emsg(_("Double ; in list of variables"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001455 return NULL;
1456 }
1457 *semicolon = 1;
1458 }
1459 else if (*p != ',')
1460 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001461 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001462 return NULL;
1463 }
1464 }
1465 return p + 1;
1466 }
1467 else
1468 return skip_var_one(arg);
1469}
1470
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001471/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001472 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001473 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001474 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001475 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001476skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001477{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001478 if (*arg == '@' && arg[1] != NUL)
1479 return arg + 2;
1480 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1481 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001482}
1483
Bram Moolenaara7043832005-01-21 11:56:39 +00001484/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001485 * List variables for hashtab "ht" with prefix "prefix".
1486 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001487 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001488 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001489list_hashtable_vars(
1490 hashtab_T *ht,
Bram Moolenaar32526b32019-01-19 17:43:09 +01001491 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01001492 int empty,
1493 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001494{
Bram Moolenaar33570922005-01-25 22:26:29 +00001495 hashitem_T *hi;
1496 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001497 int todo;
Bram Moolenaarf86db782018-10-25 13:31:37 +02001498 char_u buf[IOSIZE];
Bram Moolenaara7043832005-01-21 11:56:39 +00001499
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001500 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001501 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1502 {
1503 if (!HASHITEM_EMPTY(hi))
1504 {
1505 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001506 di = HI2DI(hi);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001507
1508 // apply :filter /pat/ to variable name
Bram Moolenaar32526b32019-01-19 17:43:09 +01001509 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1510 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001511 if (message_filtered(buf))
1512 continue;
1513
Bram Moolenaar33570922005-01-25 22:26:29 +00001514 if (empty || di->di_tv.v_type != VAR_STRING
1515 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001516 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001517 }
1518 }
1519}
1520
1521/*
1522 * List global variables.
1523 */
1524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001525list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001526{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001527 list_hashtable_vars(&globvarht, "", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001528}
1529
1530/*
1531 * List buffer variables.
1532 */
1533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001534list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001535{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001536 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001537}
1538
1539/*
1540 * List window variables.
1541 */
1542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001543list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001544{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001545 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001546}
1547
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001548/*
1549 * List tab page variables.
1550 */
1551 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001552list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001553{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001554 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001555}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001556
Bram Moolenaara7043832005-01-21 11:56:39 +00001557/*
1558 * List Vim variables.
1559 */
1560 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001561list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001562{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001563 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001564}
1565
1566/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001567 * List script-local variables, if there is a script.
1568 */
1569 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001570list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001571{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001572 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
1573 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
Bram Moolenaar32526b32019-01-19 17:43:09 +01001574 "s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001575}
1576
1577/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001578 * List variables in "arg".
1579 */
1580 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001581list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001582{
1583 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001584 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001585 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001586 char_u *name_start;
1587 char_u *arg_subsc;
1588 char_u *tofree;
1589 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001590
1591 while (!ends_excmd(*arg) && !got_int)
1592 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001593 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001594 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001595 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001596 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001597 {
1598 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001599 emsg(_(e_trailing));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001600 break;
1601 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001602 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001603 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001604 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001605 /* get_name_len() takes care of expanding curly braces */
1606 name_start = name = arg;
1607 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1608 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001609 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001610 /* This is mainly to keep test 49 working: when expanding
1611 * curly braces fails overrule the exception error message. */
1612 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001613 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001614 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001615 semsg(_(e_invarg2), arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001616 break;
1617 }
1618 error = TRUE;
1619 }
1620 else
1621 {
1622 if (tofree != NULL)
1623 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001624 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001625 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001626 else
1627 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001628 /* handle d.key, l[idx], f(expr) */
1629 arg_subsc = arg;
1630 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001631 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001632 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001633 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001634 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001635 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001636 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001637 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001638 case 'g': list_glob_vars(first); break;
1639 case 'b': list_buf_vars(first); break;
1640 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001641 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001642 case 'v': list_vim_vars(first); break;
1643 case 's': list_script_vars(first); break;
1644 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001645 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001646 semsg(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001647 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001648 }
1649 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001650 {
1651 char_u numbuf[NUMBUFLEN];
1652 char_u *tf;
1653 int c;
1654 char_u *s;
1655
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001656 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001657 c = *arg;
1658 *arg = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001659 list_one_var_a("",
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001660 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001661 tv.v_type,
1662 s == NULL ? (char_u *)"" : s,
1663 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001664 *arg = c;
1665 vim_free(tf);
1666 }
1667 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001668 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001669 }
1670 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001671
1672 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001673 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001674
1675 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001676 }
1677
1678 return arg;
1679}
1680
1681/*
1682 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1683 * Returns a pointer to the char just after the var name.
1684 * Returns NULL if there is an error.
1685 */
1686 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001687ex_let_one(
1688 char_u *arg, /* points to variable name */
1689 typval_T *tv, /* value to assign to variable */
1690 int copy, /* copy value from "tv" */
1691 char_u *endchars, /* valid chars after variable name or NULL */
1692 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001693{
1694 int c1;
1695 char_u *name;
1696 char_u *p;
1697 char_u *arg_end = NULL;
1698 int len;
1699 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001700 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001701
1702 /*
1703 * ":let $VAR = expr": Set environment variable.
1704 */
1705 if (*arg == '$')
1706 {
1707 /* Find the end of the name. */
1708 ++arg;
1709 name = arg;
1710 len = get_env_len(&arg);
1711 if (len == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001712 semsg(_(e_invarg2), name - 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001713 else
1714 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001715 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001716 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001717 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001718 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001719 emsg(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001720 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001721 {
1722 c1 = name[len];
1723 name[len] = NUL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001724 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001725 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001726 {
1727 int mustfree = FALSE;
1728 char_u *s = vim_getenv(name, &mustfree);
1729
1730 if (s != NULL)
1731 {
1732 p = tofree = concat_str(s, p);
1733 if (mustfree)
1734 vim_free(s);
1735 }
1736 }
1737 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001738 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001739 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001740 if (STRICMP(name, "HOME") == 0)
1741 init_homedir();
1742 else if (didset_vim && STRICMP(name, "VIM") == 0)
1743 didset_vim = FALSE;
1744 else if (didset_vimruntime
1745 && STRICMP(name, "VIMRUNTIME") == 0)
1746 didset_vimruntime = FALSE;
1747 arg_end = arg;
1748 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001749 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001750 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001751 }
1752 }
1753 }
1754
1755 /*
1756 * ":let &option = expr": Set option value.
1757 * ":let &l:option = expr": Set local option value.
1758 * ":let &g:option = expr": Set global option value.
1759 */
1760 else if (*arg == '&')
1761 {
1762 /* Find the end of the name. */
1763 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001764 if (p == NULL || (endchars != NULL
1765 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001766 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001767 else
1768 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001769 long n;
1770 int opt_type;
1771 long numval;
1772 char_u *stringval = NULL;
1773 char_u *s;
1774
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001775 c1 = *p;
1776 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001777
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001778 n = (long)tv_get_number(tv);
1779 s = tv_get_string_chk(tv); /* != NULL if number or string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001780 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001781 {
1782 opt_type = get_option_value(arg, &numval,
1783 &stringval, opt_flags);
1784 if ((opt_type == 1 && *op == '.')
1785 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001786 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001787 semsg(_(e_letwrong), op);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001788 s = NULL; // don't set the value
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001789 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001790 else
1791 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001792 if (opt_type == 1) // number
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001793 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001794 switch (*op)
1795 {
1796 case '+': n = numval + n; break;
1797 case '-': n = numval - n; break;
1798 case '*': n = numval * n; break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001799 case '/': n = (long)num_divide(numval, n); break;
1800 case '%': n = (long)num_modulus(numval, n); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001801 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001802 }
Bram Moolenaarff697e62019-02-12 22:28:33 +01001803 else if (opt_type == 0 && stringval != NULL) // string
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001804 {
1805 s = concat_str(stringval, s);
1806 vim_free(stringval);
1807 stringval = s;
1808 }
1809 }
1810 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001811 if (s != NULL)
1812 {
1813 set_option_value(arg, n, s, opt_flags);
1814 arg_end = p;
1815 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001816 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001817 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001818 }
1819 }
1820
1821 /*
1822 * ":let @r = expr": Set register contents.
1823 */
1824 else if (*arg == '@')
1825 {
1826 ++arg;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001827 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001828 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001829 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001830 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001831 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001832 else
1833 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001834 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001835 char_u *s;
1836
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001837 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001838 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001839 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02001840 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001841 if (s != NULL)
1842 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001843 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001844 vim_free(s);
1845 }
1846 }
1847 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001848 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001849 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001850 arg_end = arg + 1;
1851 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001852 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001853 }
1854 }
1855
1856 /*
1857 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001858 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001860 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001861 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001862 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001863
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001864 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001865 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001866 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001867 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001868 emsg(_(e_letunexp));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001869 else
1870 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001871 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001872 arg_end = p;
1873 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001874 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001875 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001876 }
1877
1878 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001879 semsg(_(e_invarg2), arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001880
1881 return arg_end;
1882}
1883
1884/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001885 * Get an lval: variable, Dict item or List item that can be assigned a value
1886 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1887 * "name.key", "name.key[expr]" etc.
1888 * Indexing only works if "name" is an existing List or Dictionary.
1889 * "name" points to the start of the name.
1890 * If "rettv" is not NULL it points to the value to be assigned.
1891 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1892 * wrong; must end in space or cmd separator.
1893 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001894 * flags:
1895 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001896 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001897 * GLV_NO_AUTOLOAD: do not use script autoloading
1898 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001899 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001900 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001901 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001902 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001903 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001904get_lval(
1905 char_u *name,
1906 typval_T *rettv,
1907 lval_T *lp,
1908 int unlet,
1909 int skip,
1910 int flags, /* GLV_ values */
1911 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001912{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001913 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001914 char_u *expr_start, *expr_end;
1915 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001916 dictitem_T *v;
1917 typval_T var1;
1918 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001919 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001920 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001921 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001922 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001923 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001924 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001925
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001926 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001927 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001928
1929 if (skip)
1930 {
1931 /* When skipping just find the end of the name. */
1932 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001933 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001934 }
1935
1936 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001937 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001938 if (expr_start != NULL)
1939 {
1940 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001941 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001942 && *p != '[' && *p != '.')
1943 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001944 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001945 return NULL;
1946 }
1947
1948 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1949 if (lp->ll_exp_name == NULL)
1950 {
1951 /* Report an invalid expression in braces, unless the
1952 * expression evaluation has been cancelled due to an
1953 * aborting error, an interrupt, or an exception. */
1954 if (!aborting() && !quiet)
1955 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001956 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001957 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001958 return NULL;
1959 }
1960 }
1961 lp->ll_name = lp->ll_exp_name;
1962 }
1963 else
1964 lp->ll_name = name;
1965
1966 /* Without [idx] or .key we are done. */
1967 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1968 return p;
1969
1970 cc = *p;
1971 *p = NUL;
Bram Moolenaar6e65d592017-12-07 22:11:27 +01001972 /* Only pass &ht when we would write to the variable, it prevents autoload
1973 * as well. */
1974 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
1975 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001976 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001977 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001978 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001979 if (v == NULL)
1980 return NULL;
1981
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001982 /*
1983 * Loop until no more [idx] or .key is following.
1984 */
Bram Moolenaar33570922005-01-25 22:26:29 +00001985 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001986 var1.v_type = VAR_UNKNOWN;
1987 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001988 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001989 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001990 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1991 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001992 && lp->ll_tv->vval.v_dict != NULL)
1993 && !(lp->ll_tv->v_type == VAR_BLOB
1994 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001995 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001996 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001997 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001998 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001999 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002000 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002001 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002002 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002003 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002004 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002005 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002006
Bram Moolenaar8c711452005-01-14 21:53:12 +00002007 len = -1;
2008 if (*p == '.')
2009 {
2010 key = p + 1;
2011 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2012 ;
2013 if (len == 0)
2014 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002015 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002016 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002017 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002018 }
2019 p = key + len;
2020 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002021 else
2022 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002023 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002024 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002025 if (*p == ':')
2026 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002027 else
2028 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002029 empty1 = FALSE;
2030 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002031 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002032 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002033 {
2034 /* not a number or string */
2035 clear_tv(&var1);
2036 return NULL;
2037 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002038 }
2039
2040 /* Optionally get the second index [ :expr]. */
2041 if (*p == ':')
2042 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002043 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002044 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002045 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002046 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002047 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002048 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002049 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002050 if (rettv != NULL
2051 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002052 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002053 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002054 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002055 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002056 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002057 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002058 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002059 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002060 }
2061 p = skipwhite(p + 1);
2062 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002063 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002064 else
2065 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002066 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002067 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2068 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002069 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002070 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002071 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002072 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002073 {
2074 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002075 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002076 clear_tv(&var2);
2077 return NULL;
2078 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002079 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002080 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002081 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002082 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002083 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002084
Bram Moolenaar8c711452005-01-14 21:53:12 +00002085 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002086 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002087 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002088 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002089 clear_tv(&var1);
2090 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002091 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002092 }
2093
2094 /* Skip to past ']'. */
2095 ++p;
2096 }
2097
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002098 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002099 {
2100 if (len == -1)
2101 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002102 /* "[key]": get key from "var1" */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002103 key = tv_get_string_chk(&var1); /* is number or string */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002104 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002105 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002106 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002107 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002108 }
2109 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002110 lp->ll_list = NULL;
2111 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002112 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002113
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002114 /* When assigning to a scope dictionary check that a function and
2115 * variable name is valid (only variable name unless it is l: or
2116 * g: dictionary). Disallow overwriting a builtin function. */
2117 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002118 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002119 int prevval;
2120 int wrong;
2121
2122 if (len != -1)
2123 {
2124 prevval = key[len];
2125 key[len] = NUL;
2126 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002127 else
2128 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002129 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2130 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002131 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002132 || !valid_varname(key);
2133 if (len != -1)
2134 key[len] = prevval;
2135 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002136 return NULL;
2137 }
2138
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002139 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002140 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01002141 // Can't add "v:" or "a:" variable.
2142 if (lp->ll_dict == &vimvardict
2143 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002144 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002145 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01002146 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002147 return NULL;
2148 }
2149
Bram Moolenaar31b81602019-02-10 22:14:27 +01002150 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002151 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002152 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002153 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002154 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002155 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002156 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002157 }
2158 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002159 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002160 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002161 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002162 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002163 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002164 p = NULL;
2165 break;
2166 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002167 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002168 else if ((flags & GLV_READ_ONLY) == 0
2169 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002170 {
2171 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002172 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002173 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002174
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002175 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002176 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002177 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002178 else if (lp->ll_tv->v_type == VAR_BLOB)
2179 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002180 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
2181
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002182 /*
2183 * Get the number and item for the only or first index of the List.
2184 */
2185 if (empty1)
2186 lp->ll_n1 = 0;
2187 else
2188 // is number or string
2189 lp->ll_n1 = (long)tv_get_number(&var1);
2190 clear_tv(&var1);
2191
2192 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002193 || lp->ll_n1 > bloblen
2194 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002195 {
2196 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002197 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002198 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002199 return NULL;
2200 }
2201 if (lp->ll_range && !lp->ll_empty2)
2202 {
2203 lp->ll_n2 = (long)tv_get_number(&var2);
2204 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002205 if (lp->ll_n2 < 0
2206 || lp->ll_n2 >= bloblen
2207 || lp->ll_n2 < lp->ll_n1)
2208 {
2209 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002210 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002211 return NULL;
2212 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002213 }
2214 lp->ll_blob = lp->ll_tv->vval.v_blob;
2215 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01002216 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002217 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002218 else
2219 {
2220 /*
2221 * Get the number and item for the only or first index of the List.
2222 */
2223 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002224 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002225 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002226 /* is number or string */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002227 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002228 clear_tv(&var1);
2229
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002230 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002231 lp->ll_list = lp->ll_tv->vval.v_list;
2232 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2233 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002234 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002235 if (lp->ll_n1 < 0)
2236 {
2237 lp->ll_n1 = 0;
2238 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2239 }
2240 }
2241 if (lp->ll_li == NULL)
2242 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002243 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002244 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002245 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002246 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002247 }
2248
2249 /*
2250 * May need to find the item or absolute index for the second
2251 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002252 * When no index given: "lp->ll_empty2" is TRUE.
2253 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002254 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002255 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002256 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002257 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002258 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002259 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002260 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002261 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002262 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002263 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002264 {
2265 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002266 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002267 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002268 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002269 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002270 }
2271
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002272 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2273 if (lp->ll_n1 < 0)
2274 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2275 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002276 {
2277 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002278 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002279 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002280 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002281 }
2282
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002283 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002284 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002285 }
2286
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002287 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002288 return p;
2289}
2290
2291/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002292 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002293 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002294 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002295clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002296{
2297 vim_free(lp->ll_exp_name);
2298 vim_free(lp->ll_newkey);
2299}
2300
2301/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002302 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002303 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01002304 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
2305 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002306 */
2307 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002308set_var_lval(
2309 lval_T *lp,
2310 char_u *endp,
2311 typval_T *rettv,
2312 int copy,
2313 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002314{
2315 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002316 listitem_T *ri;
2317 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002318
2319 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002320 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002321 cc = *endp;
2322 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002323 if (lp->ll_blob != NULL)
2324 {
2325 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002326
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002327 if (op != NULL && *op != '=')
2328 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002329 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002330 return;
2331 }
2332
2333 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2334 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002335 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002336
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002337 if (lp->ll_empty2)
2338 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
2339
2340 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002341 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002342 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002343 return;
2344 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002345 if (lp->ll_empty2)
2346 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002347
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002348 ir = 0;
2349 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
2350 blob_set(lp->ll_blob, il,
2351 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002352 }
2353 else
2354 {
2355 val = (int)tv_get_number_chk(rettv, &error);
2356 if (!error)
2357 {
2358 garray_T *gap = &lp->ll_blob->bv_ga;
2359
2360 // Allow for appending a byte. Setting a byte beyond
2361 // the end is an error otherwise.
2362 if (lp->ll_n1 < gap->ga_len
2363 || (lp->ll_n1 == gap->ga_len
2364 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
2365 {
2366 blob_set(lp->ll_blob, lp->ll_n1, val);
2367 if (lp->ll_n1 == gap->ga_len)
2368 ++gap->ga_len;
2369 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002370 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002371 }
2372 }
2373 }
2374 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002375 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002376 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002377
Bram Moolenaarff697e62019-02-12 22:28:33 +01002378 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01002379 di = NULL;
2380 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2381 &tv, &di, TRUE, FALSE) == OK)
2382 {
2383 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002384 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2385 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01002386 && tv_op(&tv, rettv, op) == OK)
2387 set_var(lp->ll_name, &tv, FALSE);
2388 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002389 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002390 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002391 else
2392 set_var(lp->ll_name, rettv, copy);
2393 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002394 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002395 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002396 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002397 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002398 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002399 else if (lp->ll_range)
2400 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002401 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002402 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002403
2404 /*
2405 * Check whether any of the list items is locked
2406 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002407 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002408 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002409 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002410 return;
2411 ri = ri->li_next;
2412 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2413 break;
2414 ll_li = ll_li->li_next;
2415 ++ll_n1;
2416 }
2417
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002418 /*
2419 * Assign the List values to the list items.
2420 */
2421 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002422 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002423 if (op != NULL && *op != '=')
2424 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2425 else
2426 {
2427 clear_tv(&lp->ll_li->li_tv);
2428 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2429 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002430 ri = ri->li_next;
2431 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2432 break;
2433 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002434 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002435 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002436 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002437 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002438 ri = NULL;
2439 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002440 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002441 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002442 lp->ll_li = lp->ll_li->li_next;
2443 ++lp->ll_n1;
2444 }
2445 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002446 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002447 else if (lp->ll_empty2
2448 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002449 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002450 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002451 }
2452 else
2453 {
2454 /*
2455 * Assign to a List or Dictionary item.
2456 */
2457 if (lp->ll_newkey != NULL)
2458 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002459 if (op != NULL && *op != '=')
2460 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002461 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002462 return;
2463 }
2464
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002465 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002466 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002467 if (di == NULL)
2468 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002469 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2470 {
2471 vim_free(di);
2472 return;
2473 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002475 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002476 else if (op != NULL && *op != '=')
2477 {
2478 tv_op(lp->ll_tv, rettv, op);
2479 return;
2480 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002481 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002483
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002484 /*
2485 * Assign the value to the variable or list item.
2486 */
2487 if (copy)
2488 copy_tv(rettv, lp->ll_tv);
2489 else
2490 {
2491 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002492 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002493 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002494 }
2495 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002496}
2497
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002498/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01002499 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2500 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002501 * Returns OK or FAIL.
2502 */
2503 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002504tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002505{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002506 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002507 char_u numbuf[NUMBUFLEN];
2508 char_u *s;
2509
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002510 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2511 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2512 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002513 {
2514 switch (tv1->v_type)
2515 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002516 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002517 case VAR_DICT:
2518 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002519 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002520 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002521 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002522 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002523 break;
2524
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002525 case VAR_BLOB:
2526 if (*op != '+' || tv2->v_type != VAR_BLOB)
2527 break;
2528 // BLOB += BLOB
2529 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2530 {
2531 blob_T *b1 = tv1->vval.v_blob;
2532 blob_T *b2 = tv2->vval.v_blob;
2533 int i, len = blob_len(b2);
2534 for (i = 0; i < len; i++)
2535 ga_append(&b1->bv_ga, blob_get(b2, i));
2536 }
2537 return OK;
2538
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002539 case VAR_LIST:
2540 if (*op != '+' || tv2->v_type != VAR_LIST)
2541 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002542 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002543 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2544 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2545 return OK;
2546
2547 case VAR_NUMBER:
2548 case VAR_STRING:
2549 if (tv2->v_type == VAR_LIST)
2550 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002551 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002552 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002553 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002554 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002555#ifdef FEAT_FLOAT
2556 if (tv2->v_type == VAR_FLOAT)
2557 {
2558 float_T f = n;
2559
Bram Moolenaarff697e62019-02-12 22:28:33 +01002560 if (*op == '%')
2561 break;
2562 switch (*op)
2563 {
2564 case '+': f += tv2->vval.v_float; break;
2565 case '-': f -= tv2->vval.v_float; break;
2566 case '*': f *= tv2->vval.v_float; break;
2567 case '/': f /= tv2->vval.v_float; break;
2568 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002569 clear_tv(tv1);
2570 tv1->v_type = VAR_FLOAT;
2571 tv1->vval.v_float = f;
2572 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002573 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002574#endif
2575 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002576 switch (*op)
2577 {
2578 case '+': n += tv_get_number(tv2); break;
2579 case '-': n -= tv_get_number(tv2); break;
2580 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01002581 case '/': n = num_divide(n, tv_get_number(tv2)); break;
2582 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002583 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002584 clear_tv(tv1);
2585 tv1->v_type = VAR_NUMBER;
2586 tv1->vval.v_number = n;
2587 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002588 }
2589 else
2590 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002591 if (tv2->v_type == VAR_FLOAT)
2592 break;
2593
Bram Moolenaarff697e62019-02-12 22:28:33 +01002594 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002595 s = tv_get_string(tv1);
2596 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002597 clear_tv(tv1);
2598 tv1->v_type = VAR_STRING;
2599 tv1->vval.v_string = s;
2600 }
2601 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002602
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002603 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002604#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002605 {
2606 float_T f;
2607
Bram Moolenaarff697e62019-02-12 22:28:33 +01002608 if (*op == '%' || *op == '.'
2609 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002610 && tv2->v_type != VAR_NUMBER
2611 && tv2->v_type != VAR_STRING))
2612 break;
2613 if (tv2->v_type == VAR_FLOAT)
2614 f = tv2->vval.v_float;
2615 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002616 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01002617 switch (*op)
2618 {
2619 case '+': tv1->vval.v_float += f; break;
2620 case '-': tv1->vval.v_float -= f; break;
2621 case '*': tv1->vval.v_float *= f; break;
2622 case '/': tv1->vval.v_float /= f; break;
2623 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002624 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002625#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002626 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002627 }
2628 }
2629
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002630 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002631 return FAIL;
2632}
2633
2634/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002635 * Evaluate the expression used in a ":for var in expr" command.
2636 * "arg" points to "var".
2637 * Set "*errp" to TRUE for an error, FALSE otherwise;
2638 * Return a pointer that holds the info. Null when there is an error.
2639 */
2640 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002641eval_for_line(
2642 char_u *arg,
2643 int *errp,
2644 char_u **nextcmdp,
2645 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002646{
Bram Moolenaar33570922005-01-25 22:26:29 +00002647 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002648 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002649 typval_T tv;
2650 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002651
2652 *errp = TRUE; /* default: there is an error */
2653
Bram Moolenaar33570922005-01-25 22:26:29 +00002654 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002655 if (fi == NULL)
2656 return NULL;
2657
2658 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2659 if (expr == NULL)
2660 return fi;
2661
2662 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002663 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002664 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002665 emsg(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002666 return fi;
2667 }
2668
2669 if (skip)
2670 ++emsg_skip;
2671 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2672 {
2673 *errp = FALSE;
2674 if (!skip)
2675 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002676 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002677 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002678 l = tv.vval.v_list;
2679 if (l == NULL)
2680 {
2681 // a null list is like an empty list: do nothing
2682 clear_tv(&tv);
2683 }
2684 else
2685 {
2686 // No need to increment the refcount, it's already set for
2687 // the list being used in "tv".
2688 fi->fi_list = l;
2689 list_add_watch(l, &fi->fi_lw);
2690 fi->fi_lw.lw_item = l->lv_first;
2691 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002692 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002693 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002694 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002695 fi->fi_bi = 0;
2696 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002697 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002698 typval_T btv;
2699
2700 // Make a copy, so that the iteration still works when the
2701 // blob is changed.
2702 blob_copy(&tv, &btv);
2703 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002704 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002705 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002706 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002707 else
2708 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002709 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002710 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002711 }
2712 }
2713 }
2714 if (skip)
2715 --emsg_skip;
2716
2717 return fi;
2718}
2719
2720/*
2721 * Use the first item in a ":for" list. Advance to the next.
2722 * Assign the values to the variable (list). "arg" points to the first one.
2723 * Return TRUE when a valid item was found, FALSE when at end of list or
2724 * something wrong.
2725 */
2726 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002727next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002728{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002729 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002730 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002731 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002732
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002733 if (fi->fi_blob != NULL)
2734 {
2735 typval_T tv;
2736
2737 if (fi->fi_bi >= blob_len(fi->fi_blob))
2738 return FALSE;
2739 tv.v_type = VAR_NUMBER;
2740 tv.v_lock = VAR_FIXED;
2741 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2742 ++fi->fi_bi;
2743 return ex_let_vars(arg, &tv, TRUE,
2744 fi->fi_semicolon, fi->fi_varcount, NULL) == OK;
2745 }
2746
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002747 item = fi->fi_lw.lw_item;
2748 if (item == NULL)
2749 result = FALSE;
2750 else
2751 {
2752 fi->fi_lw.lw_item = item->li_next;
2753 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2754 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2755 }
2756 return result;
2757}
2758
2759/*
2760 * Free the structure used to store info used by ":for".
2761 */
2762 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002763free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002764{
Bram Moolenaar33570922005-01-25 22:26:29 +00002765 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002766
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002767 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002768 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002769 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002770 list_unref(fi->fi_list);
2771 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002772 if (fi != NULL && fi->fi_blob != NULL)
2773 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002774 vim_free(fi);
2775}
2776
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2778
2779 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002780set_context_for_expression(
2781 expand_T *xp,
2782 char_u *arg,
2783 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784{
2785 int got_eq = FALSE;
2786 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002787 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002789 if (cmdidx == CMD_let)
2790 {
2791 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002792 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002793 {
2794 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002795 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002796 {
2797 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002798 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002799 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002800 break;
2801 }
2802 return;
2803 }
2804 }
2805 else
2806 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2807 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 while ((xp->xp_pattern = vim_strpbrk(arg,
2809 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2810 {
2811 c = *xp->xp_pattern;
2812 if (c == '&')
2813 {
2814 c = xp->xp_pattern[1];
2815 if (c == '&')
2816 {
2817 ++xp->xp_pattern;
2818 xp->xp_context = cmdidx != CMD_let || got_eq
2819 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2820 }
2821 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002822 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002824 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2825 xp->xp_pattern += 2;
2826
2827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 }
2829 else if (c == '$')
2830 {
2831 /* environment variable */
2832 xp->xp_context = EXPAND_ENV_VARS;
2833 }
2834 else if (c == '=')
2835 {
2836 got_eq = TRUE;
2837 xp->xp_context = EXPAND_EXPRESSION;
2838 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002839 else if (c == '#'
2840 && xp->xp_context == EXPAND_EXPRESSION)
2841 {
2842 /* Autoload function/variable contains '#'. */
2843 break;
2844 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002845 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 && xp->xp_context == EXPAND_FUNCTIONS
2847 && vim_strchr(xp->xp_pattern, '(') == NULL)
2848 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002849 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 break;
2851 }
2852 else if (cmdidx != CMD_let || got_eq)
2853 {
2854 if (c == '"') /* string */
2855 {
2856 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2857 if (c == '\\' && xp->xp_pattern[1] != NUL)
2858 ++xp->xp_pattern;
2859 xp->xp_context = EXPAND_NOTHING;
2860 }
2861 else if (c == '\'') /* literal string */
2862 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2865 /* skip */ ;
2866 xp->xp_context = EXPAND_NOTHING;
2867 }
2868 else if (c == '|')
2869 {
2870 if (xp->xp_pattern[1] == '|')
2871 {
2872 ++xp->xp_pattern;
2873 xp->xp_context = EXPAND_EXPRESSION;
2874 }
2875 else
2876 xp->xp_context = EXPAND_COMMANDS;
2877 }
2878 else
2879 xp->xp_context = EXPAND_EXPRESSION;
2880 }
2881 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002882 /* Doesn't look like something valid, expand as an expression
2883 * anyway. */
2884 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 arg = xp->xp_pattern;
2886 if (*arg != NUL)
2887 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2888 /* skip */ ;
2889 }
2890 xp->xp_pattern = arg;
2891}
2892
2893#endif /* FEAT_CMDL_COMPL */
2894
2895/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 * ":unlet[!] var1 ... " command.
2897 */
2898 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002899ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002901 ex_unletlock(eap, eap->arg, 0);
2902}
2903
2904/*
2905 * ":lockvar" and ":unlockvar" commands
2906 */
2907 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002908ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002909{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002911 int deep = 2;
2912
2913 if (eap->forceit)
2914 deep = -1;
2915 else if (vim_isdigit(*arg))
2916 {
2917 deep = getdigits(&arg);
2918 arg = skipwhite(arg);
2919 }
2920
2921 ex_unletlock(eap, arg, deep);
2922}
2923
2924/*
2925 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2926 */
2927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002928ex_unletlock(
2929 exarg_T *eap,
2930 char_u *argstart,
2931 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002932{
2933 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002936 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937
2938 do
2939 {
Bram Moolenaar137374f2018-05-13 15:59:50 +02002940 if (*arg == '$')
2941 {
2942 char_u *name = ++arg;
2943
2944 if (get_env_len(&arg) == 0)
2945 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002946 semsg(_(e_invarg2), name - 1);
Bram Moolenaar137374f2018-05-13 15:59:50 +02002947 return;
2948 }
2949 vim_unsetenv(name);
2950 arg = skipwhite(arg);
2951 continue;
2952 }
2953
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002954 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002955 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002956 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002957 if (lv.ll_name == NULL)
2958 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002959 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002960 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002962 if (name_end != NULL)
2963 {
2964 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002965 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002966 }
2967 if (!(eap->skip || error))
2968 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 break;
2970 }
2971
2972 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002973 {
2974 if (eap->cmdidx == CMD_unlet)
2975 {
2976 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2977 error = TRUE;
2978 }
2979 else
2980 {
2981 if (do_lock_var(&lv, name_end, deep,
2982 eap->cmdidx == CMD_lockvar) == FAIL)
2983 error = TRUE;
2984 }
2985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002987 if (!eap->skip)
2988 clear_lval(&lv);
2989
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 arg = skipwhite(name_end);
2991 } while (!ends_excmd(*arg));
2992
2993 eap->nextcmd = check_nextcmd(arg);
2994}
2995
Bram Moolenaar8c711452005-01-14 21:53:12 +00002996 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002997do_unlet_var(
2998 lval_T *lp,
2999 char_u *name_end,
3000 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003001{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003002 int ret = OK;
3003 int cc;
3004
3005 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003006 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003007 cc = *name_end;
3008 *name_end = NUL;
3009
3010 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003011 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003012 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003013 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003014 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003015 else if ((lp->ll_list != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003016 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003017 || (lp->ll_dict != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003018 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003019 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003020 else if (lp->ll_range)
3021 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003022 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003023 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003024 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003025
3026 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3027 {
3028 li = ll_li->li_next;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003029 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003030 return FAIL;
3031 ll_li = li;
3032 ++ll_n1;
3033 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003034
3035 /* Delete a range of List items. */
3036 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3037 {
3038 li = lp->ll_li->li_next;
3039 listitem_remove(lp->ll_list, lp->ll_li);
3040 lp->ll_li = li;
3041 ++lp->ll_n1;
3042 }
3043 }
3044 else
3045 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003046 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003047 /* unlet a List item. */
3048 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003049 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003050 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003051 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003052 }
3053
3054 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003055}
3056
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057/*
3058 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003059 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 */
3061 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003062do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063{
Bram Moolenaar33570922005-01-25 22:26:29 +00003064 hashtab_T *ht;
3065 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003066 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003067 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003068 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069
Bram Moolenaar33570922005-01-25 22:26:29 +00003070 ht = find_var_ht(name, &varname);
3071 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003073 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003074 if (d == NULL)
3075 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003076 if (ht == &globvarht)
3077 d = &globvardict;
3078 else if (ht == &compat_hashtab)
3079 d = &vimvardict;
3080 else
3081 {
3082 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3083 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3084 }
3085 if (d == NULL)
3086 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003087 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003088 return FAIL;
3089 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003090 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003091 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003092 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003093 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003094 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003095 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003096 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003097 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003098 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003099 || var_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003100 return FAIL;
3101
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003102 delete_var(ht, hi);
3103 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003106 if (forceit)
3107 return OK;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003108 semsg(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 return FAIL;
3110}
3111
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003112/*
3113 * Lock or unlock variable indicated by "lp".
3114 * "deep" is the levels to go (-1 for unlimited);
3115 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3116 */
3117 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003118do_lock_var(
3119 lval_T *lp,
3120 char_u *name_end,
3121 int deep,
3122 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003123{
3124 int ret = OK;
3125 int cc;
3126 dictitem_T *di;
3127
3128 if (deep == 0) /* nothing to do */
3129 return OK;
3130
3131 if (lp->ll_tv == NULL)
3132 {
3133 cc = *name_end;
3134 *name_end = NUL;
3135
3136 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003137 di = find_var(lp->ll_name, NULL, TRUE);
3138 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003139 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01003140 else if ((di->di_flags & DI_FLAGS_FIX)
3141 && di->di_tv.v_type != VAR_DICT
3142 && di->di_tv.v_type != VAR_LIST)
3143 /* For historic reasons this error is not given for a list or dict.
3144 * E.g., the b: dict could be locked/unlocked. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003145 semsg(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003146 else
3147 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01003148 if (lock)
3149 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003150 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01003151 di->di_flags &= ~DI_FLAGS_LOCK;
3152 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003153 }
3154 *name_end = cc;
3155 }
3156 else if (lp->ll_range)
3157 {
3158 listitem_T *li = lp->ll_li;
3159
3160 /* (un)lock a range of List items. */
3161 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3162 {
3163 item_lock(&li->li_tv, deep, lock);
3164 li = li->li_next;
3165 ++lp->ll_n1;
3166 }
3167 }
3168 else if (lp->ll_list != NULL)
3169 /* (un)lock a List item. */
3170 item_lock(&lp->ll_li->li_tv, deep, lock);
3171 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003172 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003173 item_lock(&lp->ll_di->di_tv, deep, lock);
3174
3175 return ret;
3176}
3177
3178/*
3179 * Lock or unlock an item. "deep" is nr of levels to go.
3180 */
3181 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003182item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003183{
3184 static int recurse = 0;
3185 list_T *l;
3186 listitem_T *li;
3187 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003188 blob_T *b;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003189 hashitem_T *hi;
3190 int todo;
3191
3192 if (recurse >= DICT_MAXNEST)
3193 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003194 emsg(_("E743: variable nested too deep for (un)lock"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003195 return;
3196 }
3197 if (deep == 0)
3198 return;
3199 ++recurse;
3200
3201 /* lock/unlock the item itself */
3202 if (lock)
3203 tv->v_lock |= VAR_LOCKED;
3204 else
3205 tv->v_lock &= ~VAR_LOCKED;
3206
3207 switch (tv->v_type)
3208 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003209 case VAR_UNKNOWN:
3210 case VAR_NUMBER:
3211 case VAR_STRING:
3212 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003213 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003214 case VAR_FLOAT:
3215 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003216 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003217 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003218 break;
3219
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003220 case VAR_BLOB:
3221 if ((b = tv->vval.v_blob) != NULL)
3222 {
3223 if (lock)
3224 b->bv_lock |= VAR_LOCKED;
3225 else
3226 b->bv_lock &= ~VAR_LOCKED;
3227 }
3228 break;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003229 case VAR_LIST:
3230 if ((l = tv->vval.v_list) != NULL)
3231 {
3232 if (lock)
3233 l->lv_lock |= VAR_LOCKED;
3234 else
3235 l->lv_lock &= ~VAR_LOCKED;
3236 if (deep < 0 || deep > 1)
3237 /* recursive: lock/unlock the items the List contains */
3238 for (li = l->lv_first; li != NULL; li = li->li_next)
3239 item_lock(&li->li_tv, deep - 1, lock);
3240 }
3241 break;
3242 case VAR_DICT:
3243 if ((d = tv->vval.v_dict) != NULL)
3244 {
3245 if (lock)
3246 d->dv_lock |= VAR_LOCKED;
3247 else
3248 d->dv_lock &= ~VAR_LOCKED;
3249 if (deep < 0 || deep > 1)
3250 {
3251 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003252 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003253 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3254 {
3255 if (!HASHITEM_EMPTY(hi))
3256 {
3257 --todo;
3258 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3259 }
3260 }
3261 }
3262 }
3263 }
3264 --recurse;
3265}
3266
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3268/*
3269 * Delete all "menutrans_" variables.
3270 */
3271 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003272del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273{
Bram Moolenaar33570922005-01-25 22:26:29 +00003274 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003275 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276
Bram Moolenaar33570922005-01-25 22:26:29 +00003277 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003278 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003279 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003280 {
3281 if (!HASHITEM_EMPTY(hi))
3282 {
3283 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003284 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3285 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003286 }
3287 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003288 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289}
3290#endif
3291
3292#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3293
3294/*
3295 * Local string buffer for the next two functions to store a variable name
3296 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3297 * get_user_var_name().
3298 */
3299
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300static char_u *varnamebuf = NULL;
3301static int varnamebuflen = 0;
3302
3303/*
3304 * Function to concatenate a prefix and a variable name.
3305 */
3306 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003307cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308{
3309 int len;
3310
3311 len = (int)STRLEN(name) + 3;
3312 if (len > varnamebuflen)
3313 {
3314 vim_free(varnamebuf);
3315 len += 10; /* some additional space */
3316 varnamebuf = alloc(len);
3317 if (varnamebuf == NULL)
3318 {
3319 varnamebuflen = 0;
3320 return NULL;
3321 }
3322 varnamebuflen = len;
3323 }
3324 *varnamebuf = prefix;
3325 varnamebuf[1] = ':';
3326 STRCPY(varnamebuf + 2, name);
3327 return varnamebuf;
3328}
3329
3330/*
3331 * Function given to ExpandGeneric() to obtain the list of user defined
3332 * (global/buffer/window/built-in) variable names.
3333 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003335get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003337 static long_u gdone;
3338 static long_u bdone;
3339 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003340 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003341 static int vidx;
3342 static hashitem_T *hi;
3343 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344
3345 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003346 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003347 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003348 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003349 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003350
3351 /* Global variables */
3352 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003354 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003355 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003356 else
3357 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003358 while (HASHITEM_EMPTY(hi))
3359 ++hi;
3360 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3361 return cat_prefix_varname('g', hi->hi_key);
3362 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003364
3365 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003366 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003367 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003369 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003370 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003371 else
3372 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003373 while (HASHITEM_EMPTY(hi))
3374 ++hi;
3375 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003377
3378 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003379 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003380 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003382 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003383 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003384 else
3385 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003386 while (HASHITEM_EMPTY(hi))
3387 ++hi;
3388 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003390
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003391 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003392 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003393 if (tdone < ht->ht_used)
3394 {
3395 if (tdone++ == 0)
3396 hi = ht->ht_array;
3397 else
3398 ++hi;
3399 while (HASHITEM_EMPTY(hi))
3400 ++hi;
3401 return cat_prefix_varname('t', hi->hi_key);
3402 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003403
Bram Moolenaar33570922005-01-25 22:26:29 +00003404 /* v: variables */
3405 if (vidx < VV_LEN)
3406 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407
Bram Moolenaard23a8232018-02-10 18:45:26 +01003408 VIM_CLEAR(varnamebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 varnamebuflen = 0;
3410 return NULL;
3411}
3412
3413#endif /* FEAT_CMDL_COMPL */
3414
3415/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003416 * Return TRUE if "pat" matches "text".
3417 * Does not use 'cpo' and always uses 'magic'.
3418 */
3419 static int
3420pattern_match(char_u *pat, char_u *text, int ic)
3421{
3422 int matches = FALSE;
3423 char_u *save_cpo;
3424 regmatch_T regmatch;
3425
3426 /* avoid 'l' flag in 'cpoptions' */
3427 save_cpo = p_cpo;
3428 p_cpo = (char_u *)"";
3429 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3430 if (regmatch.regprog != NULL)
3431 {
3432 regmatch.rm_ic = ic;
3433 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3434 vim_regfree(regmatch.regprog);
3435 }
3436 p_cpo = save_cpo;
3437 return matches;
3438}
3439
3440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003442 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3444 */
3445
3446/*
3447 * Handle zero level expression.
3448 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003449 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003450 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 * Return OK or FAIL.
3452 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003453 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003454eval0(
3455 char_u *arg,
3456 typval_T *rettv,
3457 char_u **nextcmd,
3458 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459{
3460 int ret;
3461 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003462 int did_emsg_before = did_emsg;
3463 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464
3465 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003466 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 if (ret == FAIL || !ends_excmd(*p))
3468 {
3469 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003470 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 /*
3472 * Report the invalid expression unless the expression evaluation has
3473 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003474 * exception, or we already gave a more specific error.
3475 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003477 if (!aborting() && did_emsg == did_emsg_before
3478 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003479 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 ret = FAIL;
3481 }
3482 if (nextcmd != NULL)
3483 *nextcmd = check_nextcmd(p);
3484
3485 return ret;
3486}
3487
3488/*
3489 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003490 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 *
3492 * "arg" must point to the first non-white of the expression.
3493 * "arg" is advanced to the next non-white after the recognized expression.
3494 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003495 * Note: "rettv.v_lock" is not set.
3496 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 * Return OK or FAIL.
3498 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003499 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003500eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501{
3502 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003503 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504
3505 /*
3506 * Get the first variable.
3507 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003508 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 return FAIL;
3510
3511 if ((*arg)[0] == '?')
3512 {
3513 result = FALSE;
3514 if (evaluate)
3515 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003516 int error = FALSE;
3517
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003518 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003520 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003521 if (error)
3522 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 }
3524
3525 /*
3526 * Get the second variable.
3527 */
3528 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003529 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 return FAIL;
3531
3532 /*
3533 * Check for the ":".
3534 */
3535 if ((*arg)[0] != ':')
3536 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003537 emsg(_("E109: Missing ':' after '?'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003539 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 return FAIL;
3541 }
3542
3543 /*
3544 * Get the third variable.
3545 */
3546 *arg = skipwhite(*arg + 1);
3547 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3548 {
3549 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003550 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 return FAIL;
3552 }
3553 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003554 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 }
3556
3557 return OK;
3558}
3559
3560/*
3561 * Handle first level expression:
3562 * expr2 || expr2 || expr2 logical OR
3563 *
3564 * "arg" must point to the first non-white of the expression.
3565 * "arg" is advanced to the next non-white after the recognized expression.
3566 *
3567 * Return OK or FAIL.
3568 */
3569 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003570eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571{
Bram Moolenaar33570922005-01-25 22:26:29 +00003572 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 long result;
3574 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003575 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576
3577 /*
3578 * Get the first variable.
3579 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003580 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 return FAIL;
3582
3583 /*
3584 * Repeat until there is no following "||".
3585 */
3586 first = TRUE;
3587 result = FALSE;
3588 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3589 {
3590 if (evaluate && first)
3591 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003592 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003594 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003595 if (error)
3596 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 first = FALSE;
3598 }
3599
3600 /*
3601 * Get the second variable.
3602 */
3603 *arg = skipwhite(*arg + 2);
3604 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3605 return FAIL;
3606
3607 /*
3608 * Compute the result.
3609 */
3610 if (evaluate && !result)
3611 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003612 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003614 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003615 if (error)
3616 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 }
3618 if (evaluate)
3619 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003620 rettv->v_type = VAR_NUMBER;
3621 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 }
3623 }
3624
3625 return OK;
3626}
3627
3628/*
3629 * Handle second level expression:
3630 * expr3 && expr3 && expr3 logical AND
3631 *
3632 * "arg" must point to the first non-white of the expression.
3633 * "arg" is advanced to the next non-white after the recognized expression.
3634 *
3635 * Return OK or FAIL.
3636 */
3637 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003638eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639{
Bram Moolenaar33570922005-01-25 22:26:29 +00003640 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 long result;
3642 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003643 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644
3645 /*
3646 * Get the first variable.
3647 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003648 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 return FAIL;
3650
3651 /*
3652 * Repeat until there is no following "&&".
3653 */
3654 first = TRUE;
3655 result = TRUE;
3656 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3657 {
3658 if (evaluate && first)
3659 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003660 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003662 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003663 if (error)
3664 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 first = FALSE;
3666 }
3667
3668 /*
3669 * Get the second variable.
3670 */
3671 *arg = skipwhite(*arg + 2);
3672 if (eval4(arg, &var2, evaluate && result) == FAIL)
3673 return FAIL;
3674
3675 /*
3676 * Compute the result.
3677 */
3678 if (evaluate && result)
3679 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003680 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003682 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003683 if (error)
3684 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 }
3686 if (evaluate)
3687 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003688 rettv->v_type = VAR_NUMBER;
3689 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 }
3691 }
3692
3693 return OK;
3694}
3695
3696/*
3697 * Handle third level expression:
3698 * var1 == var2
3699 * var1 =~ var2
3700 * var1 != var2
3701 * var1 !~ var2
3702 * var1 > var2
3703 * var1 >= var2
3704 * var1 < var2
3705 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003706 * var1 is var2
3707 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 *
3709 * "arg" must point to the first non-white of the expression.
3710 * "arg" is advanced to the next non-white after the recognized expression.
3711 *
3712 * Return OK or FAIL.
3713 */
3714 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003715eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716{
Bram Moolenaar33570922005-01-25 22:26:29 +00003717 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 char_u *p;
3719 int i;
3720 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003721 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724
3725 /*
3726 * Get the first variable.
3727 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003728 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 return FAIL;
3730
3731 p = *arg;
3732 switch (p[0])
3733 {
3734 case '=': if (p[1] == '=')
3735 type = TYPE_EQUAL;
3736 else if (p[1] == '~')
3737 type = TYPE_MATCH;
3738 break;
3739 case '!': if (p[1] == '=')
3740 type = TYPE_NEQUAL;
3741 else if (p[1] == '~')
3742 type = TYPE_NOMATCH;
3743 break;
3744 case '>': if (p[1] != '=')
3745 {
3746 type = TYPE_GREATER;
3747 len = 1;
3748 }
3749 else
3750 type = TYPE_GEQUAL;
3751 break;
3752 case '<': if (p[1] != '=')
3753 {
3754 type = TYPE_SMALLER;
3755 len = 1;
3756 }
3757 else
3758 type = TYPE_SEQUAL;
3759 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003760 case 'i': if (p[1] == 's')
3761 {
3762 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3763 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003764 i = p[len];
3765 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003766 {
3767 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3768 type_is = TRUE;
3769 }
3770 }
3771 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 }
3773
3774 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003775 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 */
3777 if (type != TYPE_UNKNOWN)
3778 {
3779 /* extra question mark appended: ignore case */
3780 if (p[len] == '?')
3781 {
3782 ic = TRUE;
3783 ++len;
3784 }
3785 /* extra '#' appended: match case */
3786 else if (p[len] == '#')
3787 {
3788 ic = FALSE;
3789 ++len;
3790 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003791 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 else
3793 ic = p_ic;
3794
3795 /*
3796 * Get the second variable.
3797 */
3798 *arg = skipwhite(p + len);
3799 if (eval5(arg, &var2, evaluate) == FAIL)
3800 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003801 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 return FAIL;
3803 }
Bram Moolenaar31988702018-02-13 12:57:42 +01003804 if (evaluate)
3805 {
3806 int ret = typval_compare(rettv, &var2, type, type_is, ic);
3807
3808 clear_tv(&var2);
3809 return ret;
3810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 }
3812
3813 return OK;
3814}
3815
3816/*
3817 * Handle fourth level expression:
3818 * + number addition
3819 * - number subtraction
3820 * . string concatenation
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003821 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 *
3823 * "arg" must point to the first non-white of the expression.
3824 * "arg" is advanced to the next non-white after the recognized expression.
3825 *
3826 * Return OK or FAIL.
3827 */
3828 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003829eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830{
Bram Moolenaar33570922005-01-25 22:26:29 +00003831 typval_T var2;
3832 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003834 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003835#ifdef FEAT_FLOAT
3836 float_T f1 = 0, f2 = 0;
3837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 char_u *s1, *s2;
3839 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3840 char_u *p;
3841
3842 /*
3843 * Get the first variable.
3844 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003845 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 return FAIL;
3847
3848 /*
3849 * Repeat computing, until no '+', '-' or '.' is following.
3850 */
3851 for (;;)
3852 {
3853 op = **arg;
3854 if (op != '+' && op != '-' && op != '.')
3855 break;
3856
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003857 if ((op != '+' || (rettv->v_type != VAR_LIST
3858 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003859#ifdef FEAT_FLOAT
3860 && (op == '.' || rettv->v_type != VAR_FLOAT)
3861#endif
3862 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003863 {
3864 /* For "list + ...", an illegal use of the first operand as
3865 * a number cannot be determined before evaluating the 2nd
3866 * operand: if this is also a list, all is ok.
3867 * For "something . ...", "something - ..." or "non-list + ...",
3868 * we know that the first operand needs to be a string or number
3869 * without evaluating the 2nd operand. So check before to avoid
3870 * side effects after an error. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003871 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003872 {
3873 clear_tv(rettv);
3874 return FAIL;
3875 }
3876 }
3877
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 /*
3879 * Get the second variable.
3880 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003881 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
3882 ++*arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003884 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003886 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 return FAIL;
3888 }
3889
3890 if (evaluate)
3891 {
3892 /*
3893 * Compute the result.
3894 */
3895 if (op == '.')
3896 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003897 s1 = tv_get_string_buf(rettv, buf1); /* already checked */
3898 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003899 if (s2 == NULL) /* type error ? */
3900 {
3901 clear_tv(rettv);
3902 clear_tv(&var2);
3903 return FAIL;
3904 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003905 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003906 clear_tv(rettv);
3907 rettv->v_type = VAR_STRING;
3908 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003910 else if (op == '+' && rettv->v_type == VAR_BLOB
3911 && var2.v_type == VAR_BLOB)
3912 {
3913 blob_T *b1 = rettv->vval.v_blob;
3914 blob_T *b2 = var2.vval.v_blob;
3915 blob_T *b = blob_alloc();
3916 int i;
3917
3918 if (b != NULL)
3919 {
3920 for (i = 0; i < blob_len(b1); i++)
3921 ga_append(&b->bv_ga, blob_get(b1, i));
3922 for (i = 0; i < blob_len(b2); i++)
3923 ga_append(&b->bv_ga, blob_get(b2, i));
3924
3925 clear_tv(rettv);
3926 rettv_blob_set(rettv, b);
3927 }
3928 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003929 else if (op == '+' && rettv->v_type == VAR_LIST
3930 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003931 {
3932 /* concatenate Lists */
3933 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3934 &var3) == FAIL)
3935 {
3936 clear_tv(rettv);
3937 clear_tv(&var2);
3938 return FAIL;
3939 }
3940 clear_tv(rettv);
3941 *rettv = var3;
3942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 else
3944 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003945 int error = FALSE;
3946
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003947#ifdef FEAT_FLOAT
3948 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003949 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003950 f1 = rettv->vval.v_float;
3951 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003952 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003953 else
3954#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003955 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003956 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003957 if (error)
3958 {
3959 /* This can only happen for "list + non-list". For
3960 * "non-list + ..." or "something - ...", we returned
3961 * before evaluating the 2nd operand. */
3962 clear_tv(rettv);
3963 return FAIL;
3964 }
3965#ifdef FEAT_FLOAT
3966 if (var2.v_type == VAR_FLOAT)
3967 f1 = n1;
3968#endif
3969 }
3970#ifdef FEAT_FLOAT
3971 if (var2.v_type == VAR_FLOAT)
3972 {
3973 f2 = var2.vval.v_float;
3974 n2 = 0;
3975 }
3976 else
3977#endif
3978 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003979 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003980 if (error)
3981 {
3982 clear_tv(rettv);
3983 clear_tv(&var2);
3984 return FAIL;
3985 }
3986#ifdef FEAT_FLOAT
3987 if (rettv->v_type == VAR_FLOAT)
3988 f2 = n2;
3989#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003990 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003991 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003992
3993#ifdef FEAT_FLOAT
3994 /* If there is a float on either side the result is a float. */
3995 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3996 {
3997 if (op == '+')
3998 f1 = f1 + f2;
3999 else
4000 f1 = f1 - f2;
4001 rettv->v_type = VAR_FLOAT;
4002 rettv->vval.v_float = f1;
4003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004005#endif
4006 {
4007 if (op == '+')
4008 n1 = n1 + n2;
4009 else
4010 n1 = n1 - n2;
4011 rettv->v_type = VAR_NUMBER;
4012 rettv->vval.v_number = n1;
4013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004015 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 }
4017 }
4018 return OK;
4019}
4020
4021/*
4022 * Handle fifth level expression:
4023 * * number multiplication
4024 * / number division
4025 * % number modulo
4026 *
4027 * "arg" must point to the first non-white of the expression.
4028 * "arg" is advanced to the next non-white after the recognized expression.
4029 *
4030 * Return OK or FAIL.
4031 */
4032 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004033eval6(
4034 char_u **arg,
4035 typval_T *rettv,
4036 int evaluate,
4037 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038{
Bram Moolenaar33570922005-01-25 22:26:29 +00004039 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004041 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004042#ifdef FEAT_FLOAT
4043 int use_float = FALSE;
4044 float_T f1 = 0, f2;
4045#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004046 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047
4048 /*
4049 * Get the first variable.
4050 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004051 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 return FAIL;
4053
4054 /*
4055 * Repeat computing, until no '*', '/' or '%' is following.
4056 */
4057 for (;;)
4058 {
4059 op = **arg;
4060 if (op != '*' && op != '/' && op != '%')
4061 break;
4062
4063 if (evaluate)
4064 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004065#ifdef FEAT_FLOAT
4066 if (rettv->v_type == VAR_FLOAT)
4067 {
4068 f1 = rettv->vval.v_float;
4069 use_float = TRUE;
4070 n1 = 0;
4071 }
4072 else
4073#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004074 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004075 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004076 if (error)
4077 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 }
4079 else
4080 n1 = 0;
4081
4082 /*
4083 * Get the second variable.
4084 */
4085 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004086 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 return FAIL;
4088
4089 if (evaluate)
4090 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004091#ifdef FEAT_FLOAT
4092 if (var2.v_type == VAR_FLOAT)
4093 {
4094 if (!use_float)
4095 {
4096 f1 = n1;
4097 use_float = TRUE;
4098 }
4099 f2 = var2.vval.v_float;
4100 n2 = 0;
4101 }
4102 else
4103#endif
4104 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004105 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004106 clear_tv(&var2);
4107 if (error)
4108 return FAIL;
4109#ifdef FEAT_FLOAT
4110 if (use_float)
4111 f2 = n2;
4112#endif
4113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114
4115 /*
4116 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004117 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004119#ifdef FEAT_FLOAT
4120 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004122 if (op == '*')
4123 f1 = f1 * f2;
4124 else if (op == '/')
4125 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004126# ifdef VMS
4127 /* VMS crashes on divide by zero, work around it */
4128 if (f2 == 0.0)
4129 {
4130 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004131 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004132 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004133 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004134 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004135 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004136 }
4137 else
4138 f1 = f1 / f2;
4139# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004140 /* We rely on the floating point library to handle divide
4141 * by zero to result in "inf" and not a crash. */
4142 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004143# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004146 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004147 emsg(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004148 return FAIL;
4149 }
4150 rettv->v_type = VAR_FLOAT;
4151 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 }
4153 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004156 if (op == '*')
4157 n1 = n1 * n2;
4158 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01004159 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01004161 n1 = num_modulus(n1, n2);
4162
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004163 rettv->v_type = VAR_NUMBER;
4164 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 }
4167 }
4168
4169 return OK;
4170}
4171
4172/*
4173 * Handle sixth level expression:
4174 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004175 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004176 * "string" string constant
4177 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 * &option-name option value
4179 * @r register contents
4180 * identifier variable value
4181 * function() function call
4182 * $VAR environment variable
4183 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004184 * [expr, expr] List
4185 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 *
4187 * Also handle:
4188 * ! in front logical NOT
4189 * - in front unary minus
4190 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004191 * trailing [] subscript in String or List
4192 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 *
4194 * "arg" must point to the first non-white of the expression.
4195 * "arg" is advanced to the next non-white after the recognized expression.
4196 *
4197 * Return OK or FAIL.
4198 */
4199 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004200eval7(
4201 char_u **arg,
4202 typval_T *rettv,
4203 int evaluate,
4204 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004206 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 int len;
4208 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 char_u *start_leader, *end_leader;
4210 int ret = OK;
4211 char_u *alias;
4212
4213 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004214 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004215 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004217 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218
4219 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004220 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 */
4222 start_leader = *arg;
4223 while (**arg == '!' || **arg == '-' || **arg == '+')
4224 *arg = skipwhite(*arg + 1);
4225 end_leader = *arg;
4226
4227 switch (**arg)
4228 {
4229 /*
4230 * Number constant.
4231 */
4232 case '0':
4233 case '1':
4234 case '2':
4235 case '3':
4236 case '4':
4237 case '5':
4238 case '6':
4239 case '7':
4240 case '8':
4241 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004242 {
4243#ifdef FEAT_FLOAT
4244 char_u *p = skipdigits(*arg + 1);
4245 int get_float = FALSE;
4246
4247 /* We accept a float when the format matches
4248 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004249 * strict to avoid backwards compatibility problems.
4250 * Don't look for a float after the "." operator, so that
4251 * ":let vers = 1.2.3" doesn't fail. */
4252 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004254 get_float = TRUE;
4255 p = skipdigits(p + 2);
4256 if (*p == 'e' || *p == 'E')
4257 {
4258 ++p;
4259 if (*p == '-' || *p == '+')
4260 ++p;
4261 if (!vim_isdigit(*p))
4262 get_float = FALSE;
4263 else
4264 p = skipdigits(p + 1);
4265 }
4266 if (ASCII_ISALPHA(*p) || *p == '.')
4267 get_float = FALSE;
4268 }
4269 if (get_float)
4270 {
4271 float_T f;
4272
4273 *arg += string2float(*arg, &f);
4274 if (evaluate)
4275 {
4276 rettv->v_type = VAR_FLOAT;
4277 rettv->vval.v_float = f;
4278 }
4279 }
4280 else
4281#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004282 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004283 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004284 char_u *bp;
Bram Moolenaare519dfd2019-01-13 15:42:02 +01004285 blob_T *blob = NULL; // init for gcc
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004286
4287 // Blob constant: 0z0123456789abcdef
4288 if (evaluate)
4289 blob = blob_alloc();
4290 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
4291 {
4292 if (!vim_isxdigit(bp[1]))
4293 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004294 if (blob != NULL)
4295 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004296 emsg(_("E973: Blob literal should have an even number of hex characters"));
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004297 ga_clear(&blob->bv_ga);
4298 VIM_CLEAR(blob);
4299 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004300 ret = FAIL;
4301 break;
4302 }
4303 if (blob != NULL)
4304 ga_append(&blob->bv_ga,
4305 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
Bram Moolenaar4131fd52019-01-17 16:32:53 +01004306 if (bp[2] == '.' && vim_isxdigit(bp[3]))
4307 ++bp;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004308 }
4309 if (blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004310 rettv_blob_set(rettv, blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004311 *arg = bp;
4312 }
4313 else
4314 {
4315 // decimal, hex or octal number
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01004316 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004317 *arg += len;
4318 if (evaluate)
4319 {
4320 rettv->v_type = VAR_NUMBER;
4321 rettv->vval.v_number = n;
4322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 }
4324 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326
4327 /*
4328 * String constant: "string".
4329 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004330 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 break;
4332
4333 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004334 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004336 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004337 break;
4338
4339 /*
4340 * List: [expr, expr]
4341 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004342 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 break;
4344
4345 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004346 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004347 * Dictionary: {key: val, key: val}
4348 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004349 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4350 if (ret == NOTDONE)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004351 ret = dict_get_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004352 break;
4353
4354 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004355 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004357 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 break;
4359
4360 /*
4361 * Environment variable: $VAR.
4362 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004363 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 break;
4365
4366 /*
4367 * Register contents: @r.
4368 */
4369 case '@': ++*arg;
4370 if (evaluate)
4371 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004372 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004373 rettv->vval.v_string = get_reg_contents(**arg,
4374 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 }
4376 if (**arg != NUL)
4377 ++*arg;
4378 break;
4379
4380 /*
4381 * nested expression: (expression).
4382 */
4383 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004384 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 if (**arg == ')')
4386 ++*arg;
4387 else if (ret == OK)
4388 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004389 emsg(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004390 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 ret = FAIL;
4392 }
4393 break;
4394
Bram Moolenaar8c711452005-01-14 21:53:12 +00004395 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 break;
4397 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004398
4399 if (ret == NOTDONE)
4400 {
4401 /*
4402 * Must be a variable or function name.
4403 * Can also be a curly-braces kind of name: {expr}.
4404 */
4405 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004406 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004407 if (alias != NULL)
4408 s = alias;
4409
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004410 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004411 ret = FAIL;
4412 else
4413 {
4414 if (**arg == '(') /* recursive! */
4415 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004416 partial_T *partial;
4417
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004418 if (!evaluate)
4419 check_vars(s, len);
4420
Bram Moolenaar8c711452005-01-14 21:53:12 +00004421 /* If "s" is the name of a variable of type VAR_FUNC
4422 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004423 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004424
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004425 /* Need to make a copy, in case evaluating the arguments makes
4426 * the name invalid. */
4427 s = vim_strsave(s);
4428 if (s == NULL)
4429 ret = FAIL;
4430 else
4431 /* Invoke the function. */
4432 ret = get_func_tv(s, len, rettv, arg,
4433 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4434 &len, evaluate, partial, NULL);
4435 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004436
4437 /* If evaluate is FALSE rettv->v_type was not set in
4438 * get_func_tv, but it's needed in handle_subscript() to parse
4439 * what follows. So set it here. */
4440 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4441 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004442 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004443 rettv->v_type = VAR_FUNC;
4444 }
4445
Bram Moolenaar8c711452005-01-14 21:53:12 +00004446 /* Stop the expression evaluation when immediately
4447 * aborting on error, or when an interrupt occurred or
4448 * an exception was thrown but not caught. */
4449 if (aborting())
4450 {
4451 if (ret == OK)
4452 clear_tv(rettv);
4453 ret = FAIL;
4454 }
4455 }
4456 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004457 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004458 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004459 {
4460 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004461 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004462 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004463 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004464 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004465 }
4466
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 *arg = skipwhite(*arg);
4468
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004469 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4470 * expr(expr). */
4471 if (ret == OK)
4472 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473
4474 /*
4475 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4476 */
4477 if (ret == OK && evaluate && end_leader > start_leader)
4478 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004479 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004480 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004481#ifdef FEAT_FLOAT
4482 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004483
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004484 if (rettv->v_type == VAR_FLOAT)
4485 f = rettv->vval.v_float;
4486 else
4487#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004488 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004489 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004491 clear_tv(rettv);
4492 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004494 else
4495 {
4496 while (end_leader > start_leader)
4497 {
4498 --end_leader;
4499 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004500 {
4501#ifdef FEAT_FLOAT
4502 if (rettv->v_type == VAR_FLOAT)
4503 f = !f;
4504 else
4505#endif
4506 val = !val;
4507 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004508 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004509 {
4510#ifdef FEAT_FLOAT
4511 if (rettv->v_type == VAR_FLOAT)
4512 f = -f;
4513 else
4514#endif
4515 val = -val;
4516 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004517 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004518#ifdef FEAT_FLOAT
4519 if (rettv->v_type == VAR_FLOAT)
4520 {
4521 clear_tv(rettv);
4522 rettv->vval.v_float = f;
4523 }
4524 else
4525#endif
4526 {
4527 clear_tv(rettv);
4528 rettv->v_type = VAR_NUMBER;
4529 rettv->vval.v_number = val;
4530 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 }
4533
4534 return ret;
4535}
4536
4537/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004538 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4539 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004540 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4541 */
4542 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004543eval_index(
4544 char_u **arg,
4545 typval_T *rettv,
4546 int evaluate,
4547 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004548{
4549 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004550 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004551 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004552 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004553 long len = -1;
4554 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004555 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004556 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004557
Bram Moolenaara03f2332016-02-06 18:09:59 +01004558 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004559 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004560 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004561 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004562 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004563 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004564 return FAIL;
4565 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004566#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004567 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004568 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004569 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004570#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004571 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004572 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004573 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004574 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004575 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004576 return FAIL;
4577 case VAR_UNKNOWN:
4578 if (evaluate)
4579 return FAIL;
4580 /* FALLTHROUGH */
4581
4582 case VAR_STRING:
4583 case VAR_NUMBER:
4584 case VAR_LIST:
4585 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004586 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004587 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004588 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004589
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004590 init_tv(&var1);
4591 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004592 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004593 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004594 /*
4595 * dict.name
4596 */
4597 key = *arg + 1;
4598 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4599 ;
4600 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004601 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004602 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004603 }
4604 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004605 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004606 /*
4607 * something[idx]
4608 *
4609 * Get the (first) variable from inside the [].
4610 */
4611 *arg = skipwhite(*arg + 1);
4612 if (**arg == ':')
4613 empty1 = TRUE;
4614 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4615 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004616 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004617 {
4618 /* not a number or string */
4619 clear_tv(&var1);
4620 return FAIL;
4621 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004622
4623 /*
4624 * Get the second variable from inside the [:].
4625 */
4626 if (**arg == ':')
4627 {
4628 range = TRUE;
4629 *arg = skipwhite(*arg + 1);
4630 if (**arg == ']')
4631 empty2 = TRUE;
4632 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4633 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004634 if (!empty1)
4635 clear_tv(&var1);
4636 return FAIL;
4637 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004638 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004639 {
4640 /* not a number or string */
4641 if (!empty1)
4642 clear_tv(&var1);
4643 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004644 return FAIL;
4645 }
4646 }
4647
4648 /* Check for the ']'. */
4649 if (**arg != ']')
4650 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004651 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004652 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004653 clear_tv(&var1);
4654 if (range)
4655 clear_tv(&var2);
4656 return FAIL;
4657 }
4658 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004659 }
4660
4661 if (evaluate)
4662 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004663 n1 = 0;
4664 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004665 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004666 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004667 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004668 }
4669 if (range)
4670 {
4671 if (empty2)
4672 n2 = -1;
4673 else
4674 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004675 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004676 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004677 }
4678 }
4679
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004680 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004681 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004682 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004683 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004684 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004685 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004686 case VAR_SPECIAL:
4687 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004688 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004689 break; /* not evaluating, skipping over subscript */
4690
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004691 case VAR_NUMBER:
4692 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004693 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004694 len = (long)STRLEN(s);
4695 if (range)
4696 {
4697 /* The resulting variable is a substring. If the indexes
4698 * are out of range the result is empty. */
4699 if (n1 < 0)
4700 {
4701 n1 = len + n1;
4702 if (n1 < 0)
4703 n1 = 0;
4704 }
4705 if (n2 < 0)
4706 n2 = len + n2;
4707 else if (n2 >= len)
4708 n2 = len;
4709 if (n1 >= len || n2 < 0 || n1 > n2)
4710 s = NULL;
4711 else
4712 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4713 }
4714 else
4715 {
4716 /* The resulting variable is a string of a single
4717 * character. If the index is too big or negative the
4718 * result is empty. */
4719 if (n1 >= len || n1 < 0)
4720 s = NULL;
4721 else
4722 s = vim_strnsave(s + n1, 1);
4723 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004724 clear_tv(rettv);
4725 rettv->v_type = VAR_STRING;
4726 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004727 break;
4728
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004729 case VAR_BLOB:
4730 len = blob_len(rettv->vval.v_blob);
4731 if (range)
4732 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01004733 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004734 // are out of range the result is empty.
4735 if (n1 < 0)
4736 {
4737 n1 = len + n1;
4738 if (n1 < 0)
4739 n1 = 0;
4740 }
4741 if (n2 < 0)
4742 n2 = len + n2;
4743 else if (n2 >= len)
4744 n2 = len - 1;
4745 if (n1 >= len || n2 < 0 || n1 > n2)
4746 {
4747 clear_tv(rettv);
4748 rettv->v_type = VAR_BLOB;
4749 rettv->vval.v_blob = NULL;
4750 }
4751 else
4752 {
4753 blob_T *blob = blob_alloc();
4754
4755 if (blob != NULL)
4756 {
4757 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
4758 {
4759 blob_free(blob);
4760 return FAIL;
4761 }
4762 blob->bv_ga.ga_len = n2 - n1 + 1;
4763 for (i = n1; i <= n2; i++)
4764 blob_set(blob, i - n1,
4765 blob_get(rettv->vval.v_blob, i));
4766
4767 clear_tv(rettv);
4768 rettv_blob_set(rettv, blob);
4769 }
4770 }
4771 }
4772 else
4773 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01004774 // The resulting variable is a byte value.
4775 // If the index is too big or negative that is an error.
4776 if (n1 < 0)
4777 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004778 if (n1 < len && n1 >= 0)
4779 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01004780 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004781
4782 clear_tv(rettv);
4783 rettv->v_type = VAR_NUMBER;
4784 rettv->vval.v_number = v;
4785 }
4786 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004787 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004788 }
4789 break;
4790
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004791 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004792 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004793 if (n1 < 0)
4794 n1 = len + n1;
4795 if (!empty1 && (n1 < 0 || n1 >= len))
4796 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004797 /* For a range we allow invalid values and return an empty
4798 * list. A list index out of range is an error. */
4799 if (!range)
4800 {
4801 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004802 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004803 return FAIL;
4804 }
4805 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004806 }
4807 if (range)
4808 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004809 list_T *l;
4810 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004811
4812 if (n2 < 0)
4813 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004814 else if (n2 >= len)
4815 n2 = len - 1;
4816 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004817 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004818 l = list_alloc();
4819 if (l == NULL)
4820 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004821 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004822 n1 <= n2; ++n1)
4823 {
4824 if (list_append_tv(l, &item->li_tv) == FAIL)
4825 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004826 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004827 return FAIL;
4828 }
4829 item = item->li_next;
4830 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004831 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004832 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004833 }
4834 else
4835 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004836 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004837 clear_tv(rettv);
4838 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004839 }
4840 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004841
4842 case VAR_DICT:
4843 if (range)
4844 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004845 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004846 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004847 if (len == -1)
4848 clear_tv(&var1);
4849 return FAIL;
4850 }
4851 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004852 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004853
4854 if (len == -1)
4855 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004856 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02004857 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004858 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004859 clear_tv(&var1);
4860 return FAIL;
4861 }
4862 }
4863
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004864 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004865
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004866 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004867 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004868 if (len == -1)
4869 clear_tv(&var1);
4870 if (item == NULL)
4871 return FAIL;
4872
4873 copy_tv(&item->di_tv, &var1);
4874 clear_tv(rettv);
4875 *rettv = var1;
4876 }
4877 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004878 }
4879 }
4880
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004881 return OK;
4882}
4883
4884/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 * Get an option value.
4886 * "arg" points to the '&' or '+' before the option name.
4887 * "arg" is advanced to character after the option name.
4888 * Return OK or FAIL.
4889 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004890 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004891get_option_tv(
4892 char_u **arg,
4893 typval_T *rettv, /* when NULL, only check if option exists */
4894 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895{
4896 char_u *option_end;
4897 long numval;
4898 char_u *stringval;
4899 int opt_type;
4900 int c;
4901 int working = (**arg == '+'); /* has("+option") */
4902 int ret = OK;
4903 int opt_flags;
4904
4905 /*
4906 * Isolate the option name and find its value.
4907 */
4908 option_end = find_option_end(arg, &opt_flags);
4909 if (option_end == NULL)
4910 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004911 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004912 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 return FAIL;
4914 }
4915
4916 if (!evaluate)
4917 {
4918 *arg = option_end;
4919 return OK;
4920 }
4921
4922 c = *option_end;
4923 *option_end = NUL;
4924 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004925 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926
4927 if (opt_type == -3) /* invalid name */
4928 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004929 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004930 semsg(_("E113: Unknown option: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 ret = FAIL;
4932 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004933 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 {
4935 if (opt_type == -2) /* hidden string option */
4936 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004937 rettv->v_type = VAR_STRING;
4938 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 }
4940 else if (opt_type == -1) /* hidden number option */
4941 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004942 rettv->v_type = VAR_NUMBER;
4943 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 }
4945 else if (opt_type == 1) /* number option */
4946 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004947 rettv->v_type = VAR_NUMBER;
4948 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 }
4950 else /* string option */
4951 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004952 rettv->v_type = VAR_STRING;
4953 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 }
4955 }
4956 else if (working && (opt_type == -2 || opt_type == -1))
4957 ret = FAIL;
4958
4959 *option_end = c; /* put back for error messages */
4960 *arg = option_end;
4961
4962 return ret;
4963}
4964
4965/*
4966 * Allocate a variable for a string constant.
4967 * Return OK or FAIL.
4968 */
4969 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004970get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971{
4972 char_u *p;
4973 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 int extra = 0;
4975
4976 /*
4977 * Find the end of the string, skipping backslashed characters.
4978 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004979 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 {
4981 if (*p == '\\' && p[1] != NUL)
4982 {
4983 ++p;
4984 /* A "\<x>" form occupies at least 4 characters, and produces up
4985 * to 6 characters: reserve space for 2 extra */
4986 if (*p == '<')
4987 extra += 2;
4988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 }
4990
4991 if (*p != '"')
4992 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004993 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 return FAIL;
4995 }
4996
4997 /* If only parsing, set *arg and return here */
4998 if (!evaluate)
4999 {
5000 *arg = p + 1;
5001 return OK;
5002 }
5003
5004 /*
5005 * Copy the string into allocated memory, handling backslashed
5006 * characters.
5007 */
5008 name = alloc((unsigned)(p - *arg + extra));
5009 if (name == NULL)
5010 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005011 rettv->v_type = VAR_STRING;
5012 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013
Bram Moolenaar8c711452005-01-14 21:53:12 +00005014 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 {
5016 if (*p == '\\')
5017 {
5018 switch (*++p)
5019 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005020 case 'b': *name++ = BS; ++p; break;
5021 case 'e': *name++ = ESC; ++p; break;
5022 case 'f': *name++ = FF; ++p; break;
5023 case 'n': *name++ = NL; ++p; break;
5024 case 'r': *name++ = CAR; ++p; break;
5025 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026
5027 case 'X': /* hex: "\x1", "\x12" */
5028 case 'x':
5029 case 'u': /* Unicode: "\u0023" */
5030 case 'U':
5031 if (vim_isxdigit(p[1]))
5032 {
5033 int n, nr;
5034 int c = toupper(*p);
5035
5036 if (c == 'X')
5037 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005038 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005040 else
5041 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 nr = 0;
5043 while (--n >= 0 && vim_isxdigit(p[1]))
5044 {
5045 ++p;
5046 nr = (nr << 4) + hex2nr(*p);
5047 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005048 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 /* For "\u" store the number according to
5050 * 'encoding'. */
5051 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005052 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005054 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 break;
5057
5058 /* octal: "\1", "\12", "\123" */
5059 case '0':
5060 case '1':
5061 case '2':
5062 case '3':
5063 case '4':
5064 case '5':
5065 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005066 case '7': *name = *p++ - '0';
5067 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005069 *name = (*name << 3) + *p++ - '0';
5070 if (*p >= '0' && *p <= '7')
5071 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005073 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 break;
5075
5076 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005077 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 if (extra != 0)
5079 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005080 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 break;
5082 }
5083 /* FALLTHROUGH */
5084
Bram Moolenaar8c711452005-01-14 21:53:12 +00005085 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 break;
5087 }
5088 }
5089 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005090 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005093 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02005094 if (*p != NUL) /* just in case */
5095 ++p;
5096 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 return OK;
5099}
5100
5101/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005102 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 * Return OK or FAIL.
5104 */
5105 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005106get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107{
5108 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005109 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005110 int reduce = 0;
5111
5112 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005113 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005114 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005115 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005116 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005117 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005118 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005119 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005120 break;
5121 ++reduce;
5122 ++p;
5123 }
5124 }
5125
Bram Moolenaar8c711452005-01-14 21:53:12 +00005126 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005127 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005128 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005129 return FAIL;
5130 }
5131
Bram Moolenaar8c711452005-01-14 21:53:12 +00005132 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005133 if (!evaluate)
5134 {
5135 *arg = p + 1;
5136 return OK;
5137 }
5138
5139 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005140 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005141 */
5142 str = alloc((unsigned)((p - *arg) - reduce));
5143 if (str == NULL)
5144 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005145 rettv->v_type = VAR_STRING;
5146 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005147
Bram Moolenaar8c711452005-01-14 21:53:12 +00005148 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005149 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005150 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005151 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005152 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005153 break;
5154 ++p;
5155 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005156 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005157 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005158 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005159 *arg = p + 1;
5160
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005161 return OK;
5162}
5163
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005164/*
5165 * Return the function name of the partial.
5166 */
5167 char_u *
5168partial_name(partial_T *pt)
5169{
5170 if (pt->pt_name != NULL)
5171 return pt->pt_name;
5172 return pt->pt_func->uf_name;
5173}
5174
Bram Moolenaarddecc252016-04-06 22:59:37 +02005175 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005176partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005177{
5178 int i;
5179
5180 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005181 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005182 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005183 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005184 if (pt->pt_name != NULL)
5185 {
5186 func_unref(pt->pt_name);
5187 vim_free(pt->pt_name);
5188 }
5189 else
5190 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005191 vim_free(pt);
5192}
5193
5194/*
5195 * Unreference a closure: decrement the reference count and free it when it
5196 * becomes zero.
5197 */
5198 void
5199partial_unref(partial_T *pt)
5200{
5201 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005202 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005203}
5204
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005205static int tv_equal_recurse_limit;
5206
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005207 static int
5208func_equal(
5209 typval_T *tv1,
5210 typval_T *tv2,
5211 int ic) /* ignore case */
5212{
5213 char_u *s1, *s2;
5214 dict_T *d1, *d2;
5215 int a1, a2;
5216 int i;
5217
5218 /* empty and NULL function name considered the same */
5219 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005220 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005221 if (s1 != NULL && *s1 == NUL)
5222 s1 = NULL;
5223 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005224 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005225 if (s2 != NULL && *s2 == NUL)
5226 s2 = NULL;
5227 if (s1 == NULL || s2 == NULL)
5228 {
5229 if (s1 != s2)
5230 return FALSE;
5231 }
5232 else if (STRCMP(s1, s2) != 0)
5233 return FALSE;
5234
5235 /* empty dict and NULL dict is different */
5236 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5237 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5238 if (d1 == NULL || d2 == NULL)
5239 {
5240 if (d1 != d2)
5241 return FALSE;
5242 }
5243 else if (!dict_equal(d1, d2, ic, TRUE))
5244 return FALSE;
5245
5246 /* empty list and no list considered the same */
5247 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5248 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5249 if (a1 != a2)
5250 return FALSE;
5251 for (i = 0; i < a1; ++i)
5252 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5253 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5254 return FALSE;
5255
5256 return TRUE;
5257}
5258
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005259/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005260 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005261 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005262 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005263 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005264 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005265tv_equal(
5266 typval_T *tv1,
5267 typval_T *tv2,
5268 int ic, /* ignore case */
5269 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005270{
5271 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005272 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005273 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005274 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005275
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005276 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005277 * recursiveness to a limit. We guess they are equal then.
5278 * A fixed limit has the problem of still taking an awful long time.
5279 * Reduce the limit every time running into it. That should work fine for
5280 * deeply linked structures that are not recursively linked and catch
5281 * recursiveness quickly. */
5282 if (!recursive)
5283 tv_equal_recurse_limit = 1000;
5284 if (recursive_cnt >= tv_equal_recurse_limit)
5285 {
5286 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005287 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005288 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005289
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005290 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5291 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005292 if ((tv1->v_type == VAR_FUNC
5293 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5294 && (tv2->v_type == VAR_FUNC
5295 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5296 {
5297 ++recursive_cnt;
5298 r = func_equal(tv1, tv2, ic);
5299 --recursive_cnt;
5300 return r;
5301 }
5302
5303 if (tv1->v_type != tv2->v_type)
5304 return FALSE;
5305
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005306 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005307 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005308 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005309 ++recursive_cnt;
5310 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5311 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005312 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005313
5314 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005315 ++recursive_cnt;
5316 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5317 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005318 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005319
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005320 case VAR_BLOB:
5321 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
5322
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005323 case VAR_NUMBER:
5324 return tv1->vval.v_number == tv2->vval.v_number;
5325
5326 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005327 s1 = tv_get_string_buf(tv1, buf1);
5328 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005329 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005330
5331 case VAR_SPECIAL:
5332 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005333
5334 case VAR_FLOAT:
5335#ifdef FEAT_FLOAT
5336 return tv1->vval.v_float == tv2->vval.v_float;
5337#endif
5338 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005339#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005340 return tv1->vval.v_job == tv2->vval.v_job;
5341#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005342 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005343#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005344 return tv1->vval.v_channel == tv2->vval.v_channel;
5345#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005346 case VAR_FUNC:
5347 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005348 case VAR_UNKNOWN:
5349 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005350 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005351
Bram Moolenaara03f2332016-02-06 18:09:59 +01005352 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5353 * does not equal anything, not even itself. */
5354 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005355}
5356
5357/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005358 * Return the next (unique) copy ID.
5359 * Used for serializing nested structures.
5360 */
5361 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005362get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005363{
5364 current_copyID += COPYID_INC;
5365 return current_copyID;
5366}
5367
5368/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005369 * Garbage collection for lists and dictionaries.
5370 *
5371 * We use reference counts to be able to free most items right away when they
5372 * are no longer used. But for composite items it's possible that it becomes
5373 * unused while the reference count is > 0: When there is a recursive
5374 * reference. Example:
5375 * :let l = [1, 2, 3]
5376 * :let d = {9: l}
5377 * :let l[1] = d
5378 *
5379 * Since this is quite unusual we handle this with garbage collection: every
5380 * once in a while find out which lists and dicts are not referenced from any
5381 * variable.
5382 *
5383 * Here is a good reference text about garbage collection (refers to Python
5384 * but it applies to all reference-counting mechanisms):
5385 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005386 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005387
5388/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005389 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005390 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005391 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005392 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005393 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005394garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005395{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005396 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005397 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005398 buf_T *buf;
5399 win_T *wp;
5400 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005401 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005402 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005403
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005404 if (!testing)
5405 {
5406 /* Only do this once. */
5407 want_garbage_collect = FALSE;
5408 may_garbage_collect = FALSE;
5409 garbage_collect_at_exit = FALSE;
5410 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005411
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005412 /* We advance by two because we add one for items referenced through
5413 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005414 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005415
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005416 /*
5417 * 1. Go through all accessible variables and mark all lists and dicts
5418 * with copyID.
5419 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005420
5421 /* Don't free variables in the previous_funccal list unless they are only
5422 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005423 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005424 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005425
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005426 /* script-local variables */
5427 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005428 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005429
5430 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005431 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005432 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5433 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005434
5435 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005436 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005437 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5438 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005439 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005440 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5441 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005442
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005443 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005444 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005445 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5446 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005447 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005448 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005449
5450 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005451 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005452
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005453 /* named functions (matters for closures) */
5454 abort = abort || set_ref_in_functions(copyID);
5455
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005456 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005457 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005458
Bram Moolenaard812df62008-11-09 12:46:09 +00005459 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005460 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005461
Bram Moolenaar1dced572012-04-05 16:54:08 +02005462#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005463 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005464#endif
5465
Bram Moolenaardb913952012-06-29 12:54:53 +02005466#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005467 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005468#endif
5469
5470#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005471 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005472#endif
5473
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005474#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005475 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005476 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005477#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005478#ifdef FEAT_NETBEANS_INTG
5479 abort = abort || set_ref_in_nb_channel(copyID);
5480#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005481
Bram Moolenaare3188e22016-05-31 21:13:04 +02005482#ifdef FEAT_TIMERS
5483 abort = abort || set_ref_in_timer(copyID);
5484#endif
5485
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005486#ifdef FEAT_QUICKFIX
5487 abort = abort || set_ref_in_quickfix(copyID);
5488#endif
5489
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005490#ifdef FEAT_TERMINAL
5491 abort = abort || set_ref_in_term(copyID);
5492#endif
5493
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005494 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005495 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005496 /*
5497 * 2. Free lists and dictionaries that are not referenced.
5498 */
5499 did_free = free_unref_items(copyID);
5500
5501 /*
5502 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005503 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005504 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005505 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005506 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005507 else if (p_verbose > 0)
5508 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005509 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005510 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005511
5512 return did_free;
5513}
5514
5515/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005516 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005517 */
5518 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005519free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005520{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005521 int did_free = FALSE;
5522
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005523 /* Let all "free" functions know that we are here. This means no
5524 * dictionaries, lists, channels or jobs are to be freed, because we will
5525 * do that here. */
5526 in_free_unref_items = TRUE;
5527
5528 /*
5529 * PASS 1: free the contents of the items. We don't free the items
5530 * themselves yet, so that it is possible to decrement refcount counters
5531 */
5532
Bram Moolenaarcd524592016-07-17 14:57:05 +02005533 /* Go through the list of dicts and free items without the copyID. */
5534 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005535
Bram Moolenaarda861d62016-07-17 15:46:27 +02005536 /* Go through the list of lists and free items without the copyID. */
5537 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005538
5539#ifdef FEAT_JOB_CHANNEL
5540 /* Go through the list of jobs and free items without the copyID. This
5541 * must happen before doing channels, because jobs refer to channels, but
5542 * the reference from the channel to the job isn't tracked. */
5543 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5544
5545 /* Go through the list of channels and free items without the copyID. */
5546 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5547#endif
5548
5549 /*
5550 * PASS 2: free the items themselves.
5551 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005552 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005553 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005554
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005555#ifdef FEAT_JOB_CHANNEL
5556 /* Go through the list of jobs and free items without the copyID. This
5557 * must happen before doing channels, because jobs refer to channels, but
5558 * the reference from the channel to the job isn't tracked. */
5559 free_unused_jobs(copyID, COPYID_MASK);
5560
5561 /* Go through the list of channels and free items without the copyID. */
5562 free_unused_channels(copyID, COPYID_MASK);
5563#endif
5564
5565 in_free_unref_items = FALSE;
5566
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005567 return did_free;
5568}
5569
5570/*
5571 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005572 * "list_stack" is used to add lists to be marked. Can be NULL.
5573 *
5574 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005575 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005576 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005577set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005578{
5579 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005580 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005581 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005582 hashtab_T *cur_ht;
5583 ht_stack_T *ht_stack = NULL;
5584 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005585
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005586 cur_ht = ht;
5587 for (;;)
5588 {
5589 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005590 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005591 /* Mark each item in the hashtab. If the item contains a hashtab
5592 * it is added to ht_stack, if it contains a list it is added to
5593 * list_stack. */
5594 todo = (int)cur_ht->ht_used;
5595 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5596 if (!HASHITEM_EMPTY(hi))
5597 {
5598 --todo;
5599 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5600 &ht_stack, list_stack);
5601 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005602 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005603
5604 if (ht_stack == NULL)
5605 break;
5606
5607 /* take an item from the stack */
5608 cur_ht = ht_stack->ht;
5609 tempitem = ht_stack;
5610 ht_stack = ht_stack->prev;
5611 free(tempitem);
5612 }
5613
5614 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005615}
5616
5617/*
5618 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005619 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5620 *
5621 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005622 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005623 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005624set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005625{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005626 listitem_T *li;
5627 int abort = FALSE;
5628 list_T *cur_l;
5629 list_stack_T *list_stack = NULL;
5630 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005631
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005632 cur_l = l;
5633 for (;;)
5634 {
5635 if (!abort)
5636 /* Mark each item in the list. If the item contains a hashtab
5637 * it is added to ht_stack, if it contains a list it is added to
5638 * list_stack. */
5639 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5640 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5641 ht_stack, &list_stack);
5642 if (list_stack == NULL)
5643 break;
5644
5645 /* take an item from the stack */
5646 cur_l = list_stack->list;
5647 tempitem = list_stack;
5648 list_stack = list_stack->prev;
5649 free(tempitem);
5650 }
5651
5652 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005653}
5654
5655/*
5656 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005657 * "list_stack" is used to add lists to be marked. Can be NULL.
5658 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5659 *
5660 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005661 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005662 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005663set_ref_in_item(
5664 typval_T *tv,
5665 int copyID,
5666 ht_stack_T **ht_stack,
5667 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005668{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005669 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005670
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005671 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005672 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005673 dict_T *dd = tv->vval.v_dict;
5674
Bram Moolenaara03f2332016-02-06 18:09:59 +01005675 if (dd != NULL && dd->dv_copyID != copyID)
5676 {
5677 /* Didn't see this dict yet. */
5678 dd->dv_copyID = copyID;
5679 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005680 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005681 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5682 }
5683 else
5684 {
5685 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5686 if (newitem == NULL)
5687 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005688 else
5689 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005690 newitem->ht = &dd->dv_hashtab;
5691 newitem->prev = *ht_stack;
5692 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005693 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005694 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005695 }
5696 }
5697 else if (tv->v_type == VAR_LIST)
5698 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005699 list_T *ll = tv->vval.v_list;
5700
Bram Moolenaara03f2332016-02-06 18:09:59 +01005701 if (ll != NULL && ll->lv_copyID != copyID)
5702 {
5703 /* Didn't see this list yet. */
5704 ll->lv_copyID = copyID;
5705 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005706 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005707 abort = set_ref_in_list(ll, copyID, ht_stack);
5708 }
5709 else
5710 {
5711 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005712 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005713 if (newitem == NULL)
5714 abort = TRUE;
5715 else
5716 {
5717 newitem->list = ll;
5718 newitem->prev = *list_stack;
5719 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005720 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005721 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005722 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005723 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005724 else if (tv->v_type == VAR_FUNC)
5725 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005726 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005727 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005728 else if (tv->v_type == VAR_PARTIAL)
5729 {
5730 partial_T *pt = tv->vval.v_partial;
5731 int i;
5732
5733 /* A partial does not have a copyID, because it cannot contain itself.
5734 */
5735 if (pt != NULL)
5736 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005737 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005738
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005739 if (pt->pt_dict != NULL)
5740 {
5741 typval_T dtv;
5742
5743 dtv.v_type = VAR_DICT;
5744 dtv.vval.v_dict = pt->pt_dict;
5745 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5746 }
5747
5748 for (i = 0; i < pt->pt_argc; ++i)
5749 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5750 ht_stack, list_stack);
5751 }
5752 }
5753#ifdef FEAT_JOB_CHANNEL
5754 else if (tv->v_type == VAR_JOB)
5755 {
5756 job_T *job = tv->vval.v_job;
5757 typval_T dtv;
5758
5759 if (job != NULL && job->jv_copyID != copyID)
5760 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005761 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005762 if (job->jv_channel != NULL)
5763 {
5764 dtv.v_type = VAR_CHANNEL;
5765 dtv.vval.v_channel = job->jv_channel;
5766 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5767 }
5768 if (job->jv_exit_partial != NULL)
5769 {
5770 dtv.v_type = VAR_PARTIAL;
5771 dtv.vval.v_partial = job->jv_exit_partial;
5772 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5773 }
5774 }
5775 }
5776 else if (tv->v_type == VAR_CHANNEL)
5777 {
5778 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005779 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005780 typval_T dtv;
5781 jsonq_T *jq;
5782 cbq_T *cq;
5783
5784 if (ch != NULL && ch->ch_copyID != copyID)
5785 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005786 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005787 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005788 {
5789 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5790 jq = jq->jq_next)
5791 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5792 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5793 cq = cq->cq_next)
5794 if (cq->cq_partial != NULL)
5795 {
5796 dtv.v_type = VAR_PARTIAL;
5797 dtv.vval.v_partial = cq->cq_partial;
5798 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5799 }
5800 if (ch->ch_part[part].ch_partial != NULL)
5801 {
5802 dtv.v_type = VAR_PARTIAL;
5803 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5804 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5805 }
5806 }
5807 if (ch->ch_partial != NULL)
5808 {
5809 dtv.v_type = VAR_PARTIAL;
5810 dtv.vval.v_partial = ch->ch_partial;
5811 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5812 }
5813 if (ch->ch_close_partial != NULL)
5814 {
5815 dtv.v_type = VAR_PARTIAL;
5816 dtv.vval.v_partial = ch->ch_close_partial;
5817 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5818 }
5819 }
5820 }
5821#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005822 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005823}
5824
Bram Moolenaar17a13432016-01-24 14:22:10 +01005825 static char *
5826get_var_special_name(int nr)
5827{
5828 switch (nr)
5829 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005830 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005831 case VVAL_TRUE: return "v:true";
5832 case VVAL_NONE: return "v:none";
5833 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005834 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01005835 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01005836 return "42";
5837}
5838
Bram Moolenaar8c711452005-01-14 21:53:12 +00005839/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005840 * Return a string with the string representation of a variable.
5841 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005842 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005843 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005844 * When both "echo_style" and "composite_val" are FALSE, put quotes around
5845 * stings as "string()", otherwise does not put quotes around strings, as
5846 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005847 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5848 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005849 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005850 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005851 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005852echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005853 typval_T *tv,
5854 char_u **tofree,
5855 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005856 int copyID,
5857 int echo_style,
5858 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005859 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005860{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005861 static int recurse = 0;
5862 char_u *r = NULL;
5863
Bram Moolenaar33570922005-01-25 22:26:29 +00005864 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005865 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005866 if (!did_echo_string_emsg)
5867 {
5868 /* Only give this message once for a recursive call to avoid
5869 * flooding the user with errors. And stop iterating over lists
5870 * and dicts. */
5871 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005872 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005873 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005874 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005875 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005876 }
5877 ++recurse;
5878
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005879 switch (tv->v_type)
5880 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005881 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005882 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005883 {
5884 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005885 r = tv->vval.v_string;
5886 if (r == NULL)
5887 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005888 }
5889 else
5890 {
5891 *tofree = string_quote(tv->vval.v_string, FALSE);
5892 r = *tofree;
5893 }
5894 break;
5895
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005896 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005897 if (echo_style)
5898 {
5899 *tofree = NULL;
5900 r = tv->vval.v_string;
5901 }
5902 else
5903 {
5904 *tofree = string_quote(tv->vval.v_string, TRUE);
5905 r = *tofree;
5906 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005907 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005908
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005909 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005910 {
5911 partial_T *pt = tv->vval.v_partial;
5912 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005913 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005914 garray_T ga;
5915 int i;
5916 char_u *tf;
5917
5918 ga_init2(&ga, 1, 100);
5919 ga_concat(&ga, (char_u *)"function(");
5920 if (fname != NULL)
5921 {
5922 ga_concat(&ga, fname);
5923 vim_free(fname);
5924 }
5925 if (pt != NULL && pt->pt_argc > 0)
5926 {
5927 ga_concat(&ga, (char_u *)", [");
5928 for (i = 0; i < pt->pt_argc; ++i)
5929 {
5930 if (i > 0)
5931 ga_concat(&ga, (char_u *)", ");
5932 ga_concat(&ga,
5933 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5934 vim_free(tf);
5935 }
5936 ga_concat(&ga, (char_u *)"]");
5937 }
5938 if (pt != NULL && pt->pt_dict != NULL)
5939 {
5940 typval_T dtv;
5941
5942 ga_concat(&ga, (char_u *)", ");
5943 dtv.v_type = VAR_DICT;
5944 dtv.vval.v_dict = pt->pt_dict;
5945 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5946 vim_free(tf);
5947 }
5948 ga_concat(&ga, (char_u *)")");
5949
5950 *tofree = ga.ga_data;
5951 r = *tofree;
5952 break;
5953 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005954
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005955 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005956 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005957 break;
5958
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005959 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005960 if (tv->vval.v_list == NULL)
5961 {
5962 *tofree = NULL;
5963 r = NULL;
5964 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005965 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5966 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005967 {
5968 *tofree = NULL;
5969 r = (char_u *)"[...]";
5970 }
5971 else
5972 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005973 int old_copyID = tv->vval.v_list->lv_copyID;
5974
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005975 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005976 *tofree = list2string(tv, copyID, restore_copyID);
5977 if (restore_copyID)
5978 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005979 r = *tofree;
5980 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005981 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005982
Bram Moolenaar8c711452005-01-14 21:53:12 +00005983 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005984 if (tv->vval.v_dict == NULL)
5985 {
5986 *tofree = NULL;
5987 r = NULL;
5988 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005989 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5990 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005991 {
5992 *tofree = NULL;
5993 r = (char_u *)"{...}";
5994 }
5995 else
5996 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005997 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005998 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005999 *tofree = dict2string(tv, copyID, restore_copyID);
6000 if (restore_copyID)
6001 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006002 r = *tofree;
6003 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006004 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006005
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006006 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006007 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006008 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006009 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006010 break;
6011
Bram Moolenaar835dc632016-02-07 14:27:38 +01006012 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006013 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006014 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006015 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006016 if (composite_val)
6017 {
6018 *tofree = string_quote(r, FALSE);
6019 r = *tofree;
6020 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006021 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006022
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006023 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006024#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006025 *tofree = NULL;
6026 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6027 r = numbuf;
6028 break;
6029#endif
6030
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006031 case VAR_SPECIAL:
6032 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006033 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006034 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006035 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006036
Bram Moolenaar8502c702014-06-17 12:51:16 +02006037 if (--recurse == 0)
6038 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006039 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006040}
6041
6042/*
6043 * Return a string with the string representation of a variable.
6044 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6045 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006046 * Does not put quotes around strings, as ":echo" displays values.
6047 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6048 * May return NULL.
6049 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006050 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006051echo_string(
6052 typval_T *tv,
6053 char_u **tofree,
6054 char_u *numbuf,
6055 int copyID)
6056{
6057 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6058}
6059
6060/*
6061 * Return a string with the string representation of a variable.
6062 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6063 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006064 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006065 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006066 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02006067 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006068tv2string(
6069 typval_T *tv,
6070 char_u **tofree,
6071 char_u *numbuf,
6072 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006073{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006074 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006075}
6076
6077/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006078 * Return string "str" in ' quotes, doubling ' characters.
6079 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006080 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006081 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006082 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006083string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006084{
Bram Moolenaar33570922005-01-25 22:26:29 +00006085 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006086 char_u *p, *r, *s;
6087
Bram Moolenaar33570922005-01-25 22:26:29 +00006088 len = (function ? 13 : 3);
6089 if (str != NULL)
6090 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006091 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006092 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00006093 if (*p == '\'')
6094 ++len;
6095 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006096 s = r = alloc(len);
6097 if (r != NULL)
6098 {
6099 if (function)
6100 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006101 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006102 r += 10;
6103 }
6104 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006105 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006106 if (str != NULL)
6107 for (p = str; *p != NUL; )
6108 {
6109 if (*p == '\'')
6110 *r++ = '\'';
6111 MB_COPY_CHAR(p, r);
6112 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006113 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006114 if (function)
6115 *r++ = ')';
6116 *r++ = NUL;
6117 }
6118 return s;
6119}
6120
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006121#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006122/*
6123 * Convert the string "text" to a floating point number.
6124 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
6125 * this always uses a decimal point.
6126 * Returns the length of the text that was consumed.
6127 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006128 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006129string2float(
6130 char_u *text,
6131 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006132{
6133 char *s = (char *)text;
6134 float_T f;
6135
Bram Moolenaar62473612017-01-08 19:25:40 +01006136 /* MS-Windows does not deal with "inf" and "nan" properly. */
6137 if (STRNICMP(text, "inf", 3) == 0)
6138 {
6139 *value = INFINITY;
6140 return 3;
6141 }
6142 if (STRNICMP(text, "-inf", 3) == 0)
6143 {
6144 *value = -INFINITY;
6145 return 4;
6146 }
6147 if (STRNICMP(text, "nan", 3) == 0)
6148 {
6149 *value = NAN;
6150 return 3;
6151 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006152 f = strtod(s, &s);
6153 *value = f;
6154 return (int)((char_u *)s - text);
6155}
6156#endif
6157
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006158/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 * Get the value of an environment variable.
6160 * "arg" is pointing to the '$'. It is advanced to after the name.
6161 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006162 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 */
6164 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006165get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166{
6167 char_u *string = NULL;
6168 int len;
6169 int cc;
6170 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006171 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172
6173 ++*arg;
6174 name = *arg;
6175 len = get_env_len(arg);
6176 if (evaluate)
6177 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006178 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006179 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006180
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006181 cc = name[len];
6182 name[len] = NUL;
6183 /* first try vim_getenv(), fast for normal environment vars */
6184 string = vim_getenv(name, &mustfree);
6185 if (string != NULL && *string != NUL)
6186 {
6187 if (!mustfree)
6188 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006190 else
6191 {
6192 if (mustfree)
6193 vim_free(string);
6194
6195 /* next try expanding things like $VIM and ${HOME} */
6196 string = expand_env_save(name - 1);
6197 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01006198 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006199 }
6200 name[len] = cc;
6201
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006202 rettv->v_type = VAR_STRING;
6203 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 }
6205
6206 return OK;
6207}
6208
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006209
6210
6211/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006213 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006215 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006216var2fpos(
6217 typval_T *varp,
6218 int dollar_lnum, /* TRUE when $ is last line */
6219 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006221 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006223 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224
Bram Moolenaara5525202006-03-02 22:52:09 +00006225 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006226 if (varp->v_type == VAR_LIST)
6227 {
6228 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006229 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006230 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006231 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006232
6233 l = varp->vval.v_list;
6234 if (l == NULL)
6235 return NULL;
6236
6237 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006238 pos.lnum = list_find_nr(l, 0L, &error);
6239 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006240 return NULL; /* invalid line number */
6241
6242 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006243 pos.col = list_find_nr(l, 1L, &error);
6244 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006245 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006246 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006247
6248 /* We accept "$" for the column number: last column. */
6249 li = list_find(l, 1L);
6250 if (li != NULL && li->li_tv.v_type == VAR_STRING
6251 && li->li_tv.vval.v_string != NULL
6252 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6253 pos.col = len + 1;
6254
Bram Moolenaara5525202006-03-02 22:52:09 +00006255 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006256 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006257 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006258 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006259
Bram Moolenaara5525202006-03-02 22:52:09 +00006260 /* Get the virtual offset. Defaults to zero. */
6261 pos.coladd = list_find_nr(l, 2L, &error);
6262 if (error)
6263 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006264
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006265 return &pos;
6266 }
6267
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006268 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006269 if (name == NULL)
6270 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006271 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006273 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6274 {
6275 if (VIsual_active)
6276 return &VIsual;
6277 return &curwin->w_cursor;
6278 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006279 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006281 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6283 return NULL;
6284 return pp;
6285 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006286
Bram Moolenaara5525202006-03-02 22:52:09 +00006287 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006288
Bram Moolenaar477933c2007-07-17 14:32:23 +00006289 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006290 {
6291 pos.col = 0;
6292 if (name[1] == '0') /* "w0": first visible line */
6293 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006294 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006295 /* In silent Ex mode topline is zero, but that's not a valid line
6296 * number; use one instead. */
6297 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006298 return &pos;
6299 }
6300 else if (name[1] == '$') /* "w$": last visible line */
6301 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006302 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006303 /* In silent Ex mode botline is zero, return zero then. */
6304 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006305 return &pos;
6306 }
6307 }
6308 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006310 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 {
6312 pos.lnum = curbuf->b_ml.ml_line_count;
6313 pos.col = 0;
6314 }
6315 else
6316 {
6317 pos.lnum = curwin->w_cursor.lnum;
6318 pos.col = (colnr_T)STRLEN(ml_get_curline());
6319 }
6320 return &pos;
6321 }
6322 return NULL;
6323}
6324
6325/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006326 * Convert list in "arg" into a position and optional file number.
6327 * When "fnump" is NULL there is no file number, only 3 items.
6328 * Note that the column is passed on as-is, the caller may want to decrement
6329 * it to use 1 for the first column.
6330 * Return FAIL when conversion is not possible, doesn't check the position for
6331 * validity.
6332 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006333 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006334list2fpos(
6335 typval_T *arg,
6336 pos_T *posp,
6337 int *fnump,
6338 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006339{
6340 list_T *l = arg->vval.v_list;
6341 long i = 0;
6342 long n;
6343
Bram Moolenaar493c1782014-05-28 14:34:46 +02006344 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6345 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006346 if (arg->v_type != VAR_LIST
6347 || l == NULL
6348 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006349 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006350 return FAIL;
6351
6352 if (fnump != NULL)
6353 {
6354 n = list_find_nr(l, i++, NULL); /* fnum */
6355 if (n < 0)
6356 return FAIL;
6357 if (n == 0)
6358 n = curbuf->b_fnum; /* current buffer */
6359 *fnump = n;
6360 }
6361
6362 n = list_find_nr(l, i++, NULL); /* lnum */
6363 if (n < 0)
6364 return FAIL;
6365 posp->lnum = n;
6366
6367 n = list_find_nr(l, i++, NULL); /* col */
6368 if (n < 0)
6369 return FAIL;
6370 posp->col = n;
6371
Bram Moolenaar493c1782014-05-28 14:34:46 +02006372 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006373 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006374 posp->coladd = 0;
6375 else
6376 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006377
Bram Moolenaar493c1782014-05-28 14:34:46 +02006378 if (curswantp != NULL)
6379 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6380
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006381 return OK;
6382}
6383
6384/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 * Get the length of an environment variable name.
6386 * Advance "arg" to the first character after the name.
6387 * Return 0 for error.
6388 */
6389 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006390get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391{
6392 char_u *p;
6393 int len;
6394
6395 for (p = *arg; vim_isIDc(*p); ++p)
6396 ;
6397 if (p == *arg) /* no name found */
6398 return 0;
6399
6400 len = (int)(p - *arg);
6401 *arg = p;
6402 return len;
6403}
6404
6405/*
6406 * Get the length of the name of a function or internal variable.
6407 * "arg" is advanced to the first non-white character after the name.
6408 * Return 0 if something is wrong.
6409 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006410 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006411get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412{
6413 char_u *p;
6414 int len;
6415
6416 /* Find the end of the name. */
6417 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006418 {
6419 if (*p == ':')
6420 {
6421 /* "s:" is start of "s:var", but "n:" is not and can be used in
6422 * slice "[n:]". Also "xx:" is not a namespace. */
6423 len = (int)(p - *arg);
6424 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6425 || len > 1)
6426 break;
6427 }
6428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 if (p == *arg) /* no name found */
6430 return 0;
6431
6432 len = (int)(p - *arg);
6433 *arg = skipwhite(p);
6434
6435 return len;
6436}
6437
6438/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006439 * Get the length of the name of a variable or function.
6440 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006442 * Return -1 if curly braces expansion failed.
6443 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 * If the name contains 'magic' {}'s, expand them and return the
6445 * expanded name in an allocated string via 'alias' - caller must free.
6446 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006447 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006448get_name_len(
6449 char_u **arg,
6450 char_u **alias,
6451 int evaluate,
6452 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453{
6454 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 char_u *p;
6456 char_u *expr_start;
6457 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458
6459 *alias = NULL; /* default to no alias */
6460
6461 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6462 && (*arg)[2] == (int)KE_SNR)
6463 {
6464 /* hard coded <SNR>, already translated */
6465 *arg += 3;
6466 return get_id_len(arg) + 3;
6467 }
6468 len = eval_fname_script(*arg);
6469 if (len > 0)
6470 {
6471 /* literal "<SID>", "s:" or "<SNR>" */
6472 *arg += len;
6473 }
6474
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006476 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006478 p = find_name_end(*arg, &expr_start, &expr_end,
6479 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 if (expr_start != NULL)
6481 {
6482 char_u *temp_string;
6483
6484 if (!evaluate)
6485 {
6486 len += (int)(p - *arg);
6487 *arg = skipwhite(p);
6488 return len;
6489 }
6490
6491 /*
6492 * Include any <SID> etc in the expanded string:
6493 * Thus the -len here.
6494 */
6495 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6496 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006497 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 *alias = temp_string;
6499 *arg = skipwhite(p);
6500 return (int)STRLEN(temp_string);
6501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502
6503 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006504 // Only give an error when there is something, otherwise it will be
6505 // reported at a higher level.
6506 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006507 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508
6509 return len;
6510}
6511
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006512/*
6513 * Find the end of a variable or function name, taking care of magic braces.
6514 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6515 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006516 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006517 * Return a pointer to just after the name. Equal to "arg" if there is no
6518 * valid name.
6519 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006520 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006521find_name_end(
6522 char_u *arg,
6523 char_u **expr_start,
6524 char_u **expr_end,
6525 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006527 int mb_nest = 0;
6528 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006530 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006532 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006534 *expr_start = NULL;
6535 *expr_end = NULL;
6536 }
6537
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006538 /* Quick check for valid starting character. */
6539 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6540 return arg;
6541
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006542 for (p = arg; *p != NUL
6543 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006544 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006545 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006546 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006547 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006548 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006549 if (*p == '\'')
6550 {
6551 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006552 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006553 ;
6554 if (*p == NUL)
6555 break;
6556 }
6557 else if (*p == '"')
6558 {
6559 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006560 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006561 if (*p == '\\' && p[1] != NUL)
6562 ++p;
6563 if (*p == NUL)
6564 break;
6565 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006566 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6567 {
6568 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006569 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006570 len = (int)(p - arg);
6571 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006572 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006573 break;
6574 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006575
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006576 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006578 if (*p == '[')
6579 ++br_nest;
6580 else if (*p == ']')
6581 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006583
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006584 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006586 if (*p == '{')
6587 {
6588 mb_nest++;
6589 if (expr_start != NULL && *expr_start == NULL)
6590 *expr_start = p;
6591 }
6592 else if (*p == '}')
6593 {
6594 mb_nest--;
6595 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6596 *expr_end = p;
6597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 }
6600
6601 return p;
6602}
6603
6604/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006605 * Expands out the 'magic' {}'s in a variable/function name.
6606 * Note that this can call itself recursively, to deal with
6607 * constructs like foo{bar}{baz}{bam}
6608 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6609 * "in_start" ^
6610 * "expr_start" ^
6611 * "expr_end" ^
6612 * "in_end" ^
6613 *
6614 * Returns a new allocated string, which the caller must free.
6615 * Returns NULL for failure.
6616 */
6617 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006618make_expanded_name(
6619 char_u *in_start,
6620 char_u *expr_start,
6621 char_u *expr_end,
6622 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006623{
6624 char_u c1;
6625 char_u *retval = NULL;
6626 char_u *temp_result;
6627 char_u *nextcmd = NULL;
6628
6629 if (expr_end == NULL || in_end == NULL)
6630 return NULL;
6631 *expr_start = NUL;
6632 *expr_end = NUL;
6633 c1 = *in_end;
6634 *in_end = NUL;
6635
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006636 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006637 if (temp_result != NULL && nextcmd == NULL)
6638 {
6639 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6640 + (in_end - expr_end) + 1));
6641 if (retval != NULL)
6642 {
6643 STRCPY(retval, in_start);
6644 STRCAT(retval, temp_result);
6645 STRCAT(retval, expr_end + 1);
6646 }
6647 }
6648 vim_free(temp_result);
6649
6650 *in_end = c1; /* put char back for error messages */
6651 *expr_start = '{';
6652 *expr_end = '}';
6653
6654 if (retval != NULL)
6655 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006656 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006657 if (expr_start != NULL)
6658 {
6659 /* Further expansion! */
6660 temp_result = make_expanded_name(retval, expr_start,
6661 expr_end, temp_result);
6662 vim_free(retval);
6663 retval = temp_result;
6664 }
6665 }
6666
6667 return retval;
6668}
6669
6670/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006672 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006674 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006675eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006677 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6678}
6679
6680/*
6681 * Return TRUE if character "c" can be used as the first character in a
6682 * variable or function name (excluding '{' and '}').
6683 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006684 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006685eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006686{
6687 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688}
6689
6690/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 * Set number v: variable to "val".
6692 */
6693 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006694set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006696 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697}
6698
6699/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006700 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006702 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006703get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006705 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706}
6707
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006708/*
6709 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01006710 * If the String variable has never been set, return an empty string.
6711 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006712 */
6713 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006714get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006715{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006716 return tv_get_string(&vimvars[idx].vv_tv);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006717}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006718
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006720 * Get List v: variable value. Caller must take care of reference count when
6721 * needed.
6722 */
6723 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006724get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006725{
6726 return vimvars[idx].vv_list;
6727}
6728
6729/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006730 * Get Dict v: variable value. Caller must take care of reference count when
6731 * needed.
6732 */
6733 dict_T *
6734get_vim_var_dict(int idx)
6735{
6736 return vimvars[idx].vv_dict;
6737}
6738
6739/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006740 * Set v:char to character "c".
6741 */
6742 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006743set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006744{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006745 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006746
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006747 if (has_mbyte)
6748 buf[(*mb_char2bytes)(c, buf)] = NUL;
6749 else
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006750 {
6751 buf[0] = c;
6752 buf[1] = NUL;
6753 }
6754 set_vim_var_string(VV_CHAR, buf, -1);
6755}
6756
6757/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006758 * Set v:count to "count" and v:count1 to "count1".
6759 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 */
6761 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006762set_vcount(
6763 long count,
6764 long count1,
6765 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006767 if (set_prevcount)
6768 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006769 vimvars[VV_COUNT].vv_nr = count;
6770 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771}
6772
6773/*
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02006774 * Save variables that might be changed as a side effect. Used when executing
6775 * a timer callback.
6776 */
6777 void
6778save_vimvars(vimvars_save_T *vvsave)
6779{
6780 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
6781 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
6782 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
6783}
6784
6785/*
6786 * Restore variables saved by save_vimvars().
6787 */
6788 void
6789restore_vimvars(vimvars_save_T *vvsave)
6790{
6791 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
6792 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
6793 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
6794}
6795
6796/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 * Set string v: variable to a copy of "val".
6798 */
6799 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006800set_vim_var_string(
6801 int idx,
6802 char_u *val,
6803 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804{
Bram Moolenaara542c682016-01-31 16:28:04 +01006805 clear_tv(&vimvars[idx].vv_di.di_tv);
6806 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006808 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006810 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006812 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813}
6814
6815/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006816 * Set List v: variable to "val".
6817 */
6818 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006819set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006820{
Bram Moolenaara542c682016-01-31 16:28:04 +01006821 clear_tv(&vimvars[idx].vv_di.di_tv);
6822 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006823 vimvars[idx].vv_list = val;
6824 if (val != NULL)
6825 ++val->lv_refcount;
6826}
6827
6828/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006829 * Set Dictionary v: variable to "val".
6830 */
6831 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006832set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006833{
Bram Moolenaara542c682016-01-31 16:28:04 +01006834 clear_tv(&vimvars[idx].vv_di.di_tv);
6835 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006836 vimvars[idx].vv_dict = val;
6837 if (val != NULL)
6838 {
6839 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006840 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02006841 }
6842}
6843
6844/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 * Set v:register if needed.
6846 */
6847 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006848set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849{
6850 char_u regname;
6851
6852 if (c == 0 || c == ' ')
6853 regname = '"';
6854 else
6855 regname = c;
6856 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006857 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 set_vim_var_string(VV_REG, &regname, 1);
6859}
6860
6861/*
6862 * Get or set v:exception. If "oldval" == NULL, return the current value.
6863 * Otherwise, restore the value to "oldval" and return NULL.
6864 * Must always be called in pairs to save and restore v:exception! Does not
6865 * take care of memory allocations.
6866 */
6867 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006868v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869{
6870 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006871 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872
Bram Moolenaare9a41262005-01-15 22:18:47 +00006873 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 return NULL;
6875}
6876
6877/*
6878 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
6879 * Otherwise, restore the value to "oldval" and return NULL.
6880 * Must always be called in pairs to save and restore v:throwpoint! Does not
6881 * take care of memory allocations.
6882 */
6883 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006884v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885{
6886 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006887 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888
Bram Moolenaare9a41262005-01-15 22:18:47 +00006889 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 return NULL;
6891}
6892
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893/*
6894 * Set v:cmdarg.
6895 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6896 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6897 * Must always be called in pairs!
6898 */
6899 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006900set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901{
6902 char_u *oldval;
6903 char_u *newval;
6904 unsigned len;
6905
Bram Moolenaare9a41262005-01-15 22:18:47 +00006906 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006907 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006909 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006910 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006911 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 }
6913
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006914 if (eap->force_bin == FORCE_BIN)
6915 len = 6;
6916 else if (eap->force_bin == FORCE_NOBIN)
6917 len = 8;
6918 else
6919 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006920
6921 if (eap->read_edit)
6922 len += 7;
6923
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006924 if (eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006925 len += 10; /* " ++ff=unix" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006926 if (eap->force_enc != 0)
6927 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006928 if (eap->bad_char != 0)
6929 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006930
6931 newval = alloc(len + 1);
6932 if (newval == NULL)
6933 return NULL;
6934
6935 if (eap->force_bin == FORCE_BIN)
6936 sprintf((char *)newval, " ++bin");
6937 else if (eap->force_bin == FORCE_NOBIN)
6938 sprintf((char *)newval, " ++nobin");
6939 else
6940 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006941
6942 if (eap->read_edit)
6943 STRCAT(newval, " ++edit");
6944
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006945 if (eap->force_ff != 0)
6946 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006947 eap->force_ff == 'u' ? "unix"
6948 : eap->force_ff == 'd' ? "dos"
6949 : "mac");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006950 if (eap->force_enc != 0)
6951 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6952 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006953 if (eap->bad_char == BAD_KEEP)
6954 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6955 else if (eap->bad_char == BAD_DROP)
6956 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6957 else if (eap->bad_char != 0)
6958 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006959 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006960 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962
6963/*
6964 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006965 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006967 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006968get_var_tv(
6969 char_u *name,
6970 int len, /* length of "name" */
6971 typval_T *rettv, /* NULL when only checking existence */
6972 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
6973 int verbose, /* may give error message */
6974 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975{
6976 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006977 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006978 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980
6981 /* truncate the name, so that we can use strcmp() */
6982 cc = name[len];
6983 name[len] = NUL;
6984
6985 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 * Check for user-defined variables.
6987 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01006988 v = find_var(name, NULL, no_autoload);
6989 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006991 tv = &v->di_tv;
6992 if (dip != NULL)
6993 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 }
6995
Bram Moolenaare9a41262005-01-15 22:18:47 +00006996 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006998 if (rettv != NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006999 semsg(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 ret = FAIL;
7001 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007002 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007003 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004
7005 name[len] = cc;
7006
7007 return ret;
7008}
7009
7010/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007011 * Check if variable "name[len]" is a local variable or an argument.
7012 * If so, "*eval_lavars_used" is set to TRUE.
7013 */
7014 static void
7015check_vars(char_u *name, int len)
7016{
7017 int cc;
7018 char_u *varname;
7019 hashtab_T *ht;
7020
7021 if (eval_lavars_used == NULL)
7022 return;
7023
7024 /* truncate the name, so that we can use strcmp() */
7025 cc = name[len];
7026 name[len] = NUL;
7027
7028 ht = find_var_ht(name, &varname);
7029 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
7030 {
7031 if (find_var(name, NULL, TRUE) != NULL)
7032 *eval_lavars_used = TRUE;
7033 }
7034
7035 name[len] = cc;
7036}
7037
7038/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007039 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
7040 * Also handle function call with Funcref variable: func(expr)
7041 * Can all be combined: dict.func(expr)[idx]['func'](expr)
7042 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007043 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007044handle_subscript(
7045 char_u **arg,
7046 typval_T *rettv,
7047 int evaluate, /* do more than finding the end */
7048 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007049{
7050 int ret = OK;
7051 dict_T *selfdict = NULL;
7052 char_u *s;
7053 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007054 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007055
7056 while (ret == OK
7057 && (**arg == '['
7058 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007059 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7060 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01007061 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007062 {
7063 if (**arg == '(')
7064 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007065 partial_T *pt = NULL;
7066
Bram Moolenaard9fba312005-06-26 22:34:35 +00007067 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007068 if (evaluate)
7069 {
7070 functv = *rettv;
7071 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007072
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007073 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007074 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007075 {
7076 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007077 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007078 }
7079 else
7080 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007081 }
7082 else
7083 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007084 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00007085 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007086 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007087
7088 /* Clear the funcref afterwards, so that deleting it while
7089 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007090 if (evaluate)
7091 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007092
7093 /* Stop the expression evaluation when immediately aborting on
7094 * error, or when an interrupt occurred or an exception was thrown
7095 * but not caught. */
7096 if (aborting())
7097 {
7098 if (ret == OK)
7099 clear_tv(rettv);
7100 ret = FAIL;
7101 }
7102 dict_unref(selfdict);
7103 selfdict = NULL;
7104 }
7105 else /* **arg == '[' || **arg == '.' */
7106 {
7107 dict_unref(selfdict);
7108 if (rettv->v_type == VAR_DICT)
7109 {
7110 selfdict = rettv->vval.v_dict;
7111 if (selfdict != NULL)
7112 ++selfdict->dv_refcount;
7113 }
7114 else
7115 selfdict = NULL;
7116 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
7117 {
7118 clear_tv(rettv);
7119 ret = FAIL;
7120 }
7121 }
7122 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007123
Bram Moolenaar1d429612016-05-24 15:44:17 +02007124 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
7125 * Don't do this when "Func" is already a partial that was bound
7126 * explicitly (pt_auto is FALSE). */
7127 if (selfdict != NULL
7128 && (rettv->v_type == VAR_FUNC
7129 || (rettv->v_type == VAR_PARTIAL
7130 && (rettv->vval.v_partial->pt_auto
7131 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007132 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007133
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007134 dict_unref(selfdict);
7135 return ret;
7136}
7137
7138/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007139 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007140 * value).
7141 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01007142 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007143alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007144{
Bram Moolenaar33570922005-01-25 22:26:29 +00007145 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007146}
7147
7148/*
7149 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 * The string "s" must have been allocated, it is consumed.
7151 * Return NULL for out of memory, the variable otherwise.
7152 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007153 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007154alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155{
Bram Moolenaar33570922005-01-25 22:26:29 +00007156 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007158 rettv = alloc_tv();
7159 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007161 rettv->v_type = VAR_STRING;
7162 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 }
7164 else
7165 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007166 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167}
7168
7169/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007170 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007172 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007173free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174{
7175 if (varp != NULL)
7176 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007177 switch (varp->v_type)
7178 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007179 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007180 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007181 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007182 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007183 vim_free(varp->vval.v_string);
7184 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007185 case VAR_PARTIAL:
7186 partial_unref(varp->vval.v_partial);
7187 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007188 case VAR_BLOB:
7189 blob_unref(varp->vval.v_blob);
7190 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007191 case VAR_LIST:
7192 list_unref(varp->vval.v_list);
7193 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007194 case VAR_DICT:
7195 dict_unref(varp->vval.v_dict);
7196 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007197 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007198#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007199 job_unref(varp->vval.v_job);
7200 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007201#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007202 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007203#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007204 channel_unref(varp->vval.v_channel);
7205 break;
7206#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007207 case VAR_NUMBER:
7208 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007209 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007210 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007211 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 vim_free(varp);
7214 }
7215}
7216
7217/*
7218 * Free the memory for a variable value and set the value to NULL or 0.
7219 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007220 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007221clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222{
7223 if (varp != NULL)
7224 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007225 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007227 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007228 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007229 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007230 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007231 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007232 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007233 case VAR_PARTIAL:
7234 partial_unref(varp->vval.v_partial);
7235 varp->vval.v_partial = NULL;
7236 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007237 case VAR_BLOB:
7238 blob_unref(varp->vval.v_blob);
7239 varp->vval.v_blob = NULL;
7240 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007241 case VAR_LIST:
7242 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007243 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007244 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007245 case VAR_DICT:
7246 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007247 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007248 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007249 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007250 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007251 varp->vval.v_number = 0;
7252 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007253 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007254#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007255 varp->vval.v_float = 0.0;
7256 break;
7257#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007258 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007259#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007260 job_unref(varp->vval.v_job);
7261 varp->vval.v_job = NULL;
7262#endif
7263 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007264 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007265#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007266 channel_unref(varp->vval.v_channel);
7267 varp->vval.v_channel = NULL;
7268#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007269 case VAR_UNKNOWN:
7270 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007272 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 }
7274}
7275
7276/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007277 * Set the value of a variable to NULL without freeing items.
7278 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007279 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007280init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007281{
7282 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007283 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007284}
7285
7286/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 * Get the number value of a variable.
7288 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007289 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007290 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007291 * caller of incompatible types: it sets *denote to TRUE if "denote"
7292 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007294 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007295tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007297 int error = FALSE;
7298
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007299 return tv_get_number_chk(varp, &error); /* return 0L on error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007300}
7301
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007302 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007303tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007304{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007305 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007307 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007309 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007310 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007311 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007312#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007313 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007314 break;
7315#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007316 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007317 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007318 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007319 break;
7320 case VAR_STRING:
7321 if (varp->vval.v_string != NULL)
7322 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01007323 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007324 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007325 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007326 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007327 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007328 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007329 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007330 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007331 case VAR_SPECIAL:
7332 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7333 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007334 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007335#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007336 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007337 break;
7338#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007339 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007340#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007341 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007342 break;
7343#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007344 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007345 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007346 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007347 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007348 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007349 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007351 if (denote == NULL) /* useful for values that must be unsigned */
7352 n = -1;
7353 else
7354 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007355 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356}
7357
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007358#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007359 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007360tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007361{
7362 switch (varp->v_type)
7363 {
7364 case VAR_NUMBER:
7365 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007366 case VAR_FLOAT:
7367 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007368 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007369 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007370 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007371 break;
7372 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007373 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007374 break;
7375 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007376 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007377 break;
7378 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007379 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007380 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007381 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007382 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007383 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007384 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007385# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007386 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007387 break;
7388# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007389 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007390# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007391 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007392 break;
7393# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007394 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007395 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007396 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007397 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007398 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007399 break;
7400 }
7401 return 0;
7402}
7403#endif
7404
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 * Get the string value of a variable.
7407 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007408 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7409 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 * If the String variable has never been set, return an empty string.
7411 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007412 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007413 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007415 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007416tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417{
7418 static char_u mybuf[NUMBUFLEN];
7419
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007420 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421}
7422
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007423 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007424tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007426 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007427
7428 return res != NULL ? res : (char_u *)"";
7429}
7430
Bram Moolenaar7d647822014-04-05 21:28:56 +02007431/*
7432 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7433 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007434 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007435tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007436{
7437 static char_u mybuf[NUMBUFLEN];
7438
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007439 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007440}
7441
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007442 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007443tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007444{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007445 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007447 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007448 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007449 (long_long_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007450 return buf;
7451 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007452 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007453 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007454 break;
7455 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007456 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007457 break;
7458 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007459 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007460 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007461 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007462#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007463 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007464 break;
7465#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007466 case VAR_STRING:
7467 if (varp->vval.v_string != NULL)
7468 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007469 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007470 case VAR_SPECIAL:
7471 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7472 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007473 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007474 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007475 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007476 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007477#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007478 {
7479 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007480 char *status;
7481
7482 if (job == NULL)
7483 return (char_u *)"no process";
7484 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007485 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007486 : "run";
7487# ifdef UNIX
7488 vim_snprintf((char *)buf, NUMBUFLEN,
7489 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01007490# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007491 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007492 "process %ld %s",
7493 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007494 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007495# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007496 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007497 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7498# endif
7499 return buf;
7500 }
7501#endif
7502 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007503 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007504#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007505 {
7506 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007507 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007508
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007509 if (channel == NULL)
7510 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7511 else
7512 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007513 "channel %d %s", channel->ch_id, status);
7514 return buf;
7515 }
7516#endif
7517 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007518 case VAR_UNKNOWN:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007519 emsg(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007520 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007522 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523}
7524
7525/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007526 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
7527 * string() on Dict, List, etc.
7528 */
7529 char_u *
7530tv_stringify(typval_T *varp, char_u *buf)
7531{
7532 if (varp->v_type == VAR_LIST
7533 || varp->v_type == VAR_DICT
7534 || varp->v_type == VAR_FUNC
7535 || varp->v_type == VAR_PARTIAL
7536 || varp->v_type == VAR_FLOAT)
7537 {
7538 typval_T tmp;
7539
7540 f_string(varp, &tmp);
7541 tv_get_string_buf(&tmp, buf);
7542 clear_tv(varp);
7543 *varp = tmp;
7544 return tmp.vval.v_string;
7545 }
7546 return tv_get_string_buf(varp, buf);
7547}
7548
7549/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 * Find variable "name" in the list of variables.
7551 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007552 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007553 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007554 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007556 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007557find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007560 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007561 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562
Bram Moolenaara7043832005-01-21 11:56:39 +00007563 ht = find_var_ht(name, &varname);
7564 if (htp != NULL)
7565 *htp = ht;
7566 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007568 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7569 if (ret != NULL)
7570 return ret;
7571
7572 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007573 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574}
7575
7576/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007577 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007578 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007580 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007581find_var_in_ht(
7582 hashtab_T *ht,
7583 int htname,
7584 char_u *varname,
7585 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007586{
Bram Moolenaar33570922005-01-25 22:26:29 +00007587 hashitem_T *hi;
7588
7589 if (*varname == NUL)
7590 {
7591 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007592 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007593 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007594 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007595 case 'g': return &globvars_var;
7596 case 'v': return &vimvars_var;
7597 case 'b': return &curbuf->b_bufvar;
7598 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007599 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007600 case 'l': return get_funccal_local_var();
7601 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007602 }
7603 return NULL;
7604 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007605
7606 hi = hash_find(ht, varname);
7607 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007608 {
7609 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007610 * worked find the variable again. Don't auto-load a script if it was
7611 * loaded already, otherwise it would be loaded every time when
7612 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007613 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007614 {
7615 /* Note: script_autoload() may make "hi" invalid. It must either
7616 * be obtained again or not used. */
7617 if (!script_autoload(varname, FALSE) || aborting())
7618 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007619 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007620 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007621 if (HASHITEM_EMPTY(hi))
7622 return NULL;
7623 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007624 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007625}
7626
7627/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007628 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007629 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007630 * Set "varname" to the start of name without ':'.
7631 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007632 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007633find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007635 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007636 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007637
Bram Moolenaar73627d02015-08-11 15:46:09 +02007638 if (name[0] == NUL)
7639 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 if (name[1] != ':')
7641 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007642 /* The name must not start with a colon or #. */
7643 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 return NULL;
7645 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007646
7647 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007648 hi = hash_find(&compat_hashtab, name);
7649 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +00007650 return &compat_hashtab;
7651
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007652 ht = get_funccal_local_ht();
7653 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007654 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007655 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 }
7657 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007658 if (*name == 'g') /* global variable */
7659 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007660 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7661 */
7662 if (vim_strchr(name + 2, ':') != NULL
7663 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007664 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007666 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007668 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007669 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007670 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007671 if (*name == 'v') /* v: variable */
7672 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007673 if (*name == 'a') /* a: function argument */
7674 return get_funccal_args_ht();
7675 if (*name == 'l') /* l: local function variable */
7676 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 if (*name == 's' /* script variable */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007678 && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
7679 return &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 return NULL;
7681}
7682
7683/*
7684 * Get the string value of a (global/local) variable.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007685 * Note: see tv_get_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 * Returns NULL when it doesn't exist.
7687 */
7688 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007689get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690{
Bram Moolenaar33570922005-01-25 22:26:29 +00007691 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007693 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 if (v == NULL)
7695 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007696 return tv_get_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697}
7698
7699/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007700 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 * sourcing this script and when executing functions defined in the script.
7702 */
7703 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007704new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705{
Bram Moolenaara7043832005-01-21 11:56:39 +00007706 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007707 hashtab_T *ht;
7708 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007709
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7711 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007712 /* Re-allocating ga_data means that an ht_array pointing to
7713 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007714 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007715 for (i = 1; i <= ga_scripts.ga_len; ++i)
7716 {
7717 ht = &SCRIPT_VARS(i);
7718 if (ht->ht_mask == HT_INIT_SIZE - 1)
7719 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007720 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007721 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007722 }
7723
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 while (ga_scripts.ga_len < id)
7725 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007726 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007727 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007728 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 }
7731 }
7732}
7733
7734/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007735 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7736 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 */
7738 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007739init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740{
Bram Moolenaar33570922005-01-25 22:26:29 +00007741 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007742 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007743 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007744 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007745 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007746 dict_var->di_tv.vval.v_dict = dict;
7747 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007748 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007749 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7750 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751}
7752
7753/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007754 * Unreference a dictionary initialized by init_var_dict().
7755 */
7756 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007757unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007758{
7759 /* Now the dict needs to be freed if no one else is using it, go back to
7760 * normal reference counting. */
7761 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7762 dict_unref(dict);
7763}
7764
7765/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007767 * Frees all allocated variables and the value they contain.
7768 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 */
7770 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007771vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007772{
7773 vars_clear_ext(ht, TRUE);
7774}
7775
7776/*
7777 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7778 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007779 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007780vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781{
Bram Moolenaara7043832005-01-21 11:56:39 +00007782 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007783 hashitem_T *hi;
7784 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785
Bram Moolenaar33570922005-01-25 22:26:29 +00007786 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007787 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007788 for (hi = ht->ht_array; todo > 0; ++hi)
7789 {
7790 if (!HASHITEM_EMPTY(hi))
7791 {
7792 --todo;
7793
Bram Moolenaar33570922005-01-25 22:26:29 +00007794 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007795 * ht_array might change then. hash_clear() takes care of it
7796 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007797 v = HI2DI(hi);
7798 if (free_val)
7799 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007800 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007801 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007802 }
7803 }
7804 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007805 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806}
7807
Bram Moolenaara7043832005-01-21 11:56:39 +00007808/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007809 * Delete a variable from hashtab "ht" at item "hi".
7810 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007811 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007813delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814{
Bram Moolenaar33570922005-01-25 22:26:29 +00007815 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007816
7817 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007818 clear_tv(&di->di_tv);
7819 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820}
7821
7822/*
7823 * List the value of one internal variable.
7824 */
7825 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01007826list_one_var(dictitem_T *v, char *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007828 char_u *tofree;
7829 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007830 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007831
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007832 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007833 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007834 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007835 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836}
7837
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007839list_one_var_a(
Bram Moolenaar32526b32019-01-19 17:43:09 +01007840 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007841 char_u *name,
7842 int type,
7843 char_u *string,
7844 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845{
Bram Moolenaar31859182007-08-14 20:41:13 +00007846 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7847 msg_start();
7848 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 if (name != NULL) /* "a:" vars don't have a name stored */
Bram Moolenaar32526b32019-01-19 17:43:09 +01007850 msg_puts((char *)name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 msg_putchar(' ');
7852 msg_advance(22);
7853 if (type == VAR_NUMBER)
7854 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007855 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007856 msg_putchar('*');
7857 else if (type == VAR_LIST)
7858 {
7859 msg_putchar('[');
7860 if (*string == '[')
7861 ++string;
7862 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007863 else if (type == VAR_DICT)
7864 {
7865 msg_putchar('{');
7866 if (*string == '{')
7867 ++string;
7868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 else
7870 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007871
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007873
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007874 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007875 msg_puts("()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007876 if (*first)
7877 {
7878 msg_clr_eos();
7879 *first = FALSE;
7880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881}
7882
7883/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007884 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 * If the variable already exists, the value is updated.
7886 * Otherwise the variable is created.
7887 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007888 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007889set_var(
7890 char_u *name,
7891 typval_T *tv,
7892 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893{
Bram Moolenaar33570922005-01-25 22:26:29 +00007894 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007896 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007898 ht = find_var_ht(name, &varname);
7899 if (ht == NULL || *varname == NUL)
7900 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007901 semsg(_(e_illvar), name);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007902 return;
7903 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007904 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007905
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007906 /* Search in parent scope which is possible to reference from lambda */
7907 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007908 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007909
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007910 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7911 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007912 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007913
Bram Moolenaar33570922005-01-25 22:26:29 +00007914 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007916 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007917 if (var_check_ro(v->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007918 || var_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007919 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007920
7921 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007922 * Handle setting internal v: variables separately where needed to
7923 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007924 */
7925 if (ht == &vimvarht)
7926 {
7927 if (v->di_tv.v_type == VAR_STRING)
7928 {
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01007929 VIM_CLEAR(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007930 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01007931 {
7932 char_u *val = tv_get_string(tv);
7933
7934 // Careful: when assigning to v:errmsg and tv_get_string()
7935 // causes an error message the variable will alrady be set.
7936 if (v->di_tv.vval.v_string == NULL)
7937 v->di_tv.vval.v_string = vim_strsave(val);
7938 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007939 else
7940 {
7941 /* Take over the string to avoid an extra alloc/free. */
7942 v->di_tv.vval.v_string = tv->vval.v_string;
7943 tv->vval.v_string = NULL;
7944 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007945 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007946 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007947 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007948 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007949 v->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007950 if (STRCMP(varname, "searchforward") == 0)
7951 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007952#ifdef FEAT_SEARCH_EXTRA
7953 else if (STRCMP(varname, "hlsearch") == 0)
7954 {
7955 no_hlsearch = !v->di_tv.vval.v_number;
7956 redraw_all_later(SOME_VALID);
7957 }
7958#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007959 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007960 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007961 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01007962 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007963 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01007964 return;
7965 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007966 }
7967
7968 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 }
7970 else /* add a new variable */
7971 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01007972 // Can't add "v:" or "a:" variable.
7973 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007974 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007975 semsg(_(e_illvar), name);
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007976 return;
7977 }
7978
Bram Moolenaar31b81602019-02-10 22:14:27 +01007979 // Make sure the variable name is valid.
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007980 if (!valid_varname(varname))
7981 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00007982
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007983 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7984 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00007985 if (v == NULL)
7986 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007987 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00007988 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007990 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 return;
7992 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007993 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007995
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007996 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00007997 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007998 else
7999 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008000 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008001 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008002 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00008003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004}
8005
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008006/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008007 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00008008 * Also give an error message.
8009 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008010 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008011var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00008012{
8013 if (flags & DI_FLAGS_RO)
8014 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008015 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008016 return TRUE;
8017 }
8018 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
8019 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008020 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008021 return TRUE;
8022 }
8023 return FALSE;
8024}
8025
8026/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008027 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
8028 * Also give an error message.
8029 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008030 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008031var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008032{
8033 if (flags & DI_FLAGS_FIX)
8034 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008035 semsg(_("E795: Cannot delete variable %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008036 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008037 return TRUE;
8038 }
8039 return FALSE;
8040}
8041
8042/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008043 * Check if a funcref is assigned to a valid variable name.
8044 * Return TRUE and give an error if not.
8045 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008046 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008047var_check_func_name(
8048 char_u *name, /* points to start of variable name */
8049 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008050{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02008051 /* Allow for w: b: s: and t:. */
8052 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008053 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
8054 ? name[2] : name[0]))
8055 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008056 semsg(_("E704: Funcref variable name must start with a capital: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008057 name);
8058 return TRUE;
8059 }
8060 /* Don't allow hiding a function. When "v" is not NULL we might be
8061 * assigning another function to the same var, the type is checked
8062 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02008063 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008064 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008065 semsg(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008066 name);
8067 return TRUE;
8068 }
8069 return FALSE;
8070}
8071
8072/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008073 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02008074 * Also give an error message, using "name" or _("name") when use_gettext is
8075 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008076 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008077 int
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008078var_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008079{
8080 if (lock & VAR_LOCKED)
8081 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008082 semsg(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008083 name == NULL ? (char_u *)_("Unknown")
8084 : use_gettext ? (char_u *)_(name)
8085 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008086 return TRUE;
8087 }
8088 if (lock & VAR_FIXED)
8089 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008090 semsg(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008091 name == NULL ? (char_u *)_("Unknown")
8092 : use_gettext ? (char_u *)_(name)
8093 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008094 return TRUE;
8095 }
8096 return FALSE;
8097}
8098
8099/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008100 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
8101 * Also give an error message, using "name" or _("name") when use_gettext is
8102 * TRUE.
8103 */
8104 static int
8105tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
8106{
8107 int lock = 0;
8108
8109 switch (tv->v_type)
8110 {
8111 case VAR_BLOB:
8112 if (tv->vval.v_blob != NULL)
8113 lock = tv->vval.v_blob->bv_lock;
8114 break;
8115 case VAR_LIST:
8116 if (tv->vval.v_list != NULL)
8117 lock = tv->vval.v_list->lv_lock;
8118 break;
8119 case VAR_DICT:
8120 if (tv->vval.v_dict != NULL)
8121 lock = tv->vval.v_dict->dv_lock;
8122 break;
8123 default:
8124 break;
8125 }
8126 return var_check_lock(tv->v_lock, name, use_gettext)
8127 || (lock != 0 && var_check_lock(lock, name, use_gettext));
8128}
8129
8130/*
8131 * Check if a variable name is valid.
8132 * Return FALSE and give an error if not.
8133 */
8134 int
8135valid_varname(char_u *varname)
8136{
8137 char_u *p;
8138
8139 for (p = varname; *p != NUL; ++p)
8140 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
8141 && *p != AUTOLOAD_CHAR)
8142 {
8143 semsg(_(e_illvar), varname);
8144 return FALSE;
8145 }
8146 return TRUE;
8147}
8148
8149/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008150 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008151 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008152 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008153 * It is OK for "from" and "to" to point to the same item. This is used to
8154 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008155 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008156 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008157copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008159 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008160 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008161 switch (from->v_type)
8162 {
8163 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008164 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008165 to->vval.v_number = from->vval.v_number;
8166 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008167 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008168#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008169 to->vval.v_float = from->vval.v_float;
8170 break;
8171#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008172 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008173#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01008174 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01008175 if (to->vval.v_job != NULL)
8176 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01008177 break;
8178#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01008179 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008180#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01008181 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01008182 if (to->vval.v_channel != NULL)
8183 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01008184 break;
8185#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008186 case VAR_STRING:
8187 case VAR_FUNC:
8188 if (from->vval.v_string == NULL)
8189 to->vval.v_string = NULL;
8190 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008191 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008192 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008193 if (from->v_type == VAR_FUNC)
8194 func_ref(to->vval.v_string);
8195 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008196 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008197 case VAR_PARTIAL:
8198 if (from->vval.v_partial == NULL)
8199 to->vval.v_partial = NULL;
8200 else
8201 {
8202 to->vval.v_partial = from->vval.v_partial;
8203 ++to->vval.v_partial->pt_refcount;
8204 }
8205 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008206 case VAR_BLOB:
8207 if (from->vval.v_blob == NULL)
8208 to->vval.v_blob = NULL;
8209 else
8210 {
8211 to->vval.v_blob = from->vval.v_blob;
8212 ++to->vval.v_blob->bv_refcount;
8213 }
8214 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008215 case VAR_LIST:
8216 if (from->vval.v_list == NULL)
8217 to->vval.v_list = NULL;
8218 else
8219 {
8220 to->vval.v_list = from->vval.v_list;
8221 ++to->vval.v_list->lv_refcount;
8222 }
8223 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008224 case VAR_DICT:
8225 if (from->vval.v_dict == NULL)
8226 to->vval.v_dict = NULL;
8227 else
8228 {
8229 to->vval.v_dict = from->vval.v_dict;
8230 ++to->vval.v_dict->dv_refcount;
8231 }
8232 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008233 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008234 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008235 break;
8236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237}
8238
8239/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008240 * Make a copy of an item.
8241 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008242 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
8243 * reference to an already copied list/dict can be used.
8244 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008245 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008246 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008247item_copy(
8248 typval_T *from,
8249 typval_T *to,
8250 int deep,
8251 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008252{
8253 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008254 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008255
Bram Moolenaar33570922005-01-25 22:26:29 +00008256 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008257 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008258 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008259 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008260 }
8261 ++recurse;
8262
8263 switch (from->v_type)
8264 {
8265 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008266 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008267 case VAR_STRING:
8268 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008269 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008270 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008271 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008272 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008273 copy_tv(from, to);
8274 break;
8275 case VAR_LIST:
8276 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008277 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008278 if (from->vval.v_list == NULL)
8279 to->vval.v_list = NULL;
8280 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8281 {
8282 /* use the copy made earlier */
8283 to->vval.v_list = from->vval.v_list->lv_copylist;
8284 ++to->vval.v_list->lv_refcount;
8285 }
8286 else
8287 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8288 if (to->vval.v_list == NULL)
8289 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008290 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008291 case VAR_BLOB:
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008292 ret = blob_copy(from, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008293 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008294 case VAR_DICT:
8295 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008296 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008297 if (from->vval.v_dict == NULL)
8298 to->vval.v_dict = NULL;
8299 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8300 {
8301 /* use the copy made earlier */
8302 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8303 ++to->vval.v_dict->dv_refcount;
8304 }
8305 else
8306 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8307 if (to->vval.v_dict == NULL)
8308 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008309 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008310 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008311 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008312 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008313 }
8314 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008315 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008316}
8317
8318/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008319 * This function is used by f_input() and f_inputdialog() functions. The third
8320 * argument to f_input() specifies the type of completion to use at the
8321 * prompt. The third argument to f_inputdialog() specifies the value to return
8322 * when the user cancels the prompt.
8323 */
8324 void
8325get_user_input(
8326 typval_T *argvars,
8327 typval_T *rettv,
8328 int inputdialog,
8329 int secret)
8330{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008331 char_u *prompt = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008332 char_u *p = NULL;
8333 int c;
8334 char_u buf[NUMBUFLEN];
8335 int cmd_silent_save = cmd_silent;
8336 char_u *defstr = (char_u *)"";
8337 int xp_type = EXPAND_NOTHING;
8338 char_u *xp_arg = NULL;
8339
8340 rettv->v_type = VAR_STRING;
8341 rettv->vval.v_string = NULL;
8342
8343#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008344 /* While starting up, there is no place to enter text. When running tests
8345 * with --not-a-term we assume feedkeys() will be used. */
8346 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008347 return;
8348#endif
8349
8350 cmd_silent = FALSE; /* Want to see the prompt. */
8351 if (prompt != NULL)
8352 {
8353 /* Only the part of the message after the last NL is considered as
8354 * prompt for the command line */
8355 p = vim_strrchr(prompt, '\n');
8356 if (p == NULL)
8357 p = prompt;
8358 else
8359 {
8360 ++p;
8361 c = *p;
8362 *p = NUL;
8363 msg_start();
8364 msg_clr_eos();
Bram Moolenaar32526b32019-01-19 17:43:09 +01008365 msg_puts_attr((char *)prompt, echo_attr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008366 msg_didout = FALSE;
8367 msg_starthere();
8368 *p = c;
8369 }
8370 cmdline_row = msg_row;
8371
8372 if (argvars[1].v_type != VAR_UNKNOWN)
8373 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008374 defstr = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008375 if (defstr != NULL)
8376 stuffReadbuffSpec(defstr);
8377
8378 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8379 {
8380 char_u *xp_name;
8381 int xp_namelen;
8382 long argt;
8383
8384 /* input() with a third argument: completion */
8385 rettv->vval.v_string = NULL;
8386
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008387 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008388 if (xp_name == NULL)
8389 return;
8390
8391 xp_namelen = (int)STRLEN(xp_name);
8392
8393 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8394 &xp_arg) == FAIL)
8395 return;
8396 }
8397 }
8398
8399 if (defstr != NULL)
8400 {
8401 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02008402
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008403 ex_normal_busy = 0;
8404 rettv->vval.v_string =
8405 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8406 xp_type, xp_arg);
8407 ex_normal_busy = save_ex_normal_busy;
8408 }
8409 if (inputdialog && rettv->vval.v_string == NULL
8410 && argvars[1].v_type != VAR_UNKNOWN
8411 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008412 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008413 &argvars[2], buf));
8414
8415 vim_free(xp_arg);
8416
8417 /* since the user typed this, no need to wait for return */
8418 need_wait_return = FALSE;
8419 msg_didout = FALSE;
8420 }
8421 cmd_silent = cmd_silent_save;
8422}
8423
8424/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 * ":echo expr1 ..." print each argument separated with a space, add a
8426 * newline at the end.
8427 * ":echon expr1 ..." print each argument plain.
8428 */
8429 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008430ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431{
8432 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008433 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008434 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 char_u *p;
8436 int needclr = TRUE;
8437 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008438 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01008439 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008440 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441
8442 if (eap->skip)
8443 ++emsg_skip;
8444 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8445 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008446 /* If eval1() causes an error message the text from the command may
8447 * still need to be cleared. E.g., "echo 22,44". */
8448 need_clr_eos = needclr;
8449
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008451 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 {
8453 /*
8454 * Report the invalid expression unless the expression evaluation
8455 * has been cancelled due to an aborting error, an interrupt, or an
8456 * exception.
8457 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008458 if (!aborting() && did_emsg == did_emsg_before
8459 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008460 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008461 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 break;
8463 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008464 need_clr_eos = FALSE;
8465
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 if (!eap->skip)
8467 {
8468 if (atstart)
8469 {
8470 atstart = FALSE;
8471 /* Call msg_start() after eval1(), evaluating the expression
8472 * may cause a message to appear. */
8473 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008474 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008475 /* Mark the saved text as finishing the line, so that what
8476 * follows is displayed on a new line when scrolling back
8477 * at the more prompt. */
8478 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 }
8482 else if (eap->cmdidx == CMD_echo)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008483 msg_puts_attr(" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008484 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008485 if (p != NULL)
8486 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008488 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008490 if (*p != TAB && needclr)
8491 {
8492 /* remove any text still there from the command */
8493 msg_clr_eos();
8494 needclr = FALSE;
8495 }
8496 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 }
8498 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008499 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008500 if (has_mbyte)
8501 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008502 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008503
8504 (void)msg_outtrans_len_attr(p, i, echo_attr);
8505 p += i - 1;
8506 }
8507 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008508 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008511 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008513 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 arg = skipwhite(arg);
8515 }
8516 eap->nextcmd = check_nextcmd(arg);
8517
8518 if (eap->skip)
8519 --emsg_skip;
8520 else
8521 {
8522 /* remove text that may still be there from the command */
8523 if (needclr)
8524 msg_clr_eos();
8525 if (eap->cmdidx == CMD_echo)
8526 msg_end();
8527 }
8528}
8529
8530/*
8531 * ":echohl {name}".
8532 */
8533 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008534ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008536 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537}
8538
8539/*
8540 * ":execute expr1 ..." execute the result of an expression.
8541 * ":echomsg expr1 ..." Print a message
8542 * ":echoerr expr1 ..." Print an error
8543 * Each gets spaces around each argument and a newline at the end for
8544 * echo commands
8545 */
8546 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008547ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548{
8549 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008550 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 int ret = OK;
8552 char_u *p;
8553 garray_T ga;
8554 int len;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01008555 int save_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556
8557 ga_init2(&ga, 1, 80);
8558
8559 if (eap->skip)
8560 ++emsg_skip;
8561 while (*arg != NUL && *arg != '|' && *arg != '\n')
8562 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01008563 ret = eval1_emsg(&arg, &rettv, !eap->skip);
8564 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566
8567 if (!eap->skip)
8568 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008569 char_u buf[NUMBUFLEN];
8570
8571 if (eap->cmdidx == CMD_execute)
8572 p = tv_get_string_buf(&rettv, buf);
8573 else
8574 p = tv_stringify(&rettv, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 len = (int)STRLEN(p);
8576 if (ga_grow(&ga, len + 2) == FAIL)
8577 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008578 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 ret = FAIL;
8580 break;
8581 }
8582 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 ga.ga_len += len;
8586 }
8587
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008588 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 arg = skipwhite(arg);
8590 }
8591
8592 if (ret != FAIL && ga.ga_data != NULL)
8593 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008594 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8595 {
8596 /* Mark the already saved text as finishing the line, so that what
8597 * follows is displayed on a new line when scrolling back at the
8598 * more prompt. */
8599 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008600 }
8601
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008603 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01008604 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008605 out_flush();
8606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 else if (eap->cmdidx == CMD_echoerr)
8608 {
8609 /* We don't want to abort following commands, restore did_emsg. */
8610 save_did_emsg = did_emsg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008611 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 if (!force_abort)
8613 did_emsg = save_did_emsg;
8614 }
8615 else if (eap->cmdidx == CMD_execute)
8616 do_cmdline((char_u *)ga.ga_data,
8617 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8618 }
8619
8620 ga_clear(&ga);
8621
8622 if (eap->skip)
8623 --emsg_skip;
8624
8625 eap->nextcmd = check_nextcmd(arg);
8626}
8627
8628/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008629 * Find window specified by "vp" in tabpage "tp".
8630 */
8631 win_T *
8632find_win_by_nr(
8633 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01008634 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008635{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008636 win_T *wp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008637 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008638
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008639 if (nr < 0)
8640 return NULL;
8641 if (nr == 0)
8642 return curwin;
8643
Bram Moolenaar29323592016-07-24 22:04:11 +02008644 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8645 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008646 if (nr >= LOWEST_WIN_ID)
8647 {
8648 if (wp->w_id == nr)
8649 return wp;
8650 }
8651 else if (--nr <= 0)
8652 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008653 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008654 if (nr >= LOWEST_WIN_ID)
8655 return NULL;
8656 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008657}
8658
8659/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02008660 * Find a window: When using a Window ID in any tab page, when using a number
8661 * in the current tab page.
8662 */
8663 win_T *
8664find_win_by_nr_or_id(typval_T *vp)
8665{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008666 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02008667
8668 if (nr >= LOWEST_WIN_ID)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01008669 return win_id2wp(tv_get_number(vp));
Bram Moolenaare6e39892018-10-25 12:32:11 +02008670 return find_win_by_nr(vp, NULL);
8671}
8672
8673/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008674 * Find window specified by "wvp" in tabpage "tvp".
8675 */
8676 win_T *
8677find_tabwin(
8678 typval_T *wvp, /* VAR_UNKNOWN for current window */
8679 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
8680{
8681 win_T *wp = NULL;
8682 tabpage_T *tp = NULL;
8683 long n;
8684
8685 if (wvp->v_type != VAR_UNKNOWN)
8686 {
8687 if (tvp->v_type != VAR_UNKNOWN)
8688 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008689 n = (long)tv_get_number(tvp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008690 if (n >= 0)
8691 tp = find_tabpage(n);
8692 }
8693 else
8694 tp = curtab;
8695
8696 if (tp != NULL)
8697 wp = find_win_by_nr(wvp, tp);
8698 }
8699 else
8700 wp = curwin;
8701
8702 return wp;
8703}
8704
8705/*
8706 * getwinvar() and gettabwinvar()
8707 */
8708 void
8709getwinvar(
8710 typval_T *argvars,
8711 typval_T *rettv,
8712 int off) /* 1 for gettabwinvar() */
8713{
8714 win_T *win;
8715 char_u *varname;
8716 dictitem_T *v;
8717 tabpage_T *tp = NULL;
8718 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008719 win_T *oldcurwin;
8720 tabpage_T *oldtabpage;
8721 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008722
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008723 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008724 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008725 else
8726 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008727 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008728 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008729 ++emsg_off;
8730
8731 rettv->v_type = VAR_STRING;
8732 rettv->vval.v_string = NULL;
8733
8734 if (win != NULL && varname != NULL)
8735 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008736 /* Set curwin to be our win, temporarily. Also set the tabpage,
8737 * otherwise the window is not valid. Only do this when needed,
8738 * autocommands get blocked. */
8739 need_switch_win = !(tp == curtab && win == curwin);
8740 if (!need_switch_win
8741 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008742 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008743 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008744 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008745 if (varname[1] == NUL)
8746 {
8747 /* get all window-local options in a dict */
8748 dict_T *opts = get_winbuf_options(FALSE);
8749
8750 if (opts != NULL)
8751 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008752 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02008753 done = TRUE;
8754 }
8755 }
8756 else if (get_option_tv(&varname, rettv, 1) == OK)
8757 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008758 done = TRUE;
8759 }
8760 else
8761 {
8762 /* Look up the variable. */
8763 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8764 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8765 varname, FALSE);
8766 if (v != NULL)
8767 {
8768 copy_tv(&v->di_tv, rettv);
8769 done = TRUE;
8770 }
8771 }
8772 }
8773
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008774 if (need_switch_win)
8775 /* restore previous notion of curwin */
8776 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008777 }
8778
8779 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8780 /* use the default return value */
8781 copy_tv(&argvars[off + 2], rettv);
8782
8783 --emsg_off;
8784}
8785
8786/*
8787 * "setwinvar()" and "settabwinvar()" functions
8788 */
8789 void
8790setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8791{
8792 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008793 win_T *save_curwin;
8794 tabpage_T *save_curtab;
8795 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008796 char_u *varname, *winvarname;
8797 typval_T *varp;
8798 char_u nbuf[NUMBUFLEN];
8799 tabpage_T *tp = NULL;
8800
Bram Moolenaare0fb7d12019-02-12 20:48:10 +01008801 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008802 return;
8803
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008804 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008805 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008806 else
8807 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008808 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008809 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008810 varp = &argvars[off + 2];
8811
8812 if (win != NULL && varname != NULL && varp != NULL)
8813 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008814 need_switch_win = !(tp == curtab && win == curwin);
8815 if (!need_switch_win
8816 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008817 {
8818 if (*varname == '&')
8819 {
8820 long numval;
8821 char_u *strval;
8822 int error = FALSE;
8823
8824 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008825 numval = (long)tv_get_number_chk(varp, &error);
8826 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008827 if (!error && strval != NULL)
8828 set_option_value(varname, numval, strval, OPT_LOCAL);
8829 }
8830 else
8831 {
8832 winvarname = alloc((unsigned)STRLEN(varname) + 3);
8833 if (winvarname != NULL)
8834 {
8835 STRCPY(winvarname, "w:");
8836 STRCPY(winvarname + 2, varname);
8837 set_var(winvarname, varp, TRUE);
8838 vim_free(winvarname);
8839 }
8840 }
8841 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008842 if (need_switch_win)
8843 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008844 }
8845}
8846
8847/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8849 * "arg" points to the "&" or '+' when called, to "option" when returning.
8850 * Returns NULL when no option name found. Otherwise pointer to the char
8851 * after the option name.
8852 */
8853 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008854find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855{
8856 char_u *p = *arg;
8857
8858 ++p;
8859 if (*p == 'g' && p[1] == ':')
8860 {
8861 *opt_flags = OPT_GLOBAL;
8862 p += 2;
8863 }
8864 else if (*p == 'l' && p[1] == ':')
8865 {
8866 *opt_flags = OPT_LOCAL;
8867 p += 2;
8868 }
8869 else
8870 *opt_flags = 0;
8871
8872 if (!ASCII_ISALPHA(*p))
8873 return NULL;
8874 *arg = p;
8875
8876 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
8877 p += 4; /* termcap option */
8878 else
8879 while (ASCII_ISALPHA(*p))
8880 ++p;
8881 return p;
8882}
8883
8884/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008885 * Return the autoload script name for a function or variable name.
8886 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02008888 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008889autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02008890{
Bram Moolenaara1544c02013-05-30 12:35:52 +02008891 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008892 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02008893
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008894 /* Get the script file name: replace '#' with '/', append ".vim". */
8895 scriptname = alloc((unsigned)(STRLEN(name) + 14));
8896 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008897 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008898 STRCPY(scriptname, "autoload/");
8899 STRCAT(scriptname, name);
8900 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
8901 STRCAT(scriptname, ".vim");
8902 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
8903 *p = '/';
8904 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008905}
8906
8907/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008908 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008909 * Return TRUE if a package was loaded.
8910 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008911 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008912script_autoload(
8913 char_u *name,
8914 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008915{
8916 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008917 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008918 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008919 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008920
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008921 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008922 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008923 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008924 return FALSE;
8925
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008926 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008927
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008928 /* Find the name in the list of previously loaded package names. Skip
8929 * "autoload/", it's always the same. */
8930 for (i = 0; i < ga_loaded.ga_len; ++i)
8931 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
8932 break;
8933 if (!reload && i < ga_loaded.ga_len)
8934 ret = FALSE; /* was loaded already */
8935 else
8936 {
8937 /* Remember the name if it wasn't loaded already. */
8938 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
8939 {
8940 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
8941 tofree = NULL;
8942 }
8943
8944 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008945 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008946 ret = TRUE;
8947 }
8948
8949 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008950 return ret;
8951}
8952
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
8954typedef enum
8955{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008956 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
8957 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
8958 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959} var_flavour_T;
8960
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01008962var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963{
8964 char_u *p = varname;
8965
8966 if (ASCII_ISUPPER(*p))
8967 {
8968 while (*(++p))
8969 if (ASCII_ISLOWER(*p))
8970 return VAR_FLAVOUR_SESSION;
8971 return VAR_FLAVOUR_VIMINFO;
8972 }
8973 else
8974 return VAR_FLAVOUR_DEFAULT;
8975}
8976#endif
8977
8978#if defined(FEAT_VIMINFO) || defined(PROTO)
8979/*
8980 * Restore global vars that start with a capital from the viminfo file
8981 */
8982 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008983read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984{
8985 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008986 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00008987 typval_T tv;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02008988 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989
8990 if (!writing && (find_viminfo_parameter('!') != NULL))
8991 {
8992 tab = vim_strchr(virp->vir_line + 1, '\t');
8993 if (tab != NULL)
8994 {
8995 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008996 switch (*tab)
8997 {
8998 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008999#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009000 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009001#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009002 case 'D': type = VAR_DICT; break;
9003 case 'L': type = VAR_LIST; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009004 case 'B': type = VAR_BLOB; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009005 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007
9008 tab = vim_strchr(tab, '\t');
9009 if (tab != NULL)
9010 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009011 tv.v_type = type;
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009012 if (type == VAR_STRING || type == VAR_DICT
9013 || type == VAR_LIST || type == VAR_BLOB)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009014 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009016#ifdef FEAT_FLOAT
9017 else if (type == VAR_FLOAT)
9018 (void)string2float(tab + 1, &tv.vval.v_float);
9019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009021 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009022 if (type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009023 {
9024 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
9025
9026 if (etv == NULL)
9027 /* Failed to parse back the dict or list, use it as a
9028 * string. */
9029 tv.v_type = VAR_STRING;
9030 else
9031 {
9032 vim_free(tv.vval.v_string);
9033 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01009034 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009035 }
9036 }
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009037 else if (type == VAR_BLOB)
9038 {
9039 blob_T *blob = string2blob(tv.vval.v_string);
9040
9041 if (blob == NULL)
9042 // Failed to parse back the blob, use it as a string.
9043 tv.v_type = VAR_STRING;
9044 else
9045 {
9046 vim_free(tv.vval.v_string);
9047 tv.v_type = VAR_BLOB;
9048 tv.vval.v_blob = blob;
9049 }
9050 }
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009051
Bram Moolenaarb20e3342016-01-18 23:29:01 +01009052 /* when in a function use global variables */
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009053 save_funccal(&funccal_entry);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009054 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009055 restore_funccal();
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009056
9057 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009058 vim_free(tv.vval.v_string);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009059 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST ||
9060 tv.v_type == VAR_BLOB)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009061 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 }
9063 }
9064 }
9065
9066 return viminfo_readline(virp);
9067}
9068
9069/*
9070 * Write global vars that start with a capital to the viminfo file
9071 */
9072 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009073write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074{
Bram Moolenaar33570922005-01-25 22:26:29 +00009075 hashitem_T *hi;
9076 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009077 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01009078 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009079 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009080 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009081 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082
9083 if (find_viminfo_parameter('!') == NULL)
9084 return;
9085
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02009086 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00009087
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009088 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009089 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009091 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009093 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009094 this_var = HI2DI(hi);
9095 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009096 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009097 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00009098 {
9099 case VAR_STRING: s = "STR"; break;
9100 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009101 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009102 case VAR_DICT: s = "DIC"; break;
9103 case VAR_LIST: s = "LIS"; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009104 case VAR_BLOB: s = "BLO"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009105 case VAR_SPECIAL: s = "XPL"; break;
9106
9107 case VAR_UNKNOWN:
9108 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009109 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01009110 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01009111 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01009112 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00009113 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009114 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009115 if (this_var->di_tv.v_type == VAR_SPECIAL)
9116 {
9117 sprintf((char *)numbuf, "%ld",
9118 (long)this_var->di_tv.vval.v_number);
9119 p = numbuf;
9120 tofree = NULL;
9121 }
9122 else
9123 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009124 if (p != NULL)
9125 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00009126 vim_free(tofree);
9127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 }
9129 }
9130}
9131#endif
9132
9133#if defined(FEAT_SESSION) || defined(PROTO)
9134 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009135store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136{
Bram Moolenaar33570922005-01-25 22:26:29 +00009137 hashitem_T *hi;
9138 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009139 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140 char_u *p, *t;
9141
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009142 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009143 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009145 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009147 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009148 this_var = HI2DI(hi);
9149 if ((this_var->di_tv.v_type == VAR_NUMBER
9150 || this_var->di_tv.v_type == VAR_STRING)
9151 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009152 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009153 /* Escape special characters with a backslash. Turn a LF and
9154 * CR into \n and \r. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009155 p = vim_strsave_escaped(tv_get_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00009156 (char_u *)"\\\"\n\r");
9157 if (p == NULL) /* out of memory */
9158 break;
9159 for (t = p; *t != NUL; ++t)
9160 if (*t == '\n')
9161 *t = 'n';
9162 else if (*t == '\r')
9163 *t = 'r';
9164 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00009165 this_var->di_key,
9166 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9167 : ' ',
9168 p,
9169 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9170 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00009171 || put_eol(fd) == FAIL)
9172 {
9173 vim_free(p);
9174 return FAIL;
9175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 vim_free(p);
9177 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009178#ifdef FEAT_FLOAT
9179 else if (this_var->di_tv.v_type == VAR_FLOAT
9180 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
9181 {
9182 float_T f = this_var->di_tv.vval.v_float;
9183 int sign = ' ';
9184
9185 if (f < 0)
9186 {
9187 f = -f;
9188 sign = '-';
9189 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01009190 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009191 this_var->di_key, sign, f) < 0)
9192 || put_eol(fd) == FAIL)
9193 return FAIL;
9194 }
9195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 }
9197 }
9198 return OK;
9199}
9200#endif
9201
Bram Moolenaar661b1822005-07-28 22:36:45 +00009202/*
9203 * Display script name where an item was last set.
9204 * Should only be invoked when 'verbose' is non-zero.
9205 */
9206 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009207last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009208{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009209 char_u *p;
9210
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009211 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009212 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009213 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009214 if (p != NULL)
9215 {
9216 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01009217 msg_puts(_("\n\tLast set from "));
9218 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009219 if (script_ctx.sc_lnum > 0)
9220 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01009221 msg_puts(_(" line "));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009222 msg_outnum((long)script_ctx.sc_lnum);
9223 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009224 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009225 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009226 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00009227 }
9228}
9229
Bram Moolenaar61be3762019-03-19 23:04:17 +01009230/*
9231 * Reset v:option_new, v:option_old and v:option_type.
9232 */
Bram Moolenaar53744302015-07-17 17:38:22 +02009233 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009234reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02009235{
9236 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
9237 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
9238 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
9239}
9240
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009241/*
9242 * Prepare "gap" for an assert error and add the sourcing position.
9243 */
9244 void
9245prepare_assert_error(garray_T *gap)
9246{
9247 char buf[NUMBUFLEN];
9248
9249 ga_init2(gap, 1, 100);
9250 if (sourcing_name != NULL)
9251 {
9252 ga_concat(gap, sourcing_name);
9253 if (sourcing_lnum > 0)
9254 ga_concat(gap, (char_u *)" ");
9255 }
9256 if (sourcing_lnum > 0)
9257 {
9258 sprintf(buf, "line %ld", (long)sourcing_lnum);
9259 ga_concat(gap, (char_u *)buf);
9260 }
9261 if (sourcing_name != NULL || sourcing_lnum > 0)
9262 ga_concat(gap, (char_u *)": ");
9263}
9264
9265/*
9266 * Add an assert error to v:errors.
9267 */
9268 void
9269assert_error(garray_T *gap)
9270{
9271 struct vimvar *vp = &vimvars[VV_ERRORS];
9272
9273 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9274 /* Make sure v:errors is a list. */
9275 set_vim_var_list(VV_ERRORS, list_alloc());
9276 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9277}
9278
Bram Moolenaar65a54642018-04-28 16:56:53 +02009279 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009280assert_equal_common(typval_T *argvars, assert_type_T atype)
9281{
9282 garray_T ga;
9283
9284 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9285 != (atype == ASSERT_EQUAL))
9286 {
9287 prepare_assert_error(&ga);
9288 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9289 atype);
9290 assert_error(&ga);
9291 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009292 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009293 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009294 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009295}
9296
Bram Moolenaar65a54642018-04-28 16:56:53 +02009297 int
Bram Moolenaard96ff162018-02-18 22:13:29 +01009298assert_equalfile(typval_T *argvars)
9299{
9300 char_u buf1[NUMBUFLEN];
9301 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009302 char_u *fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
9303 char_u *fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009304 garray_T ga;
9305 FILE *fd1;
9306 FILE *fd2;
9307
9308 if (fname1 == NULL || fname2 == NULL)
Bram Moolenaar65a54642018-04-28 16:56:53 +02009309 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009310
9311 IObuff[0] = NUL;
9312 fd1 = mch_fopen((char *)fname1, READBIN);
9313 if (fd1 == NULL)
9314 {
9315 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname1);
9316 }
9317 else
9318 {
9319 fd2 = mch_fopen((char *)fname2, READBIN);
9320 if (fd2 == NULL)
9321 {
9322 fclose(fd1);
9323 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname2);
9324 }
9325 else
9326 {
9327 int c1, c2;
9328 long count = 0;
9329
9330 for (;;)
9331 {
9332 c1 = fgetc(fd1);
9333 c2 = fgetc(fd2);
9334 if (c1 == EOF)
9335 {
9336 if (c2 != EOF)
9337 STRCPY(IObuff, "first file is shorter");
9338 break;
9339 }
9340 else if (c2 == EOF)
9341 {
9342 STRCPY(IObuff, "second file is shorter");
9343 break;
9344 }
9345 else if (c1 != c2)
9346 {
9347 vim_snprintf((char *)IObuff, IOSIZE,
9348 "difference at byte %ld", count);
9349 break;
9350 }
9351 ++count;
9352 }
Bram Moolenaar30494182018-02-20 21:46:05 +01009353 fclose(fd1);
9354 fclose(fd2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009355 }
9356 }
9357 if (IObuff[0] != NUL)
9358 {
9359 prepare_assert_error(&ga);
9360 ga_concat(&ga, IObuff);
9361 assert_error(&ga);
9362 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009363 return 1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009364 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009365 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009366}
9367
Bram Moolenaar65a54642018-04-28 16:56:53 +02009368 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009369assert_match_common(typval_T *argvars, assert_type_T atype)
9370{
9371 garray_T ga;
9372 char_u buf1[NUMBUFLEN];
9373 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009374 char_u *pat = tv_get_string_buf_chk(&argvars[0], buf1);
9375 char_u *text = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009376
9377 if (pat == NULL || text == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009378 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009379 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
9380 {
9381 prepare_assert_error(&ga);
9382 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9383 atype);
9384 assert_error(&ga);
9385 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009386 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009387 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009388 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009389}
9390
Bram Moolenaar65a54642018-04-28 16:56:53 +02009391 int
Bram Moolenaar61c04492016-07-23 15:35:35 +02009392assert_inrange(typval_T *argvars)
9393{
9394 garray_T ga;
9395 int error = FALSE;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009396 char_u *tofree;
9397 char msg[200];
9398 char_u numbuf[NUMBUFLEN];
9399
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009400#ifdef FEAT_FLOAT
9401 if (argvars[0].v_type == VAR_FLOAT
9402 || argvars[1].v_type == VAR_FLOAT
9403 || argvars[2].v_type == VAR_FLOAT)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009404 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009405 float_T flower = tv_get_float(&argvars[0]);
9406 float_T fupper = tv_get_float(&argvars[1]);
9407 float_T factual = tv_get_float(&argvars[2]);
9408
9409 if (factual < flower || factual > fupper)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009410 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009411 prepare_assert_error(&ga);
9412 if (argvars[3].v_type != VAR_UNKNOWN)
9413 {
9414 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9415 vim_free(tofree);
9416 }
9417 else
9418 {
9419 vim_snprintf(msg, 200, "Expected range %g - %g, but got %g",
9420 flower, fupper, factual);
9421 ga_concat(&ga, (char_u *)msg);
9422 }
9423 assert_error(&ga);
9424 ga_clear(&ga);
9425 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009426 }
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009427 }
9428 else
9429#endif
9430 {
9431 varnumber_T lower = tv_get_number_chk(&argvars[0], &error);
9432 varnumber_T upper = tv_get_number_chk(&argvars[1], &error);
9433 varnumber_T actual = tv_get_number_chk(&argvars[2], &error);
9434
9435 if (error)
9436 return 0;
9437 if (actual < lower || actual > upper)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009438 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009439 prepare_assert_error(&ga);
9440 if (argvars[3].v_type != VAR_UNKNOWN)
9441 {
9442 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9443 vim_free(tofree);
9444 }
9445 else
9446 {
9447 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
Bram Moolenaar61c04492016-07-23 15:35:35 +02009448 (long)lower, (long)upper, (long)actual);
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009449 ga_concat(&ga, (char_u *)msg);
9450 }
9451 assert_error(&ga);
9452 ga_clear(&ga);
9453 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009454 }
Bram Moolenaar61c04492016-07-23 15:35:35 +02009455 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009456 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009457}
9458
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009459/*
9460 * Common for assert_true() and assert_false().
Bram Moolenaar65a54642018-04-28 16:56:53 +02009461 * Return non-zero for failure.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009462 */
Bram Moolenaar65a54642018-04-28 16:56:53 +02009463 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009464assert_bool(typval_T *argvars, int isTrue)
9465{
9466 int error = FALSE;
9467 garray_T ga;
9468
9469 if (argvars[0].v_type == VAR_SPECIAL
9470 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar65a54642018-04-28 16:56:53 +02009471 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009472 if (argvars[0].v_type != VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009473 || (tv_get_number_chk(&argvars[0], &error) == 0) == isTrue
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009474 || error)
9475 {
9476 prepare_assert_error(&ga);
9477 fill_assert_error(&ga, &argvars[1],
9478 (char_u *)(isTrue ? "True" : "False"),
9479 NULL, &argvars[0], ASSERT_OTHER);
9480 assert_error(&ga);
9481 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009482 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009483 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009484 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009485}
9486
Bram Moolenaar65a54642018-04-28 16:56:53 +02009487 int
Bram Moolenaar42205552017-03-18 19:42:22 +01009488assert_report(typval_T *argvars)
9489{
9490 garray_T ga;
9491
9492 prepare_assert_error(&ga);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009493 ga_concat(&ga, tv_get_string(&argvars[0]));
Bram Moolenaar42205552017-03-18 19:42:22 +01009494 assert_error(&ga);
9495 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009496 return 1;
Bram Moolenaar42205552017-03-18 19:42:22 +01009497}
9498
Bram Moolenaar65a54642018-04-28 16:56:53 +02009499 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009500assert_exception(typval_T *argvars)
9501{
9502 garray_T ga;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009503 char_u *error = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009504
9505 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9506 {
9507 prepare_assert_error(&ga);
9508 ga_concat(&ga, (char_u *)"v:exception is not set");
9509 assert_error(&ga);
9510 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009511 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009512 }
9513 else if (error != NULL
9514 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9515 {
9516 prepare_assert_error(&ga);
9517 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9518 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9519 assert_error(&ga);
9520 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009521 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009522 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009523 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009524}
9525
Bram Moolenaar65a54642018-04-28 16:56:53 +02009526 int
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009527assert_beeps(typval_T *argvars)
9528{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009529 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009530 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009531 int ret = 0;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009532
9533 called_vim_beep = FALSE;
9534 suppress_errthrow = TRUE;
9535 emsg_silent = FALSE;
9536 do_cmdline_cmd(cmd);
9537 if (!called_vim_beep)
9538 {
9539 prepare_assert_error(&ga);
9540 ga_concat(&ga, (char_u *)"command did not beep: ");
9541 ga_concat(&ga, cmd);
9542 assert_error(&ga);
9543 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009544 ret = 1;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009545 }
9546
9547 suppress_errthrow = FALSE;
9548 emsg_on_display = FALSE;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009549 return ret;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009550}
9551
Bram Moolenaar65a54642018-04-28 16:56:53 +02009552 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009553assert_fails(typval_T *argvars)
9554{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009555 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009556 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009557 int ret = 0;
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02009558 char_u numbuf[NUMBUFLEN];
9559 char_u *tofree;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009560
9561 called_emsg = FALSE;
9562 suppress_errthrow = TRUE;
9563 emsg_silent = TRUE;
9564 do_cmdline_cmd(cmd);
9565 if (!called_emsg)
9566 {
9567 prepare_assert_error(&ga);
9568 ga_concat(&ga, (char_u *)"command did not fail: ");
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02009569 if (argvars[1].v_type != VAR_UNKNOWN
9570 && argvars[2].v_type != VAR_UNKNOWN)
9571 {
9572 ga_concat(&ga, echo_string(&argvars[2], &tofree, numbuf, 0));
9573 vim_free(tofree);
9574 }
9575 else
9576 ga_concat(&ga, cmd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009577 assert_error(&ga);
9578 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009579 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009580 }
9581 else if (argvars[1].v_type != VAR_UNKNOWN)
9582 {
9583 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009584 char *error = (char *)tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009585
9586 if (error == NULL
9587 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9588 {
9589 prepare_assert_error(&ga);
9590 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9591 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
9592 assert_error(&ga);
9593 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009594 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009595 }
9596 }
9597
9598 called_emsg = FALSE;
9599 suppress_errthrow = FALSE;
9600 emsg_silent = FALSE;
9601 emsg_on_display = FALSE;
9602 set_vim_var_string(VV_ERRMSG, NULL, 0);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009603 return ret;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009604}
9605
9606/*
Bram Moolenaar86576712019-01-25 20:48:33 +01009607 * Append "p[clen]" to "gap", escaping unprintable characters.
9608 * Changes NL to \n, CR to \r, etc.
9609 */
9610 static void
9611ga_concat_esc(garray_T *gap, char_u *p, int clen)
9612{
9613 char_u buf[NUMBUFLEN];
9614
9615 if (clen > 1)
9616 {
9617 mch_memmove(buf, p, clen);
9618 buf[clen] = NUL;
9619 ga_concat(gap, buf);
9620 }
9621 else switch (*p)
9622 {
9623 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9624 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9625 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9626 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9627 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9628 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9629 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9630 default:
9631 if (*p < ' ')
9632 {
9633 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9634 ga_concat(gap, buf);
9635 }
9636 else
9637 ga_append(gap, *p);
9638 break;
9639 }
9640}
9641
9642/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009643 * Append "str" to "gap", escaping unprintable characters.
9644 * Changes NL to \n, CR to \r, etc.
9645 */
9646 static void
Bram Moolenaar86576712019-01-25 20:48:33 +01009647ga_concat_shorten_esc(garray_T *gap, char_u *str)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009648{
9649 char_u *p;
Bram Moolenaar86576712019-01-25 20:48:33 +01009650 char_u *s;
9651 int c;
9652 int clen;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009653 char_u buf[NUMBUFLEN];
Bram Moolenaar86576712019-01-25 20:48:33 +01009654 int same_len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009655
9656 if (str == NULL)
9657 {
9658 ga_concat(gap, (char_u *)"NULL");
9659 return;
9660 }
9661
9662 for (p = str; *p != NUL; ++p)
Bram Moolenaar86576712019-01-25 20:48:33 +01009663 {
9664 same_len = 1;
9665 s = p;
9666 c = mb_ptr2char_adv(&s);
9667 clen = s - p;
9668 while (*s != NUL && c == mb_ptr2char(s))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009669 {
Bram Moolenaar86576712019-01-25 20:48:33 +01009670 ++same_len;
9671 s += clen;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009672 }
Bram Moolenaar86576712019-01-25 20:48:33 +01009673 if (same_len > 20)
9674 {
9675 ga_concat(gap, (char_u *)"\\[");
9676 ga_concat_esc(gap, p, clen);
9677 ga_concat(gap, (char_u *)" occurs ");
9678 vim_snprintf((char *)buf, NUMBUFLEN, "%d", same_len);
9679 ga_concat(gap, buf);
9680 ga_concat(gap, (char_u *)" times]");
9681 p = s - 1;
9682 }
9683 else
9684 ga_concat_esc(gap, p, clen);
9685 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009686}
9687
9688/*
9689 * Fill "gap" with information about an assert error.
9690 */
9691 void
9692fill_assert_error(
9693 garray_T *gap,
9694 typval_T *opt_msg_tv,
9695 char_u *exp_str,
9696 typval_T *exp_tv,
9697 typval_T *got_tv,
9698 assert_type_T atype)
9699{
9700 char_u numbuf[NUMBUFLEN];
9701 char_u *tofree;
9702
9703 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9704 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009705 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9706 vim_free(tofree);
9707 ga_concat(gap, (char_u *)": ");
9708 }
9709
9710 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9711 ga_concat(gap, (char_u *)"Pattern ");
9712 else if (atype == ASSERT_NOTEQUAL)
9713 ga_concat(gap, (char_u *)"Expected not equal to ");
9714 else
9715 ga_concat(gap, (char_u *)"Expected ");
9716 if (exp_str == NULL)
9717 {
Bram Moolenaar86576712019-01-25 20:48:33 +01009718 ga_concat_shorten_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009719 vim_free(tofree);
9720 }
9721 else
Bram Moolenaar86576712019-01-25 20:48:33 +01009722 ga_concat_shorten_esc(gap, exp_str);
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009723 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009724 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009725 if (atype == ASSERT_MATCH)
9726 ga_concat(gap, (char_u *)" does not match ");
9727 else if (atype == ASSERT_NOTMATCH)
9728 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009729 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009730 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar86576712019-01-25 20:48:33 +01009731 ga_concat_shorten_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009732 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009733 }
9734}
9735
Bram Moolenaar31988702018-02-13 12:57:42 +01009736/*
9737 * Compare "typ1" and "typ2". Put the result in "typ1".
9738 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009739 int
9740typval_compare(
9741 typval_T *typ1, /* first operand */
9742 typval_T *typ2, /* second operand */
9743 exptype_T type, /* operator */
9744 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +01009745 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009746{
9747 int i;
9748 varnumber_T n1, n2;
9749 char_u *s1, *s2;
9750 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
9751
Bram Moolenaar31988702018-02-13 12:57:42 +01009752 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009753 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009754 /* For "is" a different type always means FALSE, for "notis"
9755 * it means TRUE. */
9756 n1 = (type == TYPE_NEQUAL);
9757 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009758 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
9759 {
9760 if (type_is)
9761 {
9762 n1 = (typ1->v_type == typ2->v_type
9763 && typ1->vval.v_blob == typ2->vval.v_blob);
9764 if (type == TYPE_NEQUAL)
9765 n1 = !n1;
9766 }
9767 else if (typ1->v_type != typ2->v_type
9768 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9769 {
9770 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009771 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009772 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009773 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009774 clear_tv(typ1);
9775 return FAIL;
9776 }
9777 else
9778 {
9779 // Compare two Blobs for being equal or unequal.
9780 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
9781 if (type == TYPE_NEQUAL)
9782 n1 = !n1;
9783 }
9784 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009785 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
9786 {
9787 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009788 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009789 n1 = (typ1->v_type == typ2->v_type
9790 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009791 if (type == TYPE_NEQUAL)
9792 n1 = !n1;
9793 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009794 else if (typ1->v_type != typ2->v_type
9795 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009796 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009797 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009798 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009799 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009800 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009801 clear_tv(typ1);
9802 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009803 }
9804 else
9805 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009806 /* Compare two Lists for being equal or unequal. */
9807 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
9808 ic, FALSE);
9809 if (type == TYPE_NEQUAL)
9810 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009811 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009812 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009813
9814 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
9815 {
9816 if (type_is)
9817 {
9818 n1 = (typ1->v_type == typ2->v_type
9819 && typ1->vval.v_dict == typ2->vval.v_dict);
9820 if (type == TYPE_NEQUAL)
9821 n1 = !n1;
9822 }
9823 else if (typ1->v_type != typ2->v_type
9824 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9825 {
9826 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009827 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009828 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009829 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009830 clear_tv(typ1);
9831 return FAIL;
9832 }
9833 else
9834 {
9835 /* Compare two Dictionaries for being equal or unequal. */
9836 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
9837 ic, FALSE);
9838 if (type == TYPE_NEQUAL)
9839 n1 = !n1;
9840 }
9841 }
9842
9843 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
9844 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
9845 {
9846 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
9847 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009848 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009849 clear_tv(typ1);
9850 return FAIL;
9851 }
9852 if ((typ1->v_type == VAR_PARTIAL
9853 && typ1->vval.v_partial == NULL)
9854 || (typ2->v_type == VAR_PARTIAL
9855 && typ2->vval.v_partial == NULL))
9856 /* when a partial is NULL assume not equal */
9857 n1 = FALSE;
9858 else if (type_is)
9859 {
9860 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
9861 /* strings are considered the same if their value is
9862 * the same */
9863 n1 = tv_equal(typ1, typ2, ic, FALSE);
9864 else if (typ1->v_type == VAR_PARTIAL
9865 && typ2->v_type == VAR_PARTIAL)
9866 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
9867 else
9868 n1 = FALSE;
9869 }
9870 else
9871 n1 = tv_equal(typ1, typ2, ic, FALSE);
9872 if (type == TYPE_NEQUAL)
9873 n1 = !n1;
9874 }
9875
9876#ifdef FEAT_FLOAT
9877 /*
9878 * If one of the two variables is a float, compare as a float.
9879 * When using "=~" or "!~", always compare as string.
9880 */
9881 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
9882 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9883 {
9884 float_T f1, f2;
9885
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009886 f1 = tv_get_float(typ1);
9887 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009888 n1 = FALSE;
9889 switch (type)
9890 {
9891 case TYPE_EQUAL: n1 = (f1 == f2); break;
9892 case TYPE_NEQUAL: n1 = (f1 != f2); break;
9893 case TYPE_GREATER: n1 = (f1 > f2); break;
9894 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
9895 case TYPE_SMALLER: n1 = (f1 < f2); break;
9896 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
9897 case TYPE_UNKNOWN:
9898 case TYPE_MATCH:
9899 case TYPE_NOMATCH: break; /* avoid gcc warning */
9900 }
9901 }
9902#endif
9903
9904 /*
9905 * If one of the two variables is a number, compare as a number.
9906 * When using "=~" or "!~", always compare as string.
9907 */
9908 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
9909 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9910 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009911 n1 = tv_get_number(typ1);
9912 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009913 switch (type)
9914 {
9915 case TYPE_EQUAL: n1 = (n1 == n2); break;
9916 case TYPE_NEQUAL: n1 = (n1 != n2); break;
9917 case TYPE_GREATER: n1 = (n1 > n2); break;
9918 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
9919 case TYPE_SMALLER: n1 = (n1 < n2); break;
9920 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
9921 case TYPE_UNKNOWN:
9922 case TYPE_MATCH:
9923 case TYPE_NOMATCH: break; /* avoid gcc warning */
9924 }
9925 }
9926 else
9927 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009928 s1 = tv_get_string_buf(typ1, buf1);
9929 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009930 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
9931 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
9932 else
9933 i = 0;
9934 n1 = FALSE;
9935 switch (type)
9936 {
9937 case TYPE_EQUAL: n1 = (i == 0); break;
9938 case TYPE_NEQUAL: n1 = (i != 0); break;
9939 case TYPE_GREATER: n1 = (i > 0); break;
9940 case TYPE_GEQUAL: n1 = (i >= 0); break;
9941 case TYPE_SMALLER: n1 = (i < 0); break;
9942 case TYPE_SEQUAL: n1 = (i <= 0); break;
9943
9944 case TYPE_MATCH:
9945 case TYPE_NOMATCH:
9946 n1 = pattern_match(s2, s1, ic);
9947 if (type == TYPE_NOMATCH)
9948 n1 = !n1;
9949 break;
9950
9951 case TYPE_UNKNOWN: break; /* avoid gcc warning */
9952 }
9953 }
9954 clear_tv(typ1);
9955 typ1->v_type = VAR_NUMBER;
9956 typ1->vval.v_number = n1;
9957
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009958 return OK;
9959}
9960
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009961 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02009962typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009963{
9964 char_u *tofree;
9965 char_u numbuf[NUMBUFLEN];
9966 char_u *ret = NULL;
9967
9968 if (arg == NULL)
9969 return vim_strsave((char_u *)"(does not exist)");
9970 ret = tv2string(arg, &tofree, numbuf, 0);
9971 /* Make a copy if we have a value but it's not in allocated memory. */
9972 if (ret != NULL && tofree == NULL)
9973 ret = vim_strsave(ret);
9974 return ret;
9975}
9976
9977 int
9978var_exists(char_u *var)
9979{
9980 char_u *name;
9981 char_u *tofree;
9982 typval_T tv;
9983 int len = 0;
9984 int n = FALSE;
9985
9986 /* get_name_len() takes care of expanding curly braces */
9987 name = var;
9988 len = get_name_len(&var, &tofree, TRUE, FALSE);
9989 if (len > 0)
9990 {
9991 if (tofree != NULL)
9992 name = tofree;
9993 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
9994 if (n)
9995 {
9996 /* handle d.key, l[idx], f(expr) */
9997 n = (handle_subscript(&var, &tv, TRUE, FALSE) == OK);
9998 if (n)
9999 clear_tv(&tv);
10000 }
10001 }
10002 if (*var != NUL)
10003 n = FALSE;
10004
10005 vim_free(tofree);
10006 return n;
10007}
10008
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009#endif /* FEAT_EVAL */
10010
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010012#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013
Bram Moolenaar4f974752019-02-17 17:44:42 +010010014#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015/*
10016 * Functions for ":8" filename modifier: get 8.3 version of a filename.
10017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018
10019/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010020 * Get the short path (8.3) for the filename in "fnamep".
10021 * Only works for a valid file name.
10022 * When the path gets longer "fnamep" is changed and the allocated buffer
10023 * is put in "bufp".
10024 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
10025 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 */
10027 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010028get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010030 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 char_u *newbuf;
10032
10033 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010034 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 if (l > len - 1)
10036 {
10037 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010038 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039 newbuf = vim_strnsave(*fnamep, l);
10040 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010041 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042
10043 vim_free(*bufp);
10044 *fnamep = *bufp = newbuf;
10045
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010046 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010047 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048 }
10049
10050 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010051 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052}
10053
10054/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010055 * Get the short path (8.3) for the filename in "fname". The converted
10056 * path is returned in "bufp".
10057 *
10058 * Some of the directories specified in "fname" may not exist. This function
10059 * will shorten the existing directories at the beginning of the path and then
10060 * append the remaining non-existing path.
10061 *
10062 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020010063 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010064 * bufp - Pointer to an allocated buffer for the filename.
10065 * fnamelen - Length of the filename pointed to by fname
10066 *
10067 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068 */
10069 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010070shortpath_for_invalid_fname(
10071 char_u **fname,
10072 char_u **bufp,
10073 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010075 char_u *short_fname, *save_fname, *pbuf_unused;
10076 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010078 int old_len, len;
10079 int new_len, sfx_len;
10080 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081
10082 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010083 old_len = *fnamelen;
10084 save_fname = vim_strnsave(*fname, old_len);
10085 pbuf_unused = NULL;
10086 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010088 endp = save_fname + old_len - 1; /* Find the end of the copy */
10089 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010091 /*
10092 * Try shortening the supplied path till it succeeds by removing one
10093 * directory at a time from the tail of the path.
10094 */
10095 len = 0;
10096 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010098 /* go back one path-separator */
10099 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
10100 --endp;
10101 if (endp <= save_fname)
10102 break; /* processed the complete path */
10103
10104 /*
10105 * Replace the path separator with a NUL and try to shorten the
10106 * resulting path.
10107 */
10108 ch = *endp;
10109 *endp = 0;
10110 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010111 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010112 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
10113 {
10114 retval = FAIL;
10115 goto theend;
10116 }
10117 *endp = ch; /* preserve the string */
10118
10119 if (len > 0)
10120 break; /* successfully shortened the path */
10121
10122 /* failed to shorten the path. Skip the path separator */
10123 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124 }
10125
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010126 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010128 /*
10129 * Succeeded in shortening the path. Now concatenate the shortened
10130 * path with the remaining path at the tail.
10131 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010133 /* Compute the length of the new path. */
10134 sfx_len = (int)(save_endp - endp) + 1;
10135 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010137 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010139 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010141 /* There is not enough space in the currently allocated string,
10142 * copy it to a buffer big enough. */
10143 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010145 {
10146 retval = FAIL;
10147 goto theend;
10148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149 }
10150 else
10151 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010152 /* Transfer short_fname to the main buffer (it's big enough),
10153 * unless get_short_pathname() did its work in-place. */
10154 *fname = *bufp = save_fname;
10155 if (short_fname != save_fname)
10156 vim_strncpy(save_fname, short_fname, len);
10157 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010159
10160 /* concat the not-shortened part of the path */
10161 vim_strncpy(*fname + len, endp, sfx_len);
10162 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010164
10165theend:
10166 vim_free(pbuf_unused);
10167 vim_free(save_fname);
10168
10169 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170}
10171
10172/*
10173 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010174 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175 */
10176 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010177shortpath_for_partial(
10178 char_u **fnamep,
10179 char_u **bufp,
10180 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181{
10182 int sepcount, len, tflen;
10183 char_u *p;
10184 char_u *pbuf, *tfname;
10185 int hasTilde;
10186
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010187 /* Count up the path separators from the RHS.. so we know which part
10188 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010190 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191 if (vim_ispathsep(*p))
10192 ++sepcount;
10193
10194 /* Need full path first (use expand_env() to remove a "~/") */
10195 hasTilde = (**fnamep == '~');
10196 if (hasTilde)
10197 pbuf = tfname = expand_env_save(*fnamep);
10198 else
10199 pbuf = tfname = FullName_save(*fnamep, FALSE);
10200
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010201 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010203 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
10204 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205
10206 if (len == 0)
10207 {
10208 /* Don't have a valid filename, so shorten the rest of the
10209 * path if we can. This CAN give us invalid 8.3 filenames, but
10210 * there's not a lot of point in guessing what it might be.
10211 */
10212 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010213 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
10214 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215 }
10216
10217 /* Count the paths backward to find the beginning of the desired string. */
10218 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010219 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010220 if (has_mbyte)
10221 p -= mb_head_off(tfname, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222 if (vim_ispathsep(*p))
10223 {
10224 if (sepcount == 0 || (hasTilde && sepcount == 1))
10225 break;
10226 else
10227 sepcount --;
10228 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230 if (hasTilde)
10231 {
10232 --p;
10233 if (p >= tfname)
10234 *p = '~';
10235 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010236 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237 }
10238 else
10239 ++p;
10240
10241 /* Copy in the string - p indexes into tfname - allocated at pbuf */
10242 vim_free(*bufp);
10243 *fnamelen = (int)STRLEN(p);
10244 *bufp = pbuf;
10245 *fnamep = p;
10246
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010247 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010248}
Bram Moolenaar4f974752019-02-17 17:44:42 +010010249#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250
10251/*
10252 * Adjust a filename, according to a string of modifiers.
10253 * *fnamep must be NUL terminated when called. When returning, the length is
10254 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010255 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 * When there is an error, *fnamep is set to NULL.
10257 */
10258 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010259modify_fname(
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010260 char_u *src, // string with modifiers
10261 int tilde_file, // "~" is a file name, not $HOME
10262 int *usedlen, // characters after src that are used
10263 char_u **fnamep, // file name so far
10264 char_u **bufp, // buffer for allocated file name or NULL
10265 int *fnamelen) // length of fnamep
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266{
10267 int valid = 0;
10268 char_u *tail;
10269 char_u *s, *p, *pbuf;
10270 char_u dirname[MAXPATHL];
10271 int c;
10272 int has_fullname = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010273#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010274 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275 int has_shortname = 0;
10276#endif
10277
10278repeat:
10279 /* ":p" - full path/file_name */
10280 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
10281 {
10282 has_fullname = 1;
10283
10284 valid |= VALID_PATH;
10285 *usedlen += 2;
10286
10287 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
10288 if ((*fnamep)[0] == '~'
10289#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
10290 && ((*fnamep)[1] == '/'
10291# ifdef BACKSLASH_IN_FILENAME
10292 || (*fnamep)[1] == '\\'
10293# endif
10294 || (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010296 && !(tilde_file && (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297 )
10298 {
10299 *fnamep = expand_env_save(*fnamep);
10300 vim_free(*bufp); /* free any allocated file name */
10301 *bufp = *fnamep;
10302 if (*fnamep == NULL)
10303 return -1;
10304 }
10305
10306 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010307 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308 {
10309 if (vim_ispathsep(*p)
10310 && p[1] == '.'
10311 && (p[2] == NUL
10312 || vim_ispathsep(p[2])
10313 || (p[2] == '.'
10314 && (p[3] == NUL || vim_ispathsep(p[3])))))
10315 break;
10316 }
10317
10318 /* FullName_save() is slow, don't use it when not needed. */
10319 if (*p != NUL || !vim_isAbsName(*fnamep))
10320 {
10321 *fnamep = FullName_save(*fnamep, *p != NUL);
10322 vim_free(*bufp); /* free any allocated file name */
10323 *bufp = *fnamep;
10324 if (*fnamep == NULL)
10325 return -1;
10326 }
10327
Bram Moolenaar4f974752019-02-17 17:44:42 +010010328#ifdef MSWIN
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010329# if _WIN32_WINNT >= 0x0500
10330 if (vim_strchr(*fnamep, '~') != NULL)
10331 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010332 // Expand 8.3 filename to full path. Needed to make sure the same
10333 // file does not have two different names.
10334 // Note: problem does not occur if _WIN32_WINNT < 0x0500.
10335 WCHAR *wfname = enc_to_utf16(*fnamep, NULL);
10336 WCHAR buf[_MAX_PATH];
10337
10338 if (wfname != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010339 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010340 if (GetLongPathNameW(wfname, buf, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010341 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010342 char_u *p = utf16_to_enc(buf, NULL);
10343
10344 if (p != NULL)
10345 {
10346 vim_free(*bufp); // free any allocated file name
10347 *bufp = *fnamep = p;
10348 }
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010349 }
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010350 vim_free(wfname);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010351 }
10352 }
10353# endif
10354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010355 /* Append a path separator to a directory. */
10356 if (mch_isdir(*fnamep))
10357 {
10358 /* Make room for one or two extra characters. */
10359 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
10360 vim_free(*bufp); /* free any allocated file name */
10361 *bufp = *fnamep;
10362 if (*fnamep == NULL)
10363 return -1;
10364 add_pathsep(*fnamep);
10365 }
10366 }
10367
10368 /* ":." - path relative to the current directory */
10369 /* ":~" - path relative to the home directory */
10370 /* ":8" - shortname path - postponed till after */
10371 while (src[*usedlen] == ':'
10372 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
10373 {
10374 *usedlen += 2;
10375 if (c == '8')
10376 {
Bram Moolenaar4f974752019-02-17 17:44:42 +010010377#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378 has_shortname = 1; /* Postpone this. */
10379#endif
10380 continue;
10381 }
10382 pbuf = NULL;
10383 /* Need full path first (use expand_env() to remove a "~/") */
10384 if (!has_fullname)
10385 {
10386 if (c == '.' && **fnamep == '~')
10387 p = pbuf = expand_env_save(*fnamep);
10388 else
10389 p = pbuf = FullName_save(*fnamep, FALSE);
10390 }
10391 else
10392 p = *fnamep;
10393
10394 has_fullname = 0;
10395
10396 if (p != NULL)
10397 {
10398 if (c == '.')
10399 {
10400 mch_dirname(dirname, MAXPATHL);
10401 s = shorten_fname(p, dirname);
10402 if (s != NULL)
10403 {
10404 *fnamep = s;
10405 if (pbuf != NULL)
10406 {
10407 vim_free(*bufp); /* free any allocated file name */
10408 *bufp = pbuf;
10409 pbuf = NULL;
10410 }
10411 }
10412 }
10413 else
10414 {
10415 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
10416 /* Only replace it when it starts with '~' */
10417 if (*dirname == '~')
10418 {
10419 s = vim_strsave(dirname);
10420 if (s != NULL)
10421 {
10422 *fnamep = s;
10423 vim_free(*bufp);
10424 *bufp = s;
10425 }
10426 }
10427 }
10428 vim_free(pbuf);
10429 }
10430 }
10431
10432 tail = gettail(*fnamep);
10433 *fnamelen = (int)STRLEN(*fnamep);
10434
10435 /* ":h" - head, remove "/file_name", can be repeated */
10436 /* Don't remove the first "/" or "c:\" */
10437 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
10438 {
10439 valid |= VALID_HEAD;
10440 *usedlen += 2;
10441 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010442 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010443 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444 *fnamelen = (int)(tail - *fnamep);
10445#ifdef VMS
10446 if (*fnamelen > 0)
10447 *fnamelen += 1; /* the path separator is part of the path */
10448#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010449 if (*fnamelen == 0)
10450 {
10451 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
10452 p = vim_strsave((char_u *)".");
10453 if (p == NULL)
10454 return -1;
10455 vim_free(*bufp);
10456 *bufp = *fnamep = tail = p;
10457 *fnamelen = 1;
10458 }
10459 else
10460 {
10461 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010462 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464 }
10465
10466 /* ":8" - shortname */
10467 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
10468 {
10469 *usedlen += 2;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010470#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471 has_shortname = 1;
10472#endif
10473 }
10474
Bram Moolenaar4f974752019-02-17 17:44:42 +010010475#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010476 /*
10477 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478 */
10479 if (has_shortname)
10480 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010481 /* Copy the string if it is shortened by :h and when it wasn't copied
10482 * yet, because we are going to change it in place. Avoids changing
10483 * the buffer name for "%:8". */
10484 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485 {
10486 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020010487 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010488 return -1;
10489 vim_free(*bufp);
10490 *bufp = *fnamep = p;
10491 }
10492
10493 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020010494 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495 if (!has_fullname && !vim_isAbsName(*fnamep))
10496 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010497 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498 return -1;
10499 }
10500 else
10501 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010502 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503
Bram Moolenaardc935552011-08-17 15:23:23 +020010504 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010506 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507 return -1;
10508
10509 if (l == 0)
10510 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010511 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010513 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514 return -1;
10515 }
10516 *fnamelen = l;
10517 }
10518 }
Bram Moolenaar4f974752019-02-17 17:44:42 +010010519#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520
10521 /* ":t" - tail, just the basename */
10522 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
10523 {
10524 *usedlen += 2;
10525 *fnamelen -= (int)(tail - *fnamep);
10526 *fnamep = tail;
10527 }
10528
10529 /* ":e" - extension, can be repeated */
10530 /* ":r" - root, without extension, can be repeated */
10531 while (src[*usedlen] == ':'
10532 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
10533 {
10534 /* find a '.' in the tail:
10535 * - for second :e: before the current fname
10536 * - otherwise: The last '.'
10537 */
10538 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
10539 s = *fnamep - 2;
10540 else
10541 s = *fnamep + *fnamelen - 1;
10542 for ( ; s > tail; --s)
10543 if (s[0] == '.')
10544 break;
10545 if (src[*usedlen + 1] == 'e') /* :e */
10546 {
10547 if (s > tail)
10548 {
10549 *fnamelen += (int)(*fnamep - (s + 1));
10550 *fnamep = s + 1;
10551#ifdef VMS
10552 /* cut version from the extension */
10553 s = *fnamep + *fnamelen - 1;
10554 for ( ; s > *fnamep; --s)
10555 if (s[0] == ';')
10556 break;
10557 if (s > *fnamep)
10558 *fnamelen = s - *fnamep;
10559#endif
10560 }
10561 else if (*fnamep <= tail)
10562 *fnamelen = 0;
10563 }
10564 else /* :r */
10565 {
10566 if (s > tail) /* remove one extension */
10567 *fnamelen = (int)(s - *fnamep);
10568 }
10569 *usedlen += 2;
10570 }
10571
10572 /* ":s?pat?foo?" - substitute */
10573 /* ":gs?pat?foo?" - global substitute */
10574 if (src[*usedlen] == ':'
10575 && (src[*usedlen + 1] == 's'
10576 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
10577 {
10578 char_u *str;
10579 char_u *pat;
10580 char_u *sub;
10581 int sep;
10582 char_u *flags;
10583 int didit = FALSE;
10584
10585 flags = (char_u *)"";
10586 s = src + *usedlen + 2;
10587 if (src[*usedlen + 1] == 'g')
10588 {
10589 flags = (char_u *)"g";
10590 ++s;
10591 }
10592
10593 sep = *s++;
10594 if (sep)
10595 {
10596 /* find end of pattern */
10597 p = vim_strchr(s, sep);
10598 if (p != NULL)
10599 {
10600 pat = vim_strnsave(s, (int)(p - s));
10601 if (pat != NULL)
10602 {
10603 s = p + 1;
10604 /* find end of substitution */
10605 p = vim_strchr(s, sep);
10606 if (p != NULL)
10607 {
10608 sub = vim_strnsave(s, (int)(p - s));
10609 str = vim_strnsave(*fnamep, *fnamelen);
10610 if (sub != NULL && str != NULL)
10611 {
10612 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010613 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614 if (s != NULL)
10615 {
10616 *fnamep = s;
10617 *fnamelen = (int)STRLEN(s);
10618 vim_free(*bufp);
10619 *bufp = s;
10620 didit = TRUE;
10621 }
10622 }
10623 vim_free(sub);
10624 vim_free(str);
10625 }
10626 vim_free(pat);
10627 }
10628 }
10629 /* after using ":s", repeat all the modifiers */
10630 if (didit)
10631 goto repeat;
10632 }
10633 }
10634
Bram Moolenaar26df0922014-02-23 23:39:13 +010010635 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10636 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010637 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010638 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010639 if (c != NUL)
10640 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010641 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010642 if (c != NUL)
10643 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010644 if (p == NULL)
10645 return -1;
10646 vim_free(*bufp);
10647 *bufp = *fnamep = p;
10648 *fnamelen = (int)STRLEN(p);
10649 *usedlen += 2;
10650 }
10651
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652 return valid;
10653}
10654
10655/*
10656 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010657 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658 * "flags" can be "g" to do a global substitute.
10659 * Returns an allocated string, NULL for error.
10660 */
10661 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010662do_string_sub(
10663 char_u *str,
10664 char_u *pat,
10665 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010666 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010667 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668{
10669 int sublen;
10670 regmatch_T regmatch;
10671 int i;
10672 int do_all;
10673 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010674 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675 garray_T ga;
10676 char_u *ret;
10677 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010678 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679
10680 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
10681 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010682 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683
10684 ga_init2(&ga, 1, 200);
10685
10686 do_all = (flags[0] == 'g');
10687
10688 regmatch.rm_ic = p_ic;
10689 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10690 if (regmatch.regprog != NULL)
10691 {
10692 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010693 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010694 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
10695 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010010696 /* Skip empty match except for first match. */
10697 if (regmatch.startp[0] == regmatch.endp[0])
10698 {
10699 if (zero_width == regmatch.startp[0])
10700 {
10701 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020010702 i = MB_PTR2LEN(tail);
10703 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
10704 (size_t)i);
10705 ga.ga_len += i;
10706 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010707 continue;
10708 }
10709 zero_width = regmatch.startp[0];
10710 }
10711
Bram Moolenaar071d4272004-06-13 20:20:40 +000010712 /*
10713 * Get some space for a temporary buffer to do the substitution
10714 * into. It will contain:
10715 * - The text up to where the match is.
10716 * - The substituted text.
10717 * - The text after the match.
10718 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010719 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010010720 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
10722 {
10723 ga_clear(&ga);
10724 break;
10725 }
10726
10727 /* copy the text up to where the match is */
10728 i = (int)(regmatch.startp[0] - tail);
10729 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10730 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010731 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732 + ga.ga_len + i, TRUE, TRUE, FALSE);
10733 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010734 tail = regmatch.endp[0];
10735 if (*tail == NUL)
10736 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737 if (!do_all)
10738 break;
10739 }
10740
10741 if (ga.ga_data != NULL)
10742 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10743
Bram Moolenaar473de612013-06-08 18:19:48 +020010744 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745 }
10746
10747 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10748 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010749 if (p_cpo == empty_option)
10750 p_cpo = save_cpo;
10751 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010752 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010753 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754
10755 return ret;
10756}
10757
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010758 static int
10759filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10760{
10761 typval_T rettv;
10762 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010763 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010764
10765 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10766 argv[0] = vimvars[VV_KEY].vv_tv;
10767 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010768 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10769 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010770 if (map)
10771 {
10772 /* map(): replace the list item value */
10773 clear_tv(tv);
10774 rettv.v_lock = 0;
10775 *tv = rettv;
10776 }
10777 else
10778 {
10779 int error = FALSE;
10780
10781 /* filter(): when expr is zero remove the item */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010782 *remp = (tv_get_number_chk(&rettv, &error) == 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010783 clear_tv(&rettv);
10784 /* On type error, nothing has been removed; return FAIL to stop the
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010785 * loop. The error message was given by tv_get_number_chk(). */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010786 if (error)
10787 goto theend;
10788 }
10789 retval = OK;
10790theend:
10791 clear_tv(&vimvars[VV_VAL].vv_tv);
10792 return retval;
10793}
10794
10795
10796/*
10797 * Implementation of map() and filter().
10798 */
10799 void
10800filter_map(typval_T *argvars, typval_T *rettv, int map)
10801{
10802 typval_T *expr;
10803 listitem_T *li, *nli;
10804 list_T *l = NULL;
10805 dictitem_T *di;
10806 hashtab_T *ht;
10807 hashitem_T *hi;
10808 dict_T *d = NULL;
10809 typval_T save_val;
10810 typval_T save_key;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010811 blob_T *b = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010812 int rem;
10813 int todo;
10814 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10815 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10816 : N_("filter() argument"));
10817 int save_did_emsg;
10818 int idx = 0;
10819
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010820 if (argvars[0].v_type == VAR_BLOB)
10821 {
10822 if ((b = argvars[0].vval.v_blob) == NULL)
10823 return;
10824 }
10825 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010826 {
10827 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010828 || (!map && var_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010829 return;
10830 }
10831 else if (argvars[0].v_type == VAR_DICT)
10832 {
10833 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010834 || (!map && var_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010835 return;
10836 }
10837 else
10838 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010839 semsg(_(e_listdictarg), ermsg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010840 return;
10841 }
10842
10843 expr = &argvars[1];
10844 /* On type errors, the preceding call has already displayed an error
10845 * message. Avoid a misleading error message for an empty string that
10846 * was not passed as argument. */
10847 if (expr->v_type != VAR_UNKNOWN)
10848 {
10849 prepare_vimvar(VV_VAL, &save_val);
10850
10851 /* We reset "did_emsg" to be able to detect whether an error
10852 * occurred during evaluation of the expression. */
10853 save_did_emsg = did_emsg;
10854 did_emsg = FALSE;
10855
10856 prepare_vimvar(VV_KEY, &save_key);
10857 if (argvars[0].v_type == VAR_DICT)
10858 {
10859 vimvars[VV_KEY].vv_type = VAR_STRING;
10860
10861 ht = &d->dv_hashtab;
10862 hash_lock(ht);
10863 todo = (int)ht->ht_used;
10864 for (hi = ht->ht_array; todo > 0; ++hi)
10865 {
10866 if (!HASHITEM_EMPTY(hi))
10867 {
10868 int r;
10869
10870 --todo;
10871 di = HI2DI(hi);
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010872 if (map && (var_check_lock(di->di_tv.v_lock,
10873 arg_errmsg, TRUE)
10874 || var_check_ro(di->di_flags,
10875 arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010876 break;
10877 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10878 r = filter_map_one(&di->di_tv, expr, map, &rem);
10879 clear_tv(&vimvars[VV_KEY].vv_tv);
10880 if (r == FAIL || did_emsg)
10881 break;
10882 if (!map && rem)
10883 {
10884 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10885 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10886 break;
10887 dictitem_remove(d, di);
10888 }
10889 }
10890 }
10891 hash_unlock(ht);
10892 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010893 else if (argvars[0].v_type == VAR_BLOB)
10894 {
10895 int i;
10896 typval_T tv;
10897
10898 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10899 for (i = 0; i < b->bv_ga.ga_len; i++)
10900 {
10901 tv.v_type = VAR_NUMBER;
10902 tv.vval.v_number = blob_get(b, i);
10903 vimvars[VV_KEY].vv_nr = idx;
10904 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg)
10905 break;
10906 if (tv.v_type != VAR_NUMBER)
10907 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010908 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010909 return;
10910 }
10911 tv.v_type = VAR_NUMBER;
10912 blob_set(b, i, tv.vval.v_number);
10913 if (!map && rem)
10914 {
10915 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
10916
10917 mch_memmove(p + idx, p + i + 1,
10918 (size_t)b->bv_ga.ga_len - i - 1);
10919 --b->bv_ga.ga_len;
10920 --i;
10921 }
10922 }
10923 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010924 else
10925 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +010010926 // argvars[0].v_type == VAR_LIST
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010927 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10928
10929 for (li = l->lv_first; li != NULL; li = nli)
10930 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010931 if (map && var_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010932 break;
10933 nli = li->li_next;
10934 vimvars[VV_KEY].vv_nr = idx;
10935 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10936 || did_emsg)
10937 break;
10938 if (!map && rem)
10939 listitem_remove(l, li);
10940 ++idx;
10941 }
10942 }
10943
10944 restore_vimvar(VV_KEY, &save_key);
10945 restore_vimvar(VV_VAL, &save_val);
10946
10947 did_emsg |= save_did_emsg;
10948 }
10949
10950 copy_tv(&argvars[0], rettv);
10951}
10952
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */