blob: dd662875132fe0a7b0a41a7c5c69d6e01e828e98 [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 Moolenaar543c9b12019-04-05 22:50:40 +0200756 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100757eval_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 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200969 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 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200981 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 Moolenaar558ca4a2019-04-04 18:15:38 +02001252 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253
Bram Moolenaardb552d602006-03-23 22:59:57 +00001254 argend = skip_var_list(arg, &var_count, &semicolon);
1255 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001256 return;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001257 if (argend > arg && argend[-1] == '.') // for var.='str'
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001258 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001259 expr = skipwhite(argend);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001260 concat = expr[0] == '.'
1261 && ((expr[1] == '=' && current_sctx.sc_version < 2)
1262 || (expr[1] == '.' && expr[2] == '='));
1263 if (*expr != '=' && !((vim_strchr((char_u *)"+-*/%", *expr) != NULL
1264 && expr[1] == '=') || concat))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001266 /*
1267 * ":let" without "=": list variables
1268 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001269 if (*arg == '[')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001270 emsg(_(e_invarg));
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001271 else if (expr[0] == '.')
1272 emsg(_("E985: .= is not supported with script version 2"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001273 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001274 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001275 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001276 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001277 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001278 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001279 list_glob_vars(&first);
1280 list_buf_vars(&first);
1281 list_win_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001282 list_tab_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001283 list_script_vars(&first);
1284 list_func_vars(&first);
1285 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 eap->nextcmd = check_nextcmd(arg);
1288 }
1289 else
1290 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001291 op[0] = '=';
1292 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001293 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001294 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001295 if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001296 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001297 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001298 if (expr[0] == '.' && expr[1] == '.') // ..=
1299 ++expr;
1300 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001301 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001302 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001303 else
1304 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001305
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 if (eap->skip)
1307 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001308 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 if (eap->skip)
1310 {
1311 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001312 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 --emsg_skip;
1314 }
1315 else if (i != FAIL)
1316 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001317 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001318 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001319 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 }
1321 }
1322}
1323
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001324/*
1325 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1326 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001327 * When "nextchars" is not NULL it points to a string with characters that
1328 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1329 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001330 * Returns OK or FAIL;
1331 */
1332 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001333ex_let_vars(
1334 char_u *arg_start,
1335 typval_T *tv,
1336 int copy, /* copy values from "tv", don't move */
1337 int semicolon, /* from skip_var_list() */
1338 int var_count, /* from skip_var_list() */
1339 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001340{
1341 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001342 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001343 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001344 listitem_T *item;
1345 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001346
1347 if (*arg != '[')
1348 {
1349 /*
1350 * ":let var = expr" or ":for var in list"
1351 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001352 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001353 return FAIL;
1354 return OK;
1355 }
1356
1357 /*
1358 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1359 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001360 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001361 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001362 emsg(_(e_listreq));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001363 return FAIL;
1364 }
1365
1366 i = list_len(l);
1367 if (semicolon == 0 && var_count < i)
1368 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001369 emsg(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001370 return FAIL;
1371 }
1372 if (var_count - semicolon > i)
1373 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001374 emsg(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001375 return FAIL;
1376 }
1377
1378 item = l->lv_first;
1379 while (*arg != ']')
1380 {
1381 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001382 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001383 item = item->li_next;
1384 if (arg == NULL)
1385 return FAIL;
1386
1387 arg = skipwhite(arg);
1388 if (*arg == ';')
1389 {
1390 /* Put the rest of the list (may be empty) in the var after ';'.
1391 * Create a new list for this. */
1392 l = list_alloc();
1393 if (l == NULL)
1394 return FAIL;
1395 while (item != NULL)
1396 {
1397 list_append_tv(l, &item->li_tv);
1398 item = item->li_next;
1399 }
1400
1401 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001402 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001403 ltv.vval.v_list = l;
1404 l->lv_refcount = 1;
1405
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001406 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1407 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001408 clear_tv(&ltv);
1409 if (arg == NULL)
1410 return FAIL;
1411 break;
1412 }
1413 else if (*arg != ',' && *arg != ']')
1414 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001415 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001416 return FAIL;
1417 }
1418 }
1419
1420 return OK;
1421}
1422
1423/*
1424 * Skip over assignable variable "var" or list of variables "[var, var]".
1425 * Used for ":let varvar = expr" and ":for varvar in expr".
1426 * For "[var, var]" increment "*var_count" for each variable.
1427 * for "[var, var; var]" set "semicolon".
1428 * Return NULL for an error.
1429 */
1430 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001431skip_var_list(
1432 char_u *arg,
1433 int *var_count,
1434 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001435{
1436 char_u *p, *s;
1437
1438 if (*arg == '[')
1439 {
1440 /* "[var, var]": find the matching ']'. */
1441 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001442 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001443 {
1444 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1445 s = skip_var_one(p);
1446 if (s == p)
1447 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001448 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001449 return NULL;
1450 }
1451 ++*var_count;
1452
1453 p = skipwhite(s);
1454 if (*p == ']')
1455 break;
1456 else if (*p == ';')
1457 {
1458 if (*semicolon == 1)
1459 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001460 emsg(_("Double ; in list of variables"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001461 return NULL;
1462 }
1463 *semicolon = 1;
1464 }
1465 else if (*p != ',')
1466 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001467 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001468 return NULL;
1469 }
1470 }
1471 return p + 1;
1472 }
1473 else
1474 return skip_var_one(arg);
1475}
1476
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001477/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001478 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001479 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001480 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001481 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001482skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001483{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001484 if (*arg == '@' && arg[1] != NUL)
1485 return arg + 2;
1486 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1487 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001488}
1489
Bram Moolenaara7043832005-01-21 11:56:39 +00001490/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001491 * List variables for hashtab "ht" with prefix "prefix".
1492 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001493 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001494 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001495list_hashtable_vars(
1496 hashtab_T *ht,
Bram Moolenaar32526b32019-01-19 17:43:09 +01001497 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01001498 int empty,
1499 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001500{
Bram Moolenaar33570922005-01-25 22:26:29 +00001501 hashitem_T *hi;
1502 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001503 int todo;
Bram Moolenaarf86db782018-10-25 13:31:37 +02001504 char_u buf[IOSIZE];
Bram Moolenaara7043832005-01-21 11:56:39 +00001505
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001506 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001507 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1508 {
1509 if (!HASHITEM_EMPTY(hi))
1510 {
1511 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001512 di = HI2DI(hi);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001513
1514 // apply :filter /pat/ to variable name
Bram Moolenaar32526b32019-01-19 17:43:09 +01001515 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1516 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001517 if (message_filtered(buf))
1518 continue;
1519
Bram Moolenaar33570922005-01-25 22:26:29 +00001520 if (empty || di->di_tv.v_type != VAR_STRING
1521 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001522 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001523 }
1524 }
1525}
1526
1527/*
1528 * List global variables.
1529 */
1530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001531list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001532{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001533 list_hashtable_vars(&globvarht, "", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001534}
1535
1536/*
1537 * List buffer variables.
1538 */
1539 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001540list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001541{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001542 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001543}
1544
1545/*
1546 * List window variables.
1547 */
1548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001549list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001550{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001551 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001552}
1553
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001554/*
1555 * List tab page variables.
1556 */
1557 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001558list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001559{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001560 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001561}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001562
Bram Moolenaara7043832005-01-21 11:56:39 +00001563/*
1564 * List Vim variables.
1565 */
1566 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001567list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001568{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001569 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001570}
1571
1572/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001573 * List script-local variables, if there is a script.
1574 */
1575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001576list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001577{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001578 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
1579 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
Bram Moolenaar32526b32019-01-19 17:43:09 +01001580 "s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001581}
1582
1583/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001584 * List variables in "arg".
1585 */
1586 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001587list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001588{
1589 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001590 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001591 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001592 char_u *name_start;
1593 char_u *arg_subsc;
1594 char_u *tofree;
1595 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001596
1597 while (!ends_excmd(*arg) && !got_int)
1598 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001599 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001600 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001601 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001602 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001603 {
1604 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001605 emsg(_(e_trailing));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001606 break;
1607 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001608 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001609 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001610 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001611 /* get_name_len() takes care of expanding curly braces */
1612 name_start = name = arg;
1613 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1614 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001615 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001616 /* This is mainly to keep test 49 working: when expanding
1617 * curly braces fails overrule the exception error message. */
1618 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001619 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001620 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001621 semsg(_(e_invarg2), arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001622 break;
1623 }
1624 error = TRUE;
1625 }
1626 else
1627 {
1628 if (tofree != NULL)
1629 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001630 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001631 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001632 else
1633 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001634 /* handle d.key, l[idx], f(expr) */
1635 arg_subsc = arg;
1636 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001637 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001638 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001639 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001640 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001641 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001642 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001643 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001644 case 'g': list_glob_vars(first); break;
1645 case 'b': list_buf_vars(first); break;
1646 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001647 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001648 case 'v': list_vim_vars(first); break;
1649 case 's': list_script_vars(first); break;
1650 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001651 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001652 semsg(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001653 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001654 }
1655 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001656 {
1657 char_u numbuf[NUMBUFLEN];
1658 char_u *tf;
1659 int c;
1660 char_u *s;
1661
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001662 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001663 c = *arg;
1664 *arg = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001665 list_one_var_a("",
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001666 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001667 tv.v_type,
1668 s == NULL ? (char_u *)"" : s,
1669 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001670 *arg = c;
1671 vim_free(tf);
1672 }
1673 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001674 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001675 }
1676 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001677
1678 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001679 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001680
1681 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001682 }
1683
1684 return arg;
1685}
1686
1687/*
1688 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1689 * Returns a pointer to the char just after the var name.
1690 * Returns NULL if there is an error.
1691 */
1692 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001693ex_let_one(
1694 char_u *arg, /* points to variable name */
1695 typval_T *tv, /* value to assign to variable */
1696 int copy, /* copy value from "tv" */
1697 char_u *endchars, /* valid chars after variable name or NULL */
1698 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001699{
1700 int c1;
1701 char_u *name;
1702 char_u *p;
1703 char_u *arg_end = NULL;
1704 int len;
1705 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001706 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001707
1708 /*
1709 * ":let $VAR = expr": Set environment variable.
1710 */
1711 if (*arg == '$')
1712 {
1713 /* Find the end of the name. */
1714 ++arg;
1715 name = arg;
1716 len = get_env_len(&arg);
1717 if (len == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001718 semsg(_(e_invarg2), name - 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001719 else
1720 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001721 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001722 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001723 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001724 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001725 emsg(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001726 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001727 {
1728 c1 = name[len];
1729 name[len] = NUL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001730 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001731 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001732 {
1733 int mustfree = FALSE;
1734 char_u *s = vim_getenv(name, &mustfree);
1735
1736 if (s != NULL)
1737 {
1738 p = tofree = concat_str(s, p);
1739 if (mustfree)
1740 vim_free(s);
1741 }
1742 }
1743 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001744 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001745 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001746 if (STRICMP(name, "HOME") == 0)
1747 init_homedir();
1748 else if (didset_vim && STRICMP(name, "VIM") == 0)
1749 didset_vim = FALSE;
1750 else if (didset_vimruntime
1751 && STRICMP(name, "VIMRUNTIME") == 0)
1752 didset_vimruntime = FALSE;
1753 arg_end = arg;
1754 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001755 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001756 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001757 }
1758 }
1759 }
1760
1761 /*
1762 * ":let &option = expr": Set option value.
1763 * ":let &l:option = expr": Set local option value.
1764 * ":let &g:option = expr": Set global option value.
1765 */
1766 else if (*arg == '&')
1767 {
1768 /* Find the end of the name. */
1769 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001770 if (p == NULL || (endchars != NULL
1771 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001772 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001773 else
1774 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001775 long n;
1776 int opt_type;
1777 long numval;
1778 char_u *stringval = NULL;
1779 char_u *s;
1780
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001781 c1 = *p;
1782 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001783
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001784 n = (long)tv_get_number(tv);
1785 s = tv_get_string_chk(tv); /* != NULL if number or string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001786 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001787 {
1788 opt_type = get_option_value(arg, &numval,
1789 &stringval, opt_flags);
1790 if ((opt_type == 1 && *op == '.')
1791 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001792 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001793 semsg(_(e_letwrong), op);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001794 s = NULL; // don't set the value
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001795 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001796 else
1797 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001798 if (opt_type == 1) // number
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001799 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001800 switch (*op)
1801 {
1802 case '+': n = numval + n; break;
1803 case '-': n = numval - n; break;
1804 case '*': n = numval * n; break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001805 case '/': n = (long)num_divide(numval, n); break;
1806 case '%': n = (long)num_modulus(numval, n); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001807 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001808 }
Bram Moolenaarff697e62019-02-12 22:28:33 +01001809 else if (opt_type == 0 && stringval != NULL) // string
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001810 {
1811 s = concat_str(stringval, s);
1812 vim_free(stringval);
1813 stringval = s;
1814 }
1815 }
1816 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001817 if (s != NULL)
1818 {
1819 set_option_value(arg, n, s, opt_flags);
1820 arg_end = p;
1821 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001822 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001823 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001824 }
1825 }
1826
1827 /*
1828 * ":let @r = expr": Set register contents.
1829 */
1830 else if (*arg == '@')
1831 {
1832 ++arg;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001833 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001834 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001835 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001836 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001837 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001838 else
1839 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001840 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001841 char_u *s;
1842
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001843 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001844 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001845 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02001846 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001847 if (s != NULL)
1848 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001849 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001850 vim_free(s);
1851 }
1852 }
1853 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001854 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001855 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001856 arg_end = arg + 1;
1857 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001858 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 }
1860 }
1861
1862 /*
1863 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001864 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001865 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001866 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001867 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001868 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001869
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001870 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001871 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001872 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001873 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001874 emsg(_(e_letunexp));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001875 else
1876 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001877 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001878 arg_end = p;
1879 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001880 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001881 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001882 }
1883
1884 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001885 semsg(_(e_invarg2), arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001886
1887 return arg_end;
1888}
1889
1890/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001891 * Get an lval: variable, Dict item or List item that can be assigned a value
1892 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1893 * "name.key", "name.key[expr]" etc.
1894 * Indexing only works if "name" is an existing List or Dictionary.
1895 * "name" points to the start of the name.
1896 * If "rettv" is not NULL it points to the value to be assigned.
1897 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1898 * wrong; must end in space or cmd separator.
1899 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001900 * flags:
1901 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001902 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001903 * GLV_NO_AUTOLOAD: do not use script autoloading
1904 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001905 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001906 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001907 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001908 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001909 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001910get_lval(
1911 char_u *name,
1912 typval_T *rettv,
1913 lval_T *lp,
1914 int unlet,
1915 int skip,
1916 int flags, /* GLV_ values */
1917 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001918{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001919 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001920 char_u *expr_start, *expr_end;
1921 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001922 dictitem_T *v;
1923 typval_T var1;
1924 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001925 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001926 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001927 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001928 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001929 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001930 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001931
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001932 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001933 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001934
1935 if (skip)
1936 {
1937 /* When skipping just find the end of the name. */
1938 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001939 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001940 }
1941
1942 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001943 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001944 if (expr_start != NULL)
1945 {
1946 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001947 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001948 && *p != '[' && *p != '.')
1949 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001950 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001951 return NULL;
1952 }
1953
1954 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1955 if (lp->ll_exp_name == NULL)
1956 {
1957 /* Report an invalid expression in braces, unless the
1958 * expression evaluation has been cancelled due to an
1959 * aborting error, an interrupt, or an exception. */
1960 if (!aborting() && !quiet)
1961 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001962 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001963 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001964 return NULL;
1965 }
1966 }
1967 lp->ll_name = lp->ll_exp_name;
1968 }
1969 else
1970 lp->ll_name = name;
1971
1972 /* Without [idx] or .key we are done. */
1973 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1974 return p;
1975
1976 cc = *p;
1977 *p = NUL;
Bram Moolenaar6e65d592017-12-07 22:11:27 +01001978 /* Only pass &ht when we would write to the variable, it prevents autoload
1979 * as well. */
1980 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
1981 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001982 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001983 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001984 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001985 if (v == NULL)
1986 return NULL;
1987
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001988 /*
1989 * Loop until no more [idx] or .key is following.
1990 */
Bram Moolenaar33570922005-01-25 22:26:29 +00001991 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001992 var1.v_type = VAR_UNKNOWN;
1993 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001994 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001995 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001996 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1997 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001998 && lp->ll_tv->vval.v_dict != NULL)
1999 && !(lp->ll_tv->v_type == VAR_BLOB
2000 && lp->ll_tv->vval.v_blob != NULL))
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(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002004 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002005 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002006 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002007 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002008 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002009 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002010 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002011 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002012
Bram Moolenaar8c711452005-01-14 21:53:12 +00002013 len = -1;
2014 if (*p == '.')
2015 {
2016 key = p + 1;
2017 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2018 ;
2019 if (len == 0)
2020 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002021 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002022 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002023 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002024 }
2025 p = key + len;
2026 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002027 else
2028 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002029 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002030 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002031 if (*p == ':')
2032 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002033 else
2034 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002035 empty1 = FALSE;
2036 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002037 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002038 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002039 {
2040 /* not a number or string */
2041 clear_tv(&var1);
2042 return NULL;
2043 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002044 }
2045
2046 /* Optionally get the second index [ :expr]. */
2047 if (*p == ':')
2048 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002049 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002050 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002051 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002052 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002053 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002054 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002055 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002056 if (rettv != NULL
2057 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002058 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002059 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002060 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002061 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002062 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002063 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002064 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002065 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002066 }
2067 p = skipwhite(p + 1);
2068 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002069 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002070 else
2071 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002072 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002073 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2074 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002075 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002076 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002077 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002078 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002079 {
2080 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002081 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002082 clear_tv(&var2);
2083 return NULL;
2084 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002085 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002086 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002087 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002088 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002089 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002090
Bram Moolenaar8c711452005-01-14 21:53:12 +00002091 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002092 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002093 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002094 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002095 clear_tv(&var1);
2096 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002097 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002098 }
2099
2100 /* Skip to past ']'. */
2101 ++p;
2102 }
2103
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002104 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002105 {
2106 if (len == -1)
2107 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002108 /* "[key]": get key from "var1" */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002109 key = tv_get_string_chk(&var1); /* is number or string */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002110 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002111 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002112 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002113 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002114 }
2115 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002116 lp->ll_list = NULL;
2117 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002118 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002119
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002120 /* When assigning to a scope dictionary check that a function and
2121 * variable name is valid (only variable name unless it is l: or
2122 * g: dictionary). Disallow overwriting a builtin function. */
2123 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002124 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002125 int prevval;
2126 int wrong;
2127
2128 if (len != -1)
2129 {
2130 prevval = key[len];
2131 key[len] = NUL;
2132 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002133 else
2134 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002135 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2136 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002137 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002138 || !valid_varname(key);
2139 if (len != -1)
2140 key[len] = prevval;
2141 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002142 return NULL;
2143 }
2144
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002145 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002146 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01002147 // Can't add "v:" or "a:" variable.
2148 if (lp->ll_dict == &vimvardict
2149 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002150 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002151 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01002152 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002153 return NULL;
2154 }
2155
Bram Moolenaar31b81602019-02-10 22:14:27 +01002156 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002157 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002158 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002159 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002160 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002161 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002162 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002163 }
2164 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002165 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002166 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002167 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002168 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002169 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002170 p = NULL;
2171 break;
2172 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002173 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002174 else if ((flags & GLV_READ_ONLY) == 0
2175 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002176 {
2177 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002178 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002179 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002180
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002181 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002182 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002183 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002184 else if (lp->ll_tv->v_type == VAR_BLOB)
2185 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002186 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
2187
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002188 /*
2189 * Get the number and item for the only or first index of the List.
2190 */
2191 if (empty1)
2192 lp->ll_n1 = 0;
2193 else
2194 // is number or string
2195 lp->ll_n1 = (long)tv_get_number(&var1);
2196 clear_tv(&var1);
2197
2198 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002199 || lp->ll_n1 > bloblen
2200 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002201 {
2202 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002203 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002204 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002205 return NULL;
2206 }
2207 if (lp->ll_range && !lp->ll_empty2)
2208 {
2209 lp->ll_n2 = (long)tv_get_number(&var2);
2210 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002211 if (lp->ll_n2 < 0
2212 || lp->ll_n2 >= bloblen
2213 || lp->ll_n2 < lp->ll_n1)
2214 {
2215 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002216 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002217 return NULL;
2218 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002219 }
2220 lp->ll_blob = lp->ll_tv->vval.v_blob;
2221 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01002222 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002223 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002224 else
2225 {
2226 /*
2227 * Get the number and item for the only or first index of the List.
2228 */
2229 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002230 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002231 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002232 /* is number or string */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002233 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002234 clear_tv(&var1);
2235
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002236 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002237 lp->ll_list = lp->ll_tv->vval.v_list;
2238 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2239 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002240 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002241 if (lp->ll_n1 < 0)
2242 {
2243 lp->ll_n1 = 0;
2244 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2245 }
2246 }
2247 if (lp->ll_li == NULL)
2248 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002249 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002250 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002251 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002252 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002253 }
2254
2255 /*
2256 * May need to find the item or absolute index for the second
2257 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002258 * When no index given: "lp->ll_empty2" is TRUE.
2259 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002260 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002261 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002262 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002263 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002264 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002265 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002266 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002267 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002268 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002269 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002270 {
2271 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002272 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002273 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002274 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002275 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002276 }
2277
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002278 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2279 if (lp->ll_n1 < 0)
2280 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2281 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002282 {
2283 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002284 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002285 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002286 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002287 }
2288
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002289 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002290 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002291 }
2292
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002293 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002294 return p;
2295}
2296
2297/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002298 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002299 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002300 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002301clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002302{
2303 vim_free(lp->ll_exp_name);
2304 vim_free(lp->ll_newkey);
2305}
2306
2307/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002308 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002309 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01002310 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
2311 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002312 */
2313 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002314set_var_lval(
2315 lval_T *lp,
2316 char_u *endp,
2317 typval_T *rettv,
2318 int copy,
2319 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002320{
2321 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002322 listitem_T *ri;
2323 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002324
2325 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002326 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002327 cc = *endp;
2328 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002329 if (lp->ll_blob != NULL)
2330 {
2331 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002332
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002333 if (op != NULL && *op != '=')
2334 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002335 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002336 return;
2337 }
2338
2339 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2340 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002341 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002342
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002343 if (lp->ll_empty2)
2344 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
2345
2346 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002347 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002348 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002349 return;
2350 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002351 if (lp->ll_empty2)
2352 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002353
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002354 ir = 0;
2355 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
2356 blob_set(lp->ll_blob, il,
2357 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002358 }
2359 else
2360 {
2361 val = (int)tv_get_number_chk(rettv, &error);
2362 if (!error)
2363 {
2364 garray_T *gap = &lp->ll_blob->bv_ga;
2365
2366 // Allow for appending a byte. Setting a byte beyond
2367 // the end is an error otherwise.
2368 if (lp->ll_n1 < gap->ga_len
2369 || (lp->ll_n1 == gap->ga_len
2370 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
2371 {
2372 blob_set(lp->ll_blob, lp->ll_n1, val);
2373 if (lp->ll_n1 == gap->ga_len)
2374 ++gap->ga_len;
2375 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002376 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002377 }
2378 }
2379 }
2380 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002381 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002382 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002383
Bram Moolenaarff697e62019-02-12 22:28:33 +01002384 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01002385 di = NULL;
2386 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2387 &tv, &di, TRUE, FALSE) == OK)
2388 {
2389 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002390 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2391 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01002392 && tv_op(&tv, rettv, op) == OK)
2393 set_var(lp->ll_name, &tv, FALSE);
2394 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002395 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002396 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002397 else
2398 set_var(lp->ll_name, rettv, copy);
2399 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002400 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002401 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002402 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002403 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002404 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002405 else if (lp->ll_range)
2406 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002407 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002408 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002409
2410 /*
2411 * Check whether any of the list items is locked
2412 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002413 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002414 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002415 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002416 return;
2417 ri = ri->li_next;
2418 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2419 break;
2420 ll_li = ll_li->li_next;
2421 ++ll_n1;
2422 }
2423
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002424 /*
2425 * Assign the List values to the list items.
2426 */
2427 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002428 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002429 if (op != NULL && *op != '=')
2430 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2431 else
2432 {
2433 clear_tv(&lp->ll_li->li_tv);
2434 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2435 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002436 ri = ri->li_next;
2437 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2438 break;
2439 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002440 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002441 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002442 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002443 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002444 ri = NULL;
2445 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002446 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002447 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002448 lp->ll_li = lp->ll_li->li_next;
2449 ++lp->ll_n1;
2450 }
2451 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002452 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002453 else if (lp->ll_empty2
2454 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002455 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002456 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002457 }
2458 else
2459 {
2460 /*
2461 * Assign to a List or Dictionary item.
2462 */
2463 if (lp->ll_newkey != NULL)
2464 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002465 if (op != NULL && *op != '=')
2466 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002467 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002468 return;
2469 }
2470
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002471 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002472 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 if (di == NULL)
2474 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002475 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2476 {
2477 vim_free(di);
2478 return;
2479 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002480 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002481 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002482 else if (op != NULL && *op != '=')
2483 {
2484 tv_op(lp->ll_tv, rettv, op);
2485 return;
2486 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002487 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002488 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002489
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002490 /*
2491 * Assign the value to the variable or list item.
2492 */
2493 if (copy)
2494 copy_tv(rettv, lp->ll_tv);
2495 else
2496 {
2497 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002498 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002499 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002500 }
2501 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002502}
2503
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002504/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01002505 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2506 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002507 * Returns OK or FAIL.
2508 */
2509 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002510tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002511{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002512 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002513 char_u numbuf[NUMBUFLEN];
2514 char_u *s;
2515
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002516 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2517 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2518 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002519 {
2520 switch (tv1->v_type)
2521 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002522 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002523 case VAR_DICT:
2524 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002525 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002526 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002527 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002528 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002529 break;
2530
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002531 case VAR_BLOB:
2532 if (*op != '+' || tv2->v_type != VAR_BLOB)
2533 break;
2534 // BLOB += BLOB
2535 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2536 {
2537 blob_T *b1 = tv1->vval.v_blob;
2538 blob_T *b2 = tv2->vval.v_blob;
2539 int i, len = blob_len(b2);
2540 for (i = 0; i < len; i++)
2541 ga_append(&b1->bv_ga, blob_get(b2, i));
2542 }
2543 return OK;
2544
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002545 case VAR_LIST:
2546 if (*op != '+' || tv2->v_type != VAR_LIST)
2547 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002548 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002549 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2550 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2551 return OK;
2552
2553 case VAR_NUMBER:
2554 case VAR_STRING:
2555 if (tv2->v_type == VAR_LIST)
2556 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002557 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002558 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002559 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002560 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002561#ifdef FEAT_FLOAT
2562 if (tv2->v_type == VAR_FLOAT)
2563 {
2564 float_T f = n;
2565
Bram Moolenaarff697e62019-02-12 22:28:33 +01002566 if (*op == '%')
2567 break;
2568 switch (*op)
2569 {
2570 case '+': f += tv2->vval.v_float; break;
2571 case '-': f -= tv2->vval.v_float; break;
2572 case '*': f *= tv2->vval.v_float; break;
2573 case '/': f /= tv2->vval.v_float; break;
2574 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002575 clear_tv(tv1);
2576 tv1->v_type = VAR_FLOAT;
2577 tv1->vval.v_float = f;
2578 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002579 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002580#endif
2581 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002582 switch (*op)
2583 {
2584 case '+': n += tv_get_number(tv2); break;
2585 case '-': n -= tv_get_number(tv2); break;
2586 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01002587 case '/': n = num_divide(n, tv_get_number(tv2)); break;
2588 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002589 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002590 clear_tv(tv1);
2591 tv1->v_type = VAR_NUMBER;
2592 tv1->vval.v_number = n;
2593 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002594 }
2595 else
2596 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002597 if (tv2->v_type == VAR_FLOAT)
2598 break;
2599
Bram Moolenaarff697e62019-02-12 22:28:33 +01002600 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002601 s = tv_get_string(tv1);
2602 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002603 clear_tv(tv1);
2604 tv1->v_type = VAR_STRING;
2605 tv1->vval.v_string = s;
2606 }
2607 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002608
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002609 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002610#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002611 {
2612 float_T f;
2613
Bram Moolenaarff697e62019-02-12 22:28:33 +01002614 if (*op == '%' || *op == '.'
2615 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002616 && tv2->v_type != VAR_NUMBER
2617 && tv2->v_type != VAR_STRING))
2618 break;
2619 if (tv2->v_type == VAR_FLOAT)
2620 f = tv2->vval.v_float;
2621 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002622 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01002623 switch (*op)
2624 {
2625 case '+': tv1->vval.v_float += f; break;
2626 case '-': tv1->vval.v_float -= f; break;
2627 case '*': tv1->vval.v_float *= f; break;
2628 case '/': tv1->vval.v_float /= f; break;
2629 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002630 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002631#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002632 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002633 }
2634 }
2635
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002636 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002637 return FAIL;
2638}
2639
2640/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002641 * Evaluate the expression used in a ":for var in expr" command.
2642 * "arg" points to "var".
2643 * Set "*errp" to TRUE for an error, FALSE otherwise;
2644 * Return a pointer that holds the info. Null when there is an error.
2645 */
2646 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002647eval_for_line(
2648 char_u *arg,
2649 int *errp,
2650 char_u **nextcmdp,
2651 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002652{
Bram Moolenaar33570922005-01-25 22:26:29 +00002653 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002654 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002655 typval_T tv;
2656 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002657
2658 *errp = TRUE; /* default: there is an error */
2659
Bram Moolenaar33570922005-01-25 22:26:29 +00002660 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002661 if (fi == NULL)
2662 return NULL;
2663
2664 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2665 if (expr == NULL)
2666 return fi;
2667
2668 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002669 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002670 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002671 emsg(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002672 return fi;
2673 }
2674
2675 if (skip)
2676 ++emsg_skip;
2677 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2678 {
2679 *errp = FALSE;
2680 if (!skip)
2681 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002682 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002683 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002684 l = tv.vval.v_list;
2685 if (l == NULL)
2686 {
2687 // a null list is like an empty list: do nothing
2688 clear_tv(&tv);
2689 }
2690 else
2691 {
2692 // No need to increment the refcount, it's already set for
2693 // the list being used in "tv".
2694 fi->fi_list = l;
2695 list_add_watch(l, &fi->fi_lw);
2696 fi->fi_lw.lw_item = l->lv_first;
2697 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002698 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002699 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002700 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002701 fi->fi_bi = 0;
2702 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002703 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002704 typval_T btv;
2705
2706 // Make a copy, so that the iteration still works when the
2707 // blob is changed.
2708 blob_copy(&tv, &btv);
2709 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002710 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002711 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002712 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002713 else
2714 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002715 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002716 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002717 }
2718 }
2719 }
2720 if (skip)
2721 --emsg_skip;
2722
2723 return fi;
2724}
2725
2726/*
2727 * Use the first item in a ":for" list. Advance to the next.
2728 * Assign the values to the variable (list). "arg" points to the first one.
2729 * Return TRUE when a valid item was found, FALSE when at end of list or
2730 * something wrong.
2731 */
2732 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002733next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002734{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002735 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002736 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002737 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002738
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002739 if (fi->fi_blob != NULL)
2740 {
2741 typval_T tv;
2742
2743 if (fi->fi_bi >= blob_len(fi->fi_blob))
2744 return FALSE;
2745 tv.v_type = VAR_NUMBER;
2746 tv.v_lock = VAR_FIXED;
2747 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2748 ++fi->fi_bi;
2749 return ex_let_vars(arg, &tv, TRUE,
2750 fi->fi_semicolon, fi->fi_varcount, NULL) == OK;
2751 }
2752
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002753 item = fi->fi_lw.lw_item;
2754 if (item == NULL)
2755 result = FALSE;
2756 else
2757 {
2758 fi->fi_lw.lw_item = item->li_next;
2759 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2760 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2761 }
2762 return result;
2763}
2764
2765/*
2766 * Free the structure used to store info used by ":for".
2767 */
2768 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002769free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002770{
Bram Moolenaar33570922005-01-25 22:26:29 +00002771 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002772
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002773 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002774 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002775 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002776 list_unref(fi->fi_list);
2777 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002778 if (fi != NULL && fi->fi_blob != NULL)
2779 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002780 vim_free(fi);
2781}
2782
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2784
2785 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002786set_context_for_expression(
2787 expand_T *xp,
2788 char_u *arg,
2789 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790{
2791 int got_eq = FALSE;
2792 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002793 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002795 if (cmdidx == CMD_let)
2796 {
2797 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002798 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002799 {
2800 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002801 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002802 {
2803 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002804 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002805 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002806 break;
2807 }
2808 return;
2809 }
2810 }
2811 else
2812 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2813 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 while ((xp->xp_pattern = vim_strpbrk(arg,
2815 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2816 {
2817 c = *xp->xp_pattern;
2818 if (c == '&')
2819 {
2820 c = xp->xp_pattern[1];
2821 if (c == '&')
2822 {
2823 ++xp->xp_pattern;
2824 xp->xp_context = cmdidx != CMD_let || got_eq
2825 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2826 }
2827 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002828 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002830 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2831 xp->xp_pattern += 2;
2832
2833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 }
2835 else if (c == '$')
2836 {
2837 /* environment variable */
2838 xp->xp_context = EXPAND_ENV_VARS;
2839 }
2840 else if (c == '=')
2841 {
2842 got_eq = TRUE;
2843 xp->xp_context = EXPAND_EXPRESSION;
2844 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002845 else if (c == '#'
2846 && xp->xp_context == EXPAND_EXPRESSION)
2847 {
2848 /* Autoload function/variable contains '#'. */
2849 break;
2850 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002851 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 && xp->xp_context == EXPAND_FUNCTIONS
2853 && vim_strchr(xp->xp_pattern, '(') == NULL)
2854 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002855 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 break;
2857 }
2858 else if (cmdidx != CMD_let || got_eq)
2859 {
2860 if (c == '"') /* string */
2861 {
2862 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2863 if (c == '\\' && xp->xp_pattern[1] != NUL)
2864 ++xp->xp_pattern;
2865 xp->xp_context = EXPAND_NOTHING;
2866 }
2867 else if (c == '\'') /* literal string */
2868 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002869 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2871 /* skip */ ;
2872 xp->xp_context = EXPAND_NOTHING;
2873 }
2874 else if (c == '|')
2875 {
2876 if (xp->xp_pattern[1] == '|')
2877 {
2878 ++xp->xp_pattern;
2879 xp->xp_context = EXPAND_EXPRESSION;
2880 }
2881 else
2882 xp->xp_context = EXPAND_COMMANDS;
2883 }
2884 else
2885 xp->xp_context = EXPAND_EXPRESSION;
2886 }
2887 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002888 /* Doesn't look like something valid, expand as an expression
2889 * anyway. */
2890 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 arg = xp->xp_pattern;
2892 if (*arg != NUL)
2893 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2894 /* skip */ ;
2895 }
2896 xp->xp_pattern = arg;
2897}
2898
2899#endif /* FEAT_CMDL_COMPL */
2900
2901/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 * ":unlet[!] var1 ... " command.
2903 */
2904 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002905ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002907 ex_unletlock(eap, eap->arg, 0);
2908}
2909
2910/*
2911 * ":lockvar" and ":unlockvar" commands
2912 */
2913 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002914ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002915{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002917 int deep = 2;
2918
2919 if (eap->forceit)
2920 deep = -1;
2921 else if (vim_isdigit(*arg))
2922 {
2923 deep = getdigits(&arg);
2924 arg = skipwhite(arg);
2925 }
2926
2927 ex_unletlock(eap, arg, deep);
2928}
2929
2930/*
2931 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2932 */
2933 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002934ex_unletlock(
2935 exarg_T *eap,
2936 char_u *argstart,
2937 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002938{
2939 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002942 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943
2944 do
2945 {
Bram Moolenaar137374f2018-05-13 15:59:50 +02002946 if (*arg == '$')
2947 {
2948 char_u *name = ++arg;
2949
2950 if (get_env_len(&arg) == 0)
2951 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002952 semsg(_(e_invarg2), name - 1);
Bram Moolenaar137374f2018-05-13 15:59:50 +02002953 return;
2954 }
2955 vim_unsetenv(name);
2956 arg = skipwhite(arg);
2957 continue;
2958 }
2959
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002960 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002961 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002962 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002963 if (lv.ll_name == NULL)
2964 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002965 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002966 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002968 if (name_end != NULL)
2969 {
2970 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002971 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002972 }
2973 if (!(eap->skip || error))
2974 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 break;
2976 }
2977
2978 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002979 {
2980 if (eap->cmdidx == CMD_unlet)
2981 {
2982 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2983 error = TRUE;
2984 }
2985 else
2986 {
2987 if (do_lock_var(&lv, name_end, deep,
2988 eap->cmdidx == CMD_lockvar) == FAIL)
2989 error = TRUE;
2990 }
2991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002993 if (!eap->skip)
2994 clear_lval(&lv);
2995
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 arg = skipwhite(name_end);
2997 } while (!ends_excmd(*arg));
2998
2999 eap->nextcmd = check_nextcmd(arg);
3000}
3001
Bram Moolenaar8c711452005-01-14 21:53:12 +00003002 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003003do_unlet_var(
3004 lval_T *lp,
3005 char_u *name_end,
3006 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003007{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003008 int ret = OK;
3009 int cc;
3010
3011 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003012 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003013 cc = *name_end;
3014 *name_end = NUL;
3015
3016 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003017 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003018 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003019 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003020 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003021 else if ((lp->ll_list != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003022 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003023 || (lp->ll_dict != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003024 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003025 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003026 else if (lp->ll_range)
3027 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003028 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003029 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003030 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003031
3032 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3033 {
3034 li = ll_li->li_next;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003035 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003036 return FAIL;
3037 ll_li = li;
3038 ++ll_n1;
3039 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003040
3041 /* Delete a range of List items. */
3042 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3043 {
3044 li = lp->ll_li->li_next;
3045 listitem_remove(lp->ll_list, lp->ll_li);
3046 lp->ll_li = li;
3047 ++lp->ll_n1;
3048 }
3049 }
3050 else
3051 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003052 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003053 /* unlet a List item. */
3054 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003055 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003056 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003057 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003058 }
3059
3060 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003061}
3062
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063/*
3064 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003065 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 */
3067 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003068do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069{
Bram Moolenaar33570922005-01-25 22:26:29 +00003070 hashtab_T *ht;
3071 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003072 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003073 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003074 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075
Bram Moolenaar33570922005-01-25 22:26:29 +00003076 ht = find_var_ht(name, &varname);
3077 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003079 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003080 if (d == NULL)
3081 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003082 if (ht == &globvarht)
3083 d = &globvardict;
3084 else if (ht == &compat_hashtab)
3085 d = &vimvardict;
3086 else
3087 {
3088 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3089 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3090 }
3091 if (d == NULL)
3092 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003093 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003094 return FAIL;
3095 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003096 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003097 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003098 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003099 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003100 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003101 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003102 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003103 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003104 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003105 || var_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003106 return FAIL;
3107
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003108 delete_var(ht, hi);
3109 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003112 if (forceit)
3113 return OK;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003114 semsg(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 return FAIL;
3116}
3117
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003118/*
3119 * Lock or unlock variable indicated by "lp".
3120 * "deep" is the levels to go (-1 for unlimited);
3121 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3122 */
3123 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003124do_lock_var(
3125 lval_T *lp,
3126 char_u *name_end,
3127 int deep,
3128 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003129{
3130 int ret = OK;
3131 int cc;
3132 dictitem_T *di;
3133
3134 if (deep == 0) /* nothing to do */
3135 return OK;
3136
3137 if (lp->ll_tv == NULL)
3138 {
3139 cc = *name_end;
3140 *name_end = NUL;
3141
3142 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003143 di = find_var(lp->ll_name, NULL, TRUE);
3144 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003145 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01003146 else if ((di->di_flags & DI_FLAGS_FIX)
3147 && di->di_tv.v_type != VAR_DICT
3148 && di->di_tv.v_type != VAR_LIST)
3149 /* For historic reasons this error is not given for a list or dict.
3150 * E.g., the b: dict could be locked/unlocked. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003151 semsg(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003152 else
3153 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01003154 if (lock)
3155 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003156 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01003157 di->di_flags &= ~DI_FLAGS_LOCK;
3158 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003159 }
3160 *name_end = cc;
3161 }
3162 else if (lp->ll_range)
3163 {
3164 listitem_T *li = lp->ll_li;
3165
3166 /* (un)lock a range of List items. */
3167 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3168 {
3169 item_lock(&li->li_tv, deep, lock);
3170 li = li->li_next;
3171 ++lp->ll_n1;
3172 }
3173 }
3174 else if (lp->ll_list != NULL)
3175 /* (un)lock a List item. */
3176 item_lock(&lp->ll_li->li_tv, deep, lock);
3177 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003178 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003179 item_lock(&lp->ll_di->di_tv, deep, lock);
3180
3181 return ret;
3182}
3183
3184/*
3185 * Lock or unlock an item. "deep" is nr of levels to go.
3186 */
3187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003188item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003189{
3190 static int recurse = 0;
3191 list_T *l;
3192 listitem_T *li;
3193 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003194 blob_T *b;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003195 hashitem_T *hi;
3196 int todo;
3197
3198 if (recurse >= DICT_MAXNEST)
3199 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003200 emsg(_("E743: variable nested too deep for (un)lock"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003201 return;
3202 }
3203 if (deep == 0)
3204 return;
3205 ++recurse;
3206
3207 /* lock/unlock the item itself */
3208 if (lock)
3209 tv->v_lock |= VAR_LOCKED;
3210 else
3211 tv->v_lock &= ~VAR_LOCKED;
3212
3213 switch (tv->v_type)
3214 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003215 case VAR_UNKNOWN:
3216 case VAR_NUMBER:
3217 case VAR_STRING:
3218 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003219 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003220 case VAR_FLOAT:
3221 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003222 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003223 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003224 break;
3225
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003226 case VAR_BLOB:
3227 if ((b = tv->vval.v_blob) != NULL)
3228 {
3229 if (lock)
3230 b->bv_lock |= VAR_LOCKED;
3231 else
3232 b->bv_lock &= ~VAR_LOCKED;
3233 }
3234 break;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003235 case VAR_LIST:
3236 if ((l = tv->vval.v_list) != NULL)
3237 {
3238 if (lock)
3239 l->lv_lock |= VAR_LOCKED;
3240 else
3241 l->lv_lock &= ~VAR_LOCKED;
3242 if (deep < 0 || deep > 1)
3243 /* recursive: lock/unlock the items the List contains */
3244 for (li = l->lv_first; li != NULL; li = li->li_next)
3245 item_lock(&li->li_tv, deep - 1, lock);
3246 }
3247 break;
3248 case VAR_DICT:
3249 if ((d = tv->vval.v_dict) != NULL)
3250 {
3251 if (lock)
3252 d->dv_lock |= VAR_LOCKED;
3253 else
3254 d->dv_lock &= ~VAR_LOCKED;
3255 if (deep < 0 || deep > 1)
3256 {
3257 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003258 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003259 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3260 {
3261 if (!HASHITEM_EMPTY(hi))
3262 {
3263 --todo;
3264 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3265 }
3266 }
3267 }
3268 }
3269 }
3270 --recurse;
3271}
3272
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3274/*
3275 * Delete all "menutrans_" variables.
3276 */
3277 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003278del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279{
Bram Moolenaar33570922005-01-25 22:26:29 +00003280 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003281 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282
Bram Moolenaar33570922005-01-25 22:26:29 +00003283 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003284 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003285 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003286 {
3287 if (!HASHITEM_EMPTY(hi))
3288 {
3289 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003290 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3291 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003292 }
3293 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003294 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295}
3296#endif
3297
3298#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3299
3300/*
3301 * Local string buffer for the next two functions to store a variable name
3302 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3303 * get_user_var_name().
3304 */
3305
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306static char_u *varnamebuf = NULL;
3307static int varnamebuflen = 0;
3308
3309/*
3310 * Function to concatenate a prefix and a variable name.
3311 */
3312 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003313cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314{
3315 int len;
3316
3317 len = (int)STRLEN(name) + 3;
3318 if (len > varnamebuflen)
3319 {
3320 vim_free(varnamebuf);
3321 len += 10; /* some additional space */
3322 varnamebuf = alloc(len);
3323 if (varnamebuf == NULL)
3324 {
3325 varnamebuflen = 0;
3326 return NULL;
3327 }
3328 varnamebuflen = len;
3329 }
3330 *varnamebuf = prefix;
3331 varnamebuf[1] = ':';
3332 STRCPY(varnamebuf + 2, name);
3333 return varnamebuf;
3334}
3335
3336/*
3337 * Function given to ExpandGeneric() to obtain the list of user defined
3338 * (global/buffer/window/built-in) variable names.
3339 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003341get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003343 static long_u gdone;
3344 static long_u bdone;
3345 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003346 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003347 static int vidx;
3348 static hashitem_T *hi;
3349 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350
3351 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003352 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003353 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003354 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003355 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003356
3357 /* Global variables */
3358 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003360 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003361 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003362 else
3363 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003364 while (HASHITEM_EMPTY(hi))
3365 ++hi;
3366 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3367 return cat_prefix_varname('g', hi->hi_key);
3368 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003370
3371 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003372 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003373 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003375 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003376 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003377 else
3378 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003379 while (HASHITEM_EMPTY(hi))
3380 ++hi;
3381 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003383
3384 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003385 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003386 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003388 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003389 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003390 else
3391 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003392 while (HASHITEM_EMPTY(hi))
3393 ++hi;
3394 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003396
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003397 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003398 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003399 if (tdone < ht->ht_used)
3400 {
3401 if (tdone++ == 0)
3402 hi = ht->ht_array;
3403 else
3404 ++hi;
3405 while (HASHITEM_EMPTY(hi))
3406 ++hi;
3407 return cat_prefix_varname('t', hi->hi_key);
3408 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003409
Bram Moolenaar33570922005-01-25 22:26:29 +00003410 /* v: variables */
3411 if (vidx < VV_LEN)
3412 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413
Bram Moolenaard23a8232018-02-10 18:45:26 +01003414 VIM_CLEAR(varnamebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 varnamebuflen = 0;
3416 return NULL;
3417}
3418
3419#endif /* FEAT_CMDL_COMPL */
3420
3421/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003422 * Return TRUE if "pat" matches "text".
3423 * Does not use 'cpo' and always uses 'magic'.
3424 */
3425 static int
3426pattern_match(char_u *pat, char_u *text, int ic)
3427{
3428 int matches = FALSE;
3429 char_u *save_cpo;
3430 regmatch_T regmatch;
3431
3432 /* avoid 'l' flag in 'cpoptions' */
3433 save_cpo = p_cpo;
3434 p_cpo = (char_u *)"";
3435 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3436 if (regmatch.regprog != NULL)
3437 {
3438 regmatch.rm_ic = ic;
3439 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3440 vim_regfree(regmatch.regprog);
3441 }
3442 p_cpo = save_cpo;
3443 return matches;
3444}
3445
3446/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003448 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3450 */
3451
3452/*
3453 * Handle zero level expression.
3454 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003455 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003456 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 * Return OK or FAIL.
3458 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003459 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003460eval0(
3461 char_u *arg,
3462 typval_T *rettv,
3463 char_u **nextcmd,
3464 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465{
3466 int ret;
3467 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003468 int did_emsg_before = did_emsg;
3469 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470
3471 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003472 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 if (ret == FAIL || !ends_excmd(*p))
3474 {
3475 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003476 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 /*
3478 * Report the invalid expression unless the expression evaluation has
3479 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003480 * exception, or we already gave a more specific error.
3481 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003483 if (!aborting() && did_emsg == did_emsg_before
3484 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003485 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 ret = FAIL;
3487 }
3488 if (nextcmd != NULL)
3489 *nextcmd = check_nextcmd(p);
3490
3491 return ret;
3492}
3493
3494/*
3495 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003496 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 *
3498 * "arg" must point to the first non-white of the expression.
3499 * "arg" is advanced to the next non-white after the recognized expression.
3500 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003501 * Note: "rettv.v_lock" is not set.
3502 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 * Return OK or FAIL.
3504 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003505 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003506eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507{
3508 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003509 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510
3511 /*
3512 * Get the first variable.
3513 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003514 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 return FAIL;
3516
3517 if ((*arg)[0] == '?')
3518 {
3519 result = FALSE;
3520 if (evaluate)
3521 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003522 int error = FALSE;
3523
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003524 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003526 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003527 if (error)
3528 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 }
3530
3531 /*
3532 * Get the second variable.
3533 */
3534 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003535 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 return FAIL;
3537
3538 /*
3539 * Check for the ":".
3540 */
3541 if ((*arg)[0] != ':')
3542 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003543 emsg(_("E109: Missing ':' after '?'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003545 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 return FAIL;
3547 }
3548
3549 /*
3550 * Get the third variable.
3551 */
3552 *arg = skipwhite(*arg + 1);
3553 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3554 {
3555 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003556 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 return FAIL;
3558 }
3559 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003560 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 }
3562
3563 return OK;
3564}
3565
3566/*
3567 * Handle first level expression:
3568 * expr2 || expr2 || expr2 logical OR
3569 *
3570 * "arg" must point to the first non-white of the expression.
3571 * "arg" is advanced to the next non-white after the recognized expression.
3572 *
3573 * Return OK or FAIL.
3574 */
3575 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003576eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577{
Bram Moolenaar33570922005-01-25 22:26:29 +00003578 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 long result;
3580 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003581 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582
3583 /*
3584 * Get the first variable.
3585 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003586 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 return FAIL;
3588
3589 /*
3590 * Repeat until there is no following "||".
3591 */
3592 first = TRUE;
3593 result = FALSE;
3594 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3595 {
3596 if (evaluate && first)
3597 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003598 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003600 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003601 if (error)
3602 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 first = FALSE;
3604 }
3605
3606 /*
3607 * Get the second variable.
3608 */
3609 *arg = skipwhite(*arg + 2);
3610 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3611 return FAIL;
3612
3613 /*
3614 * Compute the result.
3615 */
3616 if (evaluate && !result)
3617 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003618 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003620 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003621 if (error)
3622 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 }
3624 if (evaluate)
3625 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003626 rettv->v_type = VAR_NUMBER;
3627 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 }
3629 }
3630
3631 return OK;
3632}
3633
3634/*
3635 * Handle second level expression:
3636 * expr3 && expr3 && expr3 logical AND
3637 *
3638 * "arg" must point to the first non-white of the expression.
3639 * "arg" is advanced to the next non-white after the recognized expression.
3640 *
3641 * Return OK or FAIL.
3642 */
3643 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003644eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645{
Bram Moolenaar33570922005-01-25 22:26:29 +00003646 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 long result;
3648 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003649 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650
3651 /*
3652 * Get the first variable.
3653 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003654 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 return FAIL;
3656
3657 /*
3658 * Repeat until there is no following "&&".
3659 */
3660 first = TRUE;
3661 result = TRUE;
3662 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3663 {
3664 if (evaluate && first)
3665 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003666 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003668 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003669 if (error)
3670 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 first = FALSE;
3672 }
3673
3674 /*
3675 * Get the second variable.
3676 */
3677 *arg = skipwhite(*arg + 2);
3678 if (eval4(arg, &var2, evaluate && result) == FAIL)
3679 return FAIL;
3680
3681 /*
3682 * Compute the result.
3683 */
3684 if (evaluate && result)
3685 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003686 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003688 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003689 if (error)
3690 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 }
3692 if (evaluate)
3693 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003694 rettv->v_type = VAR_NUMBER;
3695 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 }
3697 }
3698
3699 return OK;
3700}
3701
3702/*
3703 * Handle third level expression:
3704 * var1 == var2
3705 * var1 =~ var2
3706 * var1 != var2
3707 * var1 !~ var2
3708 * var1 > var2
3709 * var1 >= var2
3710 * var1 < var2
3711 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003712 * var1 is var2
3713 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 *
3715 * "arg" must point to the first non-white of the expression.
3716 * "arg" is advanced to the next non-white after the recognized expression.
3717 *
3718 * Return OK or FAIL.
3719 */
3720 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003721eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722{
Bram Moolenaar33570922005-01-25 22:26:29 +00003723 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 char_u *p;
3725 int i;
3726 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003727 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730
3731 /*
3732 * Get the first variable.
3733 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003734 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 return FAIL;
3736
3737 p = *arg;
3738 switch (p[0])
3739 {
3740 case '=': if (p[1] == '=')
3741 type = TYPE_EQUAL;
3742 else if (p[1] == '~')
3743 type = TYPE_MATCH;
3744 break;
3745 case '!': if (p[1] == '=')
3746 type = TYPE_NEQUAL;
3747 else if (p[1] == '~')
3748 type = TYPE_NOMATCH;
3749 break;
3750 case '>': if (p[1] != '=')
3751 {
3752 type = TYPE_GREATER;
3753 len = 1;
3754 }
3755 else
3756 type = TYPE_GEQUAL;
3757 break;
3758 case '<': if (p[1] != '=')
3759 {
3760 type = TYPE_SMALLER;
3761 len = 1;
3762 }
3763 else
3764 type = TYPE_SEQUAL;
3765 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003766 case 'i': if (p[1] == 's')
3767 {
3768 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3769 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003770 i = p[len];
3771 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003772 {
3773 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3774 type_is = TRUE;
3775 }
3776 }
3777 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 }
3779
3780 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003781 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 */
3783 if (type != TYPE_UNKNOWN)
3784 {
3785 /* extra question mark appended: ignore case */
3786 if (p[len] == '?')
3787 {
3788 ic = TRUE;
3789 ++len;
3790 }
3791 /* extra '#' appended: match case */
3792 else if (p[len] == '#')
3793 {
3794 ic = FALSE;
3795 ++len;
3796 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003797 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 else
3799 ic = p_ic;
3800
3801 /*
3802 * Get the second variable.
3803 */
3804 *arg = skipwhite(p + len);
3805 if (eval5(arg, &var2, evaluate) == FAIL)
3806 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003807 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 return FAIL;
3809 }
Bram Moolenaar31988702018-02-13 12:57:42 +01003810 if (evaluate)
3811 {
3812 int ret = typval_compare(rettv, &var2, type, type_is, ic);
3813
3814 clear_tv(&var2);
3815 return ret;
3816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 }
3818
3819 return OK;
3820}
3821
3822/*
3823 * Handle fourth level expression:
3824 * + number addition
3825 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003826 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003827 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 *
3829 * "arg" must point to the first non-white of the expression.
3830 * "arg" is advanced to the next non-white after the recognized expression.
3831 *
3832 * Return OK or FAIL.
3833 */
3834 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003835eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836{
Bram Moolenaar33570922005-01-25 22:26:29 +00003837 typval_T var2;
3838 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003840 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003841#ifdef FEAT_FLOAT
3842 float_T f1 = 0, f2 = 0;
3843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 char_u *s1, *s2;
3845 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3846 char_u *p;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003847 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848
3849 /*
3850 * Get the first variable.
3851 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003852 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 return FAIL;
3854
3855 /*
3856 * Repeat computing, until no '+', '-' or '.' is following.
3857 */
3858 for (;;)
3859 {
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003860 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 op = **arg;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003862 concat = op == '.'
3863 && (*(*arg + 1) == '.' || current_sctx.sc_version < 2);
3864 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 break;
3866
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003867 if ((op != '+' || (rettv->v_type != VAR_LIST
3868 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003869#ifdef FEAT_FLOAT
3870 && (op == '.' || rettv->v_type != VAR_FLOAT)
3871#endif
3872 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003873 {
3874 /* For "list + ...", an illegal use of the first operand as
3875 * a number cannot be determined before evaluating the 2nd
3876 * operand: if this is also a list, all is ok.
3877 * For "something . ...", "something - ..." or "non-list + ...",
3878 * we know that the first operand needs to be a string or number
3879 * without evaluating the 2nd operand. So check before to avoid
3880 * side effects after an error. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003881 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003882 {
3883 clear_tv(rettv);
3884 return FAIL;
3885 }
3886 }
3887
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 /*
3889 * Get the second variable.
3890 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003891 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
3892 ++*arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003894 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003896 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 return FAIL;
3898 }
3899
3900 if (evaluate)
3901 {
3902 /*
3903 * Compute the result.
3904 */
3905 if (op == '.')
3906 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003907 s1 = tv_get_string_buf(rettv, buf1); /* already checked */
3908 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003909 if (s2 == NULL) /* type error ? */
3910 {
3911 clear_tv(rettv);
3912 clear_tv(&var2);
3913 return FAIL;
3914 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003915 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003916 clear_tv(rettv);
3917 rettv->v_type = VAR_STRING;
3918 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003920 else if (op == '+' && rettv->v_type == VAR_BLOB
3921 && var2.v_type == VAR_BLOB)
3922 {
3923 blob_T *b1 = rettv->vval.v_blob;
3924 blob_T *b2 = var2.vval.v_blob;
3925 blob_T *b = blob_alloc();
3926 int i;
3927
3928 if (b != NULL)
3929 {
3930 for (i = 0; i < blob_len(b1); i++)
3931 ga_append(&b->bv_ga, blob_get(b1, i));
3932 for (i = 0; i < blob_len(b2); i++)
3933 ga_append(&b->bv_ga, blob_get(b2, i));
3934
3935 clear_tv(rettv);
3936 rettv_blob_set(rettv, b);
3937 }
3938 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003939 else if (op == '+' && rettv->v_type == VAR_LIST
3940 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003941 {
3942 /* concatenate Lists */
3943 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3944 &var3) == FAIL)
3945 {
3946 clear_tv(rettv);
3947 clear_tv(&var2);
3948 return FAIL;
3949 }
3950 clear_tv(rettv);
3951 *rettv = var3;
3952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 else
3954 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003955 int error = FALSE;
3956
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003957#ifdef FEAT_FLOAT
3958 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003959 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003960 f1 = rettv->vval.v_float;
3961 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003962 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003963 else
3964#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003965 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003966 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003967 if (error)
3968 {
3969 /* This can only happen for "list + non-list". For
3970 * "non-list + ..." or "something - ...", we returned
3971 * before evaluating the 2nd operand. */
3972 clear_tv(rettv);
3973 return FAIL;
3974 }
3975#ifdef FEAT_FLOAT
3976 if (var2.v_type == VAR_FLOAT)
3977 f1 = n1;
3978#endif
3979 }
3980#ifdef FEAT_FLOAT
3981 if (var2.v_type == VAR_FLOAT)
3982 {
3983 f2 = var2.vval.v_float;
3984 n2 = 0;
3985 }
3986 else
3987#endif
3988 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003989 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003990 if (error)
3991 {
3992 clear_tv(rettv);
3993 clear_tv(&var2);
3994 return FAIL;
3995 }
3996#ifdef FEAT_FLOAT
3997 if (rettv->v_type == VAR_FLOAT)
3998 f2 = n2;
3999#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004000 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004001 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004002
4003#ifdef FEAT_FLOAT
4004 /* If there is a float on either side the result is a float. */
4005 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4006 {
4007 if (op == '+')
4008 f1 = f1 + f2;
4009 else
4010 f1 = f1 - f2;
4011 rettv->v_type = VAR_FLOAT;
4012 rettv->vval.v_float = f1;
4013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004015#endif
4016 {
4017 if (op == '+')
4018 n1 = n1 + n2;
4019 else
4020 n1 = n1 - n2;
4021 rettv->v_type = VAR_NUMBER;
4022 rettv->vval.v_number = n1;
4023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004025 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 }
4027 }
4028 return OK;
4029}
4030
4031/*
4032 * Handle fifth level expression:
4033 * * number multiplication
4034 * / number division
4035 * % number modulo
4036 *
4037 * "arg" must point to the first non-white of the expression.
4038 * "arg" is advanced to the next non-white after the recognized expression.
4039 *
4040 * Return OK or FAIL.
4041 */
4042 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004043eval6(
4044 char_u **arg,
4045 typval_T *rettv,
4046 int evaluate,
4047 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048{
Bram Moolenaar33570922005-01-25 22:26:29 +00004049 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004051 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004052#ifdef FEAT_FLOAT
4053 int use_float = FALSE;
4054 float_T f1 = 0, f2;
4055#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004056 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057
4058 /*
4059 * Get the first variable.
4060 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004061 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 return FAIL;
4063
4064 /*
4065 * Repeat computing, until no '*', '/' or '%' is following.
4066 */
4067 for (;;)
4068 {
4069 op = **arg;
4070 if (op != '*' && op != '/' && op != '%')
4071 break;
4072
4073 if (evaluate)
4074 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004075#ifdef FEAT_FLOAT
4076 if (rettv->v_type == VAR_FLOAT)
4077 {
4078 f1 = rettv->vval.v_float;
4079 use_float = TRUE;
4080 n1 = 0;
4081 }
4082 else
4083#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004084 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004085 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004086 if (error)
4087 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 }
4089 else
4090 n1 = 0;
4091
4092 /*
4093 * Get the second variable.
4094 */
4095 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004096 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 return FAIL;
4098
4099 if (evaluate)
4100 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004101#ifdef FEAT_FLOAT
4102 if (var2.v_type == VAR_FLOAT)
4103 {
4104 if (!use_float)
4105 {
4106 f1 = n1;
4107 use_float = TRUE;
4108 }
4109 f2 = var2.vval.v_float;
4110 n2 = 0;
4111 }
4112 else
4113#endif
4114 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004115 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004116 clear_tv(&var2);
4117 if (error)
4118 return FAIL;
4119#ifdef FEAT_FLOAT
4120 if (use_float)
4121 f2 = n2;
4122#endif
4123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124
4125 /*
4126 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004127 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004129#ifdef FEAT_FLOAT
4130 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004132 if (op == '*')
4133 f1 = f1 * f2;
4134 else if (op == '/')
4135 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004136# ifdef VMS
4137 /* VMS crashes on divide by zero, work around it */
4138 if (f2 == 0.0)
4139 {
4140 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004141 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004142 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004143 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004144 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004145 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004146 }
4147 else
4148 f1 = f1 / f2;
4149# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004150 /* We rely on the floating point library to handle divide
4151 * by zero to result in "inf" and not a crash. */
4152 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004153# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004156 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004157 emsg(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004158 return FAIL;
4159 }
4160 rettv->v_type = VAR_FLOAT;
4161 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 }
4163 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004166 if (op == '*')
4167 n1 = n1 * n2;
4168 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01004169 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01004171 n1 = num_modulus(n1, n2);
4172
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004173 rettv->v_type = VAR_NUMBER;
4174 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 }
4177 }
4178
4179 return OK;
4180}
4181
4182/*
4183 * Handle sixth level expression:
4184 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004185 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004186 * "string" string constant
4187 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 * &option-name option value
4189 * @r register contents
4190 * identifier variable value
4191 * function() function call
4192 * $VAR environment variable
4193 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004194 * [expr, expr] List
4195 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 *
4197 * Also handle:
4198 * ! in front logical NOT
4199 * - in front unary minus
4200 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004201 * trailing [] subscript in String or List
4202 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 *
4204 * "arg" must point to the first non-white of the expression.
4205 * "arg" is advanced to the next non-white after the recognized expression.
4206 *
4207 * Return OK or FAIL.
4208 */
4209 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004210eval7(
4211 char_u **arg,
4212 typval_T *rettv,
4213 int evaluate,
4214 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004216 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 int len;
4218 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 char_u *start_leader, *end_leader;
4220 int ret = OK;
4221 char_u *alias;
4222
4223 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004224 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004225 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004227 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228
4229 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004230 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 */
4232 start_leader = *arg;
4233 while (**arg == '!' || **arg == '-' || **arg == '+')
4234 *arg = skipwhite(*arg + 1);
4235 end_leader = *arg;
4236
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004237 if (**arg == '.' && (!isdigit(*(*arg + 1))
4238#ifdef FEAT_FLOAT
4239 || current_sctx.sc_version < 2
4240#endif
4241 ))
4242 {
4243 semsg(_(e_invexpr2), *arg);
4244 ++*arg;
4245 return FAIL;
4246 }
4247
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 switch (**arg)
4249 {
4250 /*
4251 * Number constant.
4252 */
4253 case '0':
4254 case '1':
4255 case '2':
4256 case '3':
4257 case '4':
4258 case '5':
4259 case '6':
4260 case '7':
4261 case '8':
4262 case '9':
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004263 case '.':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004264 {
4265#ifdef FEAT_FLOAT
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004266 char_u *p;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004267 int get_float = FALSE;
4268
4269 /* We accept a float when the format matches
4270 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004271 * strict to avoid backwards compatibility problems.
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004272 * With script version 2 and later the leading digit can be
4273 * omitted.
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004274 * Don't look for a float after the "." operator, so that
4275 * ":let vers = 1.2.3" doesn't fail. */
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004276 if (**arg == '.')
4277 p = *arg;
4278 else
4279 p = skipdigits(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004280 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004282 get_float = TRUE;
4283 p = skipdigits(p + 2);
4284 if (*p == 'e' || *p == 'E')
4285 {
4286 ++p;
4287 if (*p == '-' || *p == '+')
4288 ++p;
4289 if (!vim_isdigit(*p))
4290 get_float = FALSE;
4291 else
4292 p = skipdigits(p + 1);
4293 }
4294 if (ASCII_ISALPHA(*p) || *p == '.')
4295 get_float = FALSE;
4296 }
4297 if (get_float)
4298 {
4299 float_T f;
4300
4301 *arg += string2float(*arg, &f);
4302 if (evaluate)
4303 {
4304 rettv->v_type = VAR_FLOAT;
4305 rettv->vval.v_float = f;
4306 }
4307 }
4308 else
4309#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004310 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004311 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004312 char_u *bp;
Bram Moolenaare519dfd2019-01-13 15:42:02 +01004313 blob_T *blob = NULL; // init for gcc
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004314
4315 // Blob constant: 0z0123456789abcdef
4316 if (evaluate)
4317 blob = blob_alloc();
4318 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
4319 {
4320 if (!vim_isxdigit(bp[1]))
4321 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004322 if (blob != NULL)
4323 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004324 emsg(_("E973: Blob literal should have an even number of hex characters"));
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004325 ga_clear(&blob->bv_ga);
4326 VIM_CLEAR(blob);
4327 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004328 ret = FAIL;
4329 break;
4330 }
4331 if (blob != NULL)
4332 ga_append(&blob->bv_ga,
4333 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
Bram Moolenaar4131fd52019-01-17 16:32:53 +01004334 if (bp[2] == '.' && vim_isxdigit(bp[3]))
4335 ++bp;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004336 }
4337 if (blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004338 rettv_blob_set(rettv, blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004339 *arg = bp;
4340 }
4341 else
4342 {
4343 // decimal, hex or octal number
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01004344 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004345 *arg += len;
4346 if (evaluate)
4347 {
4348 rettv->v_type = VAR_NUMBER;
4349 rettv->vval.v_number = n;
4350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 }
4352 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354
4355 /*
4356 * String constant: "string".
4357 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004358 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 break;
4360
4361 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004362 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004364 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004365 break;
4366
4367 /*
4368 * List: [expr, expr]
4369 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004370 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 break;
4372
4373 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004374 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004375 * Dictionary: {key: val, key: val}
4376 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004377 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4378 if (ret == NOTDONE)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004379 ret = dict_get_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004380 break;
4381
4382 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004383 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004385 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 break;
4387
4388 /*
4389 * Environment variable: $VAR.
4390 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004391 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 break;
4393
4394 /*
4395 * Register contents: @r.
4396 */
4397 case '@': ++*arg;
4398 if (evaluate)
4399 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004400 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004401 rettv->vval.v_string = get_reg_contents(**arg,
4402 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 }
4404 if (**arg != NUL)
4405 ++*arg;
4406 break;
4407
4408 /*
4409 * nested expression: (expression).
4410 */
4411 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004412 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 if (**arg == ')')
4414 ++*arg;
4415 else if (ret == OK)
4416 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004417 emsg(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004418 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 ret = FAIL;
4420 }
4421 break;
4422
Bram Moolenaar8c711452005-01-14 21:53:12 +00004423 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 break;
4425 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004426
4427 if (ret == NOTDONE)
4428 {
4429 /*
4430 * Must be a variable or function name.
4431 * Can also be a curly-braces kind of name: {expr}.
4432 */
4433 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004434 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004435 if (alias != NULL)
4436 s = alias;
4437
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004438 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004439 ret = FAIL;
4440 else
4441 {
4442 if (**arg == '(') /* recursive! */
4443 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004444 partial_T *partial;
4445
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004446 if (!evaluate)
4447 check_vars(s, len);
4448
Bram Moolenaar8c711452005-01-14 21:53:12 +00004449 /* If "s" is the name of a variable of type VAR_FUNC
4450 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004451 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004452
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004453 /* Need to make a copy, in case evaluating the arguments makes
4454 * the name invalid. */
4455 s = vim_strsave(s);
4456 if (s == NULL)
4457 ret = FAIL;
4458 else
4459 /* Invoke the function. */
4460 ret = get_func_tv(s, len, rettv, arg,
4461 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4462 &len, evaluate, partial, NULL);
4463 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004464
4465 /* If evaluate is FALSE rettv->v_type was not set in
4466 * get_func_tv, but it's needed in handle_subscript() to parse
4467 * what follows. So set it here. */
4468 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4469 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004470 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004471 rettv->v_type = VAR_FUNC;
4472 }
4473
Bram Moolenaar8c711452005-01-14 21:53:12 +00004474 /* Stop the expression evaluation when immediately
4475 * aborting on error, or when an interrupt occurred or
4476 * an exception was thrown but not caught. */
4477 if (aborting())
4478 {
4479 if (ret == OK)
4480 clear_tv(rettv);
4481 ret = FAIL;
4482 }
4483 }
4484 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004485 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004486 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004487 {
4488 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004489 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004490 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004491 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004492 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004493 }
4494
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 *arg = skipwhite(*arg);
4496
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004497 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4498 * expr(expr). */
4499 if (ret == OK)
4500 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501
4502 /*
4503 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4504 */
4505 if (ret == OK && evaluate && end_leader > start_leader)
4506 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004507 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004508 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004509#ifdef FEAT_FLOAT
4510 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004511
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004512 if (rettv->v_type == VAR_FLOAT)
4513 f = rettv->vval.v_float;
4514 else
4515#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004516 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004517 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004519 clear_tv(rettv);
4520 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004522 else
4523 {
4524 while (end_leader > start_leader)
4525 {
4526 --end_leader;
4527 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004528 {
4529#ifdef FEAT_FLOAT
4530 if (rettv->v_type == VAR_FLOAT)
4531 f = !f;
4532 else
4533#endif
4534 val = !val;
4535 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004536 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004537 {
4538#ifdef FEAT_FLOAT
4539 if (rettv->v_type == VAR_FLOAT)
4540 f = -f;
4541 else
4542#endif
4543 val = -val;
4544 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004545 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004546#ifdef FEAT_FLOAT
4547 if (rettv->v_type == VAR_FLOAT)
4548 {
4549 clear_tv(rettv);
4550 rettv->vval.v_float = f;
4551 }
4552 else
4553#endif
4554 {
4555 clear_tv(rettv);
4556 rettv->v_type = VAR_NUMBER;
4557 rettv->vval.v_number = val;
4558 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 }
4561
4562 return ret;
4563}
4564
4565/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004566 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4567 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004568 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4569 */
4570 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004571eval_index(
4572 char_u **arg,
4573 typval_T *rettv,
4574 int evaluate,
4575 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004576{
4577 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004578 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004579 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004580 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004581 long len = -1;
4582 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004583 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004584 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004585
Bram Moolenaara03f2332016-02-06 18:09:59 +01004586 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004587 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004588 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004589 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004590 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004591 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004592 return FAIL;
4593 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004594#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004595 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004596 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004597 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004598#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004599 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004600 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004601 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004602 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004603 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004604 return FAIL;
4605 case VAR_UNKNOWN:
4606 if (evaluate)
4607 return FAIL;
4608 /* FALLTHROUGH */
4609
4610 case VAR_STRING:
4611 case VAR_NUMBER:
4612 case VAR_LIST:
4613 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004614 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004615 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004616 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004617
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004618 init_tv(&var1);
4619 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004620 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004621 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004622 /*
4623 * dict.name
4624 */
4625 key = *arg + 1;
4626 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4627 ;
4628 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004629 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004630 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004631 }
4632 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004633 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004634 /*
4635 * something[idx]
4636 *
4637 * Get the (first) variable from inside the [].
4638 */
4639 *arg = skipwhite(*arg + 1);
4640 if (**arg == ':')
4641 empty1 = TRUE;
4642 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4643 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004644 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004645 {
4646 /* not a number or string */
4647 clear_tv(&var1);
4648 return FAIL;
4649 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004650
4651 /*
4652 * Get the second variable from inside the [:].
4653 */
4654 if (**arg == ':')
4655 {
4656 range = TRUE;
4657 *arg = skipwhite(*arg + 1);
4658 if (**arg == ']')
4659 empty2 = TRUE;
4660 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4661 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004662 if (!empty1)
4663 clear_tv(&var1);
4664 return FAIL;
4665 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004666 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004667 {
4668 /* not a number or string */
4669 if (!empty1)
4670 clear_tv(&var1);
4671 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004672 return FAIL;
4673 }
4674 }
4675
4676 /* Check for the ']'. */
4677 if (**arg != ']')
4678 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004679 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004680 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004681 clear_tv(&var1);
4682 if (range)
4683 clear_tv(&var2);
4684 return FAIL;
4685 }
4686 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004687 }
4688
4689 if (evaluate)
4690 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004691 n1 = 0;
4692 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004693 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004694 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004695 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004696 }
4697 if (range)
4698 {
4699 if (empty2)
4700 n2 = -1;
4701 else
4702 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004703 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004704 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004705 }
4706 }
4707
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004708 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004709 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004710 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004711 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004712 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004713 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004714 case VAR_SPECIAL:
4715 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004716 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004717 break; /* not evaluating, skipping over subscript */
4718
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004719 case VAR_NUMBER:
4720 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004721 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004722 len = (long)STRLEN(s);
4723 if (range)
4724 {
4725 /* The resulting variable is a substring. If the indexes
4726 * are out of range the result is empty. */
4727 if (n1 < 0)
4728 {
4729 n1 = len + n1;
4730 if (n1 < 0)
4731 n1 = 0;
4732 }
4733 if (n2 < 0)
4734 n2 = len + n2;
4735 else if (n2 >= len)
4736 n2 = len;
4737 if (n1 >= len || n2 < 0 || n1 > n2)
4738 s = NULL;
4739 else
4740 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4741 }
4742 else
4743 {
4744 /* The resulting variable is a string of a single
4745 * character. If the index is too big or negative the
4746 * result is empty. */
4747 if (n1 >= len || n1 < 0)
4748 s = NULL;
4749 else
4750 s = vim_strnsave(s + n1, 1);
4751 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004752 clear_tv(rettv);
4753 rettv->v_type = VAR_STRING;
4754 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004755 break;
4756
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004757 case VAR_BLOB:
4758 len = blob_len(rettv->vval.v_blob);
4759 if (range)
4760 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01004761 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004762 // are out of range the result is empty.
4763 if (n1 < 0)
4764 {
4765 n1 = len + n1;
4766 if (n1 < 0)
4767 n1 = 0;
4768 }
4769 if (n2 < 0)
4770 n2 = len + n2;
4771 else if (n2 >= len)
4772 n2 = len - 1;
4773 if (n1 >= len || n2 < 0 || n1 > n2)
4774 {
4775 clear_tv(rettv);
4776 rettv->v_type = VAR_BLOB;
4777 rettv->vval.v_blob = NULL;
4778 }
4779 else
4780 {
4781 blob_T *blob = blob_alloc();
4782
4783 if (blob != NULL)
4784 {
4785 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
4786 {
4787 blob_free(blob);
4788 return FAIL;
4789 }
4790 blob->bv_ga.ga_len = n2 - n1 + 1;
4791 for (i = n1; i <= n2; i++)
4792 blob_set(blob, i - n1,
4793 blob_get(rettv->vval.v_blob, i));
4794
4795 clear_tv(rettv);
4796 rettv_blob_set(rettv, blob);
4797 }
4798 }
4799 }
4800 else
4801 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01004802 // The resulting variable is a byte value.
4803 // If the index is too big or negative that is an error.
4804 if (n1 < 0)
4805 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004806 if (n1 < len && n1 >= 0)
4807 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01004808 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004809
4810 clear_tv(rettv);
4811 rettv->v_type = VAR_NUMBER;
4812 rettv->vval.v_number = v;
4813 }
4814 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004815 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004816 }
4817 break;
4818
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004819 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004820 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004821 if (n1 < 0)
4822 n1 = len + n1;
4823 if (!empty1 && (n1 < 0 || n1 >= len))
4824 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004825 /* For a range we allow invalid values and return an empty
4826 * list. A list index out of range is an error. */
4827 if (!range)
4828 {
4829 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004830 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004831 return FAIL;
4832 }
4833 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004834 }
4835 if (range)
4836 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004837 list_T *l;
4838 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004839
4840 if (n2 < 0)
4841 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004842 else if (n2 >= len)
4843 n2 = len - 1;
4844 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004845 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004846 l = list_alloc();
4847 if (l == NULL)
4848 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004849 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004850 n1 <= n2; ++n1)
4851 {
4852 if (list_append_tv(l, &item->li_tv) == FAIL)
4853 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004854 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004855 return FAIL;
4856 }
4857 item = item->li_next;
4858 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004859 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004860 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004861 }
4862 else
4863 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004864 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004865 clear_tv(rettv);
4866 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004867 }
4868 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004869
4870 case VAR_DICT:
4871 if (range)
4872 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004873 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004874 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004875 if (len == -1)
4876 clear_tv(&var1);
4877 return FAIL;
4878 }
4879 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004880 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004881
4882 if (len == -1)
4883 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004884 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02004885 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004886 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004887 clear_tv(&var1);
4888 return FAIL;
4889 }
4890 }
4891
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004892 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004893
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004894 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004895 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004896 if (len == -1)
4897 clear_tv(&var1);
4898 if (item == NULL)
4899 return FAIL;
4900
4901 copy_tv(&item->di_tv, &var1);
4902 clear_tv(rettv);
4903 *rettv = var1;
4904 }
4905 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004906 }
4907 }
4908
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004909 return OK;
4910}
4911
4912/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 * Get an option value.
4914 * "arg" points to the '&' or '+' before the option name.
4915 * "arg" is advanced to character after the option name.
4916 * Return OK or FAIL.
4917 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004918 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004919get_option_tv(
4920 char_u **arg,
4921 typval_T *rettv, /* when NULL, only check if option exists */
4922 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923{
4924 char_u *option_end;
4925 long numval;
4926 char_u *stringval;
4927 int opt_type;
4928 int c;
4929 int working = (**arg == '+'); /* has("+option") */
4930 int ret = OK;
4931 int opt_flags;
4932
4933 /*
4934 * Isolate the option name and find its value.
4935 */
4936 option_end = find_option_end(arg, &opt_flags);
4937 if (option_end == NULL)
4938 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004939 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004940 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 return FAIL;
4942 }
4943
4944 if (!evaluate)
4945 {
4946 *arg = option_end;
4947 return OK;
4948 }
4949
4950 c = *option_end;
4951 *option_end = NUL;
4952 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004953 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954
4955 if (opt_type == -3) /* invalid name */
4956 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004957 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004958 semsg(_("E113: Unknown option: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 ret = FAIL;
4960 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004961 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 {
4963 if (opt_type == -2) /* hidden string option */
4964 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004965 rettv->v_type = VAR_STRING;
4966 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 }
4968 else if (opt_type == -1) /* hidden number option */
4969 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004970 rettv->v_type = VAR_NUMBER;
4971 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 }
4973 else if (opt_type == 1) /* number option */
4974 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004975 rettv->v_type = VAR_NUMBER;
4976 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 }
4978 else /* string option */
4979 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004980 rettv->v_type = VAR_STRING;
4981 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 }
4983 }
4984 else if (working && (opt_type == -2 || opt_type == -1))
4985 ret = FAIL;
4986
4987 *option_end = c; /* put back for error messages */
4988 *arg = option_end;
4989
4990 return ret;
4991}
4992
4993/*
4994 * Allocate a variable for a string constant.
4995 * Return OK or FAIL.
4996 */
4997 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004998get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999{
5000 char_u *p;
5001 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 int extra = 0;
5003
5004 /*
5005 * Find the end of the string, skipping backslashed characters.
5006 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005007 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 {
5009 if (*p == '\\' && p[1] != NUL)
5010 {
5011 ++p;
5012 /* A "\<x>" form occupies at least 4 characters, and produces up
5013 * to 6 characters: reserve space for 2 extra */
5014 if (*p == '<')
5015 extra += 2;
5016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 }
5018
5019 if (*p != '"')
5020 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005021 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 return FAIL;
5023 }
5024
5025 /* If only parsing, set *arg and return here */
5026 if (!evaluate)
5027 {
5028 *arg = p + 1;
5029 return OK;
5030 }
5031
5032 /*
5033 * Copy the string into allocated memory, handling backslashed
5034 * characters.
5035 */
5036 name = alloc((unsigned)(p - *arg + extra));
5037 if (name == NULL)
5038 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005039 rettv->v_type = VAR_STRING;
5040 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041
Bram Moolenaar8c711452005-01-14 21:53:12 +00005042 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 {
5044 if (*p == '\\')
5045 {
5046 switch (*++p)
5047 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005048 case 'b': *name++ = BS; ++p; break;
5049 case 'e': *name++ = ESC; ++p; break;
5050 case 'f': *name++ = FF; ++p; break;
5051 case 'n': *name++ = NL; ++p; break;
5052 case 'r': *name++ = CAR; ++p; break;
5053 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054
5055 case 'X': /* hex: "\x1", "\x12" */
5056 case 'x':
5057 case 'u': /* Unicode: "\u0023" */
5058 case 'U':
5059 if (vim_isxdigit(p[1]))
5060 {
5061 int n, nr;
5062 int c = toupper(*p);
5063
5064 if (c == 'X')
5065 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005066 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005068 else
5069 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 nr = 0;
5071 while (--n >= 0 && vim_isxdigit(p[1]))
5072 {
5073 ++p;
5074 nr = (nr << 4) + hex2nr(*p);
5075 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005076 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 /* For "\u" store the number according to
5078 * 'encoding'. */
5079 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005080 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005082 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 break;
5085
5086 /* octal: "\1", "\12", "\123" */
5087 case '0':
5088 case '1':
5089 case '2':
5090 case '3':
5091 case '4':
5092 case '5':
5093 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005094 case '7': *name = *p++ - '0';
5095 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005097 *name = (*name << 3) + *p++ - '0';
5098 if (*p >= '0' && *p <= '7')
5099 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005101 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 break;
5103
5104 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005105 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 if (extra != 0)
5107 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005108 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 break;
5110 }
5111 /* FALLTHROUGH */
5112
Bram Moolenaar8c711452005-01-14 21:53:12 +00005113 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 break;
5115 }
5116 }
5117 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005118 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005121 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02005122 if (*p != NUL) /* just in case */
5123 ++p;
5124 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 return OK;
5127}
5128
5129/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005130 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 * Return OK or FAIL.
5132 */
5133 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005134get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135{
5136 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005137 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005138 int reduce = 0;
5139
5140 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005141 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005142 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005143 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005144 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005145 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005146 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005147 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005148 break;
5149 ++reduce;
5150 ++p;
5151 }
5152 }
5153
Bram Moolenaar8c711452005-01-14 21:53:12 +00005154 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005155 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005156 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005157 return FAIL;
5158 }
5159
Bram Moolenaar8c711452005-01-14 21:53:12 +00005160 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005161 if (!evaluate)
5162 {
5163 *arg = p + 1;
5164 return OK;
5165 }
5166
5167 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005168 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005169 */
5170 str = alloc((unsigned)((p - *arg) - reduce));
5171 if (str == NULL)
5172 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005173 rettv->v_type = VAR_STRING;
5174 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005175
Bram Moolenaar8c711452005-01-14 21:53:12 +00005176 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005177 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005178 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005179 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005180 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005181 break;
5182 ++p;
5183 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005184 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005185 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005186 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005187 *arg = p + 1;
5188
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005189 return OK;
5190}
5191
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005192/*
5193 * Return the function name of the partial.
5194 */
5195 char_u *
5196partial_name(partial_T *pt)
5197{
5198 if (pt->pt_name != NULL)
5199 return pt->pt_name;
5200 return pt->pt_func->uf_name;
5201}
5202
Bram Moolenaarddecc252016-04-06 22:59:37 +02005203 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005204partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005205{
5206 int i;
5207
5208 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005209 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005210 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005211 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005212 if (pt->pt_name != NULL)
5213 {
5214 func_unref(pt->pt_name);
5215 vim_free(pt->pt_name);
5216 }
5217 else
5218 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005219 vim_free(pt);
5220}
5221
5222/*
5223 * Unreference a closure: decrement the reference count and free it when it
5224 * becomes zero.
5225 */
5226 void
5227partial_unref(partial_T *pt)
5228{
5229 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005230 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005231}
5232
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005233static int tv_equal_recurse_limit;
5234
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005235 static int
5236func_equal(
5237 typval_T *tv1,
5238 typval_T *tv2,
5239 int ic) /* ignore case */
5240{
5241 char_u *s1, *s2;
5242 dict_T *d1, *d2;
5243 int a1, a2;
5244 int i;
5245
5246 /* empty and NULL function name considered the same */
5247 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005248 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005249 if (s1 != NULL && *s1 == NUL)
5250 s1 = NULL;
5251 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005252 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005253 if (s2 != NULL && *s2 == NUL)
5254 s2 = NULL;
5255 if (s1 == NULL || s2 == NULL)
5256 {
5257 if (s1 != s2)
5258 return FALSE;
5259 }
5260 else if (STRCMP(s1, s2) != 0)
5261 return FALSE;
5262
5263 /* empty dict and NULL dict is different */
5264 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5265 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5266 if (d1 == NULL || d2 == NULL)
5267 {
5268 if (d1 != d2)
5269 return FALSE;
5270 }
5271 else if (!dict_equal(d1, d2, ic, TRUE))
5272 return FALSE;
5273
5274 /* empty list and no list considered the same */
5275 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5276 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5277 if (a1 != a2)
5278 return FALSE;
5279 for (i = 0; i < a1; ++i)
5280 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5281 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5282 return FALSE;
5283
5284 return TRUE;
5285}
5286
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005287/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005288 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005289 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005290 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005291 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005292 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005293tv_equal(
5294 typval_T *tv1,
5295 typval_T *tv2,
5296 int ic, /* ignore case */
5297 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005298{
5299 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005300 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005301 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005302 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005303
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005304 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005305 * recursiveness to a limit. We guess they are equal then.
5306 * A fixed limit has the problem of still taking an awful long time.
5307 * Reduce the limit every time running into it. That should work fine for
5308 * deeply linked structures that are not recursively linked and catch
5309 * recursiveness quickly. */
5310 if (!recursive)
5311 tv_equal_recurse_limit = 1000;
5312 if (recursive_cnt >= tv_equal_recurse_limit)
5313 {
5314 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005315 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005316 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005317
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005318 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5319 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005320 if ((tv1->v_type == VAR_FUNC
5321 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5322 && (tv2->v_type == VAR_FUNC
5323 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5324 {
5325 ++recursive_cnt;
5326 r = func_equal(tv1, tv2, ic);
5327 --recursive_cnt;
5328 return r;
5329 }
5330
5331 if (tv1->v_type != tv2->v_type)
5332 return FALSE;
5333
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005334 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005335 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005336 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005337 ++recursive_cnt;
5338 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5339 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005340 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005341
5342 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005343 ++recursive_cnt;
5344 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5345 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005346 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005347
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005348 case VAR_BLOB:
5349 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
5350
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005351 case VAR_NUMBER:
5352 return tv1->vval.v_number == tv2->vval.v_number;
5353
5354 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005355 s1 = tv_get_string_buf(tv1, buf1);
5356 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005357 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005358
5359 case VAR_SPECIAL:
5360 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005361
5362 case VAR_FLOAT:
5363#ifdef FEAT_FLOAT
5364 return tv1->vval.v_float == tv2->vval.v_float;
5365#endif
5366 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005367#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005368 return tv1->vval.v_job == tv2->vval.v_job;
5369#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005370 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005371#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005372 return tv1->vval.v_channel == tv2->vval.v_channel;
5373#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005374 case VAR_FUNC:
5375 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005376 case VAR_UNKNOWN:
5377 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005378 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005379
Bram Moolenaara03f2332016-02-06 18:09:59 +01005380 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5381 * does not equal anything, not even itself. */
5382 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005383}
5384
5385/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005386 * Return the next (unique) copy ID.
5387 * Used for serializing nested structures.
5388 */
5389 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005390get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005391{
5392 current_copyID += COPYID_INC;
5393 return current_copyID;
5394}
5395
5396/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005397 * Garbage collection for lists and dictionaries.
5398 *
5399 * We use reference counts to be able to free most items right away when they
5400 * are no longer used. But for composite items it's possible that it becomes
5401 * unused while the reference count is > 0: When there is a recursive
5402 * reference. Example:
5403 * :let l = [1, 2, 3]
5404 * :let d = {9: l}
5405 * :let l[1] = d
5406 *
5407 * Since this is quite unusual we handle this with garbage collection: every
5408 * once in a while find out which lists and dicts are not referenced from any
5409 * variable.
5410 *
5411 * Here is a good reference text about garbage collection (refers to Python
5412 * but it applies to all reference-counting mechanisms):
5413 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005414 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005415
5416/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005417 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005418 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005419 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005420 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005421 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005422garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005423{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005424 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005425 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005426 buf_T *buf;
5427 win_T *wp;
5428 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005429 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005430 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005431
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005432 if (!testing)
5433 {
5434 /* Only do this once. */
5435 want_garbage_collect = FALSE;
5436 may_garbage_collect = FALSE;
5437 garbage_collect_at_exit = FALSE;
5438 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005439
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005440 /* We advance by two because we add one for items referenced through
5441 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005442 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005443
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005444 /*
5445 * 1. Go through all accessible variables and mark all lists and dicts
5446 * with copyID.
5447 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005448
5449 /* Don't free variables in the previous_funccal list unless they are only
5450 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005451 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005452 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005453
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005454 /* script-local variables */
5455 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005456 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005457
5458 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005459 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005460 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5461 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005462
5463 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005464 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005465 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5466 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005467 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005468 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5469 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005470
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005471 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005472 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005473 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5474 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005475 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005476 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005477
5478 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005479 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005480
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005481 /* named functions (matters for closures) */
5482 abort = abort || set_ref_in_functions(copyID);
5483
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005484 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005485 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005486
Bram Moolenaard812df62008-11-09 12:46:09 +00005487 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005488 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005489
Bram Moolenaar1dced572012-04-05 16:54:08 +02005490#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005491 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005492#endif
5493
Bram Moolenaardb913952012-06-29 12:54:53 +02005494#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005495 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005496#endif
5497
5498#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005499 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005500#endif
5501
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005502#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005503 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005504 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005505#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005506#ifdef FEAT_NETBEANS_INTG
5507 abort = abort || set_ref_in_nb_channel(copyID);
5508#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005509
Bram Moolenaare3188e22016-05-31 21:13:04 +02005510#ifdef FEAT_TIMERS
5511 abort = abort || set_ref_in_timer(copyID);
5512#endif
5513
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005514#ifdef FEAT_QUICKFIX
5515 abort = abort || set_ref_in_quickfix(copyID);
5516#endif
5517
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005518#ifdef FEAT_TERMINAL
5519 abort = abort || set_ref_in_term(copyID);
5520#endif
5521
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005522 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005523 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005524 /*
5525 * 2. Free lists and dictionaries that are not referenced.
5526 */
5527 did_free = free_unref_items(copyID);
5528
5529 /*
5530 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005531 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005532 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005533 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005534 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005535 else if (p_verbose > 0)
5536 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005537 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005538 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005539
5540 return did_free;
5541}
5542
5543/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005544 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005545 */
5546 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005547free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005548{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005549 int did_free = FALSE;
5550
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005551 /* Let all "free" functions know that we are here. This means no
5552 * dictionaries, lists, channels or jobs are to be freed, because we will
5553 * do that here. */
5554 in_free_unref_items = TRUE;
5555
5556 /*
5557 * PASS 1: free the contents of the items. We don't free the items
5558 * themselves yet, so that it is possible to decrement refcount counters
5559 */
5560
Bram Moolenaarcd524592016-07-17 14:57:05 +02005561 /* Go through the list of dicts and free items without the copyID. */
5562 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005563
Bram Moolenaarda861d62016-07-17 15:46:27 +02005564 /* Go through the list of lists and free items without the copyID. */
5565 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005566
5567#ifdef FEAT_JOB_CHANNEL
5568 /* Go through the list of jobs and free items without the copyID. This
5569 * must happen before doing channels, because jobs refer to channels, but
5570 * the reference from the channel to the job isn't tracked. */
5571 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5572
5573 /* Go through the list of channels and free items without the copyID. */
5574 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5575#endif
5576
5577 /*
5578 * PASS 2: free the items themselves.
5579 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005580 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005581 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005582
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005583#ifdef FEAT_JOB_CHANNEL
5584 /* Go through the list of jobs and free items without the copyID. This
5585 * must happen before doing channels, because jobs refer to channels, but
5586 * the reference from the channel to the job isn't tracked. */
5587 free_unused_jobs(copyID, COPYID_MASK);
5588
5589 /* Go through the list of channels and free items without the copyID. */
5590 free_unused_channels(copyID, COPYID_MASK);
5591#endif
5592
5593 in_free_unref_items = FALSE;
5594
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005595 return did_free;
5596}
5597
5598/*
5599 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005600 * "list_stack" is used to add lists to be marked. Can be NULL.
5601 *
5602 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005603 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005604 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005605set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005606{
5607 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005608 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005609 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005610 hashtab_T *cur_ht;
5611 ht_stack_T *ht_stack = NULL;
5612 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005613
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005614 cur_ht = ht;
5615 for (;;)
5616 {
5617 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005618 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005619 /* Mark each item in the hashtab. If the item contains a hashtab
5620 * it is added to ht_stack, if it contains a list it is added to
5621 * list_stack. */
5622 todo = (int)cur_ht->ht_used;
5623 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5624 if (!HASHITEM_EMPTY(hi))
5625 {
5626 --todo;
5627 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5628 &ht_stack, list_stack);
5629 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005630 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005631
5632 if (ht_stack == NULL)
5633 break;
5634
5635 /* take an item from the stack */
5636 cur_ht = ht_stack->ht;
5637 tempitem = ht_stack;
5638 ht_stack = ht_stack->prev;
5639 free(tempitem);
5640 }
5641
5642 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005643}
5644
5645/*
5646 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005647 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5648 *
5649 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005650 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005651 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005652set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005653{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005654 listitem_T *li;
5655 int abort = FALSE;
5656 list_T *cur_l;
5657 list_stack_T *list_stack = NULL;
5658 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005659
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005660 cur_l = l;
5661 for (;;)
5662 {
5663 if (!abort)
5664 /* Mark each item in the list. If the item contains a hashtab
5665 * it is added to ht_stack, if it contains a list it is added to
5666 * list_stack. */
5667 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5668 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5669 ht_stack, &list_stack);
5670 if (list_stack == NULL)
5671 break;
5672
5673 /* take an item from the stack */
5674 cur_l = list_stack->list;
5675 tempitem = list_stack;
5676 list_stack = list_stack->prev;
5677 free(tempitem);
5678 }
5679
5680 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005681}
5682
5683/*
5684 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005685 * "list_stack" is used to add lists to be marked. Can be NULL.
5686 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5687 *
5688 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005689 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005690 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005691set_ref_in_item(
5692 typval_T *tv,
5693 int copyID,
5694 ht_stack_T **ht_stack,
5695 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005696{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005697 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005698
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005699 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005700 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005701 dict_T *dd = tv->vval.v_dict;
5702
Bram Moolenaara03f2332016-02-06 18:09:59 +01005703 if (dd != NULL && dd->dv_copyID != copyID)
5704 {
5705 /* Didn't see this dict yet. */
5706 dd->dv_copyID = copyID;
5707 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005708 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005709 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5710 }
5711 else
5712 {
5713 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5714 if (newitem == NULL)
5715 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005716 else
5717 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005718 newitem->ht = &dd->dv_hashtab;
5719 newitem->prev = *ht_stack;
5720 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005721 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005722 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005723 }
5724 }
5725 else if (tv->v_type == VAR_LIST)
5726 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005727 list_T *ll = tv->vval.v_list;
5728
Bram Moolenaara03f2332016-02-06 18:09:59 +01005729 if (ll != NULL && ll->lv_copyID != copyID)
5730 {
5731 /* Didn't see this list yet. */
5732 ll->lv_copyID = copyID;
5733 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005734 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005735 abort = set_ref_in_list(ll, copyID, ht_stack);
5736 }
5737 else
5738 {
5739 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005740 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005741 if (newitem == NULL)
5742 abort = TRUE;
5743 else
5744 {
5745 newitem->list = ll;
5746 newitem->prev = *list_stack;
5747 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005748 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005749 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005750 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005751 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005752 else if (tv->v_type == VAR_FUNC)
5753 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005754 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005755 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005756 else if (tv->v_type == VAR_PARTIAL)
5757 {
5758 partial_T *pt = tv->vval.v_partial;
5759 int i;
5760
5761 /* A partial does not have a copyID, because it cannot contain itself.
5762 */
5763 if (pt != NULL)
5764 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005765 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005766
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005767 if (pt->pt_dict != NULL)
5768 {
5769 typval_T dtv;
5770
5771 dtv.v_type = VAR_DICT;
5772 dtv.vval.v_dict = pt->pt_dict;
5773 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5774 }
5775
5776 for (i = 0; i < pt->pt_argc; ++i)
5777 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5778 ht_stack, list_stack);
5779 }
5780 }
5781#ifdef FEAT_JOB_CHANNEL
5782 else if (tv->v_type == VAR_JOB)
5783 {
5784 job_T *job = tv->vval.v_job;
5785 typval_T dtv;
5786
5787 if (job != NULL && job->jv_copyID != copyID)
5788 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005789 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005790 if (job->jv_channel != NULL)
5791 {
5792 dtv.v_type = VAR_CHANNEL;
5793 dtv.vval.v_channel = job->jv_channel;
5794 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5795 }
5796 if (job->jv_exit_partial != NULL)
5797 {
5798 dtv.v_type = VAR_PARTIAL;
5799 dtv.vval.v_partial = job->jv_exit_partial;
5800 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5801 }
5802 }
5803 }
5804 else if (tv->v_type == VAR_CHANNEL)
5805 {
5806 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005807 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005808 typval_T dtv;
5809 jsonq_T *jq;
5810 cbq_T *cq;
5811
5812 if (ch != NULL && ch->ch_copyID != copyID)
5813 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005814 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005815 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005816 {
5817 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5818 jq = jq->jq_next)
5819 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5820 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5821 cq = cq->cq_next)
5822 if (cq->cq_partial != NULL)
5823 {
5824 dtv.v_type = VAR_PARTIAL;
5825 dtv.vval.v_partial = cq->cq_partial;
5826 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5827 }
5828 if (ch->ch_part[part].ch_partial != NULL)
5829 {
5830 dtv.v_type = VAR_PARTIAL;
5831 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5832 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5833 }
5834 }
5835 if (ch->ch_partial != NULL)
5836 {
5837 dtv.v_type = VAR_PARTIAL;
5838 dtv.vval.v_partial = ch->ch_partial;
5839 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5840 }
5841 if (ch->ch_close_partial != NULL)
5842 {
5843 dtv.v_type = VAR_PARTIAL;
5844 dtv.vval.v_partial = ch->ch_close_partial;
5845 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5846 }
5847 }
5848 }
5849#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005850 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005851}
5852
Bram Moolenaar17a13432016-01-24 14:22:10 +01005853 static char *
5854get_var_special_name(int nr)
5855{
5856 switch (nr)
5857 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005858 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005859 case VVAL_TRUE: return "v:true";
5860 case VVAL_NONE: return "v:none";
5861 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005862 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01005863 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01005864 return "42";
5865}
5866
Bram Moolenaar8c711452005-01-14 21:53:12 +00005867/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005868 * Return a string with the string representation of a variable.
5869 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005870 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005871 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005872 * When both "echo_style" and "composite_val" are FALSE, put quotes around
5873 * stings as "string()", otherwise does not put quotes around strings, as
5874 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005875 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5876 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005877 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005878 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005879 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005880echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005881 typval_T *tv,
5882 char_u **tofree,
5883 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005884 int copyID,
5885 int echo_style,
5886 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005887 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005888{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005889 static int recurse = 0;
5890 char_u *r = NULL;
5891
Bram Moolenaar33570922005-01-25 22:26:29 +00005892 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005893 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005894 if (!did_echo_string_emsg)
5895 {
5896 /* Only give this message once for a recursive call to avoid
5897 * flooding the user with errors. And stop iterating over lists
5898 * and dicts. */
5899 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005900 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005901 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005902 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005903 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005904 }
5905 ++recurse;
5906
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005907 switch (tv->v_type)
5908 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005909 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005910 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005911 {
5912 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005913 r = tv->vval.v_string;
5914 if (r == NULL)
5915 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005916 }
5917 else
5918 {
5919 *tofree = string_quote(tv->vval.v_string, FALSE);
5920 r = *tofree;
5921 }
5922 break;
5923
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005924 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005925 if (echo_style)
5926 {
5927 *tofree = NULL;
5928 r = tv->vval.v_string;
5929 }
5930 else
5931 {
5932 *tofree = string_quote(tv->vval.v_string, TRUE);
5933 r = *tofree;
5934 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005935 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005936
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005937 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005938 {
5939 partial_T *pt = tv->vval.v_partial;
5940 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005941 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005942 garray_T ga;
5943 int i;
5944 char_u *tf;
5945
5946 ga_init2(&ga, 1, 100);
5947 ga_concat(&ga, (char_u *)"function(");
5948 if (fname != NULL)
5949 {
5950 ga_concat(&ga, fname);
5951 vim_free(fname);
5952 }
5953 if (pt != NULL && pt->pt_argc > 0)
5954 {
5955 ga_concat(&ga, (char_u *)", [");
5956 for (i = 0; i < pt->pt_argc; ++i)
5957 {
5958 if (i > 0)
5959 ga_concat(&ga, (char_u *)", ");
5960 ga_concat(&ga,
5961 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5962 vim_free(tf);
5963 }
5964 ga_concat(&ga, (char_u *)"]");
5965 }
5966 if (pt != NULL && pt->pt_dict != NULL)
5967 {
5968 typval_T dtv;
5969
5970 ga_concat(&ga, (char_u *)", ");
5971 dtv.v_type = VAR_DICT;
5972 dtv.vval.v_dict = pt->pt_dict;
5973 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5974 vim_free(tf);
5975 }
5976 ga_concat(&ga, (char_u *)")");
5977
5978 *tofree = ga.ga_data;
5979 r = *tofree;
5980 break;
5981 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005982
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005983 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005984 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005985 break;
5986
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005987 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005988 if (tv->vval.v_list == NULL)
5989 {
5990 *tofree = NULL;
5991 r = NULL;
5992 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005993 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5994 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005995 {
5996 *tofree = NULL;
5997 r = (char_u *)"[...]";
5998 }
5999 else
6000 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006001 int old_copyID = tv->vval.v_list->lv_copyID;
6002
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006003 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006004 *tofree = list2string(tv, copyID, restore_copyID);
6005 if (restore_copyID)
6006 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006007 r = *tofree;
6008 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006009 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006010
Bram Moolenaar8c711452005-01-14 21:53:12 +00006011 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006012 if (tv->vval.v_dict == NULL)
6013 {
6014 *tofree = NULL;
6015 r = NULL;
6016 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006017 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6018 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006019 {
6020 *tofree = NULL;
6021 r = (char_u *)"{...}";
6022 }
6023 else
6024 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006025 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006026 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006027 *tofree = dict2string(tv, copyID, restore_copyID);
6028 if (restore_copyID)
6029 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006030 r = *tofree;
6031 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006032 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006033
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006034 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006035 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006036 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006037 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006038 break;
6039
Bram Moolenaar835dc632016-02-07 14:27:38 +01006040 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006041 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006042 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006043 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006044 if (composite_val)
6045 {
6046 *tofree = string_quote(r, FALSE);
6047 r = *tofree;
6048 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006049 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006050
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006051 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006052#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006053 *tofree = NULL;
6054 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6055 r = numbuf;
6056 break;
6057#endif
6058
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006059 case VAR_SPECIAL:
6060 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006061 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006062 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006063 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006064
Bram Moolenaar8502c702014-06-17 12:51:16 +02006065 if (--recurse == 0)
6066 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006067 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006068}
6069
6070/*
6071 * Return a string with the string representation of a variable.
6072 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6073 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006074 * Does not put quotes around strings, as ":echo" displays values.
6075 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6076 * May return NULL.
6077 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006078 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006079echo_string(
6080 typval_T *tv,
6081 char_u **tofree,
6082 char_u *numbuf,
6083 int copyID)
6084{
6085 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6086}
6087
6088/*
6089 * Return a string with the string representation of a variable.
6090 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6091 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006092 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006093 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006094 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02006095 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006096tv2string(
6097 typval_T *tv,
6098 char_u **tofree,
6099 char_u *numbuf,
6100 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006101{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006102 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006103}
6104
6105/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006106 * Return string "str" in ' quotes, doubling ' characters.
6107 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006108 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006109 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006110 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006111string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006112{
Bram Moolenaar33570922005-01-25 22:26:29 +00006113 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006114 char_u *p, *r, *s;
6115
Bram Moolenaar33570922005-01-25 22:26:29 +00006116 len = (function ? 13 : 3);
6117 if (str != NULL)
6118 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006119 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006120 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00006121 if (*p == '\'')
6122 ++len;
6123 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006124 s = r = alloc(len);
6125 if (r != NULL)
6126 {
6127 if (function)
6128 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006129 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006130 r += 10;
6131 }
6132 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006133 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006134 if (str != NULL)
6135 for (p = str; *p != NUL; )
6136 {
6137 if (*p == '\'')
6138 *r++ = '\'';
6139 MB_COPY_CHAR(p, r);
6140 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006141 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006142 if (function)
6143 *r++ = ')';
6144 *r++ = NUL;
6145 }
6146 return s;
6147}
6148
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006149#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006150/*
6151 * Convert the string "text" to a floating point number.
6152 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
6153 * this always uses a decimal point.
6154 * Returns the length of the text that was consumed.
6155 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006156 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006157string2float(
6158 char_u *text,
6159 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006160{
6161 char *s = (char *)text;
6162 float_T f;
6163
Bram Moolenaar62473612017-01-08 19:25:40 +01006164 /* MS-Windows does not deal with "inf" and "nan" properly. */
6165 if (STRNICMP(text, "inf", 3) == 0)
6166 {
6167 *value = INFINITY;
6168 return 3;
6169 }
6170 if (STRNICMP(text, "-inf", 3) == 0)
6171 {
6172 *value = -INFINITY;
6173 return 4;
6174 }
6175 if (STRNICMP(text, "nan", 3) == 0)
6176 {
6177 *value = NAN;
6178 return 3;
6179 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006180 f = strtod(s, &s);
6181 *value = f;
6182 return (int)((char_u *)s - text);
6183}
6184#endif
6185
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006186/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 * Get the value of an environment variable.
6188 * "arg" is pointing to the '$'. It is advanced to after the name.
6189 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006190 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 */
6192 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006193get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194{
6195 char_u *string = NULL;
6196 int len;
6197 int cc;
6198 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006199 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200
6201 ++*arg;
6202 name = *arg;
6203 len = get_env_len(arg);
6204 if (evaluate)
6205 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006206 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006207 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006208
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006209 cc = name[len];
6210 name[len] = NUL;
6211 /* first try vim_getenv(), fast for normal environment vars */
6212 string = vim_getenv(name, &mustfree);
6213 if (string != NULL && *string != NUL)
6214 {
6215 if (!mustfree)
6216 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006218 else
6219 {
6220 if (mustfree)
6221 vim_free(string);
6222
6223 /* next try expanding things like $VIM and ${HOME} */
6224 string = expand_env_save(name - 1);
6225 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01006226 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006227 }
6228 name[len] = cc;
6229
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006230 rettv->v_type = VAR_STRING;
6231 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 }
6233
6234 return OK;
6235}
6236
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006237
6238
6239/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006241 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006243 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006244var2fpos(
6245 typval_T *varp,
6246 int dollar_lnum, /* TRUE when $ is last line */
6247 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006249 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006251 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252
Bram Moolenaara5525202006-03-02 22:52:09 +00006253 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006254 if (varp->v_type == VAR_LIST)
6255 {
6256 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006257 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006258 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006259 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006260
6261 l = varp->vval.v_list;
6262 if (l == NULL)
6263 return NULL;
6264
6265 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006266 pos.lnum = list_find_nr(l, 0L, &error);
6267 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006268 return NULL; /* invalid line number */
6269
6270 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006271 pos.col = list_find_nr(l, 1L, &error);
6272 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006273 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006274 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006275
6276 /* We accept "$" for the column number: last column. */
6277 li = list_find(l, 1L);
6278 if (li != NULL && li->li_tv.v_type == VAR_STRING
6279 && li->li_tv.vval.v_string != NULL
6280 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6281 pos.col = len + 1;
6282
Bram Moolenaara5525202006-03-02 22:52:09 +00006283 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006284 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006285 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006286 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006287
Bram Moolenaara5525202006-03-02 22:52:09 +00006288 /* Get the virtual offset. Defaults to zero. */
6289 pos.coladd = list_find_nr(l, 2L, &error);
6290 if (error)
6291 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006292
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006293 return &pos;
6294 }
6295
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006296 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006297 if (name == NULL)
6298 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006299 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006301 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6302 {
6303 if (VIsual_active)
6304 return &VIsual;
6305 return &curwin->w_cursor;
6306 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006307 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006309 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6311 return NULL;
6312 return pp;
6313 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006314
Bram Moolenaara5525202006-03-02 22:52:09 +00006315 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006316
Bram Moolenaar477933c2007-07-17 14:32:23 +00006317 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006318 {
6319 pos.col = 0;
6320 if (name[1] == '0') /* "w0": first visible line */
6321 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006322 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006323 /* In silent Ex mode topline is zero, but that's not a valid line
6324 * number; use one instead. */
6325 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006326 return &pos;
6327 }
6328 else if (name[1] == '$') /* "w$": last visible line */
6329 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006330 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006331 /* In silent Ex mode botline is zero, return zero then. */
6332 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006333 return &pos;
6334 }
6335 }
6336 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006338 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 {
6340 pos.lnum = curbuf->b_ml.ml_line_count;
6341 pos.col = 0;
6342 }
6343 else
6344 {
6345 pos.lnum = curwin->w_cursor.lnum;
6346 pos.col = (colnr_T)STRLEN(ml_get_curline());
6347 }
6348 return &pos;
6349 }
6350 return NULL;
6351}
6352
6353/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006354 * Convert list in "arg" into a position and optional file number.
6355 * When "fnump" is NULL there is no file number, only 3 items.
6356 * Note that the column is passed on as-is, the caller may want to decrement
6357 * it to use 1 for the first column.
6358 * Return FAIL when conversion is not possible, doesn't check the position for
6359 * validity.
6360 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006361 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006362list2fpos(
6363 typval_T *arg,
6364 pos_T *posp,
6365 int *fnump,
6366 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006367{
6368 list_T *l = arg->vval.v_list;
6369 long i = 0;
6370 long n;
6371
Bram Moolenaar493c1782014-05-28 14:34:46 +02006372 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6373 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006374 if (arg->v_type != VAR_LIST
6375 || l == NULL
6376 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006377 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006378 return FAIL;
6379
6380 if (fnump != NULL)
6381 {
6382 n = list_find_nr(l, i++, NULL); /* fnum */
6383 if (n < 0)
6384 return FAIL;
6385 if (n == 0)
6386 n = curbuf->b_fnum; /* current buffer */
6387 *fnump = n;
6388 }
6389
6390 n = list_find_nr(l, i++, NULL); /* lnum */
6391 if (n < 0)
6392 return FAIL;
6393 posp->lnum = n;
6394
6395 n = list_find_nr(l, i++, NULL); /* col */
6396 if (n < 0)
6397 return FAIL;
6398 posp->col = n;
6399
Bram Moolenaar493c1782014-05-28 14:34:46 +02006400 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006401 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006402 posp->coladd = 0;
6403 else
6404 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006405
Bram Moolenaar493c1782014-05-28 14:34:46 +02006406 if (curswantp != NULL)
6407 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6408
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006409 return OK;
6410}
6411
6412/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 * Get the length of an environment variable name.
6414 * Advance "arg" to the first character after the name.
6415 * Return 0 for error.
6416 */
6417 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006418get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419{
6420 char_u *p;
6421 int len;
6422
6423 for (p = *arg; vim_isIDc(*p); ++p)
6424 ;
6425 if (p == *arg) /* no name found */
6426 return 0;
6427
6428 len = (int)(p - *arg);
6429 *arg = p;
6430 return len;
6431}
6432
6433/*
6434 * Get the length of the name of a function or internal variable.
6435 * "arg" is advanced to the first non-white character after the name.
6436 * Return 0 if something is wrong.
6437 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006438 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006439get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440{
6441 char_u *p;
6442 int len;
6443
6444 /* Find the end of the name. */
6445 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006446 {
6447 if (*p == ':')
6448 {
6449 /* "s:" is start of "s:var", but "n:" is not and can be used in
6450 * slice "[n:]". Also "xx:" is not a namespace. */
6451 len = (int)(p - *arg);
6452 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6453 || len > 1)
6454 break;
6455 }
6456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 if (p == *arg) /* no name found */
6458 return 0;
6459
6460 len = (int)(p - *arg);
6461 *arg = skipwhite(p);
6462
6463 return len;
6464}
6465
6466/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006467 * Get the length of the name of a variable or function.
6468 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006470 * Return -1 if curly braces expansion failed.
6471 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 * If the name contains 'magic' {}'s, expand them and return the
6473 * expanded name in an allocated string via 'alias' - caller must free.
6474 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006475 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006476get_name_len(
6477 char_u **arg,
6478 char_u **alias,
6479 int evaluate,
6480 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481{
6482 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 char_u *p;
6484 char_u *expr_start;
6485 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486
6487 *alias = NULL; /* default to no alias */
6488
6489 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6490 && (*arg)[2] == (int)KE_SNR)
6491 {
6492 /* hard coded <SNR>, already translated */
6493 *arg += 3;
6494 return get_id_len(arg) + 3;
6495 }
6496 len = eval_fname_script(*arg);
6497 if (len > 0)
6498 {
6499 /* literal "<SID>", "s:" or "<SNR>" */
6500 *arg += len;
6501 }
6502
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006504 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006506 p = find_name_end(*arg, &expr_start, &expr_end,
6507 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 if (expr_start != NULL)
6509 {
6510 char_u *temp_string;
6511
6512 if (!evaluate)
6513 {
6514 len += (int)(p - *arg);
6515 *arg = skipwhite(p);
6516 return len;
6517 }
6518
6519 /*
6520 * Include any <SID> etc in the expanded string:
6521 * Thus the -len here.
6522 */
6523 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6524 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006525 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 *alias = temp_string;
6527 *arg = skipwhite(p);
6528 return (int)STRLEN(temp_string);
6529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530
6531 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006532 // Only give an error when there is something, otherwise it will be
6533 // reported at a higher level.
6534 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006535 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536
6537 return len;
6538}
6539
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006540/*
6541 * Find the end of a variable or function name, taking care of magic braces.
6542 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6543 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006544 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006545 * Return a pointer to just after the name. Equal to "arg" if there is no
6546 * valid name.
6547 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006548 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006549find_name_end(
6550 char_u *arg,
6551 char_u **expr_start,
6552 char_u **expr_end,
6553 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006555 int mb_nest = 0;
6556 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006558 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006560 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006562 *expr_start = NULL;
6563 *expr_end = NULL;
6564 }
6565
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006566 /* Quick check for valid starting character. */
6567 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6568 return arg;
6569
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006570 for (p = arg; *p != NUL
6571 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006572 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006573 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006574 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006575 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006576 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006577 if (*p == '\'')
6578 {
6579 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006580 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006581 ;
6582 if (*p == NUL)
6583 break;
6584 }
6585 else if (*p == '"')
6586 {
6587 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006588 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006589 if (*p == '\\' && p[1] != NUL)
6590 ++p;
6591 if (*p == NUL)
6592 break;
6593 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006594 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6595 {
6596 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006597 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006598 len = (int)(p - arg);
6599 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006600 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006601 break;
6602 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006603
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006604 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006606 if (*p == '[')
6607 ++br_nest;
6608 else if (*p == ']')
6609 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006611
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006612 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006614 if (*p == '{')
6615 {
6616 mb_nest++;
6617 if (expr_start != NULL && *expr_start == NULL)
6618 *expr_start = p;
6619 }
6620 else if (*p == '}')
6621 {
6622 mb_nest--;
6623 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6624 *expr_end = p;
6625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 }
6628
6629 return p;
6630}
6631
6632/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006633 * Expands out the 'magic' {}'s in a variable/function name.
6634 * Note that this can call itself recursively, to deal with
6635 * constructs like foo{bar}{baz}{bam}
6636 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6637 * "in_start" ^
6638 * "expr_start" ^
6639 * "expr_end" ^
6640 * "in_end" ^
6641 *
6642 * Returns a new allocated string, which the caller must free.
6643 * Returns NULL for failure.
6644 */
6645 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006646make_expanded_name(
6647 char_u *in_start,
6648 char_u *expr_start,
6649 char_u *expr_end,
6650 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006651{
6652 char_u c1;
6653 char_u *retval = NULL;
6654 char_u *temp_result;
6655 char_u *nextcmd = NULL;
6656
6657 if (expr_end == NULL || in_end == NULL)
6658 return NULL;
6659 *expr_start = NUL;
6660 *expr_end = NUL;
6661 c1 = *in_end;
6662 *in_end = NUL;
6663
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006664 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006665 if (temp_result != NULL && nextcmd == NULL)
6666 {
6667 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6668 + (in_end - expr_end) + 1));
6669 if (retval != NULL)
6670 {
6671 STRCPY(retval, in_start);
6672 STRCAT(retval, temp_result);
6673 STRCAT(retval, expr_end + 1);
6674 }
6675 }
6676 vim_free(temp_result);
6677
6678 *in_end = c1; /* put char back for error messages */
6679 *expr_start = '{';
6680 *expr_end = '}';
6681
6682 if (retval != NULL)
6683 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006684 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006685 if (expr_start != NULL)
6686 {
6687 /* Further expansion! */
6688 temp_result = make_expanded_name(retval, expr_start,
6689 expr_end, temp_result);
6690 vim_free(retval);
6691 retval = temp_result;
6692 }
6693 }
6694
6695 return retval;
6696}
6697
6698/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006700 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006702 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006703eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006705 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6706}
6707
6708/*
6709 * Return TRUE if character "c" can be used as the first character in a
6710 * variable or function name (excluding '{' and '}').
6711 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006712 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006713eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006714{
6715 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716}
6717
6718/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 * Set number v: variable to "val".
6720 */
6721 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006722set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006724 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725}
6726
6727/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006728 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006730 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006731get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006733 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734}
6735
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006736/*
6737 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01006738 * If the String variable has never been set, return an empty string.
6739 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006740 */
6741 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006742get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006743{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006744 return tv_get_string(&vimvars[idx].vv_tv);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006745}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006746
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006748 * Get List v: variable value. Caller must take care of reference count when
6749 * needed.
6750 */
6751 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006752get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006753{
6754 return vimvars[idx].vv_list;
6755}
6756
6757/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006758 * Get Dict v: variable value. Caller must take care of reference count when
6759 * needed.
6760 */
6761 dict_T *
6762get_vim_var_dict(int idx)
6763{
6764 return vimvars[idx].vv_dict;
6765}
6766
6767/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006768 * Set v:char to character "c".
6769 */
6770 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006771set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006772{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006773 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006774
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006775 if (has_mbyte)
6776 buf[(*mb_char2bytes)(c, buf)] = NUL;
6777 else
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006778 {
6779 buf[0] = c;
6780 buf[1] = NUL;
6781 }
6782 set_vim_var_string(VV_CHAR, buf, -1);
6783}
6784
6785/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006786 * Set v:count to "count" and v:count1 to "count1".
6787 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 */
6789 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006790set_vcount(
6791 long count,
6792 long count1,
6793 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006795 if (set_prevcount)
6796 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006797 vimvars[VV_COUNT].vv_nr = count;
6798 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799}
6800
6801/*
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02006802 * Save variables that might be changed as a side effect. Used when executing
6803 * a timer callback.
6804 */
6805 void
6806save_vimvars(vimvars_save_T *vvsave)
6807{
6808 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
6809 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
6810 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
6811}
6812
6813/*
6814 * Restore variables saved by save_vimvars().
6815 */
6816 void
6817restore_vimvars(vimvars_save_T *vvsave)
6818{
6819 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
6820 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
6821 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
6822}
6823
6824/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 * Set string v: variable to a copy of "val".
6826 */
6827 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006828set_vim_var_string(
6829 int idx,
6830 char_u *val,
6831 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832{
Bram Moolenaara542c682016-01-31 16:28:04 +01006833 clear_tv(&vimvars[idx].vv_di.di_tv);
6834 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006836 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006838 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006840 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841}
6842
6843/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006844 * Set List v: variable to "val".
6845 */
6846 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006847set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006848{
Bram Moolenaara542c682016-01-31 16:28:04 +01006849 clear_tv(&vimvars[idx].vv_di.di_tv);
6850 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006851 vimvars[idx].vv_list = val;
6852 if (val != NULL)
6853 ++val->lv_refcount;
6854}
6855
6856/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006857 * Set Dictionary v: variable to "val".
6858 */
6859 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006860set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006861{
Bram Moolenaara542c682016-01-31 16:28:04 +01006862 clear_tv(&vimvars[idx].vv_di.di_tv);
6863 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006864 vimvars[idx].vv_dict = val;
6865 if (val != NULL)
6866 {
6867 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006868 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02006869 }
6870}
6871
6872/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 * Set v:register if needed.
6874 */
6875 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006876set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877{
6878 char_u regname;
6879
6880 if (c == 0 || c == ' ')
6881 regname = '"';
6882 else
6883 regname = c;
6884 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006885 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 set_vim_var_string(VV_REG, &regname, 1);
6887}
6888
6889/*
6890 * Get or set v:exception. If "oldval" == NULL, return the current value.
6891 * Otherwise, restore the value to "oldval" and return NULL.
6892 * Must always be called in pairs to save and restore v:exception! Does not
6893 * take care of memory allocations.
6894 */
6895 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006896v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897{
6898 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006899 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900
Bram Moolenaare9a41262005-01-15 22:18:47 +00006901 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 return NULL;
6903}
6904
6905/*
6906 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
6907 * Otherwise, restore the value to "oldval" and return NULL.
6908 * Must always be called in pairs to save and restore v:throwpoint! Does not
6909 * take care of memory allocations.
6910 */
6911 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006912v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913{
6914 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006915 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916
Bram Moolenaare9a41262005-01-15 22:18:47 +00006917 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 return NULL;
6919}
6920
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921/*
6922 * Set v:cmdarg.
6923 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6924 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6925 * Must always be called in pairs!
6926 */
6927 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006928set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929{
6930 char_u *oldval;
6931 char_u *newval;
6932 unsigned len;
6933
Bram Moolenaare9a41262005-01-15 22:18:47 +00006934 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006935 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006937 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006938 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006939 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 }
6941
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006942 if (eap->force_bin == FORCE_BIN)
6943 len = 6;
6944 else if (eap->force_bin == FORCE_NOBIN)
6945 len = 8;
6946 else
6947 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006948
6949 if (eap->read_edit)
6950 len += 7;
6951
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006952 if (eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006953 len += 10; /* " ++ff=unix" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006954 if (eap->force_enc != 0)
6955 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006956 if (eap->bad_char != 0)
6957 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006958
6959 newval = alloc(len + 1);
6960 if (newval == NULL)
6961 return NULL;
6962
6963 if (eap->force_bin == FORCE_BIN)
6964 sprintf((char *)newval, " ++bin");
6965 else if (eap->force_bin == FORCE_NOBIN)
6966 sprintf((char *)newval, " ++nobin");
6967 else
6968 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006969
6970 if (eap->read_edit)
6971 STRCAT(newval, " ++edit");
6972
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006973 if (eap->force_ff != 0)
6974 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006975 eap->force_ff == 'u' ? "unix"
6976 : eap->force_ff == 'd' ? "dos"
6977 : "mac");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006978 if (eap->force_enc != 0)
6979 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6980 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006981 if (eap->bad_char == BAD_KEEP)
6982 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6983 else if (eap->bad_char == BAD_DROP)
6984 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6985 else if (eap->bad_char != 0)
6986 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006987 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006988 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990
6991/*
6992 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006993 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006995 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006996get_var_tv(
6997 char_u *name,
6998 int len, /* length of "name" */
6999 typval_T *rettv, /* NULL when only checking existence */
7000 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
7001 int verbose, /* may give error message */
7002 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003{
7004 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007005 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007006 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008
7009 /* truncate the name, so that we can use strcmp() */
7010 cc = name[len];
7011 name[len] = NUL;
7012
7013 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 * Check for user-defined variables.
7015 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01007016 v = find_var(name, NULL, no_autoload);
7017 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007019 tv = &v->di_tv;
7020 if (dip != NULL)
7021 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 }
7023
Bram Moolenaare9a41262005-01-15 22:18:47 +00007024 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007026 if (rettv != NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007027 semsg(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 ret = FAIL;
7029 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007030 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007031 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032
7033 name[len] = cc;
7034
7035 return ret;
7036}
7037
7038/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007039 * Check if variable "name[len]" is a local variable or an argument.
7040 * If so, "*eval_lavars_used" is set to TRUE.
7041 */
7042 static void
7043check_vars(char_u *name, int len)
7044{
7045 int cc;
7046 char_u *varname;
7047 hashtab_T *ht;
7048
7049 if (eval_lavars_used == NULL)
7050 return;
7051
7052 /* truncate the name, so that we can use strcmp() */
7053 cc = name[len];
7054 name[len] = NUL;
7055
7056 ht = find_var_ht(name, &varname);
7057 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
7058 {
7059 if (find_var(name, NULL, TRUE) != NULL)
7060 *eval_lavars_used = TRUE;
7061 }
7062
7063 name[len] = cc;
7064}
7065
7066/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007067 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
7068 * Also handle function call with Funcref variable: func(expr)
7069 * Can all be combined: dict.func(expr)[idx]['func'](expr)
7070 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007071 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007072handle_subscript(
7073 char_u **arg,
7074 typval_T *rettv,
7075 int evaluate, /* do more than finding the end */
7076 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007077{
7078 int ret = OK;
7079 dict_T *selfdict = NULL;
7080 char_u *s;
7081 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007082 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007083
7084 while (ret == OK
7085 && (**arg == '['
7086 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007087 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7088 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01007089 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007090 {
7091 if (**arg == '(')
7092 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007093 partial_T *pt = NULL;
7094
Bram Moolenaard9fba312005-06-26 22:34:35 +00007095 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007096 if (evaluate)
7097 {
7098 functv = *rettv;
7099 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007100
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007101 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007102 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007103 {
7104 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007105 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007106 }
7107 else
7108 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007109 }
7110 else
7111 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007112 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00007113 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007114 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007115
7116 /* Clear the funcref afterwards, so that deleting it while
7117 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007118 if (evaluate)
7119 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007120
7121 /* Stop the expression evaluation when immediately aborting on
7122 * error, or when an interrupt occurred or an exception was thrown
7123 * but not caught. */
7124 if (aborting())
7125 {
7126 if (ret == OK)
7127 clear_tv(rettv);
7128 ret = FAIL;
7129 }
7130 dict_unref(selfdict);
7131 selfdict = NULL;
7132 }
7133 else /* **arg == '[' || **arg == '.' */
7134 {
7135 dict_unref(selfdict);
7136 if (rettv->v_type == VAR_DICT)
7137 {
7138 selfdict = rettv->vval.v_dict;
7139 if (selfdict != NULL)
7140 ++selfdict->dv_refcount;
7141 }
7142 else
7143 selfdict = NULL;
7144 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
7145 {
7146 clear_tv(rettv);
7147 ret = FAIL;
7148 }
7149 }
7150 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007151
Bram Moolenaar1d429612016-05-24 15:44:17 +02007152 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
7153 * Don't do this when "Func" is already a partial that was bound
7154 * explicitly (pt_auto is FALSE). */
7155 if (selfdict != NULL
7156 && (rettv->v_type == VAR_FUNC
7157 || (rettv->v_type == VAR_PARTIAL
7158 && (rettv->vval.v_partial->pt_auto
7159 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007160 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007161
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007162 dict_unref(selfdict);
7163 return ret;
7164}
7165
7166/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007167 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007168 * value).
7169 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01007170 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007171alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007172{
Bram Moolenaar33570922005-01-25 22:26:29 +00007173 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007174}
7175
7176/*
7177 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 * The string "s" must have been allocated, it is consumed.
7179 * Return NULL for out of memory, the variable otherwise.
7180 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007181 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007182alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183{
Bram Moolenaar33570922005-01-25 22:26:29 +00007184 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007186 rettv = alloc_tv();
7187 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007189 rettv->v_type = VAR_STRING;
7190 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 }
7192 else
7193 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007194 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195}
7196
7197/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007198 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007200 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007201free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202{
7203 if (varp != NULL)
7204 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007205 switch (varp->v_type)
7206 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007207 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007208 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007209 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007210 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007211 vim_free(varp->vval.v_string);
7212 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007213 case VAR_PARTIAL:
7214 partial_unref(varp->vval.v_partial);
7215 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007216 case VAR_BLOB:
7217 blob_unref(varp->vval.v_blob);
7218 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007219 case VAR_LIST:
7220 list_unref(varp->vval.v_list);
7221 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007222 case VAR_DICT:
7223 dict_unref(varp->vval.v_dict);
7224 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007225 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007226#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007227 job_unref(varp->vval.v_job);
7228 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007229#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007230 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007231#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007232 channel_unref(varp->vval.v_channel);
7233 break;
7234#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007235 case VAR_NUMBER:
7236 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007237 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007238 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007239 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 vim_free(varp);
7242 }
7243}
7244
7245/*
7246 * Free the memory for a variable value and set the value to NULL or 0.
7247 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007248 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007249clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250{
7251 if (varp != NULL)
7252 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007253 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007255 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007256 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007257 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007258 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007259 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007260 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007261 case VAR_PARTIAL:
7262 partial_unref(varp->vval.v_partial);
7263 varp->vval.v_partial = NULL;
7264 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007265 case VAR_BLOB:
7266 blob_unref(varp->vval.v_blob);
7267 varp->vval.v_blob = NULL;
7268 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007269 case VAR_LIST:
7270 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007271 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007272 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007273 case VAR_DICT:
7274 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007275 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007276 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007277 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007278 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007279 varp->vval.v_number = 0;
7280 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007281 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007282#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007283 varp->vval.v_float = 0.0;
7284 break;
7285#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007286 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007287#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007288 job_unref(varp->vval.v_job);
7289 varp->vval.v_job = NULL;
7290#endif
7291 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007292 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007293#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007294 channel_unref(varp->vval.v_channel);
7295 varp->vval.v_channel = NULL;
7296#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007297 case VAR_UNKNOWN:
7298 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007300 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 }
7302}
7303
7304/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007305 * Set the value of a variable to NULL without freeing items.
7306 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007307 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007308init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007309{
7310 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007311 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007312}
7313
7314/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 * Get the number value of a variable.
7316 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007317 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007318 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007319 * caller of incompatible types: it sets *denote to TRUE if "denote"
7320 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007322 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007323tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007325 int error = FALSE;
7326
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007327 return tv_get_number_chk(varp, &error); /* return 0L on error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007328}
7329
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007330 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007331tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007332{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007333 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007335 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007337 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007338 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007339 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007340#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007341 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007342 break;
7343#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007344 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007345 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007346 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007347 break;
7348 case VAR_STRING:
7349 if (varp->vval.v_string != NULL)
7350 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01007351 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007352 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007353 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007354 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007355 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007356 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007357 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007358 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007359 case VAR_SPECIAL:
7360 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7361 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007362 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007363#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007364 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007365 break;
7366#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007367 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007368#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007369 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007370 break;
7371#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007372 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007373 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007374 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007375 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007376 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007377 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007379 if (denote == NULL) /* useful for values that must be unsigned */
7380 n = -1;
7381 else
7382 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007383 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384}
7385
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007386#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007387 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007388tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007389{
7390 switch (varp->v_type)
7391 {
7392 case VAR_NUMBER:
7393 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007394 case VAR_FLOAT:
7395 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007396 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007397 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007398 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007399 break;
7400 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007401 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007402 break;
7403 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007404 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007405 break;
7406 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007407 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007408 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007409 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007410 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007411 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007412 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007413# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007414 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007415 break;
7416# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007417 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007418# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007419 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007420 break;
7421# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007422 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007423 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007424 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007425 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007426 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007427 break;
7428 }
7429 return 0;
7430}
7431#endif
7432
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 * Get the string value of a variable.
7435 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007436 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7437 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 * If the String variable has never been set, return an empty string.
7439 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007440 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007441 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007443 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007444tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445{
7446 static char_u mybuf[NUMBUFLEN];
7447
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007448 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449}
7450
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007451 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007452tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007454 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007455
7456 return res != NULL ? res : (char_u *)"";
7457}
7458
Bram Moolenaar7d647822014-04-05 21:28:56 +02007459/*
7460 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7461 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007462 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007463tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007464{
7465 static char_u mybuf[NUMBUFLEN];
7466
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007467 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007468}
7469
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007470 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007471tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007472{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007473 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007475 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007476 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007477 (long_long_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007478 return buf;
7479 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007480 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007481 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007482 break;
7483 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007484 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007485 break;
7486 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007487 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007488 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007489 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007490#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007491 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007492 break;
7493#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007494 case VAR_STRING:
7495 if (varp->vval.v_string != NULL)
7496 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007497 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007498 case VAR_SPECIAL:
7499 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7500 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007501 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007502 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007503 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007504 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007505#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007506 {
7507 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007508 char *status;
7509
7510 if (job == NULL)
7511 return (char_u *)"no process";
7512 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007513 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007514 : "run";
7515# ifdef UNIX
7516 vim_snprintf((char *)buf, NUMBUFLEN,
7517 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01007518# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007519 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007520 "process %ld %s",
7521 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007522 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007523# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007524 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007525 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7526# endif
7527 return buf;
7528 }
7529#endif
7530 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007531 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007532#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007533 {
7534 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007535 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007536
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007537 if (channel == NULL)
7538 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7539 else
7540 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007541 "channel %d %s", channel->ch_id, status);
7542 return buf;
7543 }
7544#endif
7545 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007546 case VAR_UNKNOWN:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007547 emsg(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007548 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007550 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551}
7552
7553/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007554 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
7555 * string() on Dict, List, etc.
7556 */
7557 char_u *
7558tv_stringify(typval_T *varp, char_u *buf)
7559{
7560 if (varp->v_type == VAR_LIST
7561 || varp->v_type == VAR_DICT
7562 || varp->v_type == VAR_FUNC
7563 || varp->v_type == VAR_PARTIAL
7564 || varp->v_type == VAR_FLOAT)
7565 {
7566 typval_T tmp;
7567
7568 f_string(varp, &tmp);
7569 tv_get_string_buf(&tmp, buf);
7570 clear_tv(varp);
7571 *varp = tmp;
7572 return tmp.vval.v_string;
7573 }
7574 return tv_get_string_buf(varp, buf);
7575}
7576
7577/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 * Find variable "name" in the list of variables.
7579 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007580 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007581 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007582 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007584 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007585find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007588 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007589 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590
Bram Moolenaara7043832005-01-21 11:56:39 +00007591 ht = find_var_ht(name, &varname);
7592 if (htp != NULL)
7593 *htp = ht;
7594 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007596 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7597 if (ret != NULL)
7598 return ret;
7599
7600 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007601 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602}
7603
7604/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007605 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007606 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007608 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007609find_var_in_ht(
7610 hashtab_T *ht,
7611 int htname,
7612 char_u *varname,
7613 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007614{
Bram Moolenaar33570922005-01-25 22:26:29 +00007615 hashitem_T *hi;
7616
7617 if (*varname == NUL)
7618 {
7619 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007620 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007621 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007622 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007623 case 'g': return &globvars_var;
7624 case 'v': return &vimvars_var;
7625 case 'b': return &curbuf->b_bufvar;
7626 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007627 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007628 case 'l': return get_funccal_local_var();
7629 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007630 }
7631 return NULL;
7632 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007633
7634 hi = hash_find(ht, varname);
7635 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007636 {
7637 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007638 * worked find the variable again. Don't auto-load a script if it was
7639 * loaded already, otherwise it would be loaded every time when
7640 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007641 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007642 {
7643 /* Note: script_autoload() may make "hi" invalid. It must either
7644 * be obtained again or not used. */
7645 if (!script_autoload(varname, FALSE) || aborting())
7646 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007647 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007648 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007649 if (HASHITEM_EMPTY(hi))
7650 return NULL;
7651 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007652 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007653}
7654
7655/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007656 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007657 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007658 * Set "varname" to the start of name without ':'.
7659 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007660 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007661find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007663 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007664 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007665
Bram Moolenaar73627d02015-08-11 15:46:09 +02007666 if (name[0] == NUL)
7667 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 if (name[1] != ':')
7669 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007670 /* The name must not start with a colon or #. */
7671 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 return NULL;
7673 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007674
Bram Moolenaard2e716e2019-04-20 14:39:52 +02007675 // "version" is "v:version" in all scopes if scriptversion < 3.
7676 // Same for a few other variables marked with VV_COMPAT.
7677 if (current_sctx.sc_version < 3)
7678 {
7679 hi = hash_find(&compat_hashtab, name);
7680 if (!HASHITEM_EMPTY(hi))
7681 return &compat_hashtab;
7682 }
Bram Moolenaar532c7802005-01-27 14:44:31 +00007683
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007684 ht = get_funccal_local_ht();
7685 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007686 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007687 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 }
7689 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007690 if (*name == 'g') /* global variable */
7691 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007692 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7693 */
7694 if (vim_strchr(name + 2, ':') != NULL
7695 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007696 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007698 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007700 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007701 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007702 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007703 if (*name == 'v') /* v: variable */
7704 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007705 if (*name == 'a') /* a: function argument */
7706 return get_funccal_args_ht();
7707 if (*name == 'l') /* l: local function variable */
7708 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 if (*name == 's' /* script variable */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007710 && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
7711 return &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 return NULL;
7713}
7714
7715/*
7716 * Get the string value of a (global/local) variable.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007717 * Note: see tv_get_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 * Returns NULL when it doesn't exist.
7719 */
7720 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007721get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722{
Bram Moolenaar33570922005-01-25 22:26:29 +00007723 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007725 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 if (v == NULL)
7727 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007728 return tv_get_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729}
7730
7731/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007732 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 * sourcing this script and when executing functions defined in the script.
7734 */
7735 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007736new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737{
Bram Moolenaara7043832005-01-21 11:56:39 +00007738 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007739 hashtab_T *ht;
7740 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007741
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7743 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007744 /* Re-allocating ga_data means that an ht_array pointing to
7745 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007746 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007747 for (i = 1; i <= ga_scripts.ga_len; ++i)
7748 {
7749 ht = &SCRIPT_VARS(i);
7750 if (ht->ht_mask == HT_INIT_SIZE - 1)
7751 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007752 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007753 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007754 }
7755
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 while (ga_scripts.ga_len < id)
7757 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007758 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007759 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007760 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 }
7763 }
7764}
7765
7766/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007767 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7768 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 */
7770 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007771init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772{
Bram Moolenaar33570922005-01-25 22:26:29 +00007773 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007774 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007775 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007776 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007777 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007778 dict_var->di_tv.vval.v_dict = dict;
7779 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007780 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007781 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7782 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783}
7784
7785/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007786 * Unreference a dictionary initialized by init_var_dict().
7787 */
7788 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007789unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007790{
7791 /* Now the dict needs to be freed if no one else is using it, go back to
7792 * normal reference counting. */
7793 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7794 dict_unref(dict);
7795}
7796
7797/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007799 * Frees all allocated variables and the value they contain.
7800 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 */
7802 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007803vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007804{
7805 vars_clear_ext(ht, TRUE);
7806}
7807
7808/*
7809 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7810 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007811 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007812vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813{
Bram Moolenaara7043832005-01-21 11:56:39 +00007814 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007815 hashitem_T *hi;
7816 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817
Bram Moolenaar33570922005-01-25 22:26:29 +00007818 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007819 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007820 for (hi = ht->ht_array; todo > 0; ++hi)
7821 {
7822 if (!HASHITEM_EMPTY(hi))
7823 {
7824 --todo;
7825
Bram Moolenaar33570922005-01-25 22:26:29 +00007826 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007827 * ht_array might change then. hash_clear() takes care of it
7828 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007829 v = HI2DI(hi);
7830 if (free_val)
7831 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007832 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007833 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007834 }
7835 }
7836 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007837 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838}
7839
Bram Moolenaara7043832005-01-21 11:56:39 +00007840/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007841 * Delete a variable from hashtab "ht" at item "hi".
7842 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007843 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007845delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846{
Bram Moolenaar33570922005-01-25 22:26:29 +00007847 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007848
7849 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007850 clear_tv(&di->di_tv);
7851 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852}
7853
7854/*
7855 * List the value of one internal variable.
7856 */
7857 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01007858list_one_var(dictitem_T *v, char *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007860 char_u *tofree;
7861 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007862 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007863
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007864 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007865 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007866 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007867 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868}
7869
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007871list_one_var_a(
Bram Moolenaar32526b32019-01-19 17:43:09 +01007872 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007873 char_u *name,
7874 int type,
7875 char_u *string,
7876 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877{
Bram Moolenaar31859182007-08-14 20:41:13 +00007878 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7879 msg_start();
7880 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 if (name != NULL) /* "a:" vars don't have a name stored */
Bram Moolenaar32526b32019-01-19 17:43:09 +01007882 msg_puts((char *)name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 msg_putchar(' ');
7884 msg_advance(22);
7885 if (type == VAR_NUMBER)
7886 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007887 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007888 msg_putchar('*');
7889 else if (type == VAR_LIST)
7890 {
7891 msg_putchar('[');
7892 if (*string == '[')
7893 ++string;
7894 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007895 else if (type == VAR_DICT)
7896 {
7897 msg_putchar('{');
7898 if (*string == '{')
7899 ++string;
7900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 else
7902 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007903
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007905
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007906 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007907 msg_puts("()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007908 if (*first)
7909 {
7910 msg_clr_eos();
7911 *first = FALSE;
7912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913}
7914
7915/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007916 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 * If the variable already exists, the value is updated.
7918 * Otherwise the variable is created.
7919 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007920 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007921set_var(
7922 char_u *name,
7923 typval_T *tv,
7924 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925{
Bram Moolenaar33570922005-01-25 22:26:29 +00007926 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007928 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007930 ht = find_var_ht(name, &varname);
7931 if (ht == NULL || *varname == NUL)
7932 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007933 semsg(_(e_illvar), name);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007934 return;
7935 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007936 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007937
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007938 /* Search in parent scope which is possible to reference from lambda */
7939 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007940 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007941
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007942 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7943 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007944 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007945
Bram Moolenaar33570922005-01-25 22:26:29 +00007946 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007948 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007949 if (var_check_ro(v->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007950 || var_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007951 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007952
7953 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007954 * Handle setting internal v: variables separately where needed to
7955 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007956 */
7957 if (ht == &vimvarht)
7958 {
7959 if (v->di_tv.v_type == VAR_STRING)
7960 {
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01007961 VIM_CLEAR(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007962 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01007963 {
7964 char_u *val = tv_get_string(tv);
7965
7966 // Careful: when assigning to v:errmsg and tv_get_string()
7967 // causes an error message the variable will alrady be set.
7968 if (v->di_tv.vval.v_string == NULL)
7969 v->di_tv.vval.v_string = vim_strsave(val);
7970 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007971 else
7972 {
7973 /* Take over the string to avoid an extra alloc/free. */
7974 v->di_tv.vval.v_string = tv->vval.v_string;
7975 tv->vval.v_string = NULL;
7976 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007977 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007978 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007979 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007980 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007981 v->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007982 if (STRCMP(varname, "searchforward") == 0)
7983 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007984#ifdef FEAT_SEARCH_EXTRA
7985 else if (STRCMP(varname, "hlsearch") == 0)
7986 {
7987 no_hlsearch = !v->di_tv.vval.v_number;
7988 redraw_all_later(SOME_VALID);
7989 }
7990#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007991 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007992 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007993 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01007994 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007995 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01007996 return;
7997 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007998 }
7999
8000 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 }
8002 else /* add a new variable */
8003 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01008004 // Can't add "v:" or "a:" variable.
8005 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00008006 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008007 semsg(_(e_illvar), name);
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00008008 return;
8009 }
8010
Bram Moolenaar31b81602019-02-10 22:14:27 +01008011 // Make sure the variable name is valid.
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008012 if (!valid_varname(varname))
8013 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00008014
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008015 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
8016 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00008017 if (v == NULL)
8018 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008019 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00008020 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008022 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 return;
8024 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02008025 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 }
Bram Moolenaara7043832005-01-21 11:56:39 +00008027
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008028 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00008029 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00008030 else
8031 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008032 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008033 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008034 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00008035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036}
8037
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008038/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008039 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00008040 * Also give an error message.
8041 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008042 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008043var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00008044{
8045 if (flags & DI_FLAGS_RO)
8046 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008047 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008048 return TRUE;
8049 }
8050 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
8051 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008052 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008053 return TRUE;
8054 }
8055 return FALSE;
8056}
8057
8058/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008059 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
8060 * Also give an error message.
8061 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008062 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008063var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008064{
8065 if (flags & DI_FLAGS_FIX)
8066 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008067 semsg(_("E795: Cannot delete variable %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008068 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008069 return TRUE;
8070 }
8071 return FALSE;
8072}
8073
8074/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008075 * Check if a funcref is assigned to a valid variable name.
8076 * Return TRUE and give an error if not.
8077 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008078 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008079var_check_func_name(
8080 char_u *name, /* points to start of variable name */
8081 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008082{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02008083 /* Allow for w: b: s: and t:. */
8084 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008085 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
8086 ? name[2] : name[0]))
8087 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008088 semsg(_("E704: Funcref variable name must start with a capital: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008089 name);
8090 return TRUE;
8091 }
8092 /* Don't allow hiding a function. When "v" is not NULL we might be
8093 * assigning another function to the same var, the type is checked
8094 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02008095 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008096 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008097 semsg(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008098 name);
8099 return TRUE;
8100 }
8101 return FALSE;
8102}
8103
8104/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008105 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02008106 * Also give an error message, using "name" or _("name") when use_gettext is
8107 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008108 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008109 int
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008110var_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008111{
8112 if (lock & VAR_LOCKED)
8113 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008114 semsg(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008115 name == NULL ? (char_u *)_("Unknown")
8116 : use_gettext ? (char_u *)_(name)
8117 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008118 return TRUE;
8119 }
8120 if (lock & VAR_FIXED)
8121 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008122 semsg(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008123 name == NULL ? (char_u *)_("Unknown")
8124 : use_gettext ? (char_u *)_(name)
8125 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008126 return TRUE;
8127 }
8128 return FALSE;
8129}
8130
8131/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008132 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
8133 * Also give an error message, using "name" or _("name") when use_gettext is
8134 * TRUE.
8135 */
8136 static int
8137tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
8138{
8139 int lock = 0;
8140
8141 switch (tv->v_type)
8142 {
8143 case VAR_BLOB:
8144 if (tv->vval.v_blob != NULL)
8145 lock = tv->vval.v_blob->bv_lock;
8146 break;
8147 case VAR_LIST:
8148 if (tv->vval.v_list != NULL)
8149 lock = tv->vval.v_list->lv_lock;
8150 break;
8151 case VAR_DICT:
8152 if (tv->vval.v_dict != NULL)
8153 lock = tv->vval.v_dict->dv_lock;
8154 break;
8155 default:
8156 break;
8157 }
8158 return var_check_lock(tv->v_lock, name, use_gettext)
8159 || (lock != 0 && var_check_lock(lock, name, use_gettext));
8160}
8161
8162/*
8163 * Check if a variable name is valid.
8164 * Return FALSE and give an error if not.
8165 */
8166 int
8167valid_varname(char_u *varname)
8168{
8169 char_u *p;
8170
8171 for (p = varname; *p != NUL; ++p)
8172 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
8173 && *p != AUTOLOAD_CHAR)
8174 {
8175 semsg(_(e_illvar), varname);
8176 return FALSE;
8177 }
8178 return TRUE;
8179}
8180
8181/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008182 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008183 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008184 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008185 * It is OK for "from" and "to" to point to the same item. This is used to
8186 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008187 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008188 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008189copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008191 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008192 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008193 switch (from->v_type)
8194 {
8195 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008196 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008197 to->vval.v_number = from->vval.v_number;
8198 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008199 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008200#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008201 to->vval.v_float = from->vval.v_float;
8202 break;
8203#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008204 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008205#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01008206 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01008207 if (to->vval.v_job != NULL)
8208 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01008209 break;
8210#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01008211 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008212#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01008213 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01008214 if (to->vval.v_channel != NULL)
8215 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01008216 break;
8217#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008218 case VAR_STRING:
8219 case VAR_FUNC:
8220 if (from->vval.v_string == NULL)
8221 to->vval.v_string = NULL;
8222 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008223 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008224 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008225 if (from->v_type == VAR_FUNC)
8226 func_ref(to->vval.v_string);
8227 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008228 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008229 case VAR_PARTIAL:
8230 if (from->vval.v_partial == NULL)
8231 to->vval.v_partial = NULL;
8232 else
8233 {
8234 to->vval.v_partial = from->vval.v_partial;
8235 ++to->vval.v_partial->pt_refcount;
8236 }
8237 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008238 case VAR_BLOB:
8239 if (from->vval.v_blob == NULL)
8240 to->vval.v_blob = NULL;
8241 else
8242 {
8243 to->vval.v_blob = from->vval.v_blob;
8244 ++to->vval.v_blob->bv_refcount;
8245 }
8246 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008247 case VAR_LIST:
8248 if (from->vval.v_list == NULL)
8249 to->vval.v_list = NULL;
8250 else
8251 {
8252 to->vval.v_list = from->vval.v_list;
8253 ++to->vval.v_list->lv_refcount;
8254 }
8255 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008256 case VAR_DICT:
8257 if (from->vval.v_dict == NULL)
8258 to->vval.v_dict = NULL;
8259 else
8260 {
8261 to->vval.v_dict = from->vval.v_dict;
8262 ++to->vval.v_dict->dv_refcount;
8263 }
8264 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008265 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008266 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008267 break;
8268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269}
8270
8271/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008272 * Make a copy of an item.
8273 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008274 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
8275 * reference to an already copied list/dict can be used.
8276 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008277 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008278 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008279item_copy(
8280 typval_T *from,
8281 typval_T *to,
8282 int deep,
8283 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008284{
8285 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008286 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008287
Bram Moolenaar33570922005-01-25 22:26:29 +00008288 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008289 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008290 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008291 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008292 }
8293 ++recurse;
8294
8295 switch (from->v_type)
8296 {
8297 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008298 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008299 case VAR_STRING:
8300 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008301 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008302 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008303 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008304 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008305 copy_tv(from, to);
8306 break;
8307 case VAR_LIST:
8308 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008309 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008310 if (from->vval.v_list == NULL)
8311 to->vval.v_list = NULL;
8312 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8313 {
8314 /* use the copy made earlier */
8315 to->vval.v_list = from->vval.v_list->lv_copylist;
8316 ++to->vval.v_list->lv_refcount;
8317 }
8318 else
8319 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8320 if (to->vval.v_list == NULL)
8321 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008322 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008323 case VAR_BLOB:
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008324 ret = blob_copy(from, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008325 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008326 case VAR_DICT:
8327 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008328 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008329 if (from->vval.v_dict == NULL)
8330 to->vval.v_dict = NULL;
8331 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8332 {
8333 /* use the copy made earlier */
8334 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8335 ++to->vval.v_dict->dv_refcount;
8336 }
8337 else
8338 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8339 if (to->vval.v_dict == NULL)
8340 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008341 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008342 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008343 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008344 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008345 }
8346 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008347 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008348}
8349
8350/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008351 * This function is used by f_input() and f_inputdialog() functions. The third
8352 * argument to f_input() specifies the type of completion to use at the
8353 * prompt. The third argument to f_inputdialog() specifies the value to return
8354 * when the user cancels the prompt.
8355 */
8356 void
8357get_user_input(
8358 typval_T *argvars,
8359 typval_T *rettv,
8360 int inputdialog,
8361 int secret)
8362{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008363 char_u *prompt = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008364 char_u *p = NULL;
8365 int c;
8366 char_u buf[NUMBUFLEN];
8367 int cmd_silent_save = cmd_silent;
8368 char_u *defstr = (char_u *)"";
8369 int xp_type = EXPAND_NOTHING;
8370 char_u *xp_arg = NULL;
8371
8372 rettv->v_type = VAR_STRING;
8373 rettv->vval.v_string = NULL;
8374
8375#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008376 /* While starting up, there is no place to enter text. When running tests
8377 * with --not-a-term we assume feedkeys() will be used. */
8378 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008379 return;
8380#endif
8381
8382 cmd_silent = FALSE; /* Want to see the prompt. */
8383 if (prompt != NULL)
8384 {
8385 /* Only the part of the message after the last NL is considered as
8386 * prompt for the command line */
8387 p = vim_strrchr(prompt, '\n');
8388 if (p == NULL)
8389 p = prompt;
8390 else
8391 {
8392 ++p;
8393 c = *p;
8394 *p = NUL;
8395 msg_start();
8396 msg_clr_eos();
Bram Moolenaar32526b32019-01-19 17:43:09 +01008397 msg_puts_attr((char *)prompt, echo_attr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008398 msg_didout = FALSE;
8399 msg_starthere();
8400 *p = c;
8401 }
8402 cmdline_row = msg_row;
8403
8404 if (argvars[1].v_type != VAR_UNKNOWN)
8405 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008406 defstr = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008407 if (defstr != NULL)
8408 stuffReadbuffSpec(defstr);
8409
8410 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8411 {
8412 char_u *xp_name;
8413 int xp_namelen;
8414 long argt;
8415
8416 /* input() with a third argument: completion */
8417 rettv->vval.v_string = NULL;
8418
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008419 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008420 if (xp_name == NULL)
8421 return;
8422
8423 xp_namelen = (int)STRLEN(xp_name);
8424
8425 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8426 &xp_arg) == FAIL)
8427 return;
8428 }
8429 }
8430
8431 if (defstr != NULL)
8432 {
8433 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02008434
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008435 ex_normal_busy = 0;
8436 rettv->vval.v_string =
8437 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8438 xp_type, xp_arg);
8439 ex_normal_busy = save_ex_normal_busy;
8440 }
8441 if (inputdialog && rettv->vval.v_string == NULL
8442 && argvars[1].v_type != VAR_UNKNOWN
8443 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008444 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008445 &argvars[2], buf));
8446
8447 vim_free(xp_arg);
8448
8449 /* since the user typed this, no need to wait for return */
8450 need_wait_return = FALSE;
8451 msg_didout = FALSE;
8452 }
8453 cmd_silent = cmd_silent_save;
8454}
8455
8456/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 * ":echo expr1 ..." print each argument separated with a space, add a
8458 * newline at the end.
8459 * ":echon expr1 ..." print each argument plain.
8460 */
8461 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008462ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463{
8464 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008465 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008466 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 char_u *p;
8468 int needclr = TRUE;
8469 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008470 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01008471 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008472 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473
8474 if (eap->skip)
8475 ++emsg_skip;
8476 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8477 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008478 /* If eval1() causes an error message the text from the command may
8479 * still need to be cleared. E.g., "echo 22,44". */
8480 need_clr_eos = needclr;
8481
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008483 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 {
8485 /*
8486 * Report the invalid expression unless the expression evaluation
8487 * has been cancelled due to an aborting error, an interrupt, or an
8488 * exception.
8489 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008490 if (!aborting() && did_emsg == did_emsg_before
8491 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008492 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008493 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 break;
8495 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008496 need_clr_eos = FALSE;
8497
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 if (!eap->skip)
8499 {
8500 if (atstart)
8501 {
8502 atstart = FALSE;
8503 /* Call msg_start() after eval1(), evaluating the expression
8504 * may cause a message to appear. */
8505 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008506 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008507 /* Mark the saved text as finishing the line, so that what
8508 * follows is displayed on a new line when scrolling back
8509 * at the more prompt. */
8510 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 }
8514 else if (eap->cmdidx == CMD_echo)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008515 msg_puts_attr(" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008516 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008517 if (p != NULL)
8518 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008520 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008522 if (*p != TAB && needclr)
8523 {
8524 /* remove any text still there from the command */
8525 msg_clr_eos();
8526 needclr = FALSE;
8527 }
8528 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 }
8530 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008531 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008532 if (has_mbyte)
8533 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008534 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008535
8536 (void)msg_outtrans_len_attr(p, i, echo_attr);
8537 p += i - 1;
8538 }
8539 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008540 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008543 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008545 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 arg = skipwhite(arg);
8547 }
8548 eap->nextcmd = check_nextcmd(arg);
8549
8550 if (eap->skip)
8551 --emsg_skip;
8552 else
8553 {
8554 /* remove text that may still be there from the command */
8555 if (needclr)
8556 msg_clr_eos();
8557 if (eap->cmdidx == CMD_echo)
8558 msg_end();
8559 }
8560}
8561
8562/*
8563 * ":echohl {name}".
8564 */
8565 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008566ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008568 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569}
8570
8571/*
8572 * ":execute expr1 ..." execute the result of an expression.
8573 * ":echomsg expr1 ..." Print a message
8574 * ":echoerr expr1 ..." Print an error
8575 * Each gets spaces around each argument and a newline at the end for
8576 * echo commands
8577 */
8578 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008579ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580{
8581 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008582 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 int ret = OK;
8584 char_u *p;
8585 garray_T ga;
8586 int len;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01008587 int save_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588
8589 ga_init2(&ga, 1, 80);
8590
8591 if (eap->skip)
8592 ++emsg_skip;
8593 while (*arg != NUL && *arg != '|' && *arg != '\n')
8594 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01008595 ret = eval1_emsg(&arg, &rettv, !eap->skip);
8596 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598
8599 if (!eap->skip)
8600 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008601 char_u buf[NUMBUFLEN];
8602
8603 if (eap->cmdidx == CMD_execute)
8604 p = tv_get_string_buf(&rettv, buf);
8605 else
8606 p = tv_stringify(&rettv, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 len = (int)STRLEN(p);
8608 if (ga_grow(&ga, len + 2) == FAIL)
8609 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008610 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 ret = FAIL;
8612 break;
8613 }
8614 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 ga.ga_len += len;
8618 }
8619
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008620 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 arg = skipwhite(arg);
8622 }
8623
8624 if (ret != FAIL && ga.ga_data != NULL)
8625 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008626 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8627 {
8628 /* Mark the already saved text as finishing the line, so that what
8629 * follows is displayed on a new line when scrolling back at the
8630 * more prompt. */
8631 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008632 }
8633
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008635 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01008636 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008637 out_flush();
8638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 else if (eap->cmdidx == CMD_echoerr)
8640 {
8641 /* We don't want to abort following commands, restore did_emsg. */
8642 save_did_emsg = did_emsg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008643 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 if (!force_abort)
8645 did_emsg = save_did_emsg;
8646 }
8647 else if (eap->cmdidx == CMD_execute)
8648 do_cmdline((char_u *)ga.ga_data,
8649 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8650 }
8651
8652 ga_clear(&ga);
8653
8654 if (eap->skip)
8655 --emsg_skip;
8656
8657 eap->nextcmd = check_nextcmd(arg);
8658}
8659
8660/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008661 * Find window specified by "vp" in tabpage "tp".
8662 */
8663 win_T *
8664find_win_by_nr(
8665 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01008666 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008667{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008668 win_T *wp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008669 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008670
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008671 if (nr < 0)
8672 return NULL;
8673 if (nr == 0)
8674 return curwin;
8675
Bram Moolenaar29323592016-07-24 22:04:11 +02008676 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8677 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008678 if (nr >= LOWEST_WIN_ID)
8679 {
8680 if (wp->w_id == nr)
8681 return wp;
8682 }
8683 else if (--nr <= 0)
8684 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008685 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008686 if (nr >= LOWEST_WIN_ID)
8687 return NULL;
8688 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008689}
8690
8691/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02008692 * Find a window: When using a Window ID in any tab page, when using a number
8693 * in the current tab page.
8694 */
8695 win_T *
8696find_win_by_nr_or_id(typval_T *vp)
8697{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008698 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02008699
8700 if (nr >= LOWEST_WIN_ID)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01008701 return win_id2wp(tv_get_number(vp));
Bram Moolenaare6e39892018-10-25 12:32:11 +02008702 return find_win_by_nr(vp, NULL);
8703}
8704
8705/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008706 * Find window specified by "wvp" in tabpage "tvp".
8707 */
8708 win_T *
8709find_tabwin(
8710 typval_T *wvp, /* VAR_UNKNOWN for current window */
8711 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
8712{
8713 win_T *wp = NULL;
8714 tabpage_T *tp = NULL;
8715 long n;
8716
8717 if (wvp->v_type != VAR_UNKNOWN)
8718 {
8719 if (tvp->v_type != VAR_UNKNOWN)
8720 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008721 n = (long)tv_get_number(tvp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008722 if (n >= 0)
8723 tp = find_tabpage(n);
8724 }
8725 else
8726 tp = curtab;
8727
8728 if (tp != NULL)
8729 wp = find_win_by_nr(wvp, tp);
8730 }
8731 else
8732 wp = curwin;
8733
8734 return wp;
8735}
8736
8737/*
8738 * getwinvar() and gettabwinvar()
8739 */
8740 void
8741getwinvar(
8742 typval_T *argvars,
8743 typval_T *rettv,
8744 int off) /* 1 for gettabwinvar() */
8745{
8746 win_T *win;
8747 char_u *varname;
8748 dictitem_T *v;
8749 tabpage_T *tp = NULL;
8750 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008751 win_T *oldcurwin;
8752 tabpage_T *oldtabpage;
8753 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008754
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008755 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008756 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008757 else
8758 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008759 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008760 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008761 ++emsg_off;
8762
8763 rettv->v_type = VAR_STRING;
8764 rettv->vval.v_string = NULL;
8765
8766 if (win != NULL && varname != NULL)
8767 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008768 /* Set curwin to be our win, temporarily. Also set the tabpage,
8769 * otherwise the window is not valid. Only do this when needed,
8770 * autocommands get blocked. */
8771 need_switch_win = !(tp == curtab && win == curwin);
8772 if (!need_switch_win
8773 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008774 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008775 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008776 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008777 if (varname[1] == NUL)
8778 {
8779 /* get all window-local options in a dict */
8780 dict_T *opts = get_winbuf_options(FALSE);
8781
8782 if (opts != NULL)
8783 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008784 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02008785 done = TRUE;
8786 }
8787 }
8788 else if (get_option_tv(&varname, rettv, 1) == OK)
8789 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008790 done = TRUE;
8791 }
8792 else
8793 {
8794 /* Look up the variable. */
8795 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8796 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8797 varname, FALSE);
8798 if (v != NULL)
8799 {
8800 copy_tv(&v->di_tv, rettv);
8801 done = TRUE;
8802 }
8803 }
8804 }
8805
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008806 if (need_switch_win)
8807 /* restore previous notion of curwin */
8808 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008809 }
8810
8811 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8812 /* use the default return value */
8813 copy_tv(&argvars[off + 2], rettv);
8814
8815 --emsg_off;
8816}
8817
8818/*
8819 * "setwinvar()" and "settabwinvar()" functions
8820 */
8821 void
8822setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8823{
8824 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008825 win_T *save_curwin;
8826 tabpage_T *save_curtab;
8827 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008828 char_u *varname, *winvarname;
8829 typval_T *varp;
8830 char_u nbuf[NUMBUFLEN];
8831 tabpage_T *tp = NULL;
8832
Bram Moolenaare0fb7d12019-02-12 20:48:10 +01008833 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008834 return;
8835
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008836 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008837 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008838 else
8839 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008840 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008841 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008842 varp = &argvars[off + 2];
8843
8844 if (win != NULL && varname != NULL && varp != NULL)
8845 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008846 need_switch_win = !(tp == curtab && win == curwin);
8847 if (!need_switch_win
8848 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008849 {
8850 if (*varname == '&')
8851 {
8852 long numval;
8853 char_u *strval;
8854 int error = FALSE;
8855
8856 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008857 numval = (long)tv_get_number_chk(varp, &error);
8858 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008859 if (!error && strval != NULL)
8860 set_option_value(varname, numval, strval, OPT_LOCAL);
8861 }
8862 else
8863 {
8864 winvarname = alloc((unsigned)STRLEN(varname) + 3);
8865 if (winvarname != NULL)
8866 {
8867 STRCPY(winvarname, "w:");
8868 STRCPY(winvarname + 2, varname);
8869 set_var(winvarname, varp, TRUE);
8870 vim_free(winvarname);
8871 }
8872 }
8873 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008874 if (need_switch_win)
8875 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008876 }
8877}
8878
8879/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8881 * "arg" points to the "&" or '+' when called, to "option" when returning.
8882 * Returns NULL when no option name found. Otherwise pointer to the char
8883 * after the option name.
8884 */
8885 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008886find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887{
8888 char_u *p = *arg;
8889
8890 ++p;
8891 if (*p == 'g' && p[1] == ':')
8892 {
8893 *opt_flags = OPT_GLOBAL;
8894 p += 2;
8895 }
8896 else if (*p == 'l' && p[1] == ':')
8897 {
8898 *opt_flags = OPT_LOCAL;
8899 p += 2;
8900 }
8901 else
8902 *opt_flags = 0;
8903
8904 if (!ASCII_ISALPHA(*p))
8905 return NULL;
8906 *arg = p;
8907
8908 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
8909 p += 4; /* termcap option */
8910 else
8911 while (ASCII_ISALPHA(*p))
8912 ++p;
8913 return p;
8914}
8915
8916/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008917 * Return the autoload script name for a function or variable name.
8918 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02008920 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008921autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02008922{
Bram Moolenaara1544c02013-05-30 12:35:52 +02008923 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008924 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02008925
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008926 /* Get the script file name: replace '#' with '/', append ".vim". */
8927 scriptname = alloc((unsigned)(STRLEN(name) + 14));
8928 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008929 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008930 STRCPY(scriptname, "autoload/");
8931 STRCAT(scriptname, name);
8932 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
8933 STRCAT(scriptname, ".vim");
8934 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
8935 *p = '/';
8936 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008937}
8938
8939/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008940 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008941 * Return TRUE if a package was loaded.
8942 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008943 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008944script_autoload(
8945 char_u *name,
8946 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008947{
8948 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008949 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008950 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008951 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008952
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008953 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008954 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008955 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008956 return FALSE;
8957
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008958 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008959
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008960 /* Find the name in the list of previously loaded package names. Skip
8961 * "autoload/", it's always the same. */
8962 for (i = 0; i < ga_loaded.ga_len; ++i)
8963 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
8964 break;
8965 if (!reload && i < ga_loaded.ga_len)
8966 ret = FALSE; /* was loaded already */
8967 else
8968 {
8969 /* Remember the name if it wasn't loaded already. */
8970 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
8971 {
8972 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
8973 tofree = NULL;
8974 }
8975
8976 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008977 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008978 ret = TRUE;
8979 }
8980
8981 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008982 return ret;
8983}
8984
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
8986typedef enum
8987{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008988 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
8989 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
8990 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991} var_flavour_T;
8992
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01008994var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995{
8996 char_u *p = varname;
8997
8998 if (ASCII_ISUPPER(*p))
8999 {
9000 while (*(++p))
9001 if (ASCII_ISLOWER(*p))
9002 return VAR_FLAVOUR_SESSION;
9003 return VAR_FLAVOUR_VIMINFO;
9004 }
9005 else
9006 return VAR_FLAVOUR_DEFAULT;
9007}
9008#endif
9009
9010#if defined(FEAT_VIMINFO) || defined(PROTO)
9011/*
9012 * Restore global vars that start with a capital from the viminfo file
9013 */
9014 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009015read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016{
9017 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009018 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00009019 typval_T tv;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009020 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021
9022 if (!writing && (find_viminfo_parameter('!') != NULL))
9023 {
9024 tab = vim_strchr(virp->vir_line + 1, '\t');
9025 if (tab != NULL)
9026 {
9027 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009028 switch (*tab)
9029 {
9030 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009031#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009032 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009033#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009034 case 'D': type = VAR_DICT; break;
9035 case 'L': type = VAR_LIST; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009036 case 'B': type = VAR_BLOB; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009037 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039
9040 tab = vim_strchr(tab, '\t');
9041 if (tab != NULL)
9042 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009043 tv.v_type = type;
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009044 if (type == VAR_STRING || type == VAR_DICT
9045 || type == VAR_LIST || type == VAR_BLOB)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009046 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009048#ifdef FEAT_FLOAT
9049 else if (type == VAR_FLOAT)
9050 (void)string2float(tab + 1, &tv.vval.v_float);
9051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009053 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009054 if (type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009055 {
9056 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
9057
9058 if (etv == NULL)
9059 /* Failed to parse back the dict or list, use it as a
9060 * string. */
9061 tv.v_type = VAR_STRING;
9062 else
9063 {
9064 vim_free(tv.vval.v_string);
9065 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01009066 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009067 }
9068 }
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009069 else if (type == VAR_BLOB)
9070 {
9071 blob_T *blob = string2blob(tv.vval.v_string);
9072
9073 if (blob == NULL)
9074 // Failed to parse back the blob, use it as a string.
9075 tv.v_type = VAR_STRING;
9076 else
9077 {
9078 vim_free(tv.vval.v_string);
9079 tv.v_type = VAR_BLOB;
9080 tv.vval.v_blob = blob;
9081 }
9082 }
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009083
Bram Moolenaarb20e3342016-01-18 23:29:01 +01009084 /* when in a function use global variables */
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009085 save_funccal(&funccal_entry);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009086 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009087 restore_funccal();
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009088
9089 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009090 vim_free(tv.vval.v_string);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009091 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST ||
9092 tv.v_type == VAR_BLOB)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009093 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094 }
9095 }
9096 }
9097
9098 return viminfo_readline(virp);
9099}
9100
9101/*
9102 * Write global vars that start with a capital to the viminfo file
9103 */
9104 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009105write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106{
Bram Moolenaar33570922005-01-25 22:26:29 +00009107 hashitem_T *hi;
9108 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009109 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01009110 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009111 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009112 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009113 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114
9115 if (find_viminfo_parameter('!') == NULL)
9116 return;
9117
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02009118 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00009119
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009120 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009121 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009123 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009125 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009126 this_var = HI2DI(hi);
9127 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009128 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009129 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00009130 {
9131 case VAR_STRING: s = "STR"; break;
9132 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009133 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009134 case VAR_DICT: s = "DIC"; break;
9135 case VAR_LIST: s = "LIS"; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009136 case VAR_BLOB: s = "BLO"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009137 case VAR_SPECIAL: s = "XPL"; break;
9138
9139 case VAR_UNKNOWN:
9140 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009141 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01009142 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01009143 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01009144 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00009145 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009146 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009147 if (this_var->di_tv.v_type == VAR_SPECIAL)
9148 {
9149 sprintf((char *)numbuf, "%ld",
9150 (long)this_var->di_tv.vval.v_number);
9151 p = numbuf;
9152 tofree = NULL;
9153 }
9154 else
9155 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009156 if (p != NULL)
9157 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00009158 vim_free(tofree);
9159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 }
9161 }
9162}
9163#endif
9164
9165#if defined(FEAT_SESSION) || defined(PROTO)
9166 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009167store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168{
Bram Moolenaar33570922005-01-25 22:26:29 +00009169 hashitem_T *hi;
9170 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009171 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172 char_u *p, *t;
9173
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009174 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009175 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009177 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009179 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009180 this_var = HI2DI(hi);
9181 if ((this_var->di_tv.v_type == VAR_NUMBER
9182 || this_var->di_tv.v_type == VAR_STRING)
9183 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009184 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009185 /* Escape special characters with a backslash. Turn a LF and
9186 * CR into \n and \r. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009187 p = vim_strsave_escaped(tv_get_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00009188 (char_u *)"\\\"\n\r");
9189 if (p == NULL) /* out of memory */
9190 break;
9191 for (t = p; *t != NUL; ++t)
9192 if (*t == '\n')
9193 *t = 'n';
9194 else if (*t == '\r')
9195 *t = 'r';
9196 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00009197 this_var->di_key,
9198 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9199 : ' ',
9200 p,
9201 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9202 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00009203 || put_eol(fd) == FAIL)
9204 {
9205 vim_free(p);
9206 return FAIL;
9207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208 vim_free(p);
9209 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009210#ifdef FEAT_FLOAT
9211 else if (this_var->di_tv.v_type == VAR_FLOAT
9212 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
9213 {
9214 float_T f = this_var->di_tv.vval.v_float;
9215 int sign = ' ';
9216
9217 if (f < 0)
9218 {
9219 f = -f;
9220 sign = '-';
9221 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01009222 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009223 this_var->di_key, sign, f) < 0)
9224 || put_eol(fd) == FAIL)
9225 return FAIL;
9226 }
9227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228 }
9229 }
9230 return OK;
9231}
9232#endif
9233
Bram Moolenaar661b1822005-07-28 22:36:45 +00009234/*
9235 * Display script name where an item was last set.
9236 * Should only be invoked when 'verbose' is non-zero.
9237 */
9238 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009239last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009240{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009241 char_u *p;
9242
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009243 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009244 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009245 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009246 if (p != NULL)
9247 {
9248 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01009249 msg_puts(_("\n\tLast set from "));
9250 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009251 if (script_ctx.sc_lnum > 0)
9252 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01009253 msg_puts(_(" line "));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009254 msg_outnum((long)script_ctx.sc_lnum);
9255 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009256 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009257 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009258 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00009259 }
9260}
9261
Bram Moolenaar61be3762019-03-19 23:04:17 +01009262/*
9263 * Reset v:option_new, v:option_old and v:option_type.
9264 */
Bram Moolenaar53744302015-07-17 17:38:22 +02009265 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009266reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02009267{
9268 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
9269 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
9270 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
9271}
9272
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009273/*
9274 * Prepare "gap" for an assert error and add the sourcing position.
9275 */
9276 void
9277prepare_assert_error(garray_T *gap)
9278{
9279 char buf[NUMBUFLEN];
9280
9281 ga_init2(gap, 1, 100);
9282 if (sourcing_name != NULL)
9283 {
9284 ga_concat(gap, sourcing_name);
9285 if (sourcing_lnum > 0)
9286 ga_concat(gap, (char_u *)" ");
9287 }
9288 if (sourcing_lnum > 0)
9289 {
9290 sprintf(buf, "line %ld", (long)sourcing_lnum);
9291 ga_concat(gap, (char_u *)buf);
9292 }
9293 if (sourcing_name != NULL || sourcing_lnum > 0)
9294 ga_concat(gap, (char_u *)": ");
9295}
9296
9297/*
9298 * Add an assert error to v:errors.
9299 */
9300 void
9301assert_error(garray_T *gap)
9302{
9303 struct vimvar *vp = &vimvars[VV_ERRORS];
9304
9305 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9306 /* Make sure v:errors is a list. */
9307 set_vim_var_list(VV_ERRORS, list_alloc());
9308 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9309}
9310
Bram Moolenaar65a54642018-04-28 16:56:53 +02009311 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009312assert_equal_common(typval_T *argvars, assert_type_T atype)
9313{
9314 garray_T ga;
9315
9316 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9317 != (atype == ASSERT_EQUAL))
9318 {
9319 prepare_assert_error(&ga);
9320 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9321 atype);
9322 assert_error(&ga);
9323 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009324 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009325 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009326 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009327}
9328
Bram Moolenaar65a54642018-04-28 16:56:53 +02009329 int
Bram Moolenaard96ff162018-02-18 22:13:29 +01009330assert_equalfile(typval_T *argvars)
9331{
9332 char_u buf1[NUMBUFLEN];
9333 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009334 char_u *fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
9335 char_u *fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009336 garray_T ga;
9337 FILE *fd1;
9338 FILE *fd2;
9339
9340 if (fname1 == NULL || fname2 == NULL)
Bram Moolenaar65a54642018-04-28 16:56:53 +02009341 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009342
9343 IObuff[0] = NUL;
9344 fd1 = mch_fopen((char *)fname1, READBIN);
9345 if (fd1 == NULL)
9346 {
9347 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname1);
9348 }
9349 else
9350 {
9351 fd2 = mch_fopen((char *)fname2, READBIN);
9352 if (fd2 == NULL)
9353 {
9354 fclose(fd1);
9355 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname2);
9356 }
9357 else
9358 {
9359 int c1, c2;
9360 long count = 0;
9361
9362 for (;;)
9363 {
9364 c1 = fgetc(fd1);
9365 c2 = fgetc(fd2);
9366 if (c1 == EOF)
9367 {
9368 if (c2 != EOF)
9369 STRCPY(IObuff, "first file is shorter");
9370 break;
9371 }
9372 else if (c2 == EOF)
9373 {
9374 STRCPY(IObuff, "second file is shorter");
9375 break;
9376 }
9377 else if (c1 != c2)
9378 {
9379 vim_snprintf((char *)IObuff, IOSIZE,
9380 "difference at byte %ld", count);
9381 break;
9382 }
9383 ++count;
9384 }
Bram Moolenaar30494182018-02-20 21:46:05 +01009385 fclose(fd1);
9386 fclose(fd2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009387 }
9388 }
9389 if (IObuff[0] != NUL)
9390 {
9391 prepare_assert_error(&ga);
9392 ga_concat(&ga, IObuff);
9393 assert_error(&ga);
9394 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009395 return 1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009396 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009397 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009398}
9399
Bram Moolenaar65a54642018-04-28 16:56:53 +02009400 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009401assert_match_common(typval_T *argvars, assert_type_T atype)
9402{
9403 garray_T ga;
9404 char_u buf1[NUMBUFLEN];
9405 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009406 char_u *pat = tv_get_string_buf_chk(&argvars[0], buf1);
9407 char_u *text = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009408
9409 if (pat == NULL || text == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009410 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009411 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
9412 {
9413 prepare_assert_error(&ga);
9414 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9415 atype);
9416 assert_error(&ga);
9417 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009418 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009419 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009420 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009421}
9422
Bram Moolenaar65a54642018-04-28 16:56:53 +02009423 int
Bram Moolenaar61c04492016-07-23 15:35:35 +02009424assert_inrange(typval_T *argvars)
9425{
9426 garray_T ga;
9427 int error = FALSE;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009428 char_u *tofree;
9429 char msg[200];
9430 char_u numbuf[NUMBUFLEN];
9431
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009432#ifdef FEAT_FLOAT
9433 if (argvars[0].v_type == VAR_FLOAT
9434 || argvars[1].v_type == VAR_FLOAT
9435 || argvars[2].v_type == VAR_FLOAT)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009436 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009437 float_T flower = tv_get_float(&argvars[0]);
9438 float_T fupper = tv_get_float(&argvars[1]);
9439 float_T factual = tv_get_float(&argvars[2]);
9440
9441 if (factual < flower || factual > fupper)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009442 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009443 prepare_assert_error(&ga);
9444 if (argvars[3].v_type != VAR_UNKNOWN)
9445 {
9446 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9447 vim_free(tofree);
9448 }
9449 else
9450 {
9451 vim_snprintf(msg, 200, "Expected range %g - %g, but got %g",
9452 flower, fupper, factual);
9453 ga_concat(&ga, (char_u *)msg);
9454 }
9455 assert_error(&ga);
9456 ga_clear(&ga);
9457 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009458 }
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009459 }
9460 else
9461#endif
9462 {
9463 varnumber_T lower = tv_get_number_chk(&argvars[0], &error);
9464 varnumber_T upper = tv_get_number_chk(&argvars[1], &error);
9465 varnumber_T actual = tv_get_number_chk(&argvars[2], &error);
9466
9467 if (error)
9468 return 0;
9469 if (actual < lower || actual > upper)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009470 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009471 prepare_assert_error(&ga);
9472 if (argvars[3].v_type != VAR_UNKNOWN)
9473 {
9474 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9475 vim_free(tofree);
9476 }
9477 else
9478 {
9479 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
Bram Moolenaar61c04492016-07-23 15:35:35 +02009480 (long)lower, (long)upper, (long)actual);
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009481 ga_concat(&ga, (char_u *)msg);
9482 }
9483 assert_error(&ga);
9484 ga_clear(&ga);
9485 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009486 }
Bram Moolenaar61c04492016-07-23 15:35:35 +02009487 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009488 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009489}
9490
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009491/*
9492 * Common for assert_true() and assert_false().
Bram Moolenaar65a54642018-04-28 16:56:53 +02009493 * Return non-zero for failure.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009494 */
Bram Moolenaar65a54642018-04-28 16:56:53 +02009495 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009496assert_bool(typval_T *argvars, int isTrue)
9497{
9498 int error = FALSE;
9499 garray_T ga;
9500
9501 if (argvars[0].v_type == VAR_SPECIAL
9502 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar65a54642018-04-28 16:56:53 +02009503 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009504 if (argvars[0].v_type != VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009505 || (tv_get_number_chk(&argvars[0], &error) == 0) == isTrue
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009506 || error)
9507 {
9508 prepare_assert_error(&ga);
9509 fill_assert_error(&ga, &argvars[1],
9510 (char_u *)(isTrue ? "True" : "False"),
9511 NULL, &argvars[0], ASSERT_OTHER);
9512 assert_error(&ga);
9513 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009514 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009515 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009516 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009517}
9518
Bram Moolenaar65a54642018-04-28 16:56:53 +02009519 int
Bram Moolenaar42205552017-03-18 19:42:22 +01009520assert_report(typval_T *argvars)
9521{
9522 garray_T ga;
9523
9524 prepare_assert_error(&ga);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009525 ga_concat(&ga, tv_get_string(&argvars[0]));
Bram Moolenaar42205552017-03-18 19:42:22 +01009526 assert_error(&ga);
9527 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009528 return 1;
Bram Moolenaar42205552017-03-18 19:42:22 +01009529}
9530
Bram Moolenaar65a54642018-04-28 16:56:53 +02009531 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009532assert_exception(typval_T *argvars)
9533{
9534 garray_T ga;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009535 char_u *error = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009536
9537 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9538 {
9539 prepare_assert_error(&ga);
9540 ga_concat(&ga, (char_u *)"v:exception is not set");
9541 assert_error(&ga);
9542 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009543 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009544 }
9545 else if (error != NULL
9546 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9547 {
9548 prepare_assert_error(&ga);
9549 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9550 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9551 assert_error(&ga);
9552 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009553 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009554 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009555 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009556}
9557
Bram Moolenaar65a54642018-04-28 16:56:53 +02009558 int
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009559assert_beeps(typval_T *argvars)
9560{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009561 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009562 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009563 int ret = 0;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009564
9565 called_vim_beep = FALSE;
9566 suppress_errthrow = TRUE;
9567 emsg_silent = FALSE;
9568 do_cmdline_cmd(cmd);
9569 if (!called_vim_beep)
9570 {
9571 prepare_assert_error(&ga);
9572 ga_concat(&ga, (char_u *)"command did not beep: ");
9573 ga_concat(&ga, cmd);
9574 assert_error(&ga);
9575 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009576 ret = 1;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009577 }
9578
9579 suppress_errthrow = FALSE;
9580 emsg_on_display = FALSE;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009581 return ret;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009582}
9583
Bram Moolenaar65a54642018-04-28 16:56:53 +02009584 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009585assert_fails(typval_T *argvars)
9586{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009587 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009588 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009589 int ret = 0;
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02009590 char_u numbuf[NUMBUFLEN];
9591 char_u *tofree;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009592
9593 called_emsg = FALSE;
9594 suppress_errthrow = TRUE;
9595 emsg_silent = TRUE;
9596 do_cmdline_cmd(cmd);
9597 if (!called_emsg)
9598 {
9599 prepare_assert_error(&ga);
9600 ga_concat(&ga, (char_u *)"command did not fail: ");
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02009601 if (argvars[1].v_type != VAR_UNKNOWN
9602 && argvars[2].v_type != VAR_UNKNOWN)
9603 {
9604 ga_concat(&ga, echo_string(&argvars[2], &tofree, numbuf, 0));
9605 vim_free(tofree);
9606 }
9607 else
9608 ga_concat(&ga, cmd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009609 assert_error(&ga);
9610 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009611 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009612 }
9613 else if (argvars[1].v_type != VAR_UNKNOWN)
9614 {
9615 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009616 char *error = (char *)tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009617
9618 if (error == NULL
9619 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9620 {
9621 prepare_assert_error(&ga);
9622 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9623 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
9624 assert_error(&ga);
9625 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009626 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009627 }
9628 }
9629
9630 called_emsg = FALSE;
9631 suppress_errthrow = FALSE;
9632 emsg_silent = FALSE;
9633 emsg_on_display = FALSE;
9634 set_vim_var_string(VV_ERRMSG, NULL, 0);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009635 return ret;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009636}
9637
9638/*
Bram Moolenaar86576712019-01-25 20:48:33 +01009639 * Append "p[clen]" to "gap", escaping unprintable characters.
9640 * Changes NL to \n, CR to \r, etc.
9641 */
9642 static void
9643ga_concat_esc(garray_T *gap, char_u *p, int clen)
9644{
9645 char_u buf[NUMBUFLEN];
9646
9647 if (clen > 1)
9648 {
9649 mch_memmove(buf, p, clen);
9650 buf[clen] = NUL;
9651 ga_concat(gap, buf);
9652 }
9653 else switch (*p)
9654 {
9655 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9656 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9657 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9658 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9659 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9660 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9661 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9662 default:
9663 if (*p < ' ')
9664 {
9665 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9666 ga_concat(gap, buf);
9667 }
9668 else
9669 ga_append(gap, *p);
9670 break;
9671 }
9672}
9673
9674/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009675 * Append "str" to "gap", escaping unprintable characters.
9676 * Changes NL to \n, CR to \r, etc.
9677 */
9678 static void
Bram Moolenaar86576712019-01-25 20:48:33 +01009679ga_concat_shorten_esc(garray_T *gap, char_u *str)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009680{
9681 char_u *p;
Bram Moolenaar86576712019-01-25 20:48:33 +01009682 char_u *s;
9683 int c;
9684 int clen;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009685 char_u buf[NUMBUFLEN];
Bram Moolenaar86576712019-01-25 20:48:33 +01009686 int same_len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009687
9688 if (str == NULL)
9689 {
9690 ga_concat(gap, (char_u *)"NULL");
9691 return;
9692 }
9693
9694 for (p = str; *p != NUL; ++p)
Bram Moolenaar86576712019-01-25 20:48:33 +01009695 {
9696 same_len = 1;
9697 s = p;
9698 c = mb_ptr2char_adv(&s);
9699 clen = s - p;
9700 while (*s != NUL && c == mb_ptr2char(s))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009701 {
Bram Moolenaar86576712019-01-25 20:48:33 +01009702 ++same_len;
9703 s += clen;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009704 }
Bram Moolenaar86576712019-01-25 20:48:33 +01009705 if (same_len > 20)
9706 {
9707 ga_concat(gap, (char_u *)"\\[");
9708 ga_concat_esc(gap, p, clen);
9709 ga_concat(gap, (char_u *)" occurs ");
9710 vim_snprintf((char *)buf, NUMBUFLEN, "%d", same_len);
9711 ga_concat(gap, buf);
9712 ga_concat(gap, (char_u *)" times]");
9713 p = s - 1;
9714 }
9715 else
9716 ga_concat_esc(gap, p, clen);
9717 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009718}
9719
9720/*
9721 * Fill "gap" with information about an assert error.
9722 */
9723 void
9724fill_assert_error(
9725 garray_T *gap,
9726 typval_T *opt_msg_tv,
9727 char_u *exp_str,
9728 typval_T *exp_tv,
9729 typval_T *got_tv,
9730 assert_type_T atype)
9731{
9732 char_u numbuf[NUMBUFLEN];
9733 char_u *tofree;
9734
9735 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9736 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009737 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9738 vim_free(tofree);
9739 ga_concat(gap, (char_u *)": ");
9740 }
9741
9742 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9743 ga_concat(gap, (char_u *)"Pattern ");
9744 else if (atype == ASSERT_NOTEQUAL)
9745 ga_concat(gap, (char_u *)"Expected not equal to ");
9746 else
9747 ga_concat(gap, (char_u *)"Expected ");
9748 if (exp_str == NULL)
9749 {
Bram Moolenaar86576712019-01-25 20:48:33 +01009750 ga_concat_shorten_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009751 vim_free(tofree);
9752 }
9753 else
Bram Moolenaar86576712019-01-25 20:48:33 +01009754 ga_concat_shorten_esc(gap, exp_str);
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009755 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009756 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009757 if (atype == ASSERT_MATCH)
9758 ga_concat(gap, (char_u *)" does not match ");
9759 else if (atype == ASSERT_NOTMATCH)
9760 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009761 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009762 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar86576712019-01-25 20:48:33 +01009763 ga_concat_shorten_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009764 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009765 }
9766}
9767
Bram Moolenaar31988702018-02-13 12:57:42 +01009768/*
9769 * Compare "typ1" and "typ2". Put the result in "typ1".
9770 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009771 int
9772typval_compare(
9773 typval_T *typ1, /* first operand */
9774 typval_T *typ2, /* second operand */
9775 exptype_T type, /* operator */
9776 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +01009777 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009778{
9779 int i;
9780 varnumber_T n1, n2;
9781 char_u *s1, *s2;
9782 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
9783
Bram Moolenaar31988702018-02-13 12:57:42 +01009784 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009785 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009786 /* For "is" a different type always means FALSE, for "notis"
9787 * it means TRUE. */
9788 n1 = (type == TYPE_NEQUAL);
9789 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009790 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
9791 {
9792 if (type_is)
9793 {
9794 n1 = (typ1->v_type == typ2->v_type
9795 && typ1->vval.v_blob == typ2->vval.v_blob);
9796 if (type == TYPE_NEQUAL)
9797 n1 = !n1;
9798 }
9799 else if (typ1->v_type != typ2->v_type
9800 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9801 {
9802 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009803 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009804 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009805 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009806 clear_tv(typ1);
9807 return FAIL;
9808 }
9809 else
9810 {
9811 // Compare two Blobs for being equal or unequal.
9812 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
9813 if (type == TYPE_NEQUAL)
9814 n1 = !n1;
9815 }
9816 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009817 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
9818 {
9819 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009820 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009821 n1 = (typ1->v_type == typ2->v_type
9822 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009823 if (type == TYPE_NEQUAL)
9824 n1 = !n1;
9825 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009826 else if (typ1->v_type != typ2->v_type
9827 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009828 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009829 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009830 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009831 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009832 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009833 clear_tv(typ1);
9834 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009835 }
9836 else
9837 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009838 /* Compare two Lists for being equal or unequal. */
9839 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
9840 ic, FALSE);
9841 if (type == TYPE_NEQUAL)
9842 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009843 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009844 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009845
9846 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
9847 {
9848 if (type_is)
9849 {
9850 n1 = (typ1->v_type == typ2->v_type
9851 && typ1->vval.v_dict == typ2->vval.v_dict);
9852 if (type == TYPE_NEQUAL)
9853 n1 = !n1;
9854 }
9855 else if (typ1->v_type != typ2->v_type
9856 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9857 {
9858 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009859 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009860 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009861 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009862 clear_tv(typ1);
9863 return FAIL;
9864 }
9865 else
9866 {
9867 /* Compare two Dictionaries for being equal or unequal. */
9868 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
9869 ic, FALSE);
9870 if (type == TYPE_NEQUAL)
9871 n1 = !n1;
9872 }
9873 }
9874
9875 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
9876 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
9877 {
9878 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
9879 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009880 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009881 clear_tv(typ1);
9882 return FAIL;
9883 }
9884 if ((typ1->v_type == VAR_PARTIAL
9885 && typ1->vval.v_partial == NULL)
9886 || (typ2->v_type == VAR_PARTIAL
9887 && typ2->vval.v_partial == NULL))
9888 /* when a partial is NULL assume not equal */
9889 n1 = FALSE;
9890 else if (type_is)
9891 {
9892 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
9893 /* strings are considered the same if their value is
9894 * the same */
9895 n1 = tv_equal(typ1, typ2, ic, FALSE);
9896 else if (typ1->v_type == VAR_PARTIAL
9897 && typ2->v_type == VAR_PARTIAL)
9898 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
9899 else
9900 n1 = FALSE;
9901 }
9902 else
9903 n1 = tv_equal(typ1, typ2, ic, FALSE);
9904 if (type == TYPE_NEQUAL)
9905 n1 = !n1;
9906 }
9907
9908#ifdef FEAT_FLOAT
9909 /*
9910 * If one of the two variables is a float, compare as a float.
9911 * When using "=~" or "!~", always compare as string.
9912 */
9913 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
9914 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9915 {
9916 float_T f1, f2;
9917
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009918 f1 = tv_get_float(typ1);
9919 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009920 n1 = FALSE;
9921 switch (type)
9922 {
9923 case TYPE_EQUAL: n1 = (f1 == f2); break;
9924 case TYPE_NEQUAL: n1 = (f1 != f2); break;
9925 case TYPE_GREATER: n1 = (f1 > f2); break;
9926 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
9927 case TYPE_SMALLER: n1 = (f1 < f2); break;
9928 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
9929 case TYPE_UNKNOWN:
9930 case TYPE_MATCH:
9931 case TYPE_NOMATCH: break; /* avoid gcc warning */
9932 }
9933 }
9934#endif
9935
9936 /*
9937 * If one of the two variables is a number, compare as a number.
9938 * When using "=~" or "!~", always compare as string.
9939 */
9940 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
9941 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9942 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009943 n1 = tv_get_number(typ1);
9944 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009945 switch (type)
9946 {
9947 case TYPE_EQUAL: n1 = (n1 == n2); break;
9948 case TYPE_NEQUAL: n1 = (n1 != n2); break;
9949 case TYPE_GREATER: n1 = (n1 > n2); break;
9950 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
9951 case TYPE_SMALLER: n1 = (n1 < n2); break;
9952 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
9953 case TYPE_UNKNOWN:
9954 case TYPE_MATCH:
9955 case TYPE_NOMATCH: break; /* avoid gcc warning */
9956 }
9957 }
9958 else
9959 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009960 s1 = tv_get_string_buf(typ1, buf1);
9961 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009962 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
9963 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
9964 else
9965 i = 0;
9966 n1 = FALSE;
9967 switch (type)
9968 {
9969 case TYPE_EQUAL: n1 = (i == 0); break;
9970 case TYPE_NEQUAL: n1 = (i != 0); break;
9971 case TYPE_GREATER: n1 = (i > 0); break;
9972 case TYPE_GEQUAL: n1 = (i >= 0); break;
9973 case TYPE_SMALLER: n1 = (i < 0); break;
9974 case TYPE_SEQUAL: n1 = (i <= 0); break;
9975
9976 case TYPE_MATCH:
9977 case TYPE_NOMATCH:
9978 n1 = pattern_match(s2, s1, ic);
9979 if (type == TYPE_NOMATCH)
9980 n1 = !n1;
9981 break;
9982
9983 case TYPE_UNKNOWN: break; /* avoid gcc warning */
9984 }
9985 }
9986 clear_tv(typ1);
9987 typ1->v_type = VAR_NUMBER;
9988 typ1->vval.v_number = n1;
9989
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009990 return OK;
9991}
9992
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009993 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02009994typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009995{
9996 char_u *tofree;
9997 char_u numbuf[NUMBUFLEN];
9998 char_u *ret = NULL;
9999
10000 if (arg == NULL)
10001 return vim_strsave((char_u *)"(does not exist)");
10002 ret = tv2string(arg, &tofree, numbuf, 0);
10003 /* Make a copy if we have a value but it's not in allocated memory. */
10004 if (ret != NULL && tofree == NULL)
10005 ret = vim_strsave(ret);
10006 return ret;
10007}
10008
10009 int
10010var_exists(char_u *var)
10011{
10012 char_u *name;
10013 char_u *tofree;
10014 typval_T tv;
10015 int len = 0;
10016 int n = FALSE;
10017
10018 /* get_name_len() takes care of expanding curly braces */
10019 name = var;
10020 len = get_name_len(&var, &tofree, TRUE, FALSE);
10021 if (len > 0)
10022 {
10023 if (tofree != NULL)
10024 name = tofree;
10025 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
10026 if (n)
10027 {
10028 /* handle d.key, l[idx], f(expr) */
10029 n = (handle_subscript(&var, &tv, TRUE, FALSE) == OK);
10030 if (n)
10031 clear_tv(&tv);
10032 }
10033 }
10034 if (*var != NUL)
10035 n = FALSE;
10036
10037 vim_free(tofree);
10038 return n;
10039}
10040
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041#endif /* FEAT_EVAL */
10042
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010044#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045
Bram Moolenaar4f974752019-02-17 17:44:42 +010010046#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047/*
10048 * Functions for ":8" filename modifier: get 8.3 version of a filename.
10049 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050
10051/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010052 * Get the short path (8.3) for the filename in "fnamep".
10053 * Only works for a valid file name.
10054 * When the path gets longer "fnamep" is changed and the allocated buffer
10055 * is put in "bufp".
10056 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
10057 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058 */
10059 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010060get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010062 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 char_u *newbuf;
10064
10065 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010066 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 if (l > len - 1)
10068 {
10069 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010070 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071 newbuf = vim_strnsave(*fnamep, l);
10072 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010073 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074
10075 vim_free(*bufp);
10076 *fnamep = *bufp = newbuf;
10077
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010078 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010079 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080 }
10081
10082 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010083 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084}
10085
10086/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010087 * Get the short path (8.3) for the filename in "fname". The converted
10088 * path is returned in "bufp".
10089 *
10090 * Some of the directories specified in "fname" may not exist. This function
10091 * will shorten the existing directories at the beginning of the path and then
10092 * append the remaining non-existing path.
10093 *
10094 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020010095 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010096 * bufp - Pointer to an allocated buffer for the filename.
10097 * fnamelen - Length of the filename pointed to by fname
10098 *
10099 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100 */
10101 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010102shortpath_for_invalid_fname(
10103 char_u **fname,
10104 char_u **bufp,
10105 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010107 char_u *short_fname, *save_fname, *pbuf_unused;
10108 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010110 int old_len, len;
10111 int new_len, sfx_len;
10112 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113
10114 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010115 old_len = *fnamelen;
10116 save_fname = vim_strnsave(*fname, old_len);
10117 pbuf_unused = NULL;
10118 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010120 endp = save_fname + old_len - 1; /* Find the end of the copy */
10121 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010123 /*
10124 * Try shortening the supplied path till it succeeds by removing one
10125 * directory at a time from the tail of the path.
10126 */
10127 len = 0;
10128 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010130 /* go back one path-separator */
10131 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
10132 --endp;
10133 if (endp <= save_fname)
10134 break; /* processed the complete path */
10135
10136 /*
10137 * Replace the path separator with a NUL and try to shorten the
10138 * resulting path.
10139 */
10140 ch = *endp;
10141 *endp = 0;
10142 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010143 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010144 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
10145 {
10146 retval = FAIL;
10147 goto theend;
10148 }
10149 *endp = ch; /* preserve the string */
10150
10151 if (len > 0)
10152 break; /* successfully shortened the path */
10153
10154 /* failed to shorten the path. Skip the path separator */
10155 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156 }
10157
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010158 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010160 /*
10161 * Succeeded in shortening the path. Now concatenate the shortened
10162 * path with the remaining path at the tail.
10163 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010165 /* Compute the length of the new path. */
10166 sfx_len = (int)(save_endp - endp) + 1;
10167 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010168
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010169 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010171 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010172 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010173 /* There is not enough space in the currently allocated string,
10174 * copy it to a buffer big enough. */
10175 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010177 {
10178 retval = FAIL;
10179 goto theend;
10180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181 }
10182 else
10183 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010184 /* Transfer short_fname to the main buffer (it's big enough),
10185 * unless get_short_pathname() did its work in-place. */
10186 *fname = *bufp = save_fname;
10187 if (short_fname != save_fname)
10188 vim_strncpy(save_fname, short_fname, len);
10189 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010191
10192 /* concat the not-shortened part of the path */
10193 vim_strncpy(*fname + len, endp, sfx_len);
10194 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010196
10197theend:
10198 vim_free(pbuf_unused);
10199 vim_free(save_fname);
10200
10201 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202}
10203
10204/*
10205 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010206 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207 */
10208 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010209shortpath_for_partial(
10210 char_u **fnamep,
10211 char_u **bufp,
10212 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213{
10214 int sepcount, len, tflen;
10215 char_u *p;
10216 char_u *pbuf, *tfname;
10217 int hasTilde;
10218
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010219 /* Count up the path separators from the RHS.. so we know which part
10220 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010222 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223 if (vim_ispathsep(*p))
10224 ++sepcount;
10225
10226 /* Need full path first (use expand_env() to remove a "~/") */
10227 hasTilde = (**fnamep == '~');
10228 if (hasTilde)
10229 pbuf = tfname = expand_env_save(*fnamep);
10230 else
10231 pbuf = tfname = FullName_save(*fnamep, FALSE);
10232
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010233 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010235 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
10236 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237
10238 if (len == 0)
10239 {
10240 /* Don't have a valid filename, so shorten the rest of the
10241 * path if we can. This CAN give us invalid 8.3 filenames, but
10242 * there's not a lot of point in guessing what it might be.
10243 */
10244 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010245 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
10246 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247 }
10248
10249 /* Count the paths backward to find the beginning of the desired string. */
10250 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010251 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010252 if (has_mbyte)
10253 p -= mb_head_off(tfname, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254 if (vim_ispathsep(*p))
10255 {
10256 if (sepcount == 0 || (hasTilde && sepcount == 1))
10257 break;
10258 else
10259 sepcount --;
10260 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262 if (hasTilde)
10263 {
10264 --p;
10265 if (p >= tfname)
10266 *p = '~';
10267 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010268 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269 }
10270 else
10271 ++p;
10272
10273 /* Copy in the string - p indexes into tfname - allocated at pbuf */
10274 vim_free(*bufp);
10275 *fnamelen = (int)STRLEN(p);
10276 *bufp = pbuf;
10277 *fnamep = p;
10278
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010279 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280}
Bram Moolenaar4f974752019-02-17 17:44:42 +010010281#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282
10283/*
10284 * Adjust a filename, according to a string of modifiers.
10285 * *fnamep must be NUL terminated when called. When returning, the length is
10286 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010287 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288 * When there is an error, *fnamep is set to NULL.
10289 */
10290 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010291modify_fname(
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010292 char_u *src, // string with modifiers
10293 int tilde_file, // "~" is a file name, not $HOME
10294 int *usedlen, // characters after src that are used
10295 char_u **fnamep, // file name so far
10296 char_u **bufp, // buffer for allocated file name or NULL
10297 int *fnamelen) // length of fnamep
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298{
10299 int valid = 0;
10300 char_u *tail;
10301 char_u *s, *p, *pbuf;
10302 char_u dirname[MAXPATHL];
10303 int c;
10304 int has_fullname = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010305#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010306 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307 int has_shortname = 0;
10308#endif
10309
10310repeat:
10311 /* ":p" - full path/file_name */
10312 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
10313 {
10314 has_fullname = 1;
10315
10316 valid |= VALID_PATH;
10317 *usedlen += 2;
10318
10319 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
10320 if ((*fnamep)[0] == '~'
10321#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
10322 && ((*fnamep)[1] == '/'
10323# ifdef BACKSLASH_IN_FILENAME
10324 || (*fnamep)[1] == '\\'
10325# endif
10326 || (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010328 && !(tilde_file && (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329 )
10330 {
10331 *fnamep = expand_env_save(*fnamep);
10332 vim_free(*bufp); /* free any allocated file name */
10333 *bufp = *fnamep;
10334 if (*fnamep == NULL)
10335 return -1;
10336 }
10337
10338 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010339 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340 {
10341 if (vim_ispathsep(*p)
10342 && p[1] == '.'
10343 && (p[2] == NUL
10344 || vim_ispathsep(p[2])
10345 || (p[2] == '.'
10346 && (p[3] == NUL || vim_ispathsep(p[3])))))
10347 break;
10348 }
10349
10350 /* FullName_save() is slow, don't use it when not needed. */
10351 if (*p != NUL || !vim_isAbsName(*fnamep))
10352 {
10353 *fnamep = FullName_save(*fnamep, *p != NUL);
10354 vim_free(*bufp); /* free any allocated file name */
10355 *bufp = *fnamep;
10356 if (*fnamep == NULL)
10357 return -1;
10358 }
10359
Bram Moolenaar4f974752019-02-17 17:44:42 +010010360#ifdef MSWIN
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010361# if _WIN32_WINNT >= 0x0500
10362 if (vim_strchr(*fnamep, '~') != NULL)
10363 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010364 // Expand 8.3 filename to full path. Needed to make sure the same
10365 // file does not have two different names.
10366 // Note: problem does not occur if _WIN32_WINNT < 0x0500.
10367 WCHAR *wfname = enc_to_utf16(*fnamep, NULL);
10368 WCHAR buf[_MAX_PATH];
10369
10370 if (wfname != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010371 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010372 if (GetLongPathNameW(wfname, buf, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010373 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010374 char_u *p = utf16_to_enc(buf, NULL);
10375
10376 if (p != NULL)
10377 {
10378 vim_free(*bufp); // free any allocated file name
10379 *bufp = *fnamep = p;
10380 }
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010381 }
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010382 vim_free(wfname);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010383 }
10384 }
10385# endif
10386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387 /* Append a path separator to a directory. */
10388 if (mch_isdir(*fnamep))
10389 {
10390 /* Make room for one or two extra characters. */
10391 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
10392 vim_free(*bufp); /* free any allocated file name */
10393 *bufp = *fnamep;
10394 if (*fnamep == NULL)
10395 return -1;
10396 add_pathsep(*fnamep);
10397 }
10398 }
10399
10400 /* ":." - path relative to the current directory */
10401 /* ":~" - path relative to the home directory */
10402 /* ":8" - shortname path - postponed till after */
10403 while (src[*usedlen] == ':'
10404 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
10405 {
10406 *usedlen += 2;
10407 if (c == '8')
10408 {
Bram Moolenaar4f974752019-02-17 17:44:42 +010010409#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410 has_shortname = 1; /* Postpone this. */
10411#endif
10412 continue;
10413 }
10414 pbuf = NULL;
10415 /* Need full path first (use expand_env() to remove a "~/") */
10416 if (!has_fullname)
10417 {
10418 if (c == '.' && **fnamep == '~')
10419 p = pbuf = expand_env_save(*fnamep);
10420 else
10421 p = pbuf = FullName_save(*fnamep, FALSE);
10422 }
10423 else
10424 p = *fnamep;
10425
10426 has_fullname = 0;
10427
10428 if (p != NULL)
10429 {
10430 if (c == '.')
10431 {
10432 mch_dirname(dirname, MAXPATHL);
10433 s = shorten_fname(p, dirname);
10434 if (s != NULL)
10435 {
10436 *fnamep = s;
10437 if (pbuf != NULL)
10438 {
10439 vim_free(*bufp); /* free any allocated file name */
10440 *bufp = pbuf;
10441 pbuf = NULL;
10442 }
10443 }
10444 }
10445 else
10446 {
10447 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
10448 /* Only replace it when it starts with '~' */
10449 if (*dirname == '~')
10450 {
10451 s = vim_strsave(dirname);
10452 if (s != NULL)
10453 {
10454 *fnamep = s;
10455 vim_free(*bufp);
10456 *bufp = s;
10457 }
10458 }
10459 }
10460 vim_free(pbuf);
10461 }
10462 }
10463
10464 tail = gettail(*fnamep);
10465 *fnamelen = (int)STRLEN(*fnamep);
10466
10467 /* ":h" - head, remove "/file_name", can be repeated */
10468 /* Don't remove the first "/" or "c:\" */
10469 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
10470 {
10471 valid |= VALID_HEAD;
10472 *usedlen += 2;
10473 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010474 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010475 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010476 *fnamelen = (int)(tail - *fnamep);
10477#ifdef VMS
10478 if (*fnamelen > 0)
10479 *fnamelen += 1; /* the path separator is part of the path */
10480#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010481 if (*fnamelen == 0)
10482 {
10483 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
10484 p = vim_strsave((char_u *)".");
10485 if (p == NULL)
10486 return -1;
10487 vim_free(*bufp);
10488 *bufp = *fnamep = tail = p;
10489 *fnamelen = 1;
10490 }
10491 else
10492 {
10493 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010494 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496 }
10497
10498 /* ":8" - shortname */
10499 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
10500 {
10501 *usedlen += 2;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010502#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503 has_shortname = 1;
10504#endif
10505 }
10506
Bram Moolenaar4f974752019-02-17 17:44:42 +010010507#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010508 /*
10509 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510 */
10511 if (has_shortname)
10512 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010513 /* Copy the string if it is shortened by :h and when it wasn't copied
10514 * yet, because we are going to change it in place. Avoids changing
10515 * the buffer name for "%:8". */
10516 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517 {
10518 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020010519 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520 return -1;
10521 vim_free(*bufp);
10522 *bufp = *fnamep = p;
10523 }
10524
10525 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020010526 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010527 if (!has_fullname && !vim_isAbsName(*fnamep))
10528 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010529 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530 return -1;
10531 }
10532 else
10533 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010534 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535
Bram Moolenaardc935552011-08-17 15:23:23 +020010536 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010538 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539 return -1;
10540
10541 if (l == 0)
10542 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010543 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010545 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546 return -1;
10547 }
10548 *fnamelen = l;
10549 }
10550 }
Bram Moolenaar4f974752019-02-17 17:44:42 +010010551#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552
10553 /* ":t" - tail, just the basename */
10554 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
10555 {
10556 *usedlen += 2;
10557 *fnamelen -= (int)(tail - *fnamep);
10558 *fnamep = tail;
10559 }
10560
10561 /* ":e" - extension, can be repeated */
10562 /* ":r" - root, without extension, can be repeated */
10563 while (src[*usedlen] == ':'
10564 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
10565 {
10566 /* find a '.' in the tail:
10567 * - for second :e: before the current fname
10568 * - otherwise: The last '.'
10569 */
10570 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
10571 s = *fnamep - 2;
10572 else
10573 s = *fnamep + *fnamelen - 1;
10574 for ( ; s > tail; --s)
10575 if (s[0] == '.')
10576 break;
10577 if (src[*usedlen + 1] == 'e') /* :e */
10578 {
10579 if (s > tail)
10580 {
10581 *fnamelen += (int)(*fnamep - (s + 1));
10582 *fnamep = s + 1;
10583#ifdef VMS
10584 /* cut version from the extension */
10585 s = *fnamep + *fnamelen - 1;
10586 for ( ; s > *fnamep; --s)
10587 if (s[0] == ';')
10588 break;
10589 if (s > *fnamep)
10590 *fnamelen = s - *fnamep;
10591#endif
10592 }
10593 else if (*fnamep <= tail)
10594 *fnamelen = 0;
10595 }
10596 else /* :r */
10597 {
10598 if (s > tail) /* remove one extension */
10599 *fnamelen = (int)(s - *fnamep);
10600 }
10601 *usedlen += 2;
10602 }
10603
10604 /* ":s?pat?foo?" - substitute */
10605 /* ":gs?pat?foo?" - global substitute */
10606 if (src[*usedlen] == ':'
10607 && (src[*usedlen + 1] == 's'
10608 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
10609 {
10610 char_u *str;
10611 char_u *pat;
10612 char_u *sub;
10613 int sep;
10614 char_u *flags;
10615 int didit = FALSE;
10616
10617 flags = (char_u *)"";
10618 s = src + *usedlen + 2;
10619 if (src[*usedlen + 1] == 'g')
10620 {
10621 flags = (char_u *)"g";
10622 ++s;
10623 }
10624
10625 sep = *s++;
10626 if (sep)
10627 {
10628 /* find end of pattern */
10629 p = vim_strchr(s, sep);
10630 if (p != NULL)
10631 {
10632 pat = vim_strnsave(s, (int)(p - s));
10633 if (pat != NULL)
10634 {
10635 s = p + 1;
10636 /* find end of substitution */
10637 p = vim_strchr(s, sep);
10638 if (p != NULL)
10639 {
10640 sub = vim_strnsave(s, (int)(p - s));
10641 str = vim_strnsave(*fnamep, *fnamelen);
10642 if (sub != NULL && str != NULL)
10643 {
10644 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010645 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646 if (s != NULL)
10647 {
10648 *fnamep = s;
10649 *fnamelen = (int)STRLEN(s);
10650 vim_free(*bufp);
10651 *bufp = s;
10652 didit = TRUE;
10653 }
10654 }
10655 vim_free(sub);
10656 vim_free(str);
10657 }
10658 vim_free(pat);
10659 }
10660 }
10661 /* after using ":s", repeat all the modifiers */
10662 if (didit)
10663 goto repeat;
10664 }
10665 }
10666
Bram Moolenaar26df0922014-02-23 23:39:13 +010010667 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10668 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010669 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010670 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010671 if (c != NUL)
10672 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010673 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010674 if (c != NUL)
10675 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010676 if (p == NULL)
10677 return -1;
10678 vim_free(*bufp);
10679 *bufp = *fnamep = p;
10680 *fnamelen = (int)STRLEN(p);
10681 *usedlen += 2;
10682 }
10683
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684 return valid;
10685}
10686
10687/*
10688 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010689 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690 * "flags" can be "g" to do a global substitute.
10691 * Returns an allocated string, NULL for error.
10692 */
10693 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010694do_string_sub(
10695 char_u *str,
10696 char_u *pat,
10697 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010698 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010699 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700{
10701 int sublen;
10702 regmatch_T regmatch;
10703 int i;
10704 int do_all;
10705 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010706 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707 garray_T ga;
10708 char_u *ret;
10709 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010710 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711
10712 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
10713 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010714 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715
10716 ga_init2(&ga, 1, 200);
10717
10718 do_all = (flags[0] == 'g');
10719
10720 regmatch.rm_ic = p_ic;
10721 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10722 if (regmatch.regprog != NULL)
10723 {
10724 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010725 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
10727 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010010728 /* Skip empty match except for first match. */
10729 if (regmatch.startp[0] == regmatch.endp[0])
10730 {
10731 if (zero_width == regmatch.startp[0])
10732 {
10733 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020010734 i = MB_PTR2LEN(tail);
10735 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
10736 (size_t)i);
10737 ga.ga_len += i;
10738 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010739 continue;
10740 }
10741 zero_width = regmatch.startp[0];
10742 }
10743
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744 /*
10745 * Get some space for a temporary buffer to do the substitution
10746 * into. It will contain:
10747 * - The text up to where the match is.
10748 * - The substituted text.
10749 * - The text after the match.
10750 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010751 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010010752 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
10754 {
10755 ga_clear(&ga);
10756 break;
10757 }
10758
10759 /* copy the text up to where the match is */
10760 i = (int)(regmatch.startp[0] - tail);
10761 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10762 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010763 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764 + ga.ga_len + i, TRUE, TRUE, FALSE);
10765 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010766 tail = regmatch.endp[0];
10767 if (*tail == NUL)
10768 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010769 if (!do_all)
10770 break;
10771 }
10772
10773 if (ga.ga_data != NULL)
10774 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10775
Bram Moolenaar473de612013-06-08 18:19:48 +020010776 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777 }
10778
10779 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10780 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010781 if (p_cpo == empty_option)
10782 p_cpo = save_cpo;
10783 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010784 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010785 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010786
10787 return ret;
10788}
10789
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010790 static int
10791filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10792{
10793 typval_T rettv;
10794 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010795 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010796
10797 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10798 argv[0] = vimvars[VV_KEY].vv_tv;
10799 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010800 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10801 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010802 if (map)
10803 {
10804 /* map(): replace the list item value */
10805 clear_tv(tv);
10806 rettv.v_lock = 0;
10807 *tv = rettv;
10808 }
10809 else
10810 {
10811 int error = FALSE;
10812
10813 /* filter(): when expr is zero remove the item */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010814 *remp = (tv_get_number_chk(&rettv, &error) == 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010815 clear_tv(&rettv);
10816 /* On type error, nothing has been removed; return FAIL to stop the
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010817 * loop. The error message was given by tv_get_number_chk(). */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010818 if (error)
10819 goto theend;
10820 }
10821 retval = OK;
10822theend:
10823 clear_tv(&vimvars[VV_VAL].vv_tv);
10824 return retval;
10825}
10826
10827
10828/*
10829 * Implementation of map() and filter().
10830 */
10831 void
10832filter_map(typval_T *argvars, typval_T *rettv, int map)
10833{
10834 typval_T *expr;
10835 listitem_T *li, *nli;
10836 list_T *l = NULL;
10837 dictitem_T *di;
10838 hashtab_T *ht;
10839 hashitem_T *hi;
10840 dict_T *d = NULL;
10841 typval_T save_val;
10842 typval_T save_key;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010843 blob_T *b = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010844 int rem;
10845 int todo;
10846 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10847 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10848 : N_("filter() argument"));
10849 int save_did_emsg;
10850 int idx = 0;
10851
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010852 if (argvars[0].v_type == VAR_BLOB)
10853 {
10854 if ((b = argvars[0].vval.v_blob) == NULL)
10855 return;
10856 }
10857 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010858 {
10859 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010860 || (!map && var_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010861 return;
10862 }
10863 else if (argvars[0].v_type == VAR_DICT)
10864 {
10865 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010866 || (!map && var_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010867 return;
10868 }
10869 else
10870 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010871 semsg(_(e_listdictarg), ermsg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010872 return;
10873 }
10874
10875 expr = &argvars[1];
10876 /* On type errors, the preceding call has already displayed an error
10877 * message. Avoid a misleading error message for an empty string that
10878 * was not passed as argument. */
10879 if (expr->v_type != VAR_UNKNOWN)
10880 {
10881 prepare_vimvar(VV_VAL, &save_val);
10882
10883 /* We reset "did_emsg" to be able to detect whether an error
10884 * occurred during evaluation of the expression. */
10885 save_did_emsg = did_emsg;
10886 did_emsg = FALSE;
10887
10888 prepare_vimvar(VV_KEY, &save_key);
10889 if (argvars[0].v_type == VAR_DICT)
10890 {
10891 vimvars[VV_KEY].vv_type = VAR_STRING;
10892
10893 ht = &d->dv_hashtab;
10894 hash_lock(ht);
10895 todo = (int)ht->ht_used;
10896 for (hi = ht->ht_array; todo > 0; ++hi)
10897 {
10898 if (!HASHITEM_EMPTY(hi))
10899 {
10900 int r;
10901
10902 --todo;
10903 di = HI2DI(hi);
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010904 if (map && (var_check_lock(di->di_tv.v_lock,
10905 arg_errmsg, TRUE)
10906 || var_check_ro(di->di_flags,
10907 arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010908 break;
10909 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10910 r = filter_map_one(&di->di_tv, expr, map, &rem);
10911 clear_tv(&vimvars[VV_KEY].vv_tv);
10912 if (r == FAIL || did_emsg)
10913 break;
10914 if (!map && rem)
10915 {
10916 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10917 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10918 break;
10919 dictitem_remove(d, di);
10920 }
10921 }
10922 }
10923 hash_unlock(ht);
10924 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010925 else if (argvars[0].v_type == VAR_BLOB)
10926 {
10927 int i;
10928 typval_T tv;
10929
10930 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10931 for (i = 0; i < b->bv_ga.ga_len; i++)
10932 {
10933 tv.v_type = VAR_NUMBER;
10934 tv.vval.v_number = blob_get(b, i);
10935 vimvars[VV_KEY].vv_nr = idx;
10936 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg)
10937 break;
10938 if (tv.v_type != VAR_NUMBER)
10939 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010940 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010941 return;
10942 }
10943 tv.v_type = VAR_NUMBER;
10944 blob_set(b, i, tv.vval.v_number);
10945 if (!map && rem)
10946 {
10947 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
10948
10949 mch_memmove(p + idx, p + i + 1,
10950 (size_t)b->bv_ga.ga_len - i - 1);
10951 --b->bv_ga.ga_len;
10952 --i;
10953 }
10954 }
10955 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010956 else
10957 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +010010958 // argvars[0].v_type == VAR_LIST
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010959 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10960
10961 for (li = l->lv_first; li != NULL; li = nli)
10962 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010963 if (map && var_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010964 break;
10965 nli = li->li_next;
10966 vimvars[VV_KEY].vv_nr = idx;
10967 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10968 || did_emsg)
10969 break;
10970 if (!map && rem)
10971 listitem_remove(l, li);
10972 ++idx;
10973 }
10974 }
10975
10976 restore_vimvar(VV_KEY, &save_key);
10977 restore_vimvar(VV_VAL, &save_val);
10978
10979 did_emsg |= save_did_emsg;
10980 }
10981
10982 copy_tv(&argvars[0], rettv);
10983}
10984
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */