blob: e2d3d48bd3c1a63fb0d0a46d42cf2f194fe74efa [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},
Bram Moolenaar37df9a42019-06-14 14:39:51 +0200196 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
197 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
198 {VV_NAME("event", VAR_DICT), VV_RO},
199 {VV_NAME("versionlong", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000200};
201
202/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000203#define vv_type vv_di.di_tv.v_type
204#define vv_nr vv_di.di_tv.vval.v_number
205#define vv_float vv_di.di_tv.vval.v_float
206#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000207#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200208#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100209#define vv_blob vv_di.di_tv.vval.v_blob
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000210#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000211
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200212static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000213#define vimvarht vimvardict.dv_hashtab
214
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100215static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
216static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
217static char_u *skip_var_one(char_u *arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100218static void list_glob_vars(int *first);
219static void list_buf_vars(int *first);
220static void list_win_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100221static void list_tab_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100222static void list_vim_vars(int *first);
223static void list_script_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100224static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
225static 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 +0100226static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
227static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100228static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
229static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
230static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
231static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000232
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100233static int eval2(char_u **arg, typval_T *rettv, int evaluate);
234static int eval3(char_u **arg, typval_T *rettv, int evaluate);
235static int eval4(char_u **arg, typval_T *rettv, int evaluate);
236static int eval5(char_u **arg, typval_T *rettv, int evaluate);
237static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
238static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000239
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100240static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
241static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100242static int free_unref_items(int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100243static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100244static int get_env_len(char_u **arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100245static 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 +0200246static void check_vars(char_u *name, int len);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100247static typval_T *alloc_string_tv(char_u *string);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100248static void delete_var(hashtab_T *ht, hashitem_T *hi);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100249static void list_one_var(dictitem_T *v, char *prefix, int *first);
250static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
Bram Moolenaar05c00c02019-02-11 22:00:11 +0100251static int tv_check_lock(typval_T *tv, char_u *name, int use_gettext);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100252static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200253
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200254/* for VIM_VERSION_ defines */
255#include "version.h"
256
Bram Moolenaare21c1582019-03-02 11:57:09 +0100257/*
258 * Return "n1" divided by "n2", taking care of dividing by zero.
259 */
260 static varnumber_T
261num_divide(varnumber_T n1, varnumber_T n2)
262{
263 varnumber_T result;
264
265 if (n2 == 0) // give an error message?
266 {
267 if (n1 == 0)
268 result = VARNUM_MIN; // similar to NaN
269 else if (n1 < 0)
270 result = -VARNUM_MAX;
271 else
272 result = VARNUM_MAX;
273 }
274 else
275 result = n1 / n2;
276
277 return result;
278}
279
280/*
281 * Return "n1" modulus "n2", taking care of dividing by zero.
282 */
283 static varnumber_T
284num_modulus(varnumber_T n1, varnumber_T n2)
285{
286 // Give an error when n2 is 0?
287 return (n2 == 0) ? 0 : (n1 % n2);
288}
289
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100290
291#if defined(EBCDIC) || defined(PROTO)
292/*
293 * Compare struct fst by function name.
294 */
295 static int
296compare_func_name(const void *s1, const void *s2)
297{
298 struct fst *p1 = (struct fst *)s1;
299 struct fst *p2 = (struct fst *)s2;
300
301 return STRCMP(p1->f_name, p2->f_name);
302}
303
304/*
305 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100306 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100307 * On machines using EBCDIC we have to sort it.
308 */
309 static void
310sortFunctions(void)
311{
312 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
313
314 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
315}
316#endif
317
318
Bram Moolenaar33570922005-01-25 22:26:29 +0000319/*
320 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000321 */
322 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100323eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000324{
Bram Moolenaar33570922005-01-25 22:26:29 +0000325 int i;
326 struct vimvar *p;
327
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200328 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
329 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200330 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000331 hash_init(&compat_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200332 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000333
334 for (i = 0; i < VV_LEN; ++i)
335 {
336 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200337 if (STRLEN(p->vv_name) > 16)
338 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100339 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200340 getout(1);
341 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000342 STRCPY(p->vv_di.di_key, p->vv_name);
343 if (p->vv_flags & VV_RO)
344 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
345 else if (p->vv_flags & VV_RO_SBX)
346 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
347 else
348 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000349
350 /* add to v: scope dict, unless the value is not always available */
351 if (p->vv_type != VAR_UNKNOWN)
352 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000353 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000354 /* add to compat scope dict */
355 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000356 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100357 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
Bram Moolenaar37df9a42019-06-14 14:39:51 +0200358 vimvars[VV_VERSIONLONG].vv_nr = VIM_VERSION_100 * 10000 + highest_patch();
Bram Moolenaara542c682016-01-31 16:28:04 +0100359
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000360 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100361 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100362 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100363 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100364 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100365
366 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
367 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
368 set_vim_var_nr(VV_NONE, VVAL_NONE);
369 set_vim_var_nr(VV_NULL, VVAL_NULL);
370
Bram Moolenaarf562e722016-07-19 17:25:25 +0200371 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
372 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
373 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
374 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
375 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
376 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
377 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
378 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
379 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
380 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100381 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
Bram Moolenaarf562e722016-07-19 17:25:25 +0200382
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200383 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200384
385#ifdef EBCDIC
386 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100387 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200388 */
389 sortFunctions();
390#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000391}
392
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000393#if defined(EXITFREE) || defined(PROTO)
394 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100395eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000396{
397 int i;
398 struct vimvar *p;
399
400 for (i = 0; i < VV_LEN; ++i)
401 {
402 p = &vimvars[i];
403 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100404 VIM_CLEAR(p->vv_str);
Bram Moolenaard812df62008-11-09 12:46:09 +0000405 else if (p->vv_di.di_tv.v_type == VAR_LIST)
406 {
407 list_unref(p->vv_list);
408 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000409 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000410 }
411 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000412 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000413 hash_clear(&compat_hashtab);
414
Bram Moolenaard9fba312005-06-26 22:34:35 +0000415 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100416# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200417 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100418# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000419
420 /* global variables */
421 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000422
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000423 /* autoloaded script names */
424 ga_clear_strings(&ga_loaded);
425
Bram Moolenaarcca74132013-09-25 21:00:28 +0200426 /* Script-local variables. First clear all the variables and in a second
427 * loop free the scriptvar_T, because a variable in one script might hold
428 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200429 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200430 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200431 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200432 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200433 ga_clear(&ga_scripts);
434
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200435 // unreferenced lists and dicts
436 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200437
438 // functions not garbage collected
439 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000440}
441#endif
442
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443
444/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 * Set an internal variable to a string value. Creates the variable if it does
446 * not already exist.
447 */
448 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100449set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000451 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000452 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453
454 val = vim_strsave(value);
455 if (val != NULL)
456 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000457 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000458 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000460 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000461 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 }
463 }
464}
465
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000466static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200467#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000468static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000469static char_u *redir_endp = NULL;
470static char_u *redir_varname = NULL;
471
472/*
473 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100474 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000475 * Returns OK if successfully completed the setup. FAIL otherwise.
476 */
477 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100478var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000479{
480 int save_emsg;
481 int err;
482 typval_T tv;
483
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000484 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000485 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000486 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100487 emsg(_(e_invarg));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000488 return FAIL;
489 }
490
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000491 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000492 redir_varname = vim_strsave(name);
493 if (redir_varname == NULL)
494 return FAIL;
495
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200496 redir_lval = ALLOC_CLEAR_ONE(lval_T);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000497 if (redir_lval == NULL)
498 {
499 var_redir_stop();
500 return FAIL;
501 }
502
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000503 /* The output is stored in growarray "redir_ga" until redirection ends. */
504 ga_init2(&redir_ga, (int)sizeof(char), 500);
505
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000506 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100507 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000508 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000509 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
510 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200511 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000512 if (redir_endp != NULL && *redir_endp != NUL)
513 /* Trailing characters are present after the variable name */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100514 emsg(_(e_trailing));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000515 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100516 emsg(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000517 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000518 var_redir_stop();
519 return FAIL;
520 }
521
522 /* check if we can write to the variable: set it to or append an empty
523 * string */
524 save_emsg = did_emsg;
525 did_emsg = FALSE;
526 tv.v_type = VAR_STRING;
527 tv.vval.v_string = (char_u *)"";
528 if (append)
529 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
530 else
531 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200532 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000533 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000534 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000535 if (err)
536 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000537 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000538 var_redir_stop();
539 return FAIL;
540 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000541
542 return OK;
543}
544
545/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000546 * Append "value[value_len]" to the variable set by var_redir_start().
547 * The actual appending is postponed until redirection ends, because the value
548 * appended may in fact be the string we write to, changing it may cause freed
549 * memory to be used:
550 * :redir => foo
551 * :let foo
552 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000553 */
554 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100555var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000556{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000557 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000558
559 if (redir_lval == NULL)
560 return;
561
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000562 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000563 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000564 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000565 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000566
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000567 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000568 {
569 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000570 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000571 }
572 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000573 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000574}
575
576/*
577 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000578 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000579 */
580 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100581var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000582{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000583 typval_T tv;
584
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200585 if (EVALCMD_BUSY)
586 {
587 redir_lval = NULL;
588 return;
589 }
590
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000591 if (redir_lval != NULL)
592 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000593 /* If there was no error: assign the text to the variable. */
594 if (redir_endp != NULL)
595 {
596 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
597 tv.v_type = VAR_STRING;
598 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200599 /* Call get_lval() again, if it's inside a Dict or List it may
600 * have changed. */
601 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100602 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200603 if (redir_endp != NULL && redir_lval->ll_name != NULL)
604 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
605 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000606 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000607
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000608 /* free the collected output */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100609 VIM_CLEAR(redir_ga.ga_data);
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000610
Bram Moolenaard23a8232018-02-10 18:45:26 +0100611 VIM_CLEAR(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000612 }
Bram Moolenaard23a8232018-02-10 18:45:26 +0100613 VIM_CLEAR(redir_varname);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000614}
615
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100617eval_charconvert(
618 char_u *enc_from,
619 char_u *enc_to,
620 char_u *fname_from,
621 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622{
623 int err = FALSE;
624
625 set_vim_var_string(VV_CC_FROM, enc_from, -1);
626 set_vim_var_string(VV_CC_TO, enc_to, -1);
627 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
628 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
629 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
630 err = TRUE;
631 set_vim_var_string(VV_CC_FROM, NULL, -1);
632 set_vim_var_string(VV_CC_TO, NULL, -1);
633 set_vim_var_string(VV_FNAME_IN, NULL, -1);
634 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
635
636 if (err)
637 return FAIL;
638 return OK;
639}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640
641# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
642 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100643eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644{
645 int err = FALSE;
646
647 set_vim_var_string(VV_FNAME_IN, fname, -1);
648 set_vim_var_string(VV_CMDARG, args, -1);
649 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
650 err = TRUE;
651 set_vim_var_string(VV_FNAME_IN, NULL, -1);
652 set_vim_var_string(VV_CMDARG, NULL, -1);
653
654 if (err)
655 {
656 mch_remove(fname);
657 return FAIL;
658 }
659 return OK;
660}
661# endif
662
663# if defined(FEAT_DIFF) || defined(PROTO)
664 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100665eval_diff(
666 char_u *origfile,
667 char_u *newfile,
668 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669{
670 int err = FALSE;
671
672 set_vim_var_string(VV_FNAME_IN, origfile, -1);
673 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
674 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
675 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
676 set_vim_var_string(VV_FNAME_IN, NULL, -1);
677 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
678 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
679}
680
681 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100682eval_patch(
683 char_u *origfile,
684 char_u *difffile,
685 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686{
687 int err;
688
689 set_vim_var_string(VV_FNAME_IN, origfile, -1);
690 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
691 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
692 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
693 set_vim_var_string(VV_FNAME_IN, NULL, -1);
694 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
695 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
696}
697# endif
698
699/*
700 * Top level evaluation function, returning a boolean.
701 * Sets "error" to TRUE if there was an error.
702 * Return TRUE or FALSE.
703 */
704 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100705eval_to_bool(
706 char_u *arg,
707 int *error,
708 char_u **nextcmd,
709 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710{
Bram Moolenaar33570922005-01-25 22:26:29 +0000711 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200712 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713
714 if (skip)
715 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000716 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 else
719 {
720 *error = FALSE;
721 if (!skip)
722 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100723 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000724 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 }
726 }
727 if (skip)
728 --emsg_skip;
729
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200730 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731}
732
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100733/*
734 * Call eval1() and give an error message if not done at a lower level.
735 */
736 static int
737eval1_emsg(char_u **arg, typval_T *rettv, int evaluate)
738{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100739 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100740 int ret;
741 int did_emsg_before = did_emsg;
742 int called_emsg_before = called_emsg;
743
744 ret = eval1(arg, rettv, evaluate);
745 if (ret == FAIL)
746 {
747 // Report the invalid expression unless the expression evaluation has
748 // been cancelled due to an aborting error, an interrupt, or an
749 // exception, or we already gave a more specific error.
750 // Also check called_emsg for when using assert_fails().
751 if (!aborting() && did_emsg == did_emsg_before
752 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100753 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100754 }
755 return ret;
756}
757
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200758 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100759eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
760{
761 char_u *s;
762 int dummy;
763 char_u buf[NUMBUFLEN];
764
765 if (expr->v_type == VAR_FUNC)
766 {
767 s = expr->vval.v_string;
768 if (s == NULL || *s == NUL)
769 return FAIL;
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200770 if (call_func(s, -1, rettv, argc, argv, NULL,
Bram Moolenaar48570482017-10-30 21:48:41 +0100771 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
772 return FAIL;
773 }
774 else if (expr->v_type == VAR_PARTIAL)
775 {
776 partial_T *partial = expr->vval.v_partial;
777
778 s = partial_name(partial);
779 if (s == NULL || *s == NUL)
780 return FAIL;
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200781 if (call_func(s, -1, rettv, argc, argv, NULL,
Bram Moolenaar48570482017-10-30 21:48:41 +0100782 0L, 0L, &dummy, TRUE, partial, NULL) == FAIL)
783 return FAIL;
784 }
785 else
786 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100787 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100788 if (s == NULL)
789 return FAIL;
790 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100791 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100792 return FAIL;
793 if (*s != NUL) /* check for trailing chars after expr */
794 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200795 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100796 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100797 return FAIL;
798 }
799 }
800 return OK;
801}
802
803/*
804 * Like eval_to_bool() but using a typval_T instead of a string.
805 * Works for string, funcref and partial.
806 */
807 int
808eval_expr_to_bool(typval_T *expr, int *error)
809{
810 typval_T rettv;
811 int res;
812
813 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
814 {
815 *error = TRUE;
816 return FALSE;
817 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100818 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100819 clear_tv(&rettv);
820 return res;
821}
822
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823/*
824 * Top level evaluation function, returning a string. If "skip" is TRUE,
825 * only parsing to "nextcmd" is done, without reporting errors. Return
826 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
827 */
828 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100829eval_to_string_skip(
830 char_u *arg,
831 char_u **nextcmd,
832 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833{
Bram Moolenaar33570922005-01-25 22:26:29 +0000834 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 char_u *retval;
836
837 if (skip)
838 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000839 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 retval = NULL;
841 else
842 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100843 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000844 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 }
846 if (skip)
847 --emsg_skip;
848
849 return retval;
850}
851
852/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000853 * Skip over an expression at "*pp".
854 * Return FAIL for an error, OK otherwise.
855 */
856 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100857skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000858{
Bram Moolenaar33570922005-01-25 22:26:29 +0000859 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000860
861 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000862 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000863}
864
865/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000867 * When "convert" is TRUE convert a List into a sequence of lines and convert
868 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 * Return pointer to allocated memory, or NULL for failure.
870 */
871 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100872eval_to_string(
873 char_u *arg,
874 char_u **nextcmd,
875 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876{
Bram Moolenaar33570922005-01-25 22:26:29 +0000877 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000879 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000880#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000881 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000884 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 retval = NULL;
886 else
887 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000888 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000889 {
890 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000891 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200892 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200893 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200894 if (tv.vval.v_list->lv_len > 0)
895 ga_append(&ga, NL);
896 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000897 ga_append(&ga, NUL);
898 retval = (char_u *)ga.ga_data;
899 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000900#ifdef FEAT_FLOAT
901 else if (convert && tv.v_type == VAR_FLOAT)
902 {
903 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
904 retval = vim_strsave(numbuf);
905 }
906#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000907 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100908 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000909 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 }
911
912 return retval;
913}
914
915/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000916 * Call eval_to_string() without using current local variables and using
917 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 */
919 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100920eval_to_string_safe(
921 char_u *arg,
922 char_u **nextcmd,
923 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924{
925 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200926 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200928 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000929 if (use_sandbox)
930 ++sandbox;
931 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000932 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000933 if (use_sandbox)
934 --sandbox;
935 --textlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200936 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 return retval;
938}
939
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940/*
941 * Top level evaluation function, returning a number.
942 * Evaluates "expr" silently.
943 * Returns -1 for an error.
944 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200945 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100946eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947{
Bram Moolenaar33570922005-01-25 22:26:29 +0000948 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200949 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000950 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951
952 ++emsg_off;
953
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000954 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 retval = -1;
956 else
957 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100958 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000959 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 }
961 --emsg_off;
962
963 return retval;
964}
965
Bram Moolenaara40058a2005-07-11 22:42:07 +0000966/*
967 * Prepare v: variable "idx" to be used.
968 * Save the current typeval in "save_tv".
969 * When not used yet add the variable to the v: hashtable.
970 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200971 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100972prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000973{
974 *save_tv = vimvars[idx].vv_tv;
975 if (vimvars[idx].vv_type == VAR_UNKNOWN)
976 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
977}
978
979/*
980 * Restore v: variable "idx" to typeval "save_tv".
981 * When no longer defined, remove the variable from the v: hashtable.
982 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200983 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100984restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000985{
986 hashitem_T *hi;
987
Bram Moolenaara40058a2005-07-11 22:42:07 +0000988 vimvars[idx].vv_tv = *save_tv;
989 if (vimvars[idx].vv_type == VAR_UNKNOWN)
990 {
991 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
992 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100993 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +0000994 else
995 hash_remove(&vimvarht, hi);
996 }
997}
998
Bram Moolenaar3c56a962006-03-12 22:19:04 +0000999#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001000/*
1001 * Evaluate an expression to a list with suggestions.
1002 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001003 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001004 */
1005 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001006eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001007{
1008 typval_T save_val;
1009 typval_T rettv;
1010 list_T *list = NULL;
1011 char_u *p = skipwhite(expr);
1012
1013 /* Set "v:val" to the bad word. */
1014 prepare_vimvar(VV_VAL, &save_val);
1015 vimvars[VV_VAL].vv_type = VAR_STRING;
1016 vimvars[VV_VAL].vv_str = badword;
1017 if (p_verbose == 0)
1018 ++emsg_off;
1019
1020 if (eval1(&p, &rettv, TRUE) == OK)
1021 {
1022 if (rettv.v_type != VAR_LIST)
1023 clear_tv(&rettv);
1024 else
1025 list = rettv.vval.v_list;
1026 }
1027
1028 if (p_verbose == 0)
1029 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001030 restore_vimvar(VV_VAL, &save_val);
1031
1032 return list;
1033}
1034
1035/*
1036 * "list" is supposed to contain two items: a word and a number. Return the
1037 * word in "pp" and the number as the return value.
1038 * Return -1 if anything isn't right.
1039 * Used to get the good word and score from the eval_spell_expr() result.
1040 */
1041 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001042get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001043{
1044 listitem_T *li;
1045
1046 li = list->lv_first;
1047 if (li == NULL)
1048 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001049 *pp = tv_get_string(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001050
1051 li = li->li_next;
1052 if (li == NULL)
1053 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001054 return (int)tv_get_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001055}
1056#endif
1057
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001058/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001059 * Top level evaluation function.
1060 * Returns an allocated typval_T with the result.
1061 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001062 */
1063 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001064eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001065{
1066 typval_T *tv;
1067
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001068 tv = ALLOC_ONE(typval_T);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001069 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001070 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001071
1072 return tv;
1073}
1074
1075
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001077 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +02001078 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
1079 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001080 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001082 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001083call_vim_function(
1084 char_u *func,
1085 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +02001086 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001087 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 int doesrange;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001090 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001092 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001093 ret = call_func(func, -1, rettv, argc, argv, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001095 &doesrange, TRUE, NULL, NULL);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001096 if (ret == FAIL)
1097 clear_tv(rettv);
1098
1099 return ret;
1100}
1101
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001102/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001103 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001104 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001105 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1106 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001107 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001108 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001109call_func_retnr(
1110 char_u *func,
1111 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001112 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001113{
1114 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001115 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001116
Bram Moolenaarded27a12018-08-01 19:06:03 +02001117 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001118 return -1;
1119
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001120 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001121 clear_tv(&rettv);
1122 return retval;
1123}
1124
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001125#if defined(FEAT_CMDL_COMPL) \
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001126 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1127
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001128# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001129/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001130 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001131 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001132 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1133 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001134 */
1135 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001136call_func_retstr(
1137 char_u *func,
1138 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001139 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001140{
1141 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001142 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001143
Bram Moolenaarded27a12018-08-01 19:06:03 +02001144 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001145 return NULL;
1146
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001147 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001148 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 return retval;
1150}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001151# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001152
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001153/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001154 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001155 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1156 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001157 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001158 */
1159 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001160call_func_retlist(
1161 char_u *func,
1162 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001163 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001164{
1165 typval_T rettv;
1166
Bram Moolenaarded27a12018-08-01 19:06:03 +02001167 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001168 return NULL;
1169
1170 if (rettv.v_type != VAR_LIST)
1171 {
1172 clear_tv(&rettv);
1173 return NULL;
1174 }
1175
1176 return rettv.vval.v_list;
1177}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178#endif
1179
Bram Moolenaar05159a02005-02-26 23:04:13 +00001180
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181#ifdef FEAT_FOLDING
1182/*
1183 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1184 * it in "*cp". Doesn't give error messages.
1185 */
1186 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001187eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188{
Bram Moolenaar33570922005-01-25 22:26:29 +00001189 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001190 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001192 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1193 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194
1195 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001196 if (use_sandbox)
1197 ++sandbox;
1198 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001200 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 retval = 0;
1202 else
1203 {
1204 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001205 if (tv.v_type == VAR_NUMBER)
1206 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001207 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 retval = 0;
1209 else
1210 {
1211 /* If the result is a string, check if there is a non-digit before
1212 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001213 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 if (!VIM_ISDIGIT(*s) && *s != '-')
1215 *cp = *s++;
1216 retval = atol((char *)s);
1217 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001218 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 }
1220 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001221 if (use_sandbox)
1222 --sandbox;
1223 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001225 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226}
1227#endif
1228
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229/*
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001230 * Get a list of lines from a HERE document. The here document is a list of
1231 * lines surrounded by a marker.
1232 * cmd << {marker}
1233 * {line1}
1234 * {line2}
1235 * ....
1236 * {marker}
1237 *
1238 * The {marker} is a string. If the optional 'trim' word is supplied before the
1239 * marker, then the leading indentation before the lines (matching the
1240 * indentation in the 'cmd' line) is stripped.
1241 * Returns a List with {lines} or NULL.
1242 */
1243 static list_T *
1244heredoc_get(exarg_T *eap, char_u *cmd)
1245{
1246 char_u *theline;
1247 char_u *marker;
1248 list_T *l;
1249 char_u *p;
1250 int indent_len = 0;
1251
1252 if (eap->getline == NULL)
1253 {
1254 emsg(_("E991: cannot use =<< here"));
1255 return NULL;
1256 }
1257
1258 // Check for the optional 'trim' word before the marker
1259 cmd = skipwhite(cmd);
1260 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
1261 {
1262 cmd = skipwhite(cmd + 4);
1263
1264 // Trim the indentation from all the lines in the here document
1265 // The amount of indentation trimmed is the same as the indentation of
1266 // the :let command line.
1267 p = *eap->cmdlinep;
1268 while (VIM_ISWHITE(*p))
1269 {
1270 p++;
1271 indent_len++;
1272 }
1273 }
1274
1275 // The marker is the next word. Default marker is "."
1276 if (*cmd != NUL && *cmd != '"')
1277 {
1278 marker = skipwhite(cmd);
1279 p = skiptowhite(marker);
1280 if (*skipwhite(p) != NUL && *skipwhite(p) != '"')
1281 {
1282 emsg(_(e_trailing));
1283 return NULL;
1284 }
1285 *p = NUL;
1286 }
1287 else
1288 marker = (char_u *)".";
1289
1290 l = list_alloc();
1291 if (l == NULL)
1292 return NULL;
1293
1294 for (;;)
1295 {
1296 int i = 0;
1297
1298 theline = eap->getline(NUL, eap->cookie, 0);
1299 if (theline != NULL && indent_len > 0)
1300 {
1301 // trim the indent matching the first line
1302 if (STRNCMP(theline, *eap->cmdlinep, indent_len) == 0)
1303 i = indent_len;
1304 }
1305
1306 if (theline == NULL)
1307 {
1308 semsg(_("E990: Missing end marker '%s'"), marker);
1309 break;
1310 }
1311 if (STRCMP(marker, theline + i) == 0)
1312 {
1313 vim_free(theline);
1314 break;
1315 }
1316
1317 if (list_append_string(l, theline + i, -1) == FAIL)
1318 break;
1319 vim_free(theline);
1320 }
1321
1322 return l;
1323}
1324
1325/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001326 * ":let" list all variable values
1327 * ":let var1 var2" list variable values
1328 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001329 * ":let var += expr" assignment command.
1330 * ":let var -= expr" assignment command.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001331 * ":let var *= expr" assignment command.
1332 * ":let var /= expr" assignment command.
1333 * ":let var %= expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001334 * ":let var .= expr" assignment command.
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001335 * ":let var ..= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001336 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 */
1338 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001339ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340{
1341 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001342 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001343 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001345 int var_count = 0;
1346 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001347 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001348 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001349 int first = TRUE;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001350 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351
Bram Moolenaardb552d602006-03-23 22:59:57 +00001352 argend = skip_var_list(arg, &var_count, &semicolon);
1353 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001354 return;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001355 if (argend > arg && argend[-1] == '.') // for var.='str'
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001356 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001357 expr = skipwhite(argend);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001358 concat = expr[0] == '.'
1359 && ((expr[1] == '=' && current_sctx.sc_version < 2)
1360 || (expr[1] == '.' && expr[2] == '='));
1361 if (*expr != '=' && !((vim_strchr((char_u *)"+-*/%", *expr) != NULL
1362 && expr[1] == '=') || concat))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001364 /*
1365 * ":let" without "=": list variables
1366 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001367 if (*arg == '[')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001368 emsg(_(e_invarg));
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001369 else if (expr[0] == '.')
1370 emsg(_("E985: .= is not supported with script version 2"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001371 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001372 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001373 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001374 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001375 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001376 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001377 list_glob_vars(&first);
1378 list_buf_vars(&first);
1379 list_win_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001380 list_tab_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001381 list_script_vars(&first);
1382 list_func_vars(&first);
1383 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 eap->nextcmd = check_nextcmd(arg);
1386 }
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001387 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
1388 {
1389 list_T *l;
1390
1391 // HERE document
1392 l = heredoc_get(eap, expr + 3);
1393 if (l != NULL)
1394 {
1395 rettv_list_set(&rettv, l);
1396 op[0] = '=';
1397 op[1] = NUL;
1398 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
1399 op);
1400 clear_tv(&rettv);
1401 }
1402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 else
1404 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001405 op[0] = '=';
1406 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001407 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001408 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001409 if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001410 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001411 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001412 if (expr[0] == '.' && expr[1] == '.') // ..=
1413 ++expr;
1414 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001415 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001416 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001417 else
1418 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001419
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 if (eap->skip)
1421 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001422 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 if (eap->skip)
1424 {
1425 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001426 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 --emsg_skip;
1428 }
1429 else if (i != FAIL)
1430 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001431 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001432 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001433 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 }
1435 }
1436}
1437
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001438/*
1439 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1440 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001441 * When "nextchars" is not NULL it points to a string with characters that
1442 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1443 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001444 * Returns OK or FAIL;
1445 */
1446 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001447ex_let_vars(
1448 char_u *arg_start,
1449 typval_T *tv,
1450 int copy, /* copy values from "tv", don't move */
1451 int semicolon, /* from skip_var_list() */
1452 int var_count, /* from skip_var_list() */
1453 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001454{
1455 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001456 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001457 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001458 listitem_T *item;
1459 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001460
1461 if (*arg != '[')
1462 {
1463 /*
1464 * ":let var = expr" or ":for var in list"
1465 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001466 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001467 return FAIL;
1468 return OK;
1469 }
1470
1471 /*
1472 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1473 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001474 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001475 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001476 emsg(_(e_listreq));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001477 return FAIL;
1478 }
1479
1480 i = list_len(l);
1481 if (semicolon == 0 && var_count < i)
1482 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001483 emsg(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001484 return FAIL;
1485 }
1486 if (var_count - semicolon > i)
1487 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001488 emsg(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001489 return FAIL;
1490 }
1491
1492 item = l->lv_first;
1493 while (*arg != ']')
1494 {
1495 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001496 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001497 item = item->li_next;
1498 if (arg == NULL)
1499 return FAIL;
1500
1501 arg = skipwhite(arg);
1502 if (*arg == ';')
1503 {
1504 /* Put the rest of the list (may be empty) in the var after ';'.
1505 * Create a new list for this. */
1506 l = list_alloc();
1507 if (l == NULL)
1508 return FAIL;
1509 while (item != NULL)
1510 {
1511 list_append_tv(l, &item->li_tv);
1512 item = item->li_next;
1513 }
1514
1515 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001516 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001517 ltv.vval.v_list = l;
1518 l->lv_refcount = 1;
1519
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001520 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1521 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001522 clear_tv(&ltv);
1523 if (arg == NULL)
1524 return FAIL;
1525 break;
1526 }
1527 else if (*arg != ',' && *arg != ']')
1528 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001529 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001530 return FAIL;
1531 }
1532 }
1533
1534 return OK;
1535}
1536
1537/*
1538 * Skip over assignable variable "var" or list of variables "[var, var]".
1539 * Used for ":let varvar = expr" and ":for varvar in expr".
1540 * For "[var, var]" increment "*var_count" for each variable.
1541 * for "[var, var; var]" set "semicolon".
1542 * Return NULL for an error.
1543 */
1544 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001545skip_var_list(
1546 char_u *arg,
1547 int *var_count,
1548 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001549{
1550 char_u *p, *s;
1551
1552 if (*arg == '[')
1553 {
1554 /* "[var, var]": find the matching ']'. */
1555 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001556 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001557 {
1558 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1559 s = skip_var_one(p);
1560 if (s == p)
1561 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001562 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001563 return NULL;
1564 }
1565 ++*var_count;
1566
1567 p = skipwhite(s);
1568 if (*p == ']')
1569 break;
1570 else if (*p == ';')
1571 {
1572 if (*semicolon == 1)
1573 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001574 emsg(_("Double ; in list of variables"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001575 return NULL;
1576 }
1577 *semicolon = 1;
1578 }
1579 else if (*p != ',')
1580 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001581 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001582 return NULL;
1583 }
1584 }
1585 return p + 1;
1586 }
1587 else
1588 return skip_var_one(arg);
1589}
1590
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001591/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001592 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001593 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001594 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001595 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001596skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001597{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001598 if (*arg == '@' && arg[1] != NUL)
1599 return arg + 2;
1600 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1601 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001602}
1603
Bram Moolenaara7043832005-01-21 11:56:39 +00001604/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001605 * List variables for hashtab "ht" with prefix "prefix".
1606 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001607 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001608 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001609list_hashtable_vars(
1610 hashtab_T *ht,
Bram Moolenaar32526b32019-01-19 17:43:09 +01001611 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01001612 int empty,
1613 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001614{
Bram Moolenaar33570922005-01-25 22:26:29 +00001615 hashitem_T *hi;
1616 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001617 int todo;
Bram Moolenaarf86db782018-10-25 13:31:37 +02001618 char_u buf[IOSIZE];
Bram Moolenaara7043832005-01-21 11:56:39 +00001619
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001620 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001621 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1622 {
1623 if (!HASHITEM_EMPTY(hi))
1624 {
1625 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001626 di = HI2DI(hi);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001627
1628 // apply :filter /pat/ to variable name
Bram Moolenaar32526b32019-01-19 17:43:09 +01001629 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1630 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001631 if (message_filtered(buf))
1632 continue;
1633
Bram Moolenaar33570922005-01-25 22:26:29 +00001634 if (empty || di->di_tv.v_type != VAR_STRING
1635 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001636 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001637 }
1638 }
1639}
1640
1641/*
1642 * List global variables.
1643 */
1644 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001645list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001646{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001647 list_hashtable_vars(&globvarht, "", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001648}
1649
1650/*
1651 * List buffer variables.
1652 */
1653 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001654list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001655{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001656 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001657}
1658
1659/*
1660 * List window variables.
1661 */
1662 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001663list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001664{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001665 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001666}
1667
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001668/*
1669 * List tab page variables.
1670 */
1671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001672list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001673{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001674 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001675}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001676
Bram Moolenaara7043832005-01-21 11:56:39 +00001677/*
1678 * List Vim variables.
1679 */
1680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001681list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001682{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001683 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001684}
1685
1686/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001687 * List script-local variables, if there is a script.
1688 */
1689 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001690list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001691{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001692 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
1693 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
Bram Moolenaar32526b32019-01-19 17:43:09 +01001694 "s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001695}
1696
1697/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001698 * List variables in "arg".
1699 */
1700 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001701list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001702{
1703 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001704 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001705 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001706 char_u *name_start;
1707 char_u *arg_subsc;
1708 char_u *tofree;
1709 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001710
1711 while (!ends_excmd(*arg) && !got_int)
1712 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001713 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001714 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001715 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001716 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001717 {
1718 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001719 emsg(_(e_trailing));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001720 break;
1721 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001722 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001723 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001724 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001725 /* get_name_len() takes care of expanding curly braces */
1726 name_start = name = arg;
1727 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1728 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001729 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001730 /* This is mainly to keep test 49 working: when expanding
1731 * curly braces fails overrule the exception error message. */
1732 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001733 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001734 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001735 semsg(_(e_invarg2), arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001736 break;
1737 }
1738 error = TRUE;
1739 }
1740 else
1741 {
1742 if (tofree != NULL)
1743 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001744 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001745 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001746 else
1747 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001748 /* handle d.key, l[idx], f(expr) */
1749 arg_subsc = arg;
1750 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001751 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001752 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001753 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001754 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001755 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001756 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001757 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001758 case 'g': list_glob_vars(first); break;
1759 case 'b': list_buf_vars(first); break;
1760 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001761 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001762 case 'v': list_vim_vars(first); break;
1763 case 's': list_script_vars(first); break;
1764 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001765 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001766 semsg(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001767 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001768 }
1769 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001770 {
1771 char_u numbuf[NUMBUFLEN];
1772 char_u *tf;
1773 int c;
1774 char_u *s;
1775
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001776 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001777 c = *arg;
1778 *arg = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001779 list_one_var_a("",
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001780 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001781 tv.v_type,
1782 s == NULL ? (char_u *)"" : s,
1783 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001784 *arg = c;
1785 vim_free(tf);
1786 }
1787 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001788 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001789 }
1790 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001791
1792 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001793 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001794
1795 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001796 }
1797
1798 return arg;
1799}
1800
1801/*
1802 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1803 * Returns a pointer to the char just after the var name.
1804 * Returns NULL if there is an error.
1805 */
1806 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001807ex_let_one(
1808 char_u *arg, /* points to variable name */
1809 typval_T *tv, /* value to assign to variable */
1810 int copy, /* copy value from "tv" */
1811 char_u *endchars, /* valid chars after variable name or NULL */
1812 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001813{
1814 int c1;
1815 char_u *name;
1816 char_u *p;
1817 char_u *arg_end = NULL;
1818 int len;
1819 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001820 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001821
1822 /*
1823 * ":let $VAR = expr": Set environment variable.
1824 */
1825 if (*arg == '$')
1826 {
1827 /* Find the end of the name. */
1828 ++arg;
1829 name = arg;
1830 len = get_env_len(&arg);
1831 if (len == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001832 semsg(_(e_invarg2), name - 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001833 else
1834 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001835 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001836 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001837 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001838 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001839 emsg(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001840 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001841 {
1842 c1 = name[len];
1843 name[len] = NUL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001844 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001845 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001846 {
1847 int mustfree = FALSE;
1848 char_u *s = vim_getenv(name, &mustfree);
1849
1850 if (s != NULL)
1851 {
1852 p = tofree = concat_str(s, p);
1853 if (mustfree)
1854 vim_free(s);
1855 }
1856 }
1857 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001858 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001859 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001860 if (STRICMP(name, "HOME") == 0)
1861 init_homedir();
1862 else if (didset_vim && STRICMP(name, "VIM") == 0)
1863 didset_vim = FALSE;
1864 else if (didset_vimruntime
1865 && STRICMP(name, "VIMRUNTIME") == 0)
1866 didset_vimruntime = FALSE;
1867 arg_end = arg;
1868 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001869 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001870 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871 }
1872 }
1873 }
1874
1875 /*
1876 * ":let &option = expr": Set option value.
1877 * ":let &l:option = expr": Set local option value.
1878 * ":let &g:option = expr": Set global option value.
1879 */
1880 else if (*arg == '&')
1881 {
1882 /* Find the end of the name. */
1883 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001884 if (p == NULL || (endchars != NULL
1885 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001886 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001887 else
1888 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001889 long n;
1890 int opt_type;
1891 long numval;
1892 char_u *stringval = NULL;
1893 char_u *s;
1894
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001895 c1 = *p;
1896 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001897
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001898 n = (long)tv_get_number(tv);
1899 s = tv_get_string_chk(tv); /* != NULL if number or string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001900 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001901 {
1902 opt_type = get_option_value(arg, &numval,
1903 &stringval, opt_flags);
1904 if ((opt_type == 1 && *op == '.')
1905 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001906 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001907 semsg(_(e_letwrong), op);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001908 s = NULL; // don't set the value
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001909 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001910 else
1911 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001912 if (opt_type == 1) // number
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001913 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001914 switch (*op)
1915 {
1916 case '+': n = numval + n; break;
1917 case '-': n = numval - n; break;
1918 case '*': n = numval * n; break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001919 case '/': n = (long)num_divide(numval, n); break;
1920 case '%': n = (long)num_modulus(numval, n); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001921 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001922 }
Bram Moolenaarff697e62019-02-12 22:28:33 +01001923 else if (opt_type == 0 && stringval != NULL) // string
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001924 {
1925 s = concat_str(stringval, s);
1926 vim_free(stringval);
1927 stringval = s;
1928 }
1929 }
1930 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001931 if (s != NULL)
1932 {
1933 set_option_value(arg, n, s, opt_flags);
1934 arg_end = p;
1935 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001936 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001937 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001938 }
1939 }
1940
1941 /*
1942 * ":let @r = expr": Set register contents.
1943 */
1944 else if (*arg == '@')
1945 {
1946 ++arg;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001947 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001948 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001949 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001950 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001951 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001952 else
1953 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001954 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001955 char_u *s;
1956
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001957 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001958 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001959 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02001960 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001961 if (s != NULL)
1962 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001963 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001964 vim_free(s);
1965 }
1966 }
1967 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001968 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001969 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001970 arg_end = arg + 1;
1971 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001972 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001973 }
1974 }
1975
1976 /*
1977 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001978 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001979 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001980 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001981 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001982 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001983
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001984 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001985 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001986 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001987 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001988 emsg(_(e_letunexp));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001989 else
1990 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001991 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001992 arg_end = p;
1993 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001994 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001995 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001996 }
1997
1998 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001999 semsg(_(e_invarg2), arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002000
2001 return arg_end;
2002}
2003
2004/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002005 * Get an lval: variable, Dict item or List item that can be assigned a value
2006 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2007 * "name.key", "name.key[expr]" etc.
2008 * Indexing only works if "name" is an existing List or Dictionary.
2009 * "name" points to the start of the name.
2010 * If "rettv" is not NULL it points to the value to be assigned.
2011 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2012 * wrong; must end in space or cmd separator.
2013 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002014 * flags:
2015 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01002016 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002017 * GLV_NO_AUTOLOAD: do not use script autoloading
2018 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002019 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002020 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002021 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002022 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002023 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002024get_lval(
2025 char_u *name,
2026 typval_T *rettv,
2027 lval_T *lp,
2028 int unlet,
2029 int skip,
2030 int flags, /* GLV_ values */
2031 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002032{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002033 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002034 char_u *expr_start, *expr_end;
2035 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002036 dictitem_T *v;
2037 typval_T var1;
2038 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002039 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002040 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002041 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002042 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002043 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002044 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002045
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002046 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002047 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002048
2049 if (skip)
2050 {
2051 /* When skipping just find the end of the name. */
2052 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002053 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002054 }
2055
2056 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002057 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002058 if (expr_start != NULL)
2059 {
2060 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002061 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002062 && *p != '[' && *p != '.')
2063 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002064 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002065 return NULL;
2066 }
2067
2068 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2069 if (lp->ll_exp_name == NULL)
2070 {
2071 /* Report an invalid expression in braces, unless the
2072 * expression evaluation has been cancelled due to an
2073 * aborting error, an interrupt, or an exception. */
2074 if (!aborting() && !quiet)
2075 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002076 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002077 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002078 return NULL;
2079 }
2080 }
2081 lp->ll_name = lp->ll_exp_name;
2082 }
2083 else
2084 lp->ll_name = name;
2085
2086 /* Without [idx] or .key we are done. */
2087 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2088 return p;
2089
2090 cc = *p;
2091 *p = NUL;
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002092 /* Only pass &ht when we would write to the variable, it prevents autoload
2093 * as well. */
2094 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
2095 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002096 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002097 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002098 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002099 if (v == NULL)
2100 return NULL;
2101
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002102 /*
2103 * Loop until no more [idx] or .key is following.
2104 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002105 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002106 var1.v_type = VAR_UNKNOWN;
2107 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002108 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002109 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002110 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2111 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002112 && lp->ll_tv->vval.v_dict != NULL)
2113 && !(lp->ll_tv->v_type == VAR_BLOB
2114 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002115 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002116 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002117 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002118 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002119 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002120 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002121 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002122 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002123 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002124 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002125 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002126
Bram Moolenaar8c711452005-01-14 21:53:12 +00002127 len = -1;
2128 if (*p == '.')
2129 {
2130 key = p + 1;
2131 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2132 ;
2133 if (len == 0)
2134 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002135 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002136 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002137 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002138 }
2139 p = key + len;
2140 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002141 else
2142 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002143 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002144 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002145 if (*p == ':')
2146 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002147 else
2148 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002149 empty1 = FALSE;
2150 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002151 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002152 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002153 {
2154 /* not a number or string */
2155 clear_tv(&var1);
2156 return NULL;
2157 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002158 }
2159
2160 /* Optionally get the second index [ :expr]. */
2161 if (*p == ':')
2162 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002163 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002164 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002165 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002166 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002167 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002168 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002169 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002170 if (rettv != NULL
2171 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002172 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002173 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002174 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002175 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002176 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002177 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002178 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002179 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002180 }
2181 p = skipwhite(p + 1);
2182 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002183 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002184 else
2185 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002186 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002187 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2188 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002189 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002190 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002191 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002192 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002193 {
2194 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002195 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002196 clear_tv(&var2);
2197 return NULL;
2198 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002199 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002200 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002201 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002202 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002203 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002204
Bram Moolenaar8c711452005-01-14 21:53:12 +00002205 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002206 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002207 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002208 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002209 clear_tv(&var1);
2210 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002211 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002212 }
2213
2214 /* Skip to past ']'. */
2215 ++p;
2216 }
2217
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002218 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002219 {
2220 if (len == -1)
2221 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002222 /* "[key]": get key from "var1" */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002223 key = tv_get_string_chk(&var1); /* is number or string */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002224 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002225 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002226 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002227 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002228 }
2229 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002230 lp->ll_list = NULL;
2231 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002232 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002233
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002234 /* When assigning to a scope dictionary check that a function and
2235 * variable name is valid (only variable name unless it is l: or
2236 * g: dictionary). Disallow overwriting a builtin function. */
2237 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002238 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002239 int prevval;
2240 int wrong;
2241
2242 if (len != -1)
2243 {
2244 prevval = key[len];
2245 key[len] = NUL;
2246 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002247 else
2248 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002249 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2250 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002251 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002252 || !valid_varname(key);
2253 if (len != -1)
2254 key[len] = prevval;
2255 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002256 return NULL;
2257 }
2258
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002259 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002260 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01002261 // Can't add "v:" or "a:" variable.
2262 if (lp->ll_dict == &vimvardict
2263 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002264 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002265 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01002266 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002267 return NULL;
2268 }
2269
Bram Moolenaar31b81602019-02-10 22:14:27 +01002270 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002271 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002272 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002273 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002274 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002275 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002276 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002277 }
2278 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002279 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002280 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002281 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002282 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002283 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002284 p = NULL;
2285 break;
2286 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002287 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002288 else if ((flags & GLV_READ_ONLY) == 0
2289 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002290 {
2291 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002292 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002293 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002294
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002295 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002296 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002297 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002298 else if (lp->ll_tv->v_type == VAR_BLOB)
2299 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002300 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
2301
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002302 /*
2303 * Get the number and item for the only or first index of the List.
2304 */
2305 if (empty1)
2306 lp->ll_n1 = 0;
2307 else
2308 // is number or string
2309 lp->ll_n1 = (long)tv_get_number(&var1);
2310 clear_tv(&var1);
2311
2312 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002313 || lp->ll_n1 > bloblen
2314 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002315 {
2316 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002317 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002318 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002319 return NULL;
2320 }
2321 if (lp->ll_range && !lp->ll_empty2)
2322 {
2323 lp->ll_n2 = (long)tv_get_number(&var2);
2324 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002325 if (lp->ll_n2 < 0
2326 || lp->ll_n2 >= bloblen
2327 || lp->ll_n2 < lp->ll_n1)
2328 {
2329 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002330 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002331 return NULL;
2332 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002333 }
2334 lp->ll_blob = lp->ll_tv->vval.v_blob;
2335 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01002336 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002337 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002338 else
2339 {
2340 /*
2341 * Get the number and item for the only or first index of the List.
2342 */
2343 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002344 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002345 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002346 /* is number or string */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002347 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002348 clear_tv(&var1);
2349
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002350 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002351 lp->ll_list = lp->ll_tv->vval.v_list;
2352 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2353 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002354 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002355 if (lp->ll_n1 < 0)
2356 {
2357 lp->ll_n1 = 0;
2358 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2359 }
2360 }
2361 if (lp->ll_li == NULL)
2362 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002363 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002364 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002365 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002366 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002367 }
2368
2369 /*
2370 * May need to find the item or absolute index for the second
2371 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002372 * When no index given: "lp->ll_empty2" is TRUE.
2373 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002374 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002375 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002376 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002377 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002378 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002379 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002380 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002381 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002382 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002383 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002384 {
2385 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002386 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002387 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002388 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002389 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002390 }
2391
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002392 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2393 if (lp->ll_n1 < 0)
2394 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2395 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002396 {
2397 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002398 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002399 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002400 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002401 }
2402
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002403 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002404 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002405 }
2406
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002407 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002408 return p;
2409}
2410
2411/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002412 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002413 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002414 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002415clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002416{
2417 vim_free(lp->ll_exp_name);
2418 vim_free(lp->ll_newkey);
2419}
2420
2421/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002422 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002423 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01002424 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
2425 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002426 */
2427 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002428set_var_lval(
2429 lval_T *lp,
2430 char_u *endp,
2431 typval_T *rettv,
2432 int copy,
2433 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002434{
2435 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002436 listitem_T *ri;
2437 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002438
2439 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002440 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002441 cc = *endp;
2442 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002443 if (lp->ll_blob != NULL)
2444 {
2445 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002446
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002447 if (op != NULL && *op != '=')
2448 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002449 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002450 return;
2451 }
2452
2453 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2454 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002455 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002456
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002457 if (lp->ll_empty2)
2458 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
2459
2460 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002461 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002462 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002463 return;
2464 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002465 if (lp->ll_empty2)
2466 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002467
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002468 ir = 0;
2469 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
2470 blob_set(lp->ll_blob, il,
2471 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002472 }
2473 else
2474 {
2475 val = (int)tv_get_number_chk(rettv, &error);
2476 if (!error)
2477 {
2478 garray_T *gap = &lp->ll_blob->bv_ga;
2479
2480 // Allow for appending a byte. Setting a byte beyond
2481 // the end is an error otherwise.
2482 if (lp->ll_n1 < gap->ga_len
2483 || (lp->ll_n1 == gap->ga_len
2484 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
2485 {
2486 blob_set(lp->ll_blob, lp->ll_n1, val);
2487 if (lp->ll_n1 == gap->ga_len)
2488 ++gap->ga_len;
2489 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002490 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002491 }
2492 }
2493 }
2494 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002495 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002496 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002497
Bram Moolenaarff697e62019-02-12 22:28:33 +01002498 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01002499 di = NULL;
2500 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2501 &tv, &di, TRUE, FALSE) == OK)
2502 {
2503 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002504 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2505 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01002506 && tv_op(&tv, rettv, op) == OK)
2507 set_var(lp->ll_name, &tv, FALSE);
2508 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002509 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002510 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002511 else
2512 set_var(lp->ll_name, rettv, copy);
2513 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002515 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002516 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002517 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002518 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 else if (lp->ll_range)
2520 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002521 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002522 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002523
2524 /*
2525 * Check whether any of the list items is locked
2526 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002527 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002528 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002529 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002530 return;
2531 ri = ri->li_next;
2532 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2533 break;
2534 ll_li = ll_li->li_next;
2535 ++ll_n1;
2536 }
2537
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538 /*
2539 * Assign the List values to the list items.
2540 */
2541 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002542 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002543 if (op != NULL && *op != '=')
2544 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2545 else
2546 {
2547 clear_tv(&lp->ll_li->li_tv);
2548 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2549 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 ri = ri->li_next;
2551 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2552 break;
2553 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002554 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002556 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002557 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 ri = NULL;
2559 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002560 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002561 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562 lp->ll_li = lp->ll_li->li_next;
2563 ++lp->ll_n1;
2564 }
2565 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002566 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002567 else if (lp->ll_empty2
2568 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002570 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 }
2572 else
2573 {
2574 /*
2575 * Assign to a List or Dictionary item.
2576 */
2577 if (lp->ll_newkey != NULL)
2578 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002579 if (op != NULL && *op != '=')
2580 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002581 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002582 return;
2583 }
2584
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002586 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 if (di == NULL)
2588 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002589 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2590 {
2591 vim_free(di);
2592 return;
2593 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002595 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002596 else if (op != NULL && *op != '=')
2597 {
2598 tv_op(lp->ll_tv, rettv, op);
2599 return;
2600 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002601 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002603
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604 /*
2605 * Assign the value to the variable or list item.
2606 */
2607 if (copy)
2608 copy_tv(rettv, lp->ll_tv);
2609 else
2610 {
2611 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002612 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002613 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002614 }
2615 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002616}
2617
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002618/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01002619 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2620 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002621 * Returns OK or FAIL.
2622 */
2623 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002624tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002625{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002626 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002627 char_u numbuf[NUMBUFLEN];
2628 char_u *s;
2629
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002630 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2631 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2632 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002633 {
2634 switch (tv1->v_type)
2635 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002636 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002637 case VAR_DICT:
2638 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002639 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002640 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002641 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002642 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002643 break;
2644
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002645 case VAR_BLOB:
2646 if (*op != '+' || tv2->v_type != VAR_BLOB)
2647 break;
2648 // BLOB += BLOB
2649 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2650 {
2651 blob_T *b1 = tv1->vval.v_blob;
2652 blob_T *b2 = tv2->vval.v_blob;
2653 int i, len = blob_len(b2);
2654 for (i = 0; i < len; i++)
2655 ga_append(&b1->bv_ga, blob_get(b2, i));
2656 }
2657 return OK;
2658
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002659 case VAR_LIST:
2660 if (*op != '+' || tv2->v_type != VAR_LIST)
2661 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002662 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002663 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2664 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2665 return OK;
2666
2667 case VAR_NUMBER:
2668 case VAR_STRING:
2669 if (tv2->v_type == VAR_LIST)
2670 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002671 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002672 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002673 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002674 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002675#ifdef FEAT_FLOAT
2676 if (tv2->v_type == VAR_FLOAT)
2677 {
2678 float_T f = n;
2679
Bram Moolenaarff697e62019-02-12 22:28:33 +01002680 if (*op == '%')
2681 break;
2682 switch (*op)
2683 {
2684 case '+': f += tv2->vval.v_float; break;
2685 case '-': f -= tv2->vval.v_float; break;
2686 case '*': f *= tv2->vval.v_float; break;
2687 case '/': f /= tv2->vval.v_float; break;
2688 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002689 clear_tv(tv1);
2690 tv1->v_type = VAR_FLOAT;
2691 tv1->vval.v_float = f;
2692 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002693 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002694#endif
2695 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002696 switch (*op)
2697 {
2698 case '+': n += tv_get_number(tv2); break;
2699 case '-': n -= tv_get_number(tv2); break;
2700 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01002701 case '/': n = num_divide(n, tv_get_number(tv2)); break;
2702 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002703 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002704 clear_tv(tv1);
2705 tv1->v_type = VAR_NUMBER;
2706 tv1->vval.v_number = n;
2707 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002708 }
2709 else
2710 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002711 if (tv2->v_type == VAR_FLOAT)
2712 break;
2713
Bram Moolenaarff697e62019-02-12 22:28:33 +01002714 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002715 s = tv_get_string(tv1);
2716 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002717 clear_tv(tv1);
2718 tv1->v_type = VAR_STRING;
2719 tv1->vval.v_string = s;
2720 }
2721 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002722
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002723 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002724#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002725 {
2726 float_T f;
2727
Bram Moolenaarff697e62019-02-12 22:28:33 +01002728 if (*op == '%' || *op == '.'
2729 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002730 && tv2->v_type != VAR_NUMBER
2731 && tv2->v_type != VAR_STRING))
2732 break;
2733 if (tv2->v_type == VAR_FLOAT)
2734 f = tv2->vval.v_float;
2735 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002736 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01002737 switch (*op)
2738 {
2739 case '+': tv1->vval.v_float += f; break;
2740 case '-': tv1->vval.v_float -= f; break;
2741 case '*': tv1->vval.v_float *= f; break;
2742 case '/': tv1->vval.v_float /= f; break;
2743 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002744 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002745#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002746 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002747 }
2748 }
2749
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002750 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002751 return FAIL;
2752}
2753
2754/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002755 * Evaluate the expression used in a ":for var in expr" command.
2756 * "arg" points to "var".
2757 * Set "*errp" to TRUE for an error, FALSE otherwise;
2758 * Return a pointer that holds the info. Null when there is an error.
2759 */
2760 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002761eval_for_line(
2762 char_u *arg,
2763 int *errp,
2764 char_u **nextcmdp,
2765 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002766{
Bram Moolenaar33570922005-01-25 22:26:29 +00002767 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002768 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002769 typval_T tv;
2770 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002771
2772 *errp = TRUE; /* default: there is an error */
2773
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002774 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002775 if (fi == NULL)
2776 return NULL;
2777
2778 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2779 if (expr == NULL)
2780 return fi;
2781
2782 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002783 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002784 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002785 emsg(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002786 return fi;
2787 }
2788
2789 if (skip)
2790 ++emsg_skip;
2791 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2792 {
2793 *errp = FALSE;
2794 if (!skip)
2795 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002796 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002797 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002798 l = tv.vval.v_list;
2799 if (l == NULL)
2800 {
2801 // a null list is like an empty list: do nothing
2802 clear_tv(&tv);
2803 }
2804 else
2805 {
2806 // No need to increment the refcount, it's already set for
2807 // the list being used in "tv".
2808 fi->fi_list = l;
2809 list_add_watch(l, &fi->fi_lw);
2810 fi->fi_lw.lw_item = l->lv_first;
2811 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002812 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002813 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002814 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002815 fi->fi_bi = 0;
2816 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002817 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002818 typval_T btv;
2819
2820 // Make a copy, so that the iteration still works when the
2821 // blob is changed.
2822 blob_copy(&tv, &btv);
2823 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002824 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002825 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002826 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002827 else
2828 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002829 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002830 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002831 }
2832 }
2833 }
2834 if (skip)
2835 --emsg_skip;
2836
2837 return fi;
2838}
2839
2840/*
2841 * Use the first item in a ":for" list. Advance to the next.
2842 * Assign the values to the variable (list). "arg" points to the first one.
2843 * Return TRUE when a valid item was found, FALSE when at end of list or
2844 * something wrong.
2845 */
2846 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002847next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002848{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002849 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002850 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002851 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002852
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002853 if (fi->fi_blob != NULL)
2854 {
2855 typval_T tv;
2856
2857 if (fi->fi_bi >= blob_len(fi->fi_blob))
2858 return FALSE;
2859 tv.v_type = VAR_NUMBER;
2860 tv.v_lock = VAR_FIXED;
2861 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2862 ++fi->fi_bi;
2863 return ex_let_vars(arg, &tv, TRUE,
2864 fi->fi_semicolon, fi->fi_varcount, NULL) == OK;
2865 }
2866
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002867 item = fi->fi_lw.lw_item;
2868 if (item == NULL)
2869 result = FALSE;
2870 else
2871 {
2872 fi->fi_lw.lw_item = item->li_next;
2873 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2874 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2875 }
2876 return result;
2877}
2878
2879/*
2880 * Free the structure used to store info used by ":for".
2881 */
2882 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002883free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002884{
Bram Moolenaar33570922005-01-25 22:26:29 +00002885 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002886
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002887 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002888 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002889 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002890 list_unref(fi->fi_list);
2891 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002892 if (fi != NULL && fi->fi_blob != NULL)
2893 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002894 vim_free(fi);
2895}
2896
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2898
2899 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002900set_context_for_expression(
2901 expand_T *xp,
2902 char_u *arg,
2903 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904{
2905 int got_eq = FALSE;
2906 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002907 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002909 if (cmdidx == CMD_let)
2910 {
2911 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002912 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002913 {
2914 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002915 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002916 {
2917 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002918 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002919 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002920 break;
2921 }
2922 return;
2923 }
2924 }
2925 else
2926 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2927 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 while ((xp->xp_pattern = vim_strpbrk(arg,
2929 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2930 {
2931 c = *xp->xp_pattern;
2932 if (c == '&')
2933 {
2934 c = xp->xp_pattern[1];
2935 if (c == '&')
2936 {
2937 ++xp->xp_pattern;
2938 xp->xp_context = cmdidx != CMD_let || got_eq
2939 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2940 }
2941 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002942 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002944 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2945 xp->xp_pattern += 2;
2946
2947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 }
2949 else if (c == '$')
2950 {
2951 /* environment variable */
2952 xp->xp_context = EXPAND_ENV_VARS;
2953 }
2954 else if (c == '=')
2955 {
2956 got_eq = TRUE;
2957 xp->xp_context = EXPAND_EXPRESSION;
2958 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002959 else if (c == '#'
2960 && xp->xp_context == EXPAND_EXPRESSION)
2961 {
2962 /* Autoload function/variable contains '#'. */
2963 break;
2964 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002965 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 && xp->xp_context == EXPAND_FUNCTIONS
2967 && vim_strchr(xp->xp_pattern, '(') == NULL)
2968 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002969 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 break;
2971 }
2972 else if (cmdidx != CMD_let || got_eq)
2973 {
2974 if (c == '"') /* string */
2975 {
2976 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2977 if (c == '\\' && xp->xp_pattern[1] != NUL)
2978 ++xp->xp_pattern;
2979 xp->xp_context = EXPAND_NOTHING;
2980 }
2981 else if (c == '\'') /* literal string */
2982 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002983 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2985 /* skip */ ;
2986 xp->xp_context = EXPAND_NOTHING;
2987 }
2988 else if (c == '|')
2989 {
2990 if (xp->xp_pattern[1] == '|')
2991 {
2992 ++xp->xp_pattern;
2993 xp->xp_context = EXPAND_EXPRESSION;
2994 }
2995 else
2996 xp->xp_context = EXPAND_COMMANDS;
2997 }
2998 else
2999 xp->xp_context = EXPAND_EXPRESSION;
3000 }
3001 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003002 /* Doesn't look like something valid, expand as an expression
3003 * anyway. */
3004 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 arg = xp->xp_pattern;
3006 if (*arg != NUL)
3007 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3008 /* skip */ ;
3009 }
3010 xp->xp_pattern = arg;
3011}
3012
3013#endif /* FEAT_CMDL_COMPL */
3014
3015/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 * ":unlet[!] var1 ... " command.
3017 */
3018 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003019ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003021 ex_unletlock(eap, eap->arg, 0);
3022}
3023
3024/*
3025 * ":lockvar" and ":unlockvar" commands
3026 */
3027 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003028ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003029{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003031 int deep = 2;
3032
3033 if (eap->forceit)
3034 deep = -1;
3035 else if (vim_isdigit(*arg))
3036 {
3037 deep = getdigits(&arg);
3038 arg = skipwhite(arg);
3039 }
3040
3041 ex_unletlock(eap, arg, deep);
3042}
3043
3044/*
3045 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3046 */
3047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003048ex_unletlock(
3049 exarg_T *eap,
3050 char_u *argstart,
3051 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003052{
3053 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003056 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057
3058 do
3059 {
Bram Moolenaar137374f2018-05-13 15:59:50 +02003060 if (*arg == '$')
3061 {
3062 char_u *name = ++arg;
3063
3064 if (get_env_len(&arg) == 0)
3065 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003066 semsg(_(e_invarg2), name - 1);
Bram Moolenaar137374f2018-05-13 15:59:50 +02003067 return;
3068 }
3069 vim_unsetenv(name);
3070 arg = skipwhite(arg);
3071 continue;
3072 }
3073
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003074 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003075 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003076 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003077 if (lv.ll_name == NULL)
3078 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003079 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003080 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003082 if (name_end != NULL)
3083 {
3084 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003085 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003086 }
3087 if (!(eap->skip || error))
3088 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 break;
3090 }
3091
3092 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003093 {
3094 if (eap->cmdidx == CMD_unlet)
3095 {
3096 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3097 error = TRUE;
3098 }
3099 else
3100 {
3101 if (do_lock_var(&lv, name_end, deep,
3102 eap->cmdidx == CMD_lockvar) == FAIL)
3103 error = TRUE;
3104 }
3105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003107 if (!eap->skip)
3108 clear_lval(&lv);
3109
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 arg = skipwhite(name_end);
3111 } while (!ends_excmd(*arg));
3112
3113 eap->nextcmd = check_nextcmd(arg);
3114}
3115
Bram Moolenaar8c711452005-01-14 21:53:12 +00003116 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003117do_unlet_var(
3118 lval_T *lp,
3119 char_u *name_end,
3120 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003121{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003122 int ret = OK;
3123 int cc;
3124
3125 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003126 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003127 cc = *name_end;
3128 *name_end = NUL;
3129
3130 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003131 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003132 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003133 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003134 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003135 else if ((lp->ll_list != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003136 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003137 || (lp->ll_dict != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003138 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003139 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003140 else if (lp->ll_range)
3141 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003142 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003143 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003144 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003145
3146 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3147 {
3148 li = ll_li->li_next;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003149 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003150 return FAIL;
3151 ll_li = li;
3152 ++ll_n1;
3153 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003154
3155 /* Delete a range of List items. */
3156 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3157 {
3158 li = lp->ll_li->li_next;
3159 listitem_remove(lp->ll_list, lp->ll_li);
3160 lp->ll_li = li;
3161 ++lp->ll_n1;
3162 }
3163 }
3164 else
3165 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003166 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003167 /* unlet a List item. */
3168 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003169 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003170 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003171 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003172 }
3173
3174 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003175}
3176
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177/*
3178 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003179 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 */
3181 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003182do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183{
Bram Moolenaar33570922005-01-25 22:26:29 +00003184 hashtab_T *ht;
3185 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003186 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003187 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003188 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189
Bram Moolenaar33570922005-01-25 22:26:29 +00003190 ht = find_var_ht(name, &varname);
3191 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003193 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003194 if (d == NULL)
3195 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003196 if (ht == &globvarht)
3197 d = &globvardict;
3198 else if (ht == &compat_hashtab)
3199 d = &vimvardict;
3200 else
3201 {
3202 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3203 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3204 }
3205 if (d == NULL)
3206 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003207 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003208 return FAIL;
3209 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003210 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003211 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003212 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003213 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003214 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003215 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003216 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003217 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003218 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003219 || var_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003220 return FAIL;
3221
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003222 delete_var(ht, hi);
3223 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003226 if (forceit)
3227 return OK;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003228 semsg(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 return FAIL;
3230}
3231
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003232/*
3233 * Lock or unlock variable indicated by "lp".
3234 * "deep" is the levels to go (-1 for unlimited);
3235 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3236 */
3237 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003238do_lock_var(
3239 lval_T *lp,
3240 char_u *name_end,
3241 int deep,
3242 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003243{
3244 int ret = OK;
3245 int cc;
3246 dictitem_T *di;
3247
3248 if (deep == 0) /* nothing to do */
3249 return OK;
3250
3251 if (lp->ll_tv == NULL)
3252 {
3253 cc = *name_end;
3254 *name_end = NUL;
3255
3256 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003257 di = find_var(lp->ll_name, NULL, TRUE);
3258 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003259 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01003260 else if ((di->di_flags & DI_FLAGS_FIX)
3261 && di->di_tv.v_type != VAR_DICT
3262 && di->di_tv.v_type != VAR_LIST)
3263 /* For historic reasons this error is not given for a list or dict.
3264 * E.g., the b: dict could be locked/unlocked. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003265 semsg(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003266 else
3267 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01003268 if (lock)
3269 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003270 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01003271 di->di_flags &= ~DI_FLAGS_LOCK;
3272 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003273 }
3274 *name_end = cc;
3275 }
3276 else if (lp->ll_range)
3277 {
3278 listitem_T *li = lp->ll_li;
3279
3280 /* (un)lock a range of List items. */
3281 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3282 {
3283 item_lock(&li->li_tv, deep, lock);
3284 li = li->li_next;
3285 ++lp->ll_n1;
3286 }
3287 }
3288 else if (lp->ll_list != NULL)
3289 /* (un)lock a List item. */
3290 item_lock(&lp->ll_li->li_tv, deep, lock);
3291 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003292 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003293 item_lock(&lp->ll_di->di_tv, deep, lock);
3294
3295 return ret;
3296}
3297
3298/*
3299 * Lock or unlock an item. "deep" is nr of levels to go.
3300 */
3301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003302item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003303{
3304 static int recurse = 0;
3305 list_T *l;
3306 listitem_T *li;
3307 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003308 blob_T *b;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003309 hashitem_T *hi;
3310 int todo;
3311
3312 if (recurse >= DICT_MAXNEST)
3313 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003314 emsg(_("E743: variable nested too deep for (un)lock"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003315 return;
3316 }
3317 if (deep == 0)
3318 return;
3319 ++recurse;
3320
3321 /* lock/unlock the item itself */
3322 if (lock)
3323 tv->v_lock |= VAR_LOCKED;
3324 else
3325 tv->v_lock &= ~VAR_LOCKED;
3326
3327 switch (tv->v_type)
3328 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003329 case VAR_UNKNOWN:
3330 case VAR_NUMBER:
3331 case VAR_STRING:
3332 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003333 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003334 case VAR_FLOAT:
3335 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003336 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003337 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003338 break;
3339
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003340 case VAR_BLOB:
3341 if ((b = tv->vval.v_blob) != NULL)
3342 {
3343 if (lock)
3344 b->bv_lock |= VAR_LOCKED;
3345 else
3346 b->bv_lock &= ~VAR_LOCKED;
3347 }
3348 break;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003349 case VAR_LIST:
3350 if ((l = tv->vval.v_list) != NULL)
3351 {
3352 if (lock)
3353 l->lv_lock |= VAR_LOCKED;
3354 else
3355 l->lv_lock &= ~VAR_LOCKED;
3356 if (deep < 0 || deep > 1)
3357 /* recursive: lock/unlock the items the List contains */
3358 for (li = l->lv_first; li != NULL; li = li->li_next)
3359 item_lock(&li->li_tv, deep - 1, lock);
3360 }
3361 break;
3362 case VAR_DICT:
3363 if ((d = tv->vval.v_dict) != NULL)
3364 {
3365 if (lock)
3366 d->dv_lock |= VAR_LOCKED;
3367 else
3368 d->dv_lock &= ~VAR_LOCKED;
3369 if (deep < 0 || deep > 1)
3370 {
3371 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003372 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003373 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3374 {
3375 if (!HASHITEM_EMPTY(hi))
3376 {
3377 --todo;
3378 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3379 }
3380 }
3381 }
3382 }
3383 }
3384 --recurse;
3385}
3386
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3388/*
3389 * Delete all "menutrans_" variables.
3390 */
3391 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003392del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393{
Bram Moolenaar33570922005-01-25 22:26:29 +00003394 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003395 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396
Bram Moolenaar33570922005-01-25 22:26:29 +00003397 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003398 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003399 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003400 {
3401 if (!HASHITEM_EMPTY(hi))
3402 {
3403 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003404 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3405 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003406 }
3407 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003408 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409}
3410#endif
3411
3412#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3413
3414/*
3415 * Local string buffer for the next two functions to store a variable name
3416 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3417 * get_user_var_name().
3418 */
3419
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420static char_u *varnamebuf = NULL;
3421static int varnamebuflen = 0;
3422
3423/*
3424 * Function to concatenate a prefix and a variable name.
3425 */
3426 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003427cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428{
3429 int len;
3430
3431 len = (int)STRLEN(name) + 3;
3432 if (len > varnamebuflen)
3433 {
3434 vim_free(varnamebuf);
3435 len += 10; /* some additional space */
3436 varnamebuf = alloc(len);
3437 if (varnamebuf == NULL)
3438 {
3439 varnamebuflen = 0;
3440 return NULL;
3441 }
3442 varnamebuflen = len;
3443 }
3444 *varnamebuf = prefix;
3445 varnamebuf[1] = ':';
3446 STRCPY(varnamebuf + 2, name);
3447 return varnamebuf;
3448}
3449
3450/*
3451 * Function given to ExpandGeneric() to obtain the list of user defined
3452 * (global/buffer/window/built-in) variable names.
3453 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003455get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003457 static long_u gdone;
3458 static long_u bdone;
3459 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003460 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003461 static int vidx;
3462 static hashitem_T *hi;
3463 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464
3465 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003466 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003467 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003468 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003469 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003470
3471 /* Global variables */
3472 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003474 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003475 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003476 else
3477 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003478 while (HASHITEM_EMPTY(hi))
3479 ++hi;
3480 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3481 return cat_prefix_varname('g', hi->hi_key);
3482 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003484
3485 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003486 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003487 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003489 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003490 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003491 else
3492 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003493 while (HASHITEM_EMPTY(hi))
3494 ++hi;
3495 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003497
3498 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003499 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003500 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003502 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003503 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003504 else
3505 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003506 while (HASHITEM_EMPTY(hi))
3507 ++hi;
3508 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003510
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003511 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003512 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003513 if (tdone < ht->ht_used)
3514 {
3515 if (tdone++ == 0)
3516 hi = ht->ht_array;
3517 else
3518 ++hi;
3519 while (HASHITEM_EMPTY(hi))
3520 ++hi;
3521 return cat_prefix_varname('t', hi->hi_key);
3522 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003523
Bram Moolenaar33570922005-01-25 22:26:29 +00003524 /* v: variables */
3525 if (vidx < VV_LEN)
3526 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527
Bram Moolenaard23a8232018-02-10 18:45:26 +01003528 VIM_CLEAR(varnamebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 varnamebuflen = 0;
3530 return NULL;
3531}
3532
3533#endif /* FEAT_CMDL_COMPL */
3534
3535/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003536 * Return TRUE if "pat" matches "text".
3537 * Does not use 'cpo' and always uses 'magic'.
3538 */
3539 static int
3540pattern_match(char_u *pat, char_u *text, int ic)
3541{
3542 int matches = FALSE;
3543 char_u *save_cpo;
3544 regmatch_T regmatch;
3545
3546 /* avoid 'l' flag in 'cpoptions' */
3547 save_cpo = p_cpo;
3548 p_cpo = (char_u *)"";
3549 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3550 if (regmatch.regprog != NULL)
3551 {
3552 regmatch.rm_ic = ic;
3553 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3554 vim_regfree(regmatch.regprog);
3555 }
3556 p_cpo = save_cpo;
3557 return matches;
3558}
3559
3560/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003562 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3564 */
3565
3566/*
3567 * Handle zero level expression.
3568 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003569 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003570 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 * Return OK or FAIL.
3572 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003573 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003574eval0(
3575 char_u *arg,
3576 typval_T *rettv,
3577 char_u **nextcmd,
3578 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579{
3580 int ret;
3581 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003582 int did_emsg_before = did_emsg;
3583 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584
3585 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003586 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 if (ret == FAIL || !ends_excmd(*p))
3588 {
3589 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003590 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 /*
3592 * Report the invalid expression unless the expression evaluation has
3593 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003594 * exception, or we already gave a more specific error.
3595 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003597 if (!aborting() && did_emsg == did_emsg_before
3598 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003599 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 ret = FAIL;
3601 }
3602 if (nextcmd != NULL)
3603 *nextcmd = check_nextcmd(p);
3604
3605 return ret;
3606}
3607
3608/*
3609 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003610 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 *
3612 * "arg" must point to the first non-white of the expression.
3613 * "arg" is advanced to the next non-white after the recognized expression.
3614 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003615 * Note: "rettv.v_lock" is not set.
3616 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 * Return OK or FAIL.
3618 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003619 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003620eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621{
3622 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003623 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624
3625 /*
3626 * Get the first variable.
3627 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003628 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 return FAIL;
3630
3631 if ((*arg)[0] == '?')
3632 {
3633 result = FALSE;
3634 if (evaluate)
3635 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003636 int error = FALSE;
3637
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003638 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003640 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003641 if (error)
3642 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 }
3644
3645 /*
3646 * Get the second variable.
3647 */
3648 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003649 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 return FAIL;
3651
3652 /*
3653 * Check for the ":".
3654 */
3655 if ((*arg)[0] != ':')
3656 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003657 emsg(_("E109: Missing ':' after '?'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003659 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 return FAIL;
3661 }
3662
3663 /*
3664 * Get the third variable.
3665 */
3666 *arg = skipwhite(*arg + 1);
3667 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3668 {
3669 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003670 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 return FAIL;
3672 }
3673 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003674 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 }
3676
3677 return OK;
3678}
3679
3680/*
3681 * Handle first level expression:
3682 * expr2 || expr2 || expr2 logical OR
3683 *
3684 * "arg" must point to the first non-white of the expression.
3685 * "arg" is advanced to the next non-white after the recognized expression.
3686 *
3687 * Return OK or FAIL.
3688 */
3689 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003690eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691{
Bram Moolenaar33570922005-01-25 22:26:29 +00003692 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 long result;
3694 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003695 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696
3697 /*
3698 * Get the first variable.
3699 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003700 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 return FAIL;
3702
3703 /*
3704 * Repeat until there is no following "||".
3705 */
3706 first = TRUE;
3707 result = FALSE;
3708 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3709 {
3710 if (evaluate && first)
3711 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003712 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003714 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003715 if (error)
3716 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 first = FALSE;
3718 }
3719
3720 /*
3721 * Get the second variable.
3722 */
3723 *arg = skipwhite(*arg + 2);
3724 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3725 return FAIL;
3726
3727 /*
3728 * Compute the result.
3729 */
3730 if (evaluate && !result)
3731 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003732 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003734 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003735 if (error)
3736 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 }
3738 if (evaluate)
3739 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003740 rettv->v_type = VAR_NUMBER;
3741 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 }
3743 }
3744
3745 return OK;
3746}
3747
3748/*
3749 * Handle second level expression:
3750 * expr3 && expr3 && expr3 logical AND
3751 *
3752 * "arg" must point to the first non-white of the expression.
3753 * "arg" is advanced to the next non-white after the recognized expression.
3754 *
3755 * Return OK or FAIL.
3756 */
3757 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003758eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759{
Bram Moolenaar33570922005-01-25 22:26:29 +00003760 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 long result;
3762 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003763 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764
3765 /*
3766 * Get the first variable.
3767 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003768 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 return FAIL;
3770
3771 /*
3772 * Repeat until there is no following "&&".
3773 */
3774 first = TRUE;
3775 result = TRUE;
3776 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3777 {
3778 if (evaluate && first)
3779 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003780 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003782 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003783 if (error)
3784 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 first = FALSE;
3786 }
3787
3788 /*
3789 * Get the second variable.
3790 */
3791 *arg = skipwhite(*arg + 2);
3792 if (eval4(arg, &var2, evaluate && result) == FAIL)
3793 return FAIL;
3794
3795 /*
3796 * Compute the result.
3797 */
3798 if (evaluate && result)
3799 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003800 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003802 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003803 if (error)
3804 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 }
3806 if (evaluate)
3807 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003808 rettv->v_type = VAR_NUMBER;
3809 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 }
3811 }
3812
3813 return OK;
3814}
3815
3816/*
3817 * Handle third level expression:
3818 * var1 == var2
3819 * var1 =~ var2
3820 * var1 != var2
3821 * var1 !~ var2
3822 * var1 > var2
3823 * var1 >= var2
3824 * var1 < var2
3825 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003826 * var1 is var2
3827 * var1 isnot var2
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 +01003835eval4(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;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 char_u *p;
3839 int i;
3840 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003841 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844
3845 /*
3846 * Get the first variable.
3847 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003848 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 return FAIL;
3850
3851 p = *arg;
3852 switch (p[0])
3853 {
3854 case '=': if (p[1] == '=')
3855 type = TYPE_EQUAL;
3856 else if (p[1] == '~')
3857 type = TYPE_MATCH;
3858 break;
3859 case '!': if (p[1] == '=')
3860 type = TYPE_NEQUAL;
3861 else if (p[1] == '~')
3862 type = TYPE_NOMATCH;
3863 break;
3864 case '>': if (p[1] != '=')
3865 {
3866 type = TYPE_GREATER;
3867 len = 1;
3868 }
3869 else
3870 type = TYPE_GEQUAL;
3871 break;
3872 case '<': if (p[1] != '=')
3873 {
3874 type = TYPE_SMALLER;
3875 len = 1;
3876 }
3877 else
3878 type = TYPE_SEQUAL;
3879 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003880 case 'i': if (p[1] == 's')
3881 {
3882 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3883 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003884 i = p[len];
3885 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003886 {
3887 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3888 type_is = TRUE;
3889 }
3890 }
3891 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 }
3893
3894 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003895 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 */
3897 if (type != TYPE_UNKNOWN)
3898 {
3899 /* extra question mark appended: ignore case */
3900 if (p[len] == '?')
3901 {
3902 ic = TRUE;
3903 ++len;
3904 }
3905 /* extra '#' appended: match case */
3906 else if (p[len] == '#')
3907 {
3908 ic = FALSE;
3909 ++len;
3910 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003911 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 else
3913 ic = p_ic;
3914
3915 /*
3916 * Get the second variable.
3917 */
3918 *arg = skipwhite(p + len);
3919 if (eval5(arg, &var2, evaluate) == FAIL)
3920 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003921 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 return FAIL;
3923 }
Bram Moolenaar31988702018-02-13 12:57:42 +01003924 if (evaluate)
3925 {
3926 int ret = typval_compare(rettv, &var2, type, type_is, ic);
3927
3928 clear_tv(&var2);
3929 return ret;
3930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 }
3932
3933 return OK;
3934}
3935
3936/*
3937 * Handle fourth level expression:
3938 * + number addition
3939 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003940 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003941 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 *
3943 * "arg" must point to the first non-white of the expression.
3944 * "arg" is advanced to the next non-white after the recognized expression.
3945 *
3946 * Return OK or FAIL.
3947 */
3948 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003949eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950{
Bram Moolenaar33570922005-01-25 22:26:29 +00003951 typval_T var2;
3952 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003954 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003955#ifdef FEAT_FLOAT
3956 float_T f1 = 0, f2 = 0;
3957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 char_u *s1, *s2;
3959 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3960 char_u *p;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003961 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962
3963 /*
3964 * Get the first variable.
3965 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003966 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 return FAIL;
3968
3969 /*
3970 * Repeat computing, until no '+', '-' or '.' is following.
3971 */
3972 for (;;)
3973 {
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003974 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 op = **arg;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003976 concat = op == '.'
3977 && (*(*arg + 1) == '.' || current_sctx.sc_version < 2);
3978 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 break;
3980
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003981 if ((op != '+' || (rettv->v_type != VAR_LIST
3982 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003983#ifdef FEAT_FLOAT
3984 && (op == '.' || rettv->v_type != VAR_FLOAT)
3985#endif
3986 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003987 {
3988 /* For "list + ...", an illegal use of the first operand as
3989 * a number cannot be determined before evaluating the 2nd
3990 * operand: if this is also a list, all is ok.
3991 * For "something . ...", "something - ..." or "non-list + ...",
3992 * we know that the first operand needs to be a string or number
3993 * without evaluating the 2nd operand. So check before to avoid
3994 * side effects after an error. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003995 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003996 {
3997 clear_tv(rettv);
3998 return FAIL;
3999 }
4000 }
4001
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 /*
4003 * Get the second variable.
4004 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02004005 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
4006 ++*arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004008 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004010 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 return FAIL;
4012 }
4013
4014 if (evaluate)
4015 {
4016 /*
4017 * Compute the result.
4018 */
4019 if (op == '.')
4020 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004021 s1 = tv_get_string_buf(rettv, buf1); /* already checked */
4022 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004023 if (s2 == NULL) /* type error ? */
4024 {
4025 clear_tv(rettv);
4026 clear_tv(&var2);
4027 return FAIL;
4028 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004029 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004030 clear_tv(rettv);
4031 rettv->v_type = VAR_STRING;
4032 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004034 else if (op == '+' && rettv->v_type == VAR_BLOB
4035 && var2.v_type == VAR_BLOB)
4036 {
4037 blob_T *b1 = rettv->vval.v_blob;
4038 blob_T *b2 = var2.vval.v_blob;
4039 blob_T *b = blob_alloc();
4040 int i;
4041
4042 if (b != NULL)
4043 {
4044 for (i = 0; i < blob_len(b1); i++)
4045 ga_append(&b->bv_ga, blob_get(b1, i));
4046 for (i = 0; i < blob_len(b2); i++)
4047 ga_append(&b->bv_ga, blob_get(b2, i));
4048
4049 clear_tv(rettv);
4050 rettv_blob_set(rettv, b);
4051 }
4052 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004053 else if (op == '+' && rettv->v_type == VAR_LIST
4054 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004055 {
4056 /* concatenate Lists */
4057 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4058 &var3) == FAIL)
4059 {
4060 clear_tv(rettv);
4061 clear_tv(&var2);
4062 return FAIL;
4063 }
4064 clear_tv(rettv);
4065 *rettv = var3;
4066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 else
4068 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004069 int error = FALSE;
4070
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004071#ifdef FEAT_FLOAT
4072 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004073 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004074 f1 = rettv->vval.v_float;
4075 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004076 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004077 else
4078#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004079 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004080 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004081 if (error)
4082 {
4083 /* This can only happen for "list + non-list". For
4084 * "non-list + ..." or "something - ...", we returned
4085 * before evaluating the 2nd operand. */
4086 clear_tv(rettv);
4087 return FAIL;
4088 }
4089#ifdef FEAT_FLOAT
4090 if (var2.v_type == VAR_FLOAT)
4091 f1 = n1;
4092#endif
4093 }
4094#ifdef FEAT_FLOAT
4095 if (var2.v_type == VAR_FLOAT)
4096 {
4097 f2 = var2.vval.v_float;
4098 n2 = 0;
4099 }
4100 else
4101#endif
4102 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004103 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004104 if (error)
4105 {
4106 clear_tv(rettv);
4107 clear_tv(&var2);
4108 return FAIL;
4109 }
4110#ifdef FEAT_FLOAT
4111 if (rettv->v_type == VAR_FLOAT)
4112 f2 = n2;
4113#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004114 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004115 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004116
4117#ifdef FEAT_FLOAT
4118 /* If there is a float on either side the result is a float. */
4119 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4120 {
4121 if (op == '+')
4122 f1 = f1 + f2;
4123 else
4124 f1 = f1 - f2;
4125 rettv->v_type = VAR_FLOAT;
4126 rettv->vval.v_float = f1;
4127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004129#endif
4130 {
4131 if (op == '+')
4132 n1 = n1 + n2;
4133 else
4134 n1 = n1 - n2;
4135 rettv->v_type = VAR_NUMBER;
4136 rettv->vval.v_number = n1;
4137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004139 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 }
4141 }
4142 return OK;
4143}
4144
4145/*
4146 * Handle fifth level expression:
4147 * * number multiplication
4148 * / number division
4149 * % number modulo
4150 *
4151 * "arg" must point to the first non-white of the expression.
4152 * "arg" is advanced to the next non-white after the recognized expression.
4153 *
4154 * Return OK or FAIL.
4155 */
4156 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004157eval6(
4158 char_u **arg,
4159 typval_T *rettv,
4160 int evaluate,
4161 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162{
Bram Moolenaar33570922005-01-25 22:26:29 +00004163 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004165 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004166#ifdef FEAT_FLOAT
4167 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02004168 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004169#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004170 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171
4172 /*
4173 * Get the first variable.
4174 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004175 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 return FAIL;
4177
4178 /*
4179 * Repeat computing, until no '*', '/' or '%' is following.
4180 */
4181 for (;;)
4182 {
4183 op = **arg;
4184 if (op != '*' && op != '/' && op != '%')
4185 break;
4186
4187 if (evaluate)
4188 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004189#ifdef FEAT_FLOAT
4190 if (rettv->v_type == VAR_FLOAT)
4191 {
4192 f1 = rettv->vval.v_float;
4193 use_float = TRUE;
4194 n1 = 0;
4195 }
4196 else
4197#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004198 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004199 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004200 if (error)
4201 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 }
4203 else
4204 n1 = 0;
4205
4206 /*
4207 * Get the second variable.
4208 */
4209 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004210 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 return FAIL;
4212
4213 if (evaluate)
4214 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004215#ifdef FEAT_FLOAT
4216 if (var2.v_type == VAR_FLOAT)
4217 {
4218 if (!use_float)
4219 {
4220 f1 = n1;
4221 use_float = TRUE;
4222 }
4223 f2 = var2.vval.v_float;
4224 n2 = 0;
4225 }
4226 else
4227#endif
4228 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004229 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004230 clear_tv(&var2);
4231 if (error)
4232 return FAIL;
4233#ifdef FEAT_FLOAT
4234 if (use_float)
4235 f2 = n2;
4236#endif
4237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238
4239 /*
4240 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004241 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004243#ifdef FEAT_FLOAT
4244 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004246 if (op == '*')
4247 f1 = f1 * f2;
4248 else if (op == '/')
4249 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004250# ifdef VMS
4251 /* VMS crashes on divide by zero, work around it */
4252 if (f2 == 0.0)
4253 {
4254 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004255 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004256 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004257 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004258 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004259 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004260 }
4261 else
4262 f1 = f1 / f2;
4263# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004264 /* We rely on the floating point library to handle divide
4265 * by zero to result in "inf" and not a crash. */
4266 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004267# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004270 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004271 emsg(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004272 return FAIL;
4273 }
4274 rettv->v_type = VAR_FLOAT;
4275 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 }
4277 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004280 if (op == '*')
4281 n1 = n1 * n2;
4282 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01004283 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01004285 n1 = num_modulus(n1, n2);
4286
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004287 rettv->v_type = VAR_NUMBER;
4288 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 }
4291 }
4292
4293 return OK;
4294}
4295
4296/*
4297 * Handle sixth level expression:
4298 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004299 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004300 * "string" string constant
4301 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 * &option-name option value
4303 * @r register contents
4304 * identifier variable value
4305 * function() function call
4306 * $VAR environment variable
4307 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004308 * [expr, expr] List
4309 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 *
4311 * Also handle:
4312 * ! in front logical NOT
4313 * - in front unary minus
4314 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004315 * trailing [] subscript in String or List
4316 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 *
4318 * "arg" must point to the first non-white of the expression.
4319 * "arg" is advanced to the next non-white after the recognized expression.
4320 *
4321 * Return OK or FAIL.
4322 */
4323 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004324eval7(
4325 char_u **arg,
4326 typval_T *rettv,
4327 int evaluate,
4328 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004330 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 int len;
4332 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 char_u *start_leader, *end_leader;
4334 int ret = OK;
4335 char_u *alias;
4336
4337 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004338 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004339 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004341 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342
4343 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004344 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 */
4346 start_leader = *arg;
4347 while (**arg == '!' || **arg == '-' || **arg == '+')
4348 *arg = skipwhite(*arg + 1);
4349 end_leader = *arg;
4350
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004351 if (**arg == '.' && (!isdigit(*(*arg + 1))
4352#ifdef FEAT_FLOAT
4353 || current_sctx.sc_version < 2
4354#endif
4355 ))
4356 {
4357 semsg(_(e_invexpr2), *arg);
4358 ++*arg;
4359 return FAIL;
4360 }
4361
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 switch (**arg)
4363 {
4364 /*
4365 * Number constant.
4366 */
4367 case '0':
4368 case '1':
4369 case '2':
4370 case '3':
4371 case '4':
4372 case '5':
4373 case '6':
4374 case '7':
4375 case '8':
4376 case '9':
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004377 case '.':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004378 {
4379#ifdef FEAT_FLOAT
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004380 char_u *p;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004381 int get_float = FALSE;
4382
4383 /* We accept a float when the format matches
4384 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004385 * strict to avoid backwards compatibility problems.
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004386 * With script version 2 and later the leading digit can be
4387 * omitted.
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004388 * Don't look for a float after the "." operator, so that
4389 * ":let vers = 1.2.3" doesn't fail. */
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004390 if (**arg == '.')
4391 p = *arg;
4392 else
4393 p = skipdigits(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004394 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004396 get_float = TRUE;
4397 p = skipdigits(p + 2);
4398 if (*p == 'e' || *p == 'E')
4399 {
4400 ++p;
4401 if (*p == '-' || *p == '+')
4402 ++p;
4403 if (!vim_isdigit(*p))
4404 get_float = FALSE;
4405 else
4406 p = skipdigits(p + 1);
4407 }
4408 if (ASCII_ISALPHA(*p) || *p == '.')
4409 get_float = FALSE;
4410 }
4411 if (get_float)
4412 {
4413 float_T f;
4414
4415 *arg += string2float(*arg, &f);
4416 if (evaluate)
4417 {
4418 rettv->v_type = VAR_FLOAT;
4419 rettv->vval.v_float = f;
4420 }
4421 }
4422 else
4423#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004424 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004425 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004426 char_u *bp;
Bram Moolenaare519dfd2019-01-13 15:42:02 +01004427 blob_T *blob = NULL; // init for gcc
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004428
4429 // Blob constant: 0z0123456789abcdef
4430 if (evaluate)
4431 blob = blob_alloc();
4432 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
4433 {
4434 if (!vim_isxdigit(bp[1]))
4435 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004436 if (blob != NULL)
4437 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004438 emsg(_("E973: Blob literal should have an even number of hex characters"));
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004439 ga_clear(&blob->bv_ga);
4440 VIM_CLEAR(blob);
4441 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004442 ret = FAIL;
4443 break;
4444 }
4445 if (blob != NULL)
4446 ga_append(&blob->bv_ga,
4447 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
Bram Moolenaar4131fd52019-01-17 16:32:53 +01004448 if (bp[2] == '.' && vim_isxdigit(bp[3]))
4449 ++bp;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004450 }
4451 if (blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004452 rettv_blob_set(rettv, blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004453 *arg = bp;
4454 }
4455 else
4456 {
4457 // decimal, hex or octal number
Bram Moolenaar16e9b852019-05-19 19:59:35 +02004458 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0, TRUE);
4459 if (len == 0)
4460 {
4461 semsg(_(e_invexpr2), *arg);
4462 ret = FAIL;
4463 break;
4464 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004465 *arg += len;
4466 if (evaluate)
4467 {
4468 rettv->v_type = VAR_NUMBER;
4469 rettv->vval.v_number = n;
4470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 }
4472 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474
4475 /*
4476 * String constant: "string".
4477 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004478 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 break;
4480
4481 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004482 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004484 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004485 break;
4486
4487 /*
4488 * List: [expr, expr]
4489 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004490 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 break;
4492
4493 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004494 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004495 * Dictionary: {key: val, key: val}
4496 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004497 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4498 if (ret == NOTDONE)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004499 ret = dict_get_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004500 break;
4501
4502 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004503 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004505 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 break;
4507
4508 /*
4509 * Environment variable: $VAR.
4510 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004511 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 break;
4513
4514 /*
4515 * Register contents: @r.
4516 */
4517 case '@': ++*arg;
4518 if (evaluate)
4519 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004520 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004521 rettv->vval.v_string = get_reg_contents(**arg,
4522 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 }
4524 if (**arg != NUL)
4525 ++*arg;
4526 break;
4527
4528 /*
4529 * nested expression: (expression).
4530 */
4531 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004532 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 if (**arg == ')')
4534 ++*arg;
4535 else if (ret == OK)
4536 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004537 emsg(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004538 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 ret = FAIL;
4540 }
4541 break;
4542
Bram Moolenaar8c711452005-01-14 21:53:12 +00004543 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 break;
4545 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004546
4547 if (ret == NOTDONE)
4548 {
4549 /*
4550 * Must be a variable or function name.
4551 * Can also be a curly-braces kind of name: {expr}.
4552 */
4553 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004554 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004555 if (alias != NULL)
4556 s = alias;
4557
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004558 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004559 ret = FAIL;
4560 else
4561 {
4562 if (**arg == '(') /* recursive! */
4563 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004564 partial_T *partial;
4565
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004566 if (!evaluate)
4567 check_vars(s, len);
4568
Bram Moolenaar8c711452005-01-14 21:53:12 +00004569 /* If "s" is the name of a variable of type VAR_FUNC
4570 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004571 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004572
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004573 /* Need to make a copy, in case evaluating the arguments makes
4574 * the name invalid. */
4575 s = vim_strsave(s);
4576 if (s == NULL)
4577 ret = FAIL;
4578 else
4579 /* Invoke the function. */
4580 ret = get_func_tv(s, len, rettv, arg,
4581 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4582 &len, evaluate, partial, NULL);
4583 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004584
4585 /* If evaluate is FALSE rettv->v_type was not set in
4586 * get_func_tv, but it's needed in handle_subscript() to parse
4587 * what follows. So set it here. */
4588 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4589 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004590 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004591 rettv->v_type = VAR_FUNC;
4592 }
4593
Bram Moolenaar8c711452005-01-14 21:53:12 +00004594 /* Stop the expression evaluation when immediately
4595 * aborting on error, or when an interrupt occurred or
4596 * an exception was thrown but not caught. */
Bram Moolenaar60640732019-06-07 23:15:22 +02004597 if (evaluate && aborting())
Bram Moolenaar8c711452005-01-14 21:53:12 +00004598 {
4599 if (ret == OK)
4600 clear_tv(rettv);
4601 ret = FAIL;
4602 }
4603 }
4604 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004605 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004606 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004607 {
4608 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004609 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004610 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004611 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004612 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004613 }
4614
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 *arg = skipwhite(*arg);
4616
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004617 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4618 * expr(expr). */
4619 if (ret == OK)
4620 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621
4622 /*
4623 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4624 */
4625 if (ret == OK && evaluate && end_leader > start_leader)
4626 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004627 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004628 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004629#ifdef FEAT_FLOAT
4630 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004631
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004632 if (rettv->v_type == VAR_FLOAT)
4633 f = rettv->vval.v_float;
4634 else
4635#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004636 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004637 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004639 clear_tv(rettv);
4640 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004642 else
4643 {
4644 while (end_leader > start_leader)
4645 {
4646 --end_leader;
4647 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004648 {
4649#ifdef FEAT_FLOAT
4650 if (rettv->v_type == VAR_FLOAT)
4651 f = !f;
4652 else
4653#endif
4654 val = !val;
4655 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004656 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004657 {
4658#ifdef FEAT_FLOAT
4659 if (rettv->v_type == VAR_FLOAT)
4660 f = -f;
4661 else
4662#endif
4663 val = -val;
4664 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004665 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004666#ifdef FEAT_FLOAT
4667 if (rettv->v_type == VAR_FLOAT)
4668 {
4669 clear_tv(rettv);
4670 rettv->vval.v_float = f;
4671 }
4672 else
4673#endif
4674 {
4675 clear_tv(rettv);
4676 rettv->v_type = VAR_NUMBER;
4677 rettv->vval.v_number = val;
4678 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 }
4681
4682 return ret;
4683}
4684
4685/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004686 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4687 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004688 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4689 */
4690 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004691eval_index(
4692 char_u **arg,
4693 typval_T *rettv,
4694 int evaluate,
4695 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004696{
4697 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004698 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004699 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004700 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004701 long len = -1;
4702 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004703 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004704 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004705
Bram Moolenaara03f2332016-02-06 18:09:59 +01004706 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004707 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004708 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004709 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004710 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004711 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004712 return FAIL;
4713 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004714#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004715 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004716 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004717 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004718#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004719 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004720 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004721 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004722 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004723 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004724 return FAIL;
4725 case VAR_UNKNOWN:
4726 if (evaluate)
4727 return FAIL;
4728 /* FALLTHROUGH */
4729
4730 case VAR_STRING:
4731 case VAR_NUMBER:
4732 case VAR_LIST:
4733 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004734 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004735 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004736 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004737
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004738 init_tv(&var1);
4739 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004740 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004741 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004742 /*
4743 * dict.name
4744 */
4745 key = *arg + 1;
4746 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4747 ;
4748 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004749 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004750 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004751 }
4752 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004753 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004754 /*
4755 * something[idx]
4756 *
4757 * Get the (first) variable from inside the [].
4758 */
4759 *arg = skipwhite(*arg + 1);
4760 if (**arg == ':')
4761 empty1 = TRUE;
4762 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4763 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004764 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004765 {
4766 /* not a number or string */
4767 clear_tv(&var1);
4768 return FAIL;
4769 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004770
4771 /*
4772 * Get the second variable from inside the [:].
4773 */
4774 if (**arg == ':')
4775 {
4776 range = TRUE;
4777 *arg = skipwhite(*arg + 1);
4778 if (**arg == ']')
4779 empty2 = TRUE;
4780 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4781 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004782 if (!empty1)
4783 clear_tv(&var1);
4784 return FAIL;
4785 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004786 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004787 {
4788 /* not a number or string */
4789 if (!empty1)
4790 clear_tv(&var1);
4791 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004792 return FAIL;
4793 }
4794 }
4795
4796 /* Check for the ']'. */
4797 if (**arg != ']')
4798 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004799 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004800 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004801 clear_tv(&var1);
4802 if (range)
4803 clear_tv(&var2);
4804 return FAIL;
4805 }
4806 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004807 }
4808
4809 if (evaluate)
4810 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004811 n1 = 0;
4812 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004813 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004814 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004815 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004816 }
4817 if (range)
4818 {
4819 if (empty2)
4820 n2 = -1;
4821 else
4822 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004823 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004824 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004825 }
4826 }
4827
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004828 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004829 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004830 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004831 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004832 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004833 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004834 case VAR_SPECIAL:
4835 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004836 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004837 break; /* not evaluating, skipping over subscript */
4838
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004839 case VAR_NUMBER:
4840 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004841 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004842 len = (long)STRLEN(s);
4843 if (range)
4844 {
4845 /* The resulting variable is a substring. If the indexes
4846 * are out of range the result is empty. */
4847 if (n1 < 0)
4848 {
4849 n1 = len + n1;
4850 if (n1 < 0)
4851 n1 = 0;
4852 }
4853 if (n2 < 0)
4854 n2 = len + n2;
4855 else if (n2 >= len)
4856 n2 = len;
4857 if (n1 >= len || n2 < 0 || n1 > n2)
4858 s = NULL;
4859 else
4860 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4861 }
4862 else
4863 {
4864 /* The resulting variable is a string of a single
4865 * character. If the index is too big or negative the
4866 * result is empty. */
4867 if (n1 >= len || n1 < 0)
4868 s = NULL;
4869 else
4870 s = vim_strnsave(s + n1, 1);
4871 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004872 clear_tv(rettv);
4873 rettv->v_type = VAR_STRING;
4874 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004875 break;
4876
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004877 case VAR_BLOB:
4878 len = blob_len(rettv->vval.v_blob);
4879 if (range)
4880 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01004881 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004882 // are out of range the result is empty.
4883 if (n1 < 0)
4884 {
4885 n1 = len + n1;
4886 if (n1 < 0)
4887 n1 = 0;
4888 }
4889 if (n2 < 0)
4890 n2 = len + n2;
4891 else if (n2 >= len)
4892 n2 = len - 1;
4893 if (n1 >= len || n2 < 0 || n1 > n2)
4894 {
4895 clear_tv(rettv);
4896 rettv->v_type = VAR_BLOB;
4897 rettv->vval.v_blob = NULL;
4898 }
4899 else
4900 {
4901 blob_T *blob = blob_alloc();
4902
4903 if (blob != NULL)
4904 {
4905 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
4906 {
4907 blob_free(blob);
4908 return FAIL;
4909 }
4910 blob->bv_ga.ga_len = n2 - n1 + 1;
4911 for (i = n1; i <= n2; i++)
4912 blob_set(blob, i - n1,
4913 blob_get(rettv->vval.v_blob, i));
4914
4915 clear_tv(rettv);
4916 rettv_blob_set(rettv, blob);
4917 }
4918 }
4919 }
4920 else
4921 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01004922 // The resulting variable is a byte value.
4923 // If the index is too big or negative that is an error.
4924 if (n1 < 0)
4925 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004926 if (n1 < len && n1 >= 0)
4927 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01004928 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004929
4930 clear_tv(rettv);
4931 rettv->v_type = VAR_NUMBER;
4932 rettv->vval.v_number = v;
4933 }
4934 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004935 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004936 }
4937 break;
4938
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004939 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004940 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004941 if (n1 < 0)
4942 n1 = len + n1;
4943 if (!empty1 && (n1 < 0 || n1 >= len))
4944 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004945 /* For a range we allow invalid values and return an empty
4946 * list. A list index out of range is an error. */
4947 if (!range)
4948 {
4949 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004950 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004951 return FAIL;
4952 }
4953 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004954 }
4955 if (range)
4956 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004957 list_T *l;
4958 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004959
4960 if (n2 < 0)
4961 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004962 else if (n2 >= len)
4963 n2 = len - 1;
4964 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004965 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004966 l = list_alloc();
4967 if (l == NULL)
4968 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004969 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004970 n1 <= n2; ++n1)
4971 {
4972 if (list_append_tv(l, &item->li_tv) == FAIL)
4973 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004974 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004975 return FAIL;
4976 }
4977 item = item->li_next;
4978 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004979 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004980 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004981 }
4982 else
4983 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004984 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004985 clear_tv(rettv);
4986 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004987 }
4988 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004989
4990 case VAR_DICT:
4991 if (range)
4992 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004993 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004994 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004995 if (len == -1)
4996 clear_tv(&var1);
4997 return FAIL;
4998 }
4999 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005000 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005001
5002 if (len == -1)
5003 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005004 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005005 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005006 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005007 clear_tv(&var1);
5008 return FAIL;
5009 }
5010 }
5011
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005012 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005013
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005014 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005015 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005016 if (len == -1)
5017 clear_tv(&var1);
5018 if (item == NULL)
5019 return FAIL;
5020
5021 copy_tv(&item->di_tv, &var1);
5022 clear_tv(rettv);
5023 *rettv = var1;
5024 }
5025 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005026 }
5027 }
5028
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005029 return OK;
5030}
5031
5032/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 * Get an option value.
5034 * "arg" points to the '&' or '+' before the option name.
5035 * "arg" is advanced to character after the option name.
5036 * Return OK or FAIL.
5037 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005038 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005039get_option_tv(
5040 char_u **arg,
5041 typval_T *rettv, /* when NULL, only check if option exists */
5042 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043{
5044 char_u *option_end;
5045 long numval;
5046 char_u *stringval;
5047 int opt_type;
5048 int c;
5049 int working = (**arg == '+'); /* has("+option") */
5050 int ret = OK;
5051 int opt_flags;
5052
5053 /*
5054 * Isolate the option name and find its value.
5055 */
5056 option_end = find_option_end(arg, &opt_flags);
5057 if (option_end == NULL)
5058 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005059 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005060 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 return FAIL;
5062 }
5063
5064 if (!evaluate)
5065 {
5066 *arg = option_end;
5067 return OK;
5068 }
5069
5070 c = *option_end;
5071 *option_end = NUL;
5072 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005073 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074
5075 if (opt_type == -3) /* invalid name */
5076 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005077 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005078 semsg(_("E113: Unknown option: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 ret = FAIL;
5080 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005081 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 {
5083 if (opt_type == -2) /* hidden string option */
5084 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005085 rettv->v_type = VAR_STRING;
5086 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 }
5088 else if (opt_type == -1) /* hidden number option */
5089 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005090 rettv->v_type = VAR_NUMBER;
5091 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 }
5093 else if (opt_type == 1) /* number option */
5094 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005095 rettv->v_type = VAR_NUMBER;
5096 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 }
5098 else /* string option */
5099 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005100 rettv->v_type = VAR_STRING;
5101 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 }
5103 }
5104 else if (working && (opt_type == -2 || opt_type == -1))
5105 ret = FAIL;
5106
5107 *option_end = c; /* put back for error messages */
5108 *arg = option_end;
5109
5110 return ret;
5111}
5112
5113/*
5114 * Allocate a variable for a string constant.
5115 * Return OK or FAIL.
5116 */
5117 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005118get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119{
5120 char_u *p;
5121 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 int extra = 0;
5123
5124 /*
5125 * Find the end of the string, skipping backslashed characters.
5126 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005127 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 {
5129 if (*p == '\\' && p[1] != NUL)
5130 {
5131 ++p;
5132 /* A "\<x>" form occupies at least 4 characters, and produces up
5133 * to 6 characters: reserve space for 2 extra */
5134 if (*p == '<')
5135 extra += 2;
5136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 }
5138
5139 if (*p != '"')
5140 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005141 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 return FAIL;
5143 }
5144
5145 /* If only parsing, set *arg and return here */
5146 if (!evaluate)
5147 {
5148 *arg = p + 1;
5149 return OK;
5150 }
5151
5152 /*
5153 * Copy the string into allocated memory, handling backslashed
5154 * characters.
5155 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005156 name = alloc(p - *arg + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 if (name == NULL)
5158 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005159 rettv->v_type = VAR_STRING;
5160 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161
Bram Moolenaar8c711452005-01-14 21:53:12 +00005162 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 {
5164 if (*p == '\\')
5165 {
5166 switch (*++p)
5167 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005168 case 'b': *name++ = BS; ++p; break;
5169 case 'e': *name++ = ESC; ++p; break;
5170 case 'f': *name++ = FF; ++p; break;
5171 case 'n': *name++ = NL; ++p; break;
5172 case 'r': *name++ = CAR; ++p; break;
5173 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174
5175 case 'X': /* hex: "\x1", "\x12" */
5176 case 'x':
5177 case 'u': /* Unicode: "\u0023" */
5178 case 'U':
5179 if (vim_isxdigit(p[1]))
5180 {
5181 int n, nr;
5182 int c = toupper(*p);
5183
5184 if (c == 'X')
5185 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005186 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005188 else
5189 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 nr = 0;
5191 while (--n >= 0 && vim_isxdigit(p[1]))
5192 {
5193 ++p;
5194 nr = (nr << 4) + hex2nr(*p);
5195 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005196 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 /* For "\u" store the number according to
5198 * 'encoding'. */
5199 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005200 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005202 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 break;
5205
5206 /* octal: "\1", "\12", "\123" */
5207 case '0':
5208 case '1':
5209 case '2':
5210 case '3':
5211 case '4':
5212 case '5':
5213 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005214 case '7': *name = *p++ - '0';
5215 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005217 *name = (*name << 3) + *p++ - '0';
5218 if (*p >= '0' && *p <= '7')
5219 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005221 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 break;
5223
5224 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005225 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 if (extra != 0)
5227 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005228 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 break;
5230 }
5231 /* FALLTHROUGH */
5232
Bram Moolenaar8c711452005-01-14 21:53:12 +00005233 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 break;
5235 }
5236 }
5237 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005238 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005241 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02005242 if (*p != NUL) /* just in case */
5243 ++p;
5244 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 return OK;
5247}
5248
5249/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005250 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 * Return OK or FAIL.
5252 */
5253 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005254get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255{
5256 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005257 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005258 int reduce = 0;
5259
5260 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005261 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005262 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005263 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005264 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005265 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005266 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005267 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005268 break;
5269 ++reduce;
5270 ++p;
5271 }
5272 }
5273
Bram Moolenaar8c711452005-01-14 21:53:12 +00005274 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005275 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005276 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005277 return FAIL;
5278 }
5279
Bram Moolenaar8c711452005-01-14 21:53:12 +00005280 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005281 if (!evaluate)
5282 {
5283 *arg = p + 1;
5284 return OK;
5285 }
5286
5287 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005288 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005289 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005290 str = alloc((p - *arg) - reduce);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005291 if (str == NULL)
5292 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005293 rettv->v_type = VAR_STRING;
5294 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005295
Bram Moolenaar8c711452005-01-14 21:53:12 +00005296 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005297 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005298 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005299 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005300 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005301 break;
5302 ++p;
5303 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005304 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005305 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005306 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005307 *arg = p + 1;
5308
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005309 return OK;
5310}
5311
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005312/*
5313 * Return the function name of the partial.
5314 */
5315 char_u *
5316partial_name(partial_T *pt)
5317{
5318 if (pt->pt_name != NULL)
5319 return pt->pt_name;
5320 return pt->pt_func->uf_name;
5321}
5322
Bram Moolenaarddecc252016-04-06 22:59:37 +02005323 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005324partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005325{
5326 int i;
5327
5328 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005329 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005330 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005331 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005332 if (pt->pt_name != NULL)
5333 {
5334 func_unref(pt->pt_name);
5335 vim_free(pt->pt_name);
5336 }
5337 else
5338 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005339 vim_free(pt);
5340}
5341
5342/*
5343 * Unreference a closure: decrement the reference count and free it when it
5344 * becomes zero.
5345 */
5346 void
5347partial_unref(partial_T *pt)
5348{
5349 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005350 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005351}
5352
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005353static int tv_equal_recurse_limit;
5354
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005355 static int
5356func_equal(
5357 typval_T *tv1,
5358 typval_T *tv2,
5359 int ic) /* ignore case */
5360{
5361 char_u *s1, *s2;
5362 dict_T *d1, *d2;
5363 int a1, a2;
5364 int i;
5365
5366 /* empty and NULL function name considered the same */
5367 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005368 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005369 if (s1 != NULL && *s1 == NUL)
5370 s1 = NULL;
5371 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005372 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005373 if (s2 != NULL && *s2 == NUL)
5374 s2 = NULL;
5375 if (s1 == NULL || s2 == NULL)
5376 {
5377 if (s1 != s2)
5378 return FALSE;
5379 }
5380 else if (STRCMP(s1, s2) != 0)
5381 return FALSE;
5382
5383 /* empty dict and NULL dict is different */
5384 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5385 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5386 if (d1 == NULL || d2 == NULL)
5387 {
5388 if (d1 != d2)
5389 return FALSE;
5390 }
5391 else if (!dict_equal(d1, d2, ic, TRUE))
5392 return FALSE;
5393
5394 /* empty list and no list considered the same */
5395 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5396 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5397 if (a1 != a2)
5398 return FALSE;
5399 for (i = 0; i < a1; ++i)
5400 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5401 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5402 return FALSE;
5403
5404 return TRUE;
5405}
5406
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005407/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005408 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005409 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005410 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005411 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005412 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005413tv_equal(
5414 typval_T *tv1,
5415 typval_T *tv2,
5416 int ic, /* ignore case */
5417 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005418{
5419 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005420 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005421 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005422 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005423
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005424 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005425 * recursiveness to a limit. We guess they are equal then.
5426 * A fixed limit has the problem of still taking an awful long time.
5427 * Reduce the limit every time running into it. That should work fine for
5428 * deeply linked structures that are not recursively linked and catch
5429 * recursiveness quickly. */
5430 if (!recursive)
5431 tv_equal_recurse_limit = 1000;
5432 if (recursive_cnt >= tv_equal_recurse_limit)
5433 {
5434 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005435 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005436 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005437
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005438 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5439 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005440 if ((tv1->v_type == VAR_FUNC
5441 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5442 && (tv2->v_type == VAR_FUNC
5443 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5444 {
5445 ++recursive_cnt;
5446 r = func_equal(tv1, tv2, ic);
5447 --recursive_cnt;
5448 return r;
5449 }
5450
5451 if (tv1->v_type != tv2->v_type)
5452 return FALSE;
5453
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005454 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005455 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005456 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005457 ++recursive_cnt;
5458 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5459 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005460 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005461
5462 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005463 ++recursive_cnt;
5464 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5465 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005466 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005467
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005468 case VAR_BLOB:
5469 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
5470
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005471 case VAR_NUMBER:
5472 return tv1->vval.v_number == tv2->vval.v_number;
5473
5474 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005475 s1 = tv_get_string_buf(tv1, buf1);
5476 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005477 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005478
5479 case VAR_SPECIAL:
5480 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005481
5482 case VAR_FLOAT:
5483#ifdef FEAT_FLOAT
5484 return tv1->vval.v_float == tv2->vval.v_float;
5485#endif
5486 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005487#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005488 return tv1->vval.v_job == tv2->vval.v_job;
5489#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005490 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005491#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005492 return tv1->vval.v_channel == tv2->vval.v_channel;
5493#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005494 case VAR_FUNC:
5495 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005496 case VAR_UNKNOWN:
5497 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005498 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005499
Bram Moolenaara03f2332016-02-06 18:09:59 +01005500 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5501 * does not equal anything, not even itself. */
5502 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005503}
5504
5505/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005506 * Return the next (unique) copy ID.
5507 * Used for serializing nested structures.
5508 */
5509 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005510get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005511{
5512 current_copyID += COPYID_INC;
5513 return current_copyID;
5514}
5515
5516/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005517 * Garbage collection for lists and dictionaries.
5518 *
5519 * We use reference counts to be able to free most items right away when they
5520 * are no longer used. But for composite items it's possible that it becomes
5521 * unused while the reference count is > 0: When there is a recursive
5522 * reference. Example:
5523 * :let l = [1, 2, 3]
5524 * :let d = {9: l}
5525 * :let l[1] = d
5526 *
5527 * Since this is quite unusual we handle this with garbage collection: every
5528 * once in a while find out which lists and dicts are not referenced from any
5529 * variable.
5530 *
5531 * Here is a good reference text about garbage collection (refers to Python
5532 * but it applies to all reference-counting mechanisms):
5533 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005534 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005535
5536/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005537 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005538 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005539 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005540 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005541 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005542garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005543{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005544 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005545 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005546 buf_T *buf;
5547 win_T *wp;
5548 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005549 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005550 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005551
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005552 if (!testing)
5553 {
5554 /* Only do this once. */
5555 want_garbage_collect = FALSE;
5556 may_garbage_collect = FALSE;
5557 garbage_collect_at_exit = FALSE;
5558 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005559
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005560 /* We advance by two because we add one for items referenced through
5561 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005562 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005563
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005564 /*
5565 * 1. Go through all accessible variables and mark all lists and dicts
5566 * with copyID.
5567 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005568
5569 /* Don't free variables in the previous_funccal list unless they are only
5570 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005571 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005572 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005573
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005574 /* script-local variables */
5575 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005576 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005577
5578 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005579 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005580 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5581 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005582
5583 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005584 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005585 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5586 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005587 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005588 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5589 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005590#ifdef FEAT_TEXT_PROP
5591 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
5592 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5593 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005594 FOR_ALL_TABPAGES(tp)
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02005595 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005596 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5597 NULL, NULL);
5598#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005599
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005600 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005601 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005602 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5603 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005604 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005605 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005606
5607 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005608 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005609
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005610 /* named functions (matters for closures) */
5611 abort = abort || set_ref_in_functions(copyID);
5612
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005613 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005614 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005615
Bram Moolenaard812df62008-11-09 12:46:09 +00005616 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005617 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005618
Bram Moolenaar1dced572012-04-05 16:54:08 +02005619#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005620 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005621#endif
5622
Bram Moolenaardb913952012-06-29 12:54:53 +02005623#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005624 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005625#endif
5626
5627#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005628 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005629#endif
5630
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005631#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005632 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005633 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005634#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005635#ifdef FEAT_NETBEANS_INTG
5636 abort = abort || set_ref_in_nb_channel(copyID);
5637#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005638
Bram Moolenaare3188e22016-05-31 21:13:04 +02005639#ifdef FEAT_TIMERS
5640 abort = abort || set_ref_in_timer(copyID);
5641#endif
5642
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005643#ifdef FEAT_QUICKFIX
5644 abort = abort || set_ref_in_quickfix(copyID);
5645#endif
5646
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005647#ifdef FEAT_TERMINAL
5648 abort = abort || set_ref_in_term(copyID);
5649#endif
5650
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005651 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005652 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005653 /*
5654 * 2. Free lists and dictionaries that are not referenced.
5655 */
5656 did_free = free_unref_items(copyID);
5657
5658 /*
5659 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005660 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005661 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005662 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005663 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005664 else if (p_verbose > 0)
5665 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005666 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005667 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005668
5669 return did_free;
5670}
5671
5672/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005673 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005674 */
5675 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005676free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005677{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005678 int did_free = FALSE;
5679
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005680 /* Let all "free" functions know that we are here. This means no
5681 * dictionaries, lists, channels or jobs are to be freed, because we will
5682 * do that here. */
5683 in_free_unref_items = TRUE;
5684
5685 /*
5686 * PASS 1: free the contents of the items. We don't free the items
5687 * themselves yet, so that it is possible to decrement refcount counters
5688 */
5689
Bram Moolenaarcd524592016-07-17 14:57:05 +02005690 /* Go through the list of dicts and free items without the copyID. */
5691 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005692
Bram Moolenaarda861d62016-07-17 15:46:27 +02005693 /* Go through the list of lists and free items without the copyID. */
5694 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005695
5696#ifdef FEAT_JOB_CHANNEL
5697 /* Go through the list of jobs and free items without the copyID. This
5698 * must happen before doing channels, because jobs refer to channels, but
5699 * the reference from the channel to the job isn't tracked. */
5700 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5701
5702 /* Go through the list of channels and free items without the copyID. */
5703 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5704#endif
5705
5706 /*
5707 * PASS 2: free the items themselves.
5708 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005709 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005710 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005711
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005712#ifdef FEAT_JOB_CHANNEL
5713 /* Go through the list of jobs and free items without the copyID. This
5714 * must happen before doing channels, because jobs refer to channels, but
5715 * the reference from the channel to the job isn't tracked. */
5716 free_unused_jobs(copyID, COPYID_MASK);
5717
5718 /* Go through the list of channels and free items without the copyID. */
5719 free_unused_channels(copyID, COPYID_MASK);
5720#endif
5721
5722 in_free_unref_items = FALSE;
5723
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005724 return did_free;
5725}
5726
5727/*
5728 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005729 * "list_stack" is used to add lists to be marked. Can be NULL.
5730 *
5731 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005732 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005733 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005734set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005735{
5736 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005737 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005738 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005739 hashtab_T *cur_ht;
5740 ht_stack_T *ht_stack = NULL;
5741 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005742
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005743 cur_ht = ht;
5744 for (;;)
5745 {
5746 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005747 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005748 /* Mark each item in the hashtab. If the item contains a hashtab
5749 * it is added to ht_stack, if it contains a list it is added to
5750 * list_stack. */
5751 todo = (int)cur_ht->ht_used;
5752 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5753 if (!HASHITEM_EMPTY(hi))
5754 {
5755 --todo;
5756 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5757 &ht_stack, list_stack);
5758 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005759 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005760
5761 if (ht_stack == NULL)
5762 break;
5763
5764 /* take an item from the stack */
5765 cur_ht = ht_stack->ht;
5766 tempitem = ht_stack;
5767 ht_stack = ht_stack->prev;
5768 free(tempitem);
5769 }
5770
5771 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005772}
5773
5774/*
5775 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005776 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5777 *
5778 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005779 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005780 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005781set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005782{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005783 listitem_T *li;
5784 int abort = FALSE;
5785 list_T *cur_l;
5786 list_stack_T *list_stack = NULL;
5787 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005788
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005789 cur_l = l;
5790 for (;;)
5791 {
5792 if (!abort)
5793 /* Mark each item in the list. If the item contains a hashtab
5794 * it is added to ht_stack, if it contains a list it is added to
5795 * list_stack. */
5796 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5797 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5798 ht_stack, &list_stack);
5799 if (list_stack == NULL)
5800 break;
5801
5802 /* take an item from the stack */
5803 cur_l = list_stack->list;
5804 tempitem = list_stack;
5805 list_stack = list_stack->prev;
5806 free(tempitem);
5807 }
5808
5809 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005810}
5811
5812/*
5813 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005814 * "list_stack" is used to add lists to be marked. Can be NULL.
5815 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5816 *
5817 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005818 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005819 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005820set_ref_in_item(
5821 typval_T *tv,
5822 int copyID,
5823 ht_stack_T **ht_stack,
5824 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005825{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005826 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005827
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005828 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005829 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005830 dict_T *dd = tv->vval.v_dict;
5831
Bram Moolenaara03f2332016-02-06 18:09:59 +01005832 if (dd != NULL && dd->dv_copyID != copyID)
5833 {
5834 /* Didn't see this dict yet. */
5835 dd->dv_copyID = copyID;
5836 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005837 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005838 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5839 }
5840 else
5841 {
5842 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5843 if (newitem == NULL)
5844 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005845 else
5846 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005847 newitem->ht = &dd->dv_hashtab;
5848 newitem->prev = *ht_stack;
5849 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005850 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005851 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005852 }
5853 }
5854 else if (tv->v_type == VAR_LIST)
5855 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005856 list_T *ll = tv->vval.v_list;
5857
Bram Moolenaara03f2332016-02-06 18:09:59 +01005858 if (ll != NULL && ll->lv_copyID != copyID)
5859 {
5860 /* Didn't see this list yet. */
5861 ll->lv_copyID = copyID;
5862 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005863 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005864 abort = set_ref_in_list(ll, copyID, ht_stack);
5865 }
5866 else
5867 {
5868 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005869 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005870 if (newitem == NULL)
5871 abort = TRUE;
5872 else
5873 {
5874 newitem->list = ll;
5875 newitem->prev = *list_stack;
5876 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005877 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005878 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005879 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005880 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005881 else if (tv->v_type == VAR_FUNC)
5882 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005883 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005884 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005885 else if (tv->v_type == VAR_PARTIAL)
5886 {
5887 partial_T *pt = tv->vval.v_partial;
5888 int i;
5889
5890 /* A partial does not have a copyID, because it cannot contain itself.
5891 */
5892 if (pt != NULL)
5893 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005894 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005895
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005896 if (pt->pt_dict != NULL)
5897 {
5898 typval_T dtv;
5899
5900 dtv.v_type = VAR_DICT;
5901 dtv.vval.v_dict = pt->pt_dict;
5902 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5903 }
5904
5905 for (i = 0; i < pt->pt_argc; ++i)
5906 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5907 ht_stack, list_stack);
5908 }
5909 }
5910#ifdef FEAT_JOB_CHANNEL
5911 else if (tv->v_type == VAR_JOB)
5912 {
5913 job_T *job = tv->vval.v_job;
5914 typval_T dtv;
5915
5916 if (job != NULL && job->jv_copyID != copyID)
5917 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005918 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005919 if (job->jv_channel != NULL)
5920 {
5921 dtv.v_type = VAR_CHANNEL;
5922 dtv.vval.v_channel = job->jv_channel;
5923 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5924 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005925 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005926 {
5927 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005928 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005929 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5930 }
5931 }
5932 }
5933 else if (tv->v_type == VAR_CHANNEL)
5934 {
5935 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005936 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005937 typval_T dtv;
5938 jsonq_T *jq;
5939 cbq_T *cq;
5940
5941 if (ch != NULL && ch->ch_copyID != copyID)
5942 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005943 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005944 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005945 {
5946 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5947 jq = jq->jq_next)
5948 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5949 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5950 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005951 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005952 {
5953 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005954 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005955 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5956 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005957 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005958 {
5959 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005960 dtv.vval.v_partial =
5961 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005962 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5963 }
5964 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005965 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005966 {
5967 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005968 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005969 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5970 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005971 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005972 {
5973 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005974 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005975 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5976 }
5977 }
5978 }
5979#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005980 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005981}
5982
Bram Moolenaar17a13432016-01-24 14:22:10 +01005983 static char *
5984get_var_special_name(int nr)
5985{
5986 switch (nr)
5987 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005988 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005989 case VVAL_TRUE: return "v:true";
5990 case VVAL_NONE: return "v:none";
5991 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005992 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01005993 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01005994 return "42";
5995}
5996
Bram Moolenaar8c711452005-01-14 21:53:12 +00005997/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005998 * Return a string with the string representation of a variable.
5999 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006000 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006001 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02006002 * When both "echo_style" and "composite_val" are FALSE, put quotes around
6003 * stings as "string()", otherwise does not put quotes around strings, as
6004 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006005 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6006 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006007 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006008 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006009 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006010echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006011 typval_T *tv,
6012 char_u **tofree,
6013 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006014 int copyID,
6015 int echo_style,
6016 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006017 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006018{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006019 static int recurse = 0;
6020 char_u *r = NULL;
6021
Bram Moolenaar33570922005-01-25 22:26:29 +00006022 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006023 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006024 if (!did_echo_string_emsg)
6025 {
6026 /* Only give this message once for a recursive call to avoid
6027 * flooding the user with errors. And stop iterating over lists
6028 * and dicts. */
6029 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006030 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006031 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006032 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006033 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006034 }
6035 ++recurse;
6036
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006037 switch (tv->v_type)
6038 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006039 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006040 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006041 {
6042 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02006043 r = tv->vval.v_string;
6044 if (r == NULL)
6045 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006046 }
6047 else
6048 {
6049 *tofree = string_quote(tv->vval.v_string, FALSE);
6050 r = *tofree;
6051 }
6052 break;
6053
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006054 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006055 if (echo_style)
6056 {
6057 *tofree = NULL;
6058 r = tv->vval.v_string;
6059 }
6060 else
6061 {
6062 *tofree = string_quote(tv->vval.v_string, TRUE);
6063 r = *tofree;
6064 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006065 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006066
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006067 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006068 {
6069 partial_T *pt = tv->vval.v_partial;
6070 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006071 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006072 garray_T ga;
6073 int i;
6074 char_u *tf;
6075
6076 ga_init2(&ga, 1, 100);
6077 ga_concat(&ga, (char_u *)"function(");
6078 if (fname != NULL)
6079 {
6080 ga_concat(&ga, fname);
6081 vim_free(fname);
6082 }
6083 if (pt != NULL && pt->pt_argc > 0)
6084 {
6085 ga_concat(&ga, (char_u *)", [");
6086 for (i = 0; i < pt->pt_argc; ++i)
6087 {
6088 if (i > 0)
6089 ga_concat(&ga, (char_u *)", ");
6090 ga_concat(&ga,
6091 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6092 vim_free(tf);
6093 }
6094 ga_concat(&ga, (char_u *)"]");
6095 }
6096 if (pt != NULL && pt->pt_dict != NULL)
6097 {
6098 typval_T dtv;
6099
6100 ga_concat(&ga, (char_u *)", ");
6101 dtv.v_type = VAR_DICT;
6102 dtv.vval.v_dict = pt->pt_dict;
6103 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6104 vim_free(tf);
6105 }
6106 ga_concat(&ga, (char_u *)")");
6107
6108 *tofree = ga.ga_data;
6109 r = *tofree;
6110 break;
6111 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006112
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006113 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006114 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006115 break;
6116
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006117 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006118 if (tv->vval.v_list == NULL)
6119 {
6120 *tofree = NULL;
6121 r = NULL;
6122 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006123 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6124 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006125 {
6126 *tofree = NULL;
6127 r = (char_u *)"[...]";
6128 }
6129 else
6130 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006131 int old_copyID = tv->vval.v_list->lv_copyID;
6132
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006133 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006134 *tofree = list2string(tv, copyID, restore_copyID);
6135 if (restore_copyID)
6136 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006137 r = *tofree;
6138 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006139 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006140
Bram Moolenaar8c711452005-01-14 21:53:12 +00006141 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006142 if (tv->vval.v_dict == NULL)
6143 {
6144 *tofree = NULL;
6145 r = NULL;
6146 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006147 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6148 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006149 {
6150 *tofree = NULL;
6151 r = (char_u *)"{...}";
6152 }
6153 else
6154 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006155 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006156 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006157 *tofree = dict2string(tv, copyID, restore_copyID);
6158 if (restore_copyID)
6159 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006160 r = *tofree;
6161 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006162 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006163
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006164 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006165 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006166 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006167 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006168 break;
6169
Bram Moolenaar835dc632016-02-07 14:27:38 +01006170 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006171 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006172 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006173 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006174 if (composite_val)
6175 {
6176 *tofree = string_quote(r, FALSE);
6177 r = *tofree;
6178 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006179 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006180
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006181 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006182#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006183 *tofree = NULL;
6184 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6185 r = numbuf;
6186 break;
6187#endif
6188
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006189 case VAR_SPECIAL:
6190 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006191 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006192 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006193 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006194
Bram Moolenaar8502c702014-06-17 12:51:16 +02006195 if (--recurse == 0)
6196 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006197 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006198}
6199
6200/*
6201 * Return a string with the string representation of a variable.
6202 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6203 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006204 * Does not put quotes around strings, as ":echo" displays values.
6205 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6206 * May return NULL.
6207 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006208 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006209echo_string(
6210 typval_T *tv,
6211 char_u **tofree,
6212 char_u *numbuf,
6213 int copyID)
6214{
6215 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6216}
6217
6218/*
6219 * Return a string with the string representation of a variable.
6220 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6221 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006222 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006223 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006224 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02006225 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006226tv2string(
6227 typval_T *tv,
6228 char_u **tofree,
6229 char_u *numbuf,
6230 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006231{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006232 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006233}
6234
6235/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006236 * Return string "str" in ' quotes, doubling ' characters.
6237 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006238 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006239 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006240 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006241string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006242{
Bram Moolenaar33570922005-01-25 22:26:29 +00006243 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006244 char_u *p, *r, *s;
6245
Bram Moolenaar33570922005-01-25 22:26:29 +00006246 len = (function ? 13 : 3);
6247 if (str != NULL)
6248 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006249 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006250 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00006251 if (*p == '\'')
6252 ++len;
6253 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006254 s = r = alloc(len);
6255 if (r != NULL)
6256 {
6257 if (function)
6258 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006259 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006260 r += 10;
6261 }
6262 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006263 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006264 if (str != NULL)
6265 for (p = str; *p != NUL; )
6266 {
6267 if (*p == '\'')
6268 *r++ = '\'';
6269 MB_COPY_CHAR(p, r);
6270 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006271 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006272 if (function)
6273 *r++ = ')';
6274 *r++ = NUL;
6275 }
6276 return s;
6277}
6278
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006279#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006280/*
6281 * Convert the string "text" to a floating point number.
6282 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
6283 * this always uses a decimal point.
6284 * Returns the length of the text that was consumed.
6285 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006286 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006287string2float(
6288 char_u *text,
6289 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006290{
6291 char *s = (char *)text;
6292 float_T f;
6293
Bram Moolenaar62473612017-01-08 19:25:40 +01006294 /* MS-Windows does not deal with "inf" and "nan" properly. */
6295 if (STRNICMP(text, "inf", 3) == 0)
6296 {
6297 *value = INFINITY;
6298 return 3;
6299 }
6300 if (STRNICMP(text, "-inf", 3) == 0)
6301 {
6302 *value = -INFINITY;
6303 return 4;
6304 }
6305 if (STRNICMP(text, "nan", 3) == 0)
6306 {
6307 *value = NAN;
6308 return 3;
6309 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006310 f = strtod(s, &s);
6311 *value = f;
6312 return (int)((char_u *)s - text);
6313}
6314#endif
6315
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006316/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 * Get the value of an environment variable.
6318 * "arg" is pointing to the '$'. It is advanced to after the name.
6319 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006320 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 */
6322 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006323get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324{
6325 char_u *string = NULL;
6326 int len;
6327 int cc;
6328 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006329 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330
6331 ++*arg;
6332 name = *arg;
6333 len = get_env_len(arg);
6334 if (evaluate)
6335 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006336 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006337 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006338
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006339 cc = name[len];
6340 name[len] = NUL;
6341 /* first try vim_getenv(), fast for normal environment vars */
6342 string = vim_getenv(name, &mustfree);
6343 if (string != NULL && *string != NUL)
6344 {
6345 if (!mustfree)
6346 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006348 else
6349 {
6350 if (mustfree)
6351 vim_free(string);
6352
6353 /* next try expanding things like $VIM and ${HOME} */
6354 string = expand_env_save(name - 1);
6355 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01006356 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006357 }
6358 name[len] = cc;
6359
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006360 rettv->v_type = VAR_STRING;
6361 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 }
6363
6364 return OK;
6365}
6366
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006367
6368
6369/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006371 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006373 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006374var2fpos(
6375 typval_T *varp,
6376 int dollar_lnum, /* TRUE when $ is last line */
6377 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006379 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006381 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382
Bram Moolenaara5525202006-03-02 22:52:09 +00006383 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006384 if (varp->v_type == VAR_LIST)
6385 {
6386 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006387 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006388 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006389 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006390
6391 l = varp->vval.v_list;
6392 if (l == NULL)
6393 return NULL;
6394
6395 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006396 pos.lnum = list_find_nr(l, 0L, &error);
6397 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006398 return NULL; /* invalid line number */
6399
6400 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006401 pos.col = list_find_nr(l, 1L, &error);
6402 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006403 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006404 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006405
6406 /* We accept "$" for the column number: last column. */
6407 li = list_find(l, 1L);
6408 if (li != NULL && li->li_tv.v_type == VAR_STRING
6409 && li->li_tv.vval.v_string != NULL
6410 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6411 pos.col = len + 1;
6412
Bram Moolenaara5525202006-03-02 22:52:09 +00006413 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006414 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006415 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006416 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006417
Bram Moolenaara5525202006-03-02 22:52:09 +00006418 /* Get the virtual offset. Defaults to zero. */
6419 pos.coladd = list_find_nr(l, 2L, &error);
6420 if (error)
6421 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006422
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006423 return &pos;
6424 }
6425
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006426 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006427 if (name == NULL)
6428 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006429 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006431 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6432 {
6433 if (VIsual_active)
6434 return &VIsual;
6435 return &curwin->w_cursor;
6436 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006437 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006439 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6441 return NULL;
6442 return pp;
6443 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006444
Bram Moolenaara5525202006-03-02 22:52:09 +00006445 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006446
Bram Moolenaar477933c2007-07-17 14:32:23 +00006447 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006448 {
6449 pos.col = 0;
6450 if (name[1] == '0') /* "w0": first visible line */
6451 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006452 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006453 /* In silent Ex mode topline is zero, but that's not a valid line
6454 * number; use one instead. */
6455 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006456 return &pos;
6457 }
6458 else if (name[1] == '$') /* "w$": last visible line */
6459 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006460 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006461 /* In silent Ex mode botline is zero, return zero then. */
6462 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006463 return &pos;
6464 }
6465 }
6466 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006468 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 {
6470 pos.lnum = curbuf->b_ml.ml_line_count;
6471 pos.col = 0;
6472 }
6473 else
6474 {
6475 pos.lnum = curwin->w_cursor.lnum;
6476 pos.col = (colnr_T)STRLEN(ml_get_curline());
6477 }
6478 return &pos;
6479 }
6480 return NULL;
6481}
6482
6483/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006484 * Convert list in "arg" into a position and optional file number.
6485 * When "fnump" is NULL there is no file number, only 3 items.
6486 * Note that the column is passed on as-is, the caller may want to decrement
6487 * it to use 1 for the first column.
6488 * Return FAIL when conversion is not possible, doesn't check the position for
6489 * validity.
6490 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006491 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006492list2fpos(
6493 typval_T *arg,
6494 pos_T *posp,
6495 int *fnump,
6496 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006497{
6498 list_T *l = arg->vval.v_list;
6499 long i = 0;
6500 long n;
6501
Bram Moolenaar493c1782014-05-28 14:34:46 +02006502 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6503 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006504 if (arg->v_type != VAR_LIST
6505 || l == NULL
6506 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006507 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006508 return FAIL;
6509
6510 if (fnump != NULL)
6511 {
6512 n = list_find_nr(l, i++, NULL); /* fnum */
6513 if (n < 0)
6514 return FAIL;
6515 if (n == 0)
6516 n = curbuf->b_fnum; /* current buffer */
6517 *fnump = n;
6518 }
6519
6520 n = list_find_nr(l, i++, NULL); /* lnum */
6521 if (n < 0)
6522 return FAIL;
6523 posp->lnum = n;
6524
6525 n = list_find_nr(l, i++, NULL); /* col */
6526 if (n < 0)
6527 return FAIL;
6528 posp->col = n;
6529
Bram Moolenaar493c1782014-05-28 14:34:46 +02006530 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006531 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006532 posp->coladd = 0;
6533 else
6534 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006535
Bram Moolenaar493c1782014-05-28 14:34:46 +02006536 if (curswantp != NULL)
6537 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6538
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006539 return OK;
6540}
6541
6542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 * Get the length of an environment variable name.
6544 * Advance "arg" to the first character after the name.
6545 * Return 0 for error.
6546 */
6547 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006548get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549{
6550 char_u *p;
6551 int len;
6552
6553 for (p = *arg; vim_isIDc(*p); ++p)
6554 ;
6555 if (p == *arg) /* no name found */
6556 return 0;
6557
6558 len = (int)(p - *arg);
6559 *arg = p;
6560 return len;
6561}
6562
6563/*
6564 * Get the length of the name of a function or internal variable.
6565 * "arg" is advanced to the first non-white character after the name.
6566 * Return 0 if something is wrong.
6567 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006568 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006569get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570{
6571 char_u *p;
6572 int len;
6573
6574 /* Find the end of the name. */
6575 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006576 {
6577 if (*p == ':')
6578 {
6579 /* "s:" is start of "s:var", but "n:" is not and can be used in
6580 * slice "[n:]". Also "xx:" is not a namespace. */
6581 len = (int)(p - *arg);
6582 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6583 || len > 1)
6584 break;
6585 }
6586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 if (p == *arg) /* no name found */
6588 return 0;
6589
6590 len = (int)(p - *arg);
6591 *arg = skipwhite(p);
6592
6593 return len;
6594}
6595
6596/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006597 * Get the length of the name of a variable or function.
6598 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006600 * Return -1 if curly braces expansion failed.
6601 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 * If the name contains 'magic' {}'s, expand them and return the
6603 * expanded name in an allocated string via 'alias' - caller must free.
6604 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006605 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006606get_name_len(
6607 char_u **arg,
6608 char_u **alias,
6609 int evaluate,
6610 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611{
6612 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 char_u *p;
6614 char_u *expr_start;
6615 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616
6617 *alias = NULL; /* default to no alias */
6618
6619 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6620 && (*arg)[2] == (int)KE_SNR)
6621 {
6622 /* hard coded <SNR>, already translated */
6623 *arg += 3;
6624 return get_id_len(arg) + 3;
6625 }
6626 len = eval_fname_script(*arg);
6627 if (len > 0)
6628 {
6629 /* literal "<SID>", "s:" or "<SNR>" */
6630 *arg += len;
6631 }
6632
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006634 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006636 p = find_name_end(*arg, &expr_start, &expr_end,
6637 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 if (expr_start != NULL)
6639 {
6640 char_u *temp_string;
6641
6642 if (!evaluate)
6643 {
6644 len += (int)(p - *arg);
6645 *arg = skipwhite(p);
6646 return len;
6647 }
6648
6649 /*
6650 * Include any <SID> etc in the expanded string:
6651 * Thus the -len here.
6652 */
6653 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6654 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006655 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 *alias = temp_string;
6657 *arg = skipwhite(p);
6658 return (int)STRLEN(temp_string);
6659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660
6661 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006662 // Only give an error when there is something, otherwise it will be
6663 // reported at a higher level.
6664 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006665 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666
6667 return len;
6668}
6669
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006670/*
6671 * Find the end of a variable or function name, taking care of magic braces.
6672 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6673 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006674 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006675 * Return a pointer to just after the name. Equal to "arg" if there is no
6676 * valid name.
6677 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006678 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006679find_name_end(
6680 char_u *arg,
6681 char_u **expr_start,
6682 char_u **expr_end,
6683 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006685 int mb_nest = 0;
6686 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006688 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006690 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006692 *expr_start = NULL;
6693 *expr_end = NULL;
6694 }
6695
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006696 /* Quick check for valid starting character. */
6697 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6698 return arg;
6699
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006700 for (p = arg; *p != NUL
6701 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006702 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006703 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006704 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006705 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006706 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006707 if (*p == '\'')
6708 {
6709 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006710 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006711 ;
6712 if (*p == NUL)
6713 break;
6714 }
6715 else if (*p == '"')
6716 {
6717 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006718 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006719 if (*p == '\\' && p[1] != NUL)
6720 ++p;
6721 if (*p == NUL)
6722 break;
6723 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006724 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6725 {
6726 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006727 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006728 len = (int)(p - arg);
6729 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006730 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006731 break;
6732 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006733
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006734 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006736 if (*p == '[')
6737 ++br_nest;
6738 else if (*p == ']')
6739 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006741
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006742 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006744 if (*p == '{')
6745 {
6746 mb_nest++;
6747 if (expr_start != NULL && *expr_start == NULL)
6748 *expr_start = p;
6749 }
6750 else if (*p == '}')
6751 {
6752 mb_nest--;
6753 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6754 *expr_end = p;
6755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 }
6758
6759 return p;
6760}
6761
6762/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006763 * Expands out the 'magic' {}'s in a variable/function name.
6764 * Note that this can call itself recursively, to deal with
6765 * constructs like foo{bar}{baz}{bam}
6766 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6767 * "in_start" ^
6768 * "expr_start" ^
6769 * "expr_end" ^
6770 * "in_end" ^
6771 *
6772 * Returns a new allocated string, which the caller must free.
6773 * Returns NULL for failure.
6774 */
6775 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006776make_expanded_name(
6777 char_u *in_start,
6778 char_u *expr_start,
6779 char_u *expr_end,
6780 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006781{
6782 char_u c1;
6783 char_u *retval = NULL;
6784 char_u *temp_result;
6785 char_u *nextcmd = NULL;
6786
6787 if (expr_end == NULL || in_end == NULL)
6788 return NULL;
6789 *expr_start = NUL;
6790 *expr_end = NUL;
6791 c1 = *in_end;
6792 *in_end = NUL;
6793
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006794 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006795 if (temp_result != NULL && nextcmd == NULL)
6796 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006797 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6798 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006799 if (retval != NULL)
6800 {
6801 STRCPY(retval, in_start);
6802 STRCAT(retval, temp_result);
6803 STRCAT(retval, expr_end + 1);
6804 }
6805 }
6806 vim_free(temp_result);
6807
6808 *in_end = c1; /* put char back for error messages */
6809 *expr_start = '{';
6810 *expr_end = '}';
6811
6812 if (retval != NULL)
6813 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006814 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006815 if (expr_start != NULL)
6816 {
6817 /* Further expansion! */
6818 temp_result = make_expanded_name(retval, expr_start,
6819 expr_end, temp_result);
6820 vim_free(retval);
6821 retval = temp_result;
6822 }
6823 }
6824
6825 return retval;
6826}
6827
6828/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006830 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006832 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006833eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006835 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6836}
6837
6838/*
6839 * Return TRUE if character "c" can be used as the first character in a
6840 * variable or function name (excluding '{' and '}').
6841 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006842 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006843eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006844{
6845 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846}
6847
6848/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 * Set number v: variable to "val".
6850 */
6851 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006852set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006854 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855}
6856
6857/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006858 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006860 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006861get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006863 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864}
6865
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006866/*
6867 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01006868 * If the String variable has never been set, return an empty string.
6869 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006870 */
6871 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006872get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006873{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006874 return tv_get_string(&vimvars[idx].vv_tv);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006875}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006876
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006878 * Get List v: variable value. Caller must take care of reference count when
6879 * needed.
6880 */
6881 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006882get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006883{
6884 return vimvars[idx].vv_list;
6885}
6886
6887/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006888 * Get Dict v: variable value. Caller must take care of reference count when
6889 * needed.
6890 */
6891 dict_T *
6892get_vim_var_dict(int idx)
6893{
6894 return vimvars[idx].vv_dict;
6895}
6896
6897/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006898 * Set v:char to character "c".
6899 */
6900 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006901set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006902{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006903 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006904
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006905 if (has_mbyte)
6906 buf[(*mb_char2bytes)(c, buf)] = NUL;
6907 else
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006908 {
6909 buf[0] = c;
6910 buf[1] = NUL;
6911 }
6912 set_vim_var_string(VV_CHAR, buf, -1);
6913}
6914
6915/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006916 * Set v:count to "count" and v:count1 to "count1".
6917 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 */
6919 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006920set_vcount(
6921 long count,
6922 long count1,
6923 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006925 if (set_prevcount)
6926 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006927 vimvars[VV_COUNT].vv_nr = count;
6928 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929}
6930
6931/*
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02006932 * Save variables that might be changed as a side effect. Used when executing
6933 * a timer callback.
6934 */
6935 void
6936save_vimvars(vimvars_save_T *vvsave)
6937{
6938 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
6939 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
6940 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
6941}
6942
6943/*
6944 * Restore variables saved by save_vimvars().
6945 */
6946 void
6947restore_vimvars(vimvars_save_T *vvsave)
6948{
6949 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
6950 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
6951 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
6952}
6953
6954/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 * Set string v: variable to a copy of "val".
6956 */
6957 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006958set_vim_var_string(
6959 int idx,
6960 char_u *val,
6961 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962{
Bram Moolenaara542c682016-01-31 16:28:04 +01006963 clear_tv(&vimvars[idx].vv_di.di_tv);
6964 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006966 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006968 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006970 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971}
6972
6973/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006974 * Set List v: variable to "val".
6975 */
6976 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006977set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006978{
Bram Moolenaara542c682016-01-31 16:28:04 +01006979 clear_tv(&vimvars[idx].vv_di.di_tv);
6980 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006981 vimvars[idx].vv_list = val;
6982 if (val != NULL)
6983 ++val->lv_refcount;
6984}
6985
6986/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006987 * Set Dictionary v: variable to "val".
6988 */
6989 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006990set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006991{
Bram Moolenaara542c682016-01-31 16:28:04 +01006992 clear_tv(&vimvars[idx].vv_di.di_tv);
6993 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006994 vimvars[idx].vv_dict = val;
6995 if (val != NULL)
6996 {
6997 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006998 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02006999 }
7000}
7001
7002/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 * Set v:register if needed.
7004 */
7005 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007006set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007{
7008 char_u regname;
7009
7010 if (c == 0 || c == ' ')
7011 regname = '"';
7012 else
7013 regname = c;
7014 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007015 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 set_vim_var_string(VV_REG, &regname, 1);
7017}
7018
7019/*
7020 * Get or set v:exception. If "oldval" == NULL, return the current value.
7021 * Otherwise, restore the value to "oldval" and return NULL.
7022 * Must always be called in pairs to save and restore v:exception! Does not
7023 * take care of memory allocations.
7024 */
7025 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007026v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027{
7028 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007029 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030
Bram Moolenaare9a41262005-01-15 22:18:47 +00007031 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 return NULL;
7033}
7034
7035/*
7036 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
7037 * Otherwise, restore the value to "oldval" and return NULL.
7038 * Must always be called in pairs to save and restore v:throwpoint! Does not
7039 * take care of memory allocations.
7040 */
7041 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007042v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043{
7044 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007045 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046
Bram Moolenaare9a41262005-01-15 22:18:47 +00007047 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 return NULL;
7049}
7050
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051/*
7052 * Set v:cmdarg.
7053 * If "eap" != NULL, use "eap" to generate the value and return the old value.
7054 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
7055 * Must always be called in pairs!
7056 */
7057 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007058set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059{
7060 char_u *oldval;
7061 char_u *newval;
7062 unsigned len;
7063
Bram Moolenaare9a41262005-01-15 22:18:47 +00007064 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007065 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007067 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007068 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007069 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 }
7071
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007072 if (eap->force_bin == FORCE_BIN)
7073 len = 6;
7074 else if (eap->force_bin == FORCE_NOBIN)
7075 len = 8;
7076 else
7077 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007078
7079 if (eap->read_edit)
7080 len += 7;
7081
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007082 if (eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02007083 len += 10; /* " ++ff=unix" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007084 if (eap->force_enc != 0)
7085 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02007086 if (eap->bad_char != 0)
7087 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007088
7089 newval = alloc(len + 1);
7090 if (newval == NULL)
7091 return NULL;
7092
7093 if (eap->force_bin == FORCE_BIN)
7094 sprintf((char *)newval, " ++bin");
7095 else if (eap->force_bin == FORCE_NOBIN)
7096 sprintf((char *)newval, " ++nobin");
7097 else
7098 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007099
7100 if (eap->read_edit)
7101 STRCAT(newval, " ++edit");
7102
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007103 if (eap->force_ff != 0)
7104 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
Bram Moolenaar333b80a2018-04-04 22:57:29 +02007105 eap->force_ff == 'u' ? "unix"
7106 : eap->force_ff == 'd' ? "dos"
7107 : "mac");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007108 if (eap->force_enc != 0)
7109 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
7110 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02007111 if (eap->bad_char == BAD_KEEP)
7112 STRCPY(newval + STRLEN(newval), " ++bad=keep");
7113 else if (eap->bad_char == BAD_DROP)
7114 STRCPY(newval + STRLEN(newval), " ++bad=drop");
7115 else if (eap->bad_char != 0)
7116 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007117 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007118 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120
7121/*
7122 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01007123 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007125 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007126get_var_tv(
7127 char_u *name,
7128 int len, /* length of "name" */
7129 typval_T *rettv, /* NULL when only checking existence */
7130 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
7131 int verbose, /* may give error message */
7132 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133{
7134 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007135 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007136 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138
7139 /* truncate the name, so that we can use strcmp() */
7140 cc = name[len];
7141 name[len] = NUL;
7142
7143 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 * Check for user-defined variables.
7145 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01007146 v = find_var(name, NULL, no_autoload);
7147 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007149 tv = &v->di_tv;
7150 if (dip != NULL)
7151 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 }
7153
Bram Moolenaare9a41262005-01-15 22:18:47 +00007154 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007156 if (rettv != NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007157 semsg(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 ret = FAIL;
7159 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007160 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007161 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162
7163 name[len] = cc;
7164
7165 return ret;
7166}
7167
7168/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007169 * Check if variable "name[len]" is a local variable or an argument.
7170 * If so, "*eval_lavars_used" is set to TRUE.
7171 */
7172 static void
7173check_vars(char_u *name, int len)
7174{
7175 int cc;
7176 char_u *varname;
7177 hashtab_T *ht;
7178
7179 if (eval_lavars_used == NULL)
7180 return;
7181
7182 /* truncate the name, so that we can use strcmp() */
7183 cc = name[len];
7184 name[len] = NUL;
7185
7186 ht = find_var_ht(name, &varname);
7187 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
7188 {
7189 if (find_var(name, NULL, TRUE) != NULL)
7190 *eval_lavars_used = TRUE;
7191 }
7192
7193 name[len] = cc;
7194}
7195
7196/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007197 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
7198 * Also handle function call with Funcref variable: func(expr)
7199 * Can all be combined: dict.func(expr)[idx]['func'](expr)
7200 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007201 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007202handle_subscript(
7203 char_u **arg,
7204 typval_T *rettv,
7205 int evaluate, /* do more than finding the end */
7206 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007207{
7208 int ret = OK;
7209 dict_T *selfdict = NULL;
7210 char_u *s;
7211 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007212 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007213
7214 while (ret == OK
7215 && (**arg == '['
7216 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007217 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7218 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01007219 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007220 {
7221 if (**arg == '(')
7222 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007223 partial_T *pt = NULL;
7224
Bram Moolenaard9fba312005-06-26 22:34:35 +00007225 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007226 if (evaluate)
7227 {
7228 functv = *rettv;
7229 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007230
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007231 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007232 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007233 {
7234 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007235 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007236 }
7237 else
7238 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007239 }
7240 else
7241 s = (char_u *)"";
Bram Moolenaar6ed88192019-05-11 18:37:44 +02007242 ret = get_func_tv(s, -1, rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00007243 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007244 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007245
7246 /* Clear the funcref afterwards, so that deleting it while
7247 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007248 if (evaluate)
7249 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007250
7251 /* Stop the expression evaluation when immediately aborting on
7252 * error, or when an interrupt occurred or an exception was thrown
7253 * but not caught. */
7254 if (aborting())
7255 {
7256 if (ret == OK)
7257 clear_tv(rettv);
7258 ret = FAIL;
7259 }
7260 dict_unref(selfdict);
7261 selfdict = NULL;
7262 }
7263 else /* **arg == '[' || **arg == '.' */
7264 {
7265 dict_unref(selfdict);
7266 if (rettv->v_type == VAR_DICT)
7267 {
7268 selfdict = rettv->vval.v_dict;
7269 if (selfdict != NULL)
7270 ++selfdict->dv_refcount;
7271 }
7272 else
7273 selfdict = NULL;
7274 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
7275 {
7276 clear_tv(rettv);
7277 ret = FAIL;
7278 }
7279 }
7280 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007281
Bram Moolenaar1d429612016-05-24 15:44:17 +02007282 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
7283 * Don't do this when "Func" is already a partial that was bound
7284 * explicitly (pt_auto is FALSE). */
7285 if (selfdict != NULL
7286 && (rettv->v_type == VAR_FUNC
7287 || (rettv->v_type == VAR_PARTIAL
7288 && (rettv->vval.v_partial->pt_auto
7289 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007290 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007291
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007292 dict_unref(selfdict);
7293 return ret;
7294}
7295
7296/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007297 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007298 * value).
7299 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01007300 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007301alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007302{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007303 return ALLOC_CLEAR_ONE(typval_T);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007304}
7305
7306/*
7307 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 * The string "s" must have been allocated, it is consumed.
7309 * Return NULL for out of memory, the variable otherwise.
7310 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007311 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007312alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313{
Bram Moolenaar33570922005-01-25 22:26:29 +00007314 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007316 rettv = alloc_tv();
7317 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007319 rettv->v_type = VAR_STRING;
7320 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 }
7322 else
7323 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007324 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325}
7326
7327/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007328 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007330 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007331free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332{
7333 if (varp != NULL)
7334 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007335 switch (varp->v_type)
7336 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007337 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007338 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007339 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007340 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007341 vim_free(varp->vval.v_string);
7342 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007343 case VAR_PARTIAL:
7344 partial_unref(varp->vval.v_partial);
7345 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007346 case VAR_BLOB:
7347 blob_unref(varp->vval.v_blob);
7348 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007349 case VAR_LIST:
7350 list_unref(varp->vval.v_list);
7351 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007352 case VAR_DICT:
7353 dict_unref(varp->vval.v_dict);
7354 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007355 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007356#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007357 job_unref(varp->vval.v_job);
7358 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007359#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007360 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007361#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007362 channel_unref(varp->vval.v_channel);
7363 break;
7364#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007365 case VAR_NUMBER:
7366 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007367 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007368 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007369 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 vim_free(varp);
7372 }
7373}
7374
7375/*
7376 * Free the memory for a variable value and set the value to NULL or 0.
7377 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007378 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007379clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380{
7381 if (varp != NULL)
7382 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007383 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007385 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007386 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007387 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007388 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007389 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007390 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007391 case VAR_PARTIAL:
7392 partial_unref(varp->vval.v_partial);
7393 varp->vval.v_partial = NULL;
7394 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007395 case VAR_BLOB:
7396 blob_unref(varp->vval.v_blob);
7397 varp->vval.v_blob = NULL;
7398 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007399 case VAR_LIST:
7400 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007401 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007402 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007403 case VAR_DICT:
7404 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007405 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007406 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007407 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007408 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007409 varp->vval.v_number = 0;
7410 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007411 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007412#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007413 varp->vval.v_float = 0.0;
7414 break;
7415#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007416 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007417#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007418 job_unref(varp->vval.v_job);
7419 varp->vval.v_job = NULL;
7420#endif
7421 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007422 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007423#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007424 channel_unref(varp->vval.v_channel);
7425 varp->vval.v_channel = NULL;
7426#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007427 case VAR_UNKNOWN:
7428 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007430 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 }
7432}
7433
7434/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007435 * Set the value of a variable to NULL without freeing items.
7436 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007437 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007438init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007439{
7440 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007441 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007442}
7443
7444/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 * Get the number value of a variable.
7446 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007447 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007448 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007449 * caller of incompatible types: it sets *denote to TRUE if "denote"
7450 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007452 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007453tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007455 int error = FALSE;
7456
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007457 return tv_get_number_chk(varp, &error); /* return 0L on error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007458}
7459
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007460 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007461tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007462{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007463 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007465 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007467 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007468 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007469 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007470#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007471 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007472 break;
7473#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007474 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007475 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007476 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007477 break;
7478 case VAR_STRING:
7479 if (varp->vval.v_string != NULL)
7480 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02007481 STR2NR_ALL, &n, NULL, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007482 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007483 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007484 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007485 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007486 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007487 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007488 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007489 case VAR_SPECIAL:
7490 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7491 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007492 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007493#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007494 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007495 break;
7496#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007497 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007498#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007499 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007500 break;
7501#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007502 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007503 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007504 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007505 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007506 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007507 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007509 if (denote == NULL) /* useful for values that must be unsigned */
7510 n = -1;
7511 else
7512 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007513 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514}
7515
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007516#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007517 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007518tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007519{
7520 switch (varp->v_type)
7521 {
7522 case VAR_NUMBER:
7523 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007524 case VAR_FLOAT:
7525 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007526 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007527 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007528 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007529 break;
7530 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007531 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007532 break;
7533 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007534 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007535 break;
7536 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007537 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007538 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007539 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007540 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007541 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007542 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007543# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007544 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007545 break;
7546# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007547 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007548# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007549 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007550 break;
7551# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007552 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007553 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007554 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007555 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007556 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007557 break;
7558 }
7559 return 0;
7560}
7561#endif
7562
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 * Get the string value of a variable.
7565 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007566 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7567 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 * If the String variable has never been set, return an empty string.
7569 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007570 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007571 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007573 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007574tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575{
7576 static char_u mybuf[NUMBUFLEN];
7577
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007578 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579}
7580
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007581 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007582tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007584 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007585
7586 return res != NULL ? res : (char_u *)"";
7587}
7588
Bram Moolenaar7d647822014-04-05 21:28:56 +02007589/*
7590 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7591 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007592 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007593tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007594{
7595 static char_u mybuf[NUMBUFLEN];
7596
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007597 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007598}
7599
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007600 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007601tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007602{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007603 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007605 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007606 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007607 (long_long_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007608 return buf;
7609 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007610 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007611 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007612 break;
7613 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007614 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007615 break;
7616 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007617 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007618 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007619 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007620#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007621 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007622 break;
7623#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007624 case VAR_STRING:
7625 if (varp->vval.v_string != NULL)
7626 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007627 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007628 case VAR_SPECIAL:
7629 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7630 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007631 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007632 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007633 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007634 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007635#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007636 {
7637 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007638 char *status;
7639
7640 if (job == NULL)
7641 return (char_u *)"no process";
7642 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007643 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007644 : "run";
7645# ifdef UNIX
7646 vim_snprintf((char *)buf, NUMBUFLEN,
7647 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01007648# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007649 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007650 "process %ld %s",
7651 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007652 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007653# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007654 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007655 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7656# endif
7657 return buf;
7658 }
7659#endif
7660 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007661 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007662#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007663 {
7664 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007665 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007666
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007667 if (channel == NULL)
7668 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7669 else
7670 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007671 "channel %d %s", channel->ch_id, status);
7672 return buf;
7673 }
7674#endif
7675 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007676 case VAR_UNKNOWN:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007677 emsg(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007678 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007680 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681}
7682
7683/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007684 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
7685 * string() on Dict, List, etc.
7686 */
7687 char_u *
7688tv_stringify(typval_T *varp, char_u *buf)
7689{
7690 if (varp->v_type == VAR_LIST
7691 || varp->v_type == VAR_DICT
7692 || varp->v_type == VAR_FUNC
7693 || varp->v_type == VAR_PARTIAL
7694 || varp->v_type == VAR_FLOAT)
7695 {
7696 typval_T tmp;
7697
7698 f_string(varp, &tmp);
7699 tv_get_string_buf(&tmp, buf);
7700 clear_tv(varp);
7701 *varp = tmp;
7702 return tmp.vval.v_string;
7703 }
7704 return tv_get_string_buf(varp, buf);
7705}
7706
7707/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 * Find variable "name" in the list of variables.
7709 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007710 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007711 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007712 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007714 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007715find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007718 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007719 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720
Bram Moolenaara7043832005-01-21 11:56:39 +00007721 ht = find_var_ht(name, &varname);
7722 if (htp != NULL)
7723 *htp = ht;
7724 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007726 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7727 if (ret != NULL)
7728 return ret;
7729
7730 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007731 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732}
7733
7734/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007735 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007736 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007738 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007739find_var_in_ht(
7740 hashtab_T *ht,
7741 int htname,
7742 char_u *varname,
7743 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007744{
Bram Moolenaar33570922005-01-25 22:26:29 +00007745 hashitem_T *hi;
7746
7747 if (*varname == NUL)
7748 {
7749 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007750 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007751 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007752 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007753 case 'g': return &globvars_var;
7754 case 'v': return &vimvars_var;
7755 case 'b': return &curbuf->b_bufvar;
7756 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007757 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007758 case 'l': return get_funccal_local_var();
7759 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007760 }
7761 return NULL;
7762 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007763
7764 hi = hash_find(ht, varname);
7765 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007766 {
7767 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007768 * worked find the variable again. Don't auto-load a script if it was
7769 * loaded already, otherwise it would be loaded every time when
7770 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007771 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007772 {
7773 /* Note: script_autoload() may make "hi" invalid. It must either
7774 * be obtained again or not used. */
7775 if (!script_autoload(varname, FALSE) || aborting())
7776 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007777 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007778 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007779 if (HASHITEM_EMPTY(hi))
7780 return NULL;
7781 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007782 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007783}
7784
7785/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007786 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007787 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007788 * Set "varname" to the start of name without ':'.
7789 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007790 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007791find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007793 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007794 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007795
Bram Moolenaar73627d02015-08-11 15:46:09 +02007796 if (name[0] == NUL)
7797 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 if (name[1] != ':')
7799 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007800 /* The name must not start with a colon or #. */
7801 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 return NULL;
7803 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007804
Bram Moolenaard2e716e2019-04-20 14:39:52 +02007805 // "version" is "v:version" in all scopes if scriptversion < 3.
7806 // Same for a few other variables marked with VV_COMPAT.
7807 if (current_sctx.sc_version < 3)
7808 {
7809 hi = hash_find(&compat_hashtab, name);
7810 if (!HASHITEM_EMPTY(hi))
7811 return &compat_hashtab;
7812 }
Bram Moolenaar532c7802005-01-27 14:44:31 +00007813
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007814 ht = get_funccal_local_ht();
7815 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007816 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007817 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 }
7819 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007820 if (*name == 'g') /* global variable */
7821 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007822 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7823 */
7824 if (vim_strchr(name + 2, ':') != NULL
7825 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007826 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007828 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007830 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007831 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007832 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007833 if (*name == 'v') /* v: variable */
7834 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007835 if (*name == 'a') /* a: function argument */
7836 return get_funccal_args_ht();
7837 if (*name == 'l') /* l: local function variable */
7838 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 if (*name == 's' /* script variable */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007840 && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
7841 return &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 return NULL;
7843}
7844
7845/*
7846 * Get the string value of a (global/local) variable.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007847 * Note: see tv_get_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 * Returns NULL when it doesn't exist.
7849 */
7850 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007851get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852{
Bram Moolenaar33570922005-01-25 22:26:29 +00007853 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007855 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 if (v == NULL)
7857 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007858 return tv_get_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859}
7860
7861/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007862 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 * sourcing this script and when executing functions defined in the script.
7864 */
7865 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007866new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867{
Bram Moolenaara7043832005-01-21 11:56:39 +00007868 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007869 hashtab_T *ht;
7870 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007871
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7873 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007874 /* Re-allocating ga_data means that an ht_array pointing to
7875 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007876 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007877 for (i = 1; i <= ga_scripts.ga_len; ++i)
7878 {
7879 ht = &SCRIPT_VARS(i);
7880 if (ht->ht_mask == HT_INIT_SIZE - 1)
7881 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007882 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007883 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007884 }
7885
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 while (ga_scripts.ga_len < id)
7887 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007888 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007889 ALLOC_CLEAR_ONE(scriptvar_T);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007890 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 }
7893 }
7894}
7895
7896/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007897 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7898 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 */
7900 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007901init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902{
Bram Moolenaar33570922005-01-25 22:26:29 +00007903 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007904 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007905 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007906 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007907 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007908 dict_var->di_tv.vval.v_dict = dict;
7909 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007910 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007911 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7912 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913}
7914
7915/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007916 * Unreference a dictionary initialized by init_var_dict().
7917 */
7918 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007919unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007920{
7921 /* Now the dict needs to be freed if no one else is using it, go back to
7922 * normal reference counting. */
7923 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7924 dict_unref(dict);
7925}
7926
7927/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007929 * Frees all allocated variables and the value they contain.
7930 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 */
7932 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007933vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007934{
7935 vars_clear_ext(ht, TRUE);
7936}
7937
7938/*
7939 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7940 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007941 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007942vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943{
Bram Moolenaara7043832005-01-21 11:56:39 +00007944 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007945 hashitem_T *hi;
7946 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947
Bram Moolenaar33570922005-01-25 22:26:29 +00007948 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007949 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007950 for (hi = ht->ht_array; todo > 0; ++hi)
7951 {
7952 if (!HASHITEM_EMPTY(hi))
7953 {
7954 --todo;
7955
Bram Moolenaar33570922005-01-25 22:26:29 +00007956 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007957 * ht_array might change then. hash_clear() takes care of it
7958 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007959 v = HI2DI(hi);
7960 if (free_val)
7961 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007962 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007963 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007964 }
7965 }
7966 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007967 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968}
7969
Bram Moolenaara7043832005-01-21 11:56:39 +00007970/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007971 * Delete a variable from hashtab "ht" at item "hi".
7972 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007973 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007975delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976{
Bram Moolenaar33570922005-01-25 22:26:29 +00007977 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007978
7979 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007980 clear_tv(&di->di_tv);
7981 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982}
7983
7984/*
7985 * List the value of one internal variable.
7986 */
7987 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01007988list_one_var(dictitem_T *v, char *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007990 char_u *tofree;
7991 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007992 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007993
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007994 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007995 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007996 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007997 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998}
7999
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008001list_one_var_a(
Bram Moolenaar32526b32019-01-19 17:43:09 +01008002 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01008003 char_u *name,
8004 int type,
8005 char_u *string,
8006 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007{
Bram Moolenaar31859182007-08-14 20:41:13 +00008008 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
8009 msg_start();
8010 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 if (name != NULL) /* "a:" vars don't have a name stored */
Bram Moolenaar32526b32019-01-19 17:43:09 +01008012 msg_puts((char *)name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 msg_putchar(' ');
8014 msg_advance(22);
8015 if (type == VAR_NUMBER)
8016 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008017 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008018 msg_putchar('*');
8019 else if (type == VAR_LIST)
8020 {
8021 msg_putchar('[');
8022 if (*string == '[')
8023 ++string;
8024 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008025 else if (type == VAR_DICT)
8026 {
8027 msg_putchar('{');
8028 if (*string == '{')
8029 ++string;
8030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 else
8032 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008033
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008035
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008036 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008037 msg_puts("()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00008038 if (*first)
8039 {
8040 msg_clr_eos();
8041 *first = FALSE;
8042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043}
8044
8045/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008046 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 * If the variable already exists, the value is updated.
8048 * Otherwise the variable is created.
8049 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008050 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008051set_var(
8052 char_u *name,
8053 typval_T *tv,
8054 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055{
Bram Moolenaar33570922005-01-25 22:26:29 +00008056 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008058 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008060 ht = find_var_ht(name, &varname);
8061 if (ht == NULL || *varname == NUL)
8062 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008063 semsg(_(e_illvar), name);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008064 return;
8065 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02008066 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008067
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008068 /* Search in parent scope which is possible to reference from lambda */
8069 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02008070 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008071
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008072 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
8073 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008074 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008075
Bram Moolenaar33570922005-01-25 22:26:29 +00008076 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008078 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02008079 if (var_check_ro(v->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008080 || var_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00008081 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008082
8083 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008084 * Handle setting internal v: variables separately where needed to
8085 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00008086 */
8087 if (ht == &vimvarht)
8088 {
8089 if (v->di_tv.v_type == VAR_STRING)
8090 {
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01008091 VIM_CLEAR(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008092 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01008093 {
8094 char_u *val = tv_get_string(tv);
8095
8096 // Careful: when assigning to v:errmsg and tv_get_string()
8097 // causes an error message the variable will alrady be set.
8098 if (v->di_tv.vval.v_string == NULL)
8099 v->di_tv.vval.v_string = vim_strsave(val);
8100 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008101 else
8102 {
8103 /* Take over the string to avoid an extra alloc/free. */
8104 v->di_tv.vval.v_string = tv->vval.v_string;
8105 tv->vval.v_string = NULL;
8106 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008107 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008108 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008109 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008110 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008111 v->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008112 if (STRCMP(varname, "searchforward") == 0)
8113 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01008114#ifdef FEAT_SEARCH_EXTRA
8115 else if (STRCMP(varname, "hlsearch") == 0)
8116 {
8117 no_hlsearch = !v->di_tv.vval.v_number;
8118 redraw_all_later(SOME_VALID);
8119 }
8120#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008121 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008122 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008123 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01008124 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008125 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01008126 return;
8127 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008128 }
8129
8130 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 }
8132 else /* add a new variable */
8133 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01008134 // Can't add "v:" or "a:" variable.
8135 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00008136 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008137 semsg(_(e_illvar), name);
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00008138 return;
8139 }
8140
Bram Moolenaar31b81602019-02-10 22:14:27 +01008141 // Make sure the variable name is valid.
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008142 if (!valid_varname(varname))
8143 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00008144
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008145 v = alloc(sizeof(dictitem_T) + STRLEN(varname));
Bram Moolenaara7043832005-01-21 11:56:39 +00008146 if (v == NULL)
8147 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008148 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00008149 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008151 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 return;
8153 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02008154 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 }
Bram Moolenaara7043832005-01-21 11:56:39 +00008156
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008157 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00008158 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00008159 else
8160 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008161 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008162 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008163 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00008164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165}
8166
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008167/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008168 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00008169 * Also give an error message.
8170 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008171 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008172var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00008173{
8174 if (flags & DI_FLAGS_RO)
8175 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008176 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008177 return TRUE;
8178 }
8179 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
8180 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008181 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008182 return TRUE;
8183 }
8184 return FALSE;
8185}
8186
8187/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008188 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
8189 * Also give an error message.
8190 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008191 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008192var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008193{
8194 if (flags & DI_FLAGS_FIX)
8195 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008196 semsg(_("E795: Cannot delete variable %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008197 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008198 return TRUE;
8199 }
8200 return FALSE;
8201}
8202
8203/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008204 * Check if a funcref is assigned to a valid variable name.
8205 * Return TRUE and give an error if not.
8206 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008207 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008208var_check_func_name(
8209 char_u *name, /* points to start of variable name */
8210 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008211{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02008212 /* Allow for w: b: s: and t:. */
8213 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008214 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
8215 ? name[2] : name[0]))
8216 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008217 semsg(_("E704: Funcref variable name must start with a capital: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008218 name);
8219 return TRUE;
8220 }
8221 /* Don't allow hiding a function. When "v" is not NULL we might be
8222 * assigning another function to the same var, the type is checked
8223 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02008224 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008225 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008226 semsg(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008227 name);
8228 return TRUE;
8229 }
8230 return FALSE;
8231}
8232
8233/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008234 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02008235 * Also give an error message, using "name" or _("name") when use_gettext is
8236 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008237 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008238 int
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008239var_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008240{
8241 if (lock & VAR_LOCKED)
8242 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008243 semsg(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008244 name == NULL ? (char_u *)_("Unknown")
8245 : use_gettext ? (char_u *)_(name)
8246 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008247 return TRUE;
8248 }
8249 if (lock & VAR_FIXED)
8250 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008251 semsg(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008252 name == NULL ? (char_u *)_("Unknown")
8253 : use_gettext ? (char_u *)_(name)
8254 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008255 return TRUE;
8256 }
8257 return FALSE;
8258}
8259
8260/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008261 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
8262 * Also give an error message, using "name" or _("name") when use_gettext is
8263 * TRUE.
8264 */
8265 static int
8266tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
8267{
8268 int lock = 0;
8269
8270 switch (tv->v_type)
8271 {
8272 case VAR_BLOB:
8273 if (tv->vval.v_blob != NULL)
8274 lock = tv->vval.v_blob->bv_lock;
8275 break;
8276 case VAR_LIST:
8277 if (tv->vval.v_list != NULL)
8278 lock = tv->vval.v_list->lv_lock;
8279 break;
8280 case VAR_DICT:
8281 if (tv->vval.v_dict != NULL)
8282 lock = tv->vval.v_dict->dv_lock;
8283 break;
8284 default:
8285 break;
8286 }
8287 return var_check_lock(tv->v_lock, name, use_gettext)
8288 || (lock != 0 && var_check_lock(lock, name, use_gettext));
8289}
8290
8291/*
8292 * Check if a variable name is valid.
8293 * Return FALSE and give an error if not.
8294 */
8295 int
8296valid_varname(char_u *varname)
8297{
8298 char_u *p;
8299
8300 for (p = varname; *p != NUL; ++p)
8301 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
8302 && *p != AUTOLOAD_CHAR)
8303 {
8304 semsg(_(e_illvar), varname);
8305 return FALSE;
8306 }
8307 return TRUE;
8308}
8309
8310/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008311 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008312 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008313 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008314 * It is OK for "from" and "to" to point to the same item. This is used to
8315 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008316 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008317 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008318copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008320 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008321 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008322 switch (from->v_type)
8323 {
8324 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008325 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008326 to->vval.v_number = from->vval.v_number;
8327 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008328 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008329#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008330 to->vval.v_float = from->vval.v_float;
8331 break;
8332#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008333 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008334#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01008335 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01008336 if (to->vval.v_job != NULL)
8337 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01008338 break;
8339#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01008340 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008341#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01008342 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01008343 if (to->vval.v_channel != NULL)
8344 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01008345 break;
8346#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008347 case VAR_STRING:
8348 case VAR_FUNC:
8349 if (from->vval.v_string == NULL)
8350 to->vval.v_string = NULL;
8351 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008352 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008353 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008354 if (from->v_type == VAR_FUNC)
8355 func_ref(to->vval.v_string);
8356 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008357 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008358 case VAR_PARTIAL:
8359 if (from->vval.v_partial == NULL)
8360 to->vval.v_partial = NULL;
8361 else
8362 {
8363 to->vval.v_partial = from->vval.v_partial;
8364 ++to->vval.v_partial->pt_refcount;
8365 }
8366 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008367 case VAR_BLOB:
8368 if (from->vval.v_blob == NULL)
8369 to->vval.v_blob = NULL;
8370 else
8371 {
8372 to->vval.v_blob = from->vval.v_blob;
8373 ++to->vval.v_blob->bv_refcount;
8374 }
8375 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008376 case VAR_LIST:
8377 if (from->vval.v_list == NULL)
8378 to->vval.v_list = NULL;
8379 else
8380 {
8381 to->vval.v_list = from->vval.v_list;
8382 ++to->vval.v_list->lv_refcount;
8383 }
8384 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008385 case VAR_DICT:
8386 if (from->vval.v_dict == NULL)
8387 to->vval.v_dict = NULL;
8388 else
8389 {
8390 to->vval.v_dict = from->vval.v_dict;
8391 ++to->vval.v_dict->dv_refcount;
8392 }
8393 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008394 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008395 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008396 break;
8397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398}
8399
8400/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008401 * Make a copy of an item.
8402 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008403 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
8404 * reference to an already copied list/dict can be used.
8405 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008406 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008407 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008408item_copy(
8409 typval_T *from,
8410 typval_T *to,
8411 int deep,
8412 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008413{
8414 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008415 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008416
Bram Moolenaar33570922005-01-25 22:26:29 +00008417 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008418 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008419 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008420 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008421 }
8422 ++recurse;
8423
8424 switch (from->v_type)
8425 {
8426 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008427 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008428 case VAR_STRING:
8429 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008430 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008431 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008432 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008433 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008434 copy_tv(from, to);
8435 break;
8436 case VAR_LIST:
8437 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008438 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008439 if (from->vval.v_list == NULL)
8440 to->vval.v_list = NULL;
8441 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8442 {
8443 /* use the copy made earlier */
8444 to->vval.v_list = from->vval.v_list->lv_copylist;
8445 ++to->vval.v_list->lv_refcount;
8446 }
8447 else
8448 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8449 if (to->vval.v_list == NULL)
8450 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008451 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008452 case VAR_BLOB:
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008453 ret = blob_copy(from, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008454 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008455 case VAR_DICT:
8456 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008457 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008458 if (from->vval.v_dict == NULL)
8459 to->vval.v_dict = NULL;
8460 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8461 {
8462 /* use the copy made earlier */
8463 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8464 ++to->vval.v_dict->dv_refcount;
8465 }
8466 else
8467 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8468 if (to->vval.v_dict == NULL)
8469 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008470 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008471 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008472 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008473 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008474 }
8475 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008476 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008477}
8478
8479/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008480 * This function is used by f_input() and f_inputdialog() functions. The third
8481 * argument to f_input() specifies the type of completion to use at the
8482 * prompt. The third argument to f_inputdialog() specifies the value to return
8483 * when the user cancels the prompt.
8484 */
8485 void
8486get_user_input(
8487 typval_T *argvars,
8488 typval_T *rettv,
8489 int inputdialog,
8490 int secret)
8491{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008492 char_u *prompt = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008493 char_u *p = NULL;
8494 int c;
8495 char_u buf[NUMBUFLEN];
8496 int cmd_silent_save = cmd_silent;
8497 char_u *defstr = (char_u *)"";
8498 int xp_type = EXPAND_NOTHING;
8499 char_u *xp_arg = NULL;
8500
8501 rettv->v_type = VAR_STRING;
8502 rettv->vval.v_string = NULL;
8503
8504#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008505 /* While starting up, there is no place to enter text. When running tests
8506 * with --not-a-term we assume feedkeys() will be used. */
8507 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008508 return;
8509#endif
8510
8511 cmd_silent = FALSE; /* Want to see the prompt. */
8512 if (prompt != NULL)
8513 {
8514 /* Only the part of the message after the last NL is considered as
8515 * prompt for the command line */
8516 p = vim_strrchr(prompt, '\n');
8517 if (p == NULL)
8518 p = prompt;
8519 else
8520 {
8521 ++p;
8522 c = *p;
8523 *p = NUL;
8524 msg_start();
8525 msg_clr_eos();
Bram Moolenaar32526b32019-01-19 17:43:09 +01008526 msg_puts_attr((char *)prompt, echo_attr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008527 msg_didout = FALSE;
8528 msg_starthere();
8529 *p = c;
8530 }
8531 cmdline_row = msg_row;
8532
8533 if (argvars[1].v_type != VAR_UNKNOWN)
8534 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008535 defstr = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008536 if (defstr != NULL)
8537 stuffReadbuffSpec(defstr);
8538
8539 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8540 {
8541 char_u *xp_name;
8542 int xp_namelen;
8543 long argt;
8544
8545 /* input() with a third argument: completion */
8546 rettv->vval.v_string = NULL;
8547
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008548 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008549 if (xp_name == NULL)
8550 return;
8551
8552 xp_namelen = (int)STRLEN(xp_name);
8553
8554 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8555 &xp_arg) == FAIL)
8556 return;
8557 }
8558 }
8559
8560 if (defstr != NULL)
8561 {
8562 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02008563
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008564 ex_normal_busy = 0;
8565 rettv->vval.v_string =
8566 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8567 xp_type, xp_arg);
8568 ex_normal_busy = save_ex_normal_busy;
8569 }
8570 if (inputdialog && rettv->vval.v_string == NULL
8571 && argvars[1].v_type != VAR_UNKNOWN
8572 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008573 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008574 &argvars[2], buf));
8575
8576 vim_free(xp_arg);
8577
8578 /* since the user typed this, no need to wait for return */
8579 need_wait_return = FALSE;
8580 msg_didout = FALSE;
8581 }
8582 cmd_silent = cmd_silent_save;
8583}
8584
8585/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 * ":echo expr1 ..." print each argument separated with a space, add a
8587 * newline at the end.
8588 * ":echon expr1 ..." print each argument plain.
8589 */
8590 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008591ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592{
8593 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008594 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008595 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 char_u *p;
8597 int needclr = TRUE;
8598 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008599 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01008600 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008601 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602
8603 if (eap->skip)
8604 ++emsg_skip;
8605 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8606 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008607 /* If eval1() causes an error message the text from the command may
8608 * still need to be cleared. E.g., "echo 22,44". */
8609 need_clr_eos = needclr;
8610
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008612 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 {
8614 /*
8615 * Report the invalid expression unless the expression evaluation
8616 * has been cancelled due to an aborting error, an interrupt, or an
8617 * exception.
8618 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008619 if (!aborting() && did_emsg == did_emsg_before
8620 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008621 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008622 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 break;
8624 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008625 need_clr_eos = FALSE;
8626
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 if (!eap->skip)
8628 {
8629 if (atstart)
8630 {
8631 atstart = FALSE;
8632 /* Call msg_start() after eval1(), evaluating the expression
8633 * may cause a message to appear. */
8634 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008635 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008636 /* Mark the saved text as finishing the line, so that what
8637 * follows is displayed on a new line when scrolling back
8638 * at the more prompt. */
8639 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 }
8643 else if (eap->cmdidx == CMD_echo)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008644 msg_puts_attr(" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008645 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008646 if (p != NULL)
8647 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008649 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008651 if (*p != TAB && needclr)
8652 {
8653 /* remove any text still there from the command */
8654 msg_clr_eos();
8655 needclr = FALSE;
8656 }
8657 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658 }
8659 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008660 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008661 if (has_mbyte)
8662 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008663 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008664
8665 (void)msg_outtrans_len_attr(p, i, echo_attr);
8666 p += i - 1;
8667 }
8668 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008669 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008672 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008674 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 arg = skipwhite(arg);
8676 }
8677 eap->nextcmd = check_nextcmd(arg);
8678
8679 if (eap->skip)
8680 --emsg_skip;
8681 else
8682 {
8683 /* remove text that may still be there from the command */
8684 if (needclr)
8685 msg_clr_eos();
8686 if (eap->cmdidx == CMD_echo)
8687 msg_end();
8688 }
8689}
8690
8691/*
8692 * ":echohl {name}".
8693 */
8694 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008695ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008697 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698}
8699
8700/*
8701 * ":execute expr1 ..." execute the result of an expression.
8702 * ":echomsg expr1 ..." Print a message
8703 * ":echoerr expr1 ..." Print an error
8704 * Each gets spaces around each argument and a newline at the end for
8705 * echo commands
8706 */
8707 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008708ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709{
8710 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008711 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 int ret = OK;
8713 char_u *p;
8714 garray_T ga;
8715 int len;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01008716 int save_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717
8718 ga_init2(&ga, 1, 80);
8719
8720 if (eap->skip)
8721 ++emsg_skip;
8722 while (*arg != NUL && *arg != '|' && *arg != '\n')
8723 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01008724 ret = eval1_emsg(&arg, &rettv, !eap->skip);
8725 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727
8728 if (!eap->skip)
8729 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008730 char_u buf[NUMBUFLEN];
8731
8732 if (eap->cmdidx == CMD_execute)
8733 p = tv_get_string_buf(&rettv, buf);
8734 else
8735 p = tv_stringify(&rettv, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 len = (int)STRLEN(p);
8737 if (ga_grow(&ga, len + 2) == FAIL)
8738 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008739 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 ret = FAIL;
8741 break;
8742 }
8743 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 ga.ga_len += len;
8747 }
8748
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008749 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 arg = skipwhite(arg);
8751 }
8752
8753 if (ret != FAIL && ga.ga_data != NULL)
8754 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008755 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8756 {
8757 /* Mark the already saved text as finishing the line, so that what
8758 * follows is displayed on a new line when scrolling back at the
8759 * more prompt. */
8760 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008761 }
8762
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008764 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01008765 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008766 out_flush();
8767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 else if (eap->cmdidx == CMD_echoerr)
8769 {
8770 /* We don't want to abort following commands, restore did_emsg. */
8771 save_did_emsg = did_emsg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008772 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 if (!force_abort)
8774 did_emsg = save_did_emsg;
8775 }
8776 else if (eap->cmdidx == CMD_execute)
8777 do_cmdline((char_u *)ga.ga_data,
8778 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8779 }
8780
8781 ga_clear(&ga);
8782
8783 if (eap->skip)
8784 --emsg_skip;
8785
8786 eap->nextcmd = check_nextcmd(arg);
8787}
8788
8789/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008790 * Find window specified by "vp" in tabpage "tp".
8791 */
8792 win_T *
8793find_win_by_nr(
8794 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01008795 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008796{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008797 win_T *wp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008798 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008799
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008800 if (nr < 0)
8801 return NULL;
8802 if (nr == 0)
8803 return curwin;
8804
Bram Moolenaar29323592016-07-24 22:04:11 +02008805 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8806 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008807 if (nr >= LOWEST_WIN_ID)
8808 {
8809 if (wp->w_id == nr)
8810 return wp;
8811 }
8812 else if (--nr <= 0)
8813 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008814 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008815 if (nr >= LOWEST_WIN_ID)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008816 {
8817#ifdef FEAT_TEXT_PROP
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02008818 // check tab-local popup windows
8819 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008820 if (wp->w_id == nr)
8821 return wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02008822 // check global popup windows
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008823 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
8824 if (wp->w_id == nr)
8825 return wp;
8826#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008827 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008828 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008829 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008830}
8831
8832/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02008833 * Find a window: When using a Window ID in any tab page, when using a number
8834 * in the current tab page.
8835 */
8836 win_T *
8837find_win_by_nr_or_id(typval_T *vp)
8838{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008839 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02008840
8841 if (nr >= LOWEST_WIN_ID)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01008842 return win_id2wp(tv_get_number(vp));
Bram Moolenaare6e39892018-10-25 12:32:11 +02008843 return find_win_by_nr(vp, NULL);
8844}
8845
8846/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008847 * Find window specified by "wvp" in tabpage "tvp".
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008848 * Returns the tab page in 'ptp'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008849 */
8850 win_T *
8851find_tabwin(
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008852 typval_T *wvp, // VAR_UNKNOWN for current window
8853 typval_T *tvp, // VAR_UNKNOWN for current tab page
8854 tabpage_T **ptp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008855{
8856 win_T *wp = NULL;
8857 tabpage_T *tp = NULL;
8858 long n;
8859
8860 if (wvp->v_type != VAR_UNKNOWN)
8861 {
8862 if (tvp->v_type != VAR_UNKNOWN)
8863 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008864 n = (long)tv_get_number(tvp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008865 if (n >= 0)
8866 tp = find_tabpage(n);
8867 }
8868 else
8869 tp = curtab;
8870
8871 if (tp != NULL)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008872 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008873 wp = find_win_by_nr(wvp, tp);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008874 if (wp == NULL && wvp->v_type == VAR_NUMBER
8875 && wvp->vval.v_number != -1)
8876 // A window with the specified number is not found
8877 tp = NULL;
8878 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008879 }
8880 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008881 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008882 wp = curwin;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008883 tp = curtab;
8884 }
8885
8886 if (ptp != NULL)
8887 *ptp = tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008888
8889 return wp;
8890}
8891
8892/*
8893 * getwinvar() and gettabwinvar()
8894 */
8895 void
8896getwinvar(
8897 typval_T *argvars,
8898 typval_T *rettv,
8899 int off) /* 1 for gettabwinvar() */
8900{
8901 win_T *win;
8902 char_u *varname;
8903 dictitem_T *v;
8904 tabpage_T *tp = NULL;
8905 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008906 win_T *oldcurwin;
8907 tabpage_T *oldtabpage;
8908 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008909
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008910 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008911 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008912 else
8913 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008914 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008915 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008916 ++emsg_off;
8917
8918 rettv->v_type = VAR_STRING;
8919 rettv->vval.v_string = NULL;
8920
8921 if (win != NULL && varname != NULL)
8922 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008923 /* Set curwin to be our win, temporarily. Also set the tabpage,
8924 * otherwise the window is not valid. Only do this when needed,
8925 * autocommands get blocked. */
8926 need_switch_win = !(tp == curtab && win == curwin);
8927 if (!need_switch_win
8928 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008929 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008930 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008931 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008932 if (varname[1] == NUL)
8933 {
8934 /* get all window-local options in a dict */
8935 dict_T *opts = get_winbuf_options(FALSE);
8936
8937 if (opts != NULL)
8938 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008939 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02008940 done = TRUE;
8941 }
8942 }
8943 else if (get_option_tv(&varname, rettv, 1) == OK)
8944 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008945 done = TRUE;
8946 }
8947 else
8948 {
8949 /* Look up the variable. */
8950 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8951 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8952 varname, FALSE);
8953 if (v != NULL)
8954 {
8955 copy_tv(&v->di_tv, rettv);
8956 done = TRUE;
8957 }
8958 }
8959 }
8960
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008961 if (need_switch_win)
8962 /* restore previous notion of curwin */
8963 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008964 }
8965
8966 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8967 /* use the default return value */
8968 copy_tv(&argvars[off + 2], rettv);
8969
8970 --emsg_off;
8971}
8972
8973/*
8974 * "setwinvar()" and "settabwinvar()" functions
8975 */
8976 void
8977setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8978{
8979 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008980 win_T *save_curwin;
8981 tabpage_T *save_curtab;
8982 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008983 char_u *varname, *winvarname;
8984 typval_T *varp;
8985 char_u nbuf[NUMBUFLEN];
8986 tabpage_T *tp = NULL;
8987
Bram Moolenaare0fb7d12019-02-12 20:48:10 +01008988 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008989 return;
8990
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008991 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008992 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008993 else
8994 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008995 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008996 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008997 varp = &argvars[off + 2];
8998
8999 if (win != NULL && varname != NULL && varp != NULL)
9000 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009001 need_switch_win = !(tp == curtab && win == curwin);
9002 if (!need_switch_win
9003 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009004 {
9005 if (*varname == '&')
9006 {
9007 long numval;
9008 char_u *strval;
9009 int error = FALSE;
9010
9011 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009012 numval = (long)tv_get_number_chk(varp, &error);
9013 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009014 if (!error && strval != NULL)
9015 set_option_value(varname, numval, strval, OPT_LOCAL);
9016 }
9017 else
9018 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02009019 winvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009020 if (winvarname != NULL)
9021 {
9022 STRCPY(winvarname, "w:");
9023 STRCPY(winvarname + 2, varname);
9024 set_var(winvarname, varp, TRUE);
9025 vim_free(winvarname);
9026 }
9027 }
9028 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009029 if (need_switch_win)
9030 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009031 }
9032}
9033
9034/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
9036 * "arg" points to the "&" or '+' when called, to "option" when returning.
9037 * Returns NULL when no option name found. Otherwise pointer to the char
9038 * after the option name.
9039 */
9040 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009041find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042{
9043 char_u *p = *arg;
9044
9045 ++p;
9046 if (*p == 'g' && p[1] == ':')
9047 {
9048 *opt_flags = OPT_GLOBAL;
9049 p += 2;
9050 }
9051 else if (*p == 'l' && p[1] == ':')
9052 {
9053 *opt_flags = OPT_LOCAL;
9054 p += 2;
9055 }
9056 else
9057 *opt_flags = 0;
9058
9059 if (!ASCII_ISALPHA(*p))
9060 return NULL;
9061 *arg = p;
9062
9063 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
9064 p += 4; /* termcap option */
9065 else
9066 while (ASCII_ISALPHA(*p))
9067 ++p;
9068 return p;
9069}
9070
9071/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009072 * Return the autoload script name for a function or variable name.
9073 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02009075 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009076autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02009077{
Bram Moolenaara1544c02013-05-30 12:35:52 +02009078 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009079 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02009080
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009081 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaar964b3742019-05-24 18:54:09 +02009082 scriptname = alloc(STRLEN(name) + 14);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009083 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02009084 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009085 STRCPY(scriptname, "autoload/");
9086 STRCAT(scriptname, name);
9087 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
9088 STRCAT(scriptname, ".vim");
9089 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
9090 *p = '/';
9091 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009092}
9093
9094/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009095 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009096 * Return TRUE if a package was loaded.
9097 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009098 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009099script_autoload(
9100 char_u *name,
9101 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009102{
9103 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009104 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009105 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009106 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009107
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009108 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00009109 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009110 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009111 return FALSE;
9112
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009113 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009114
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009115 /* Find the name in the list of previously loaded package names. Skip
9116 * "autoload/", it's always the same. */
9117 for (i = 0; i < ga_loaded.ga_len; ++i)
9118 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
9119 break;
9120 if (!reload && i < ga_loaded.ga_len)
9121 ret = FALSE; /* was loaded already */
9122 else
9123 {
9124 /* Remember the name if it wasn't loaded already. */
9125 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
9126 {
9127 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
9128 tofree = NULL;
9129 }
9130
9131 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009132 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009133 ret = TRUE;
9134 }
9135
9136 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009137 return ret;
9138}
9139
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
9141typedef enum
9142{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009143 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
9144 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
9145 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146} var_flavour_T;
9147
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01009149var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150{
9151 char_u *p = varname;
9152
9153 if (ASCII_ISUPPER(*p))
9154 {
9155 while (*(++p))
9156 if (ASCII_ISLOWER(*p))
9157 return VAR_FLAVOUR_SESSION;
9158 return VAR_FLAVOUR_VIMINFO;
9159 }
9160 else
9161 return VAR_FLAVOUR_DEFAULT;
9162}
9163#endif
9164
9165#if defined(FEAT_VIMINFO) || defined(PROTO)
9166/*
9167 * Restore global vars that start with a capital from the viminfo file
9168 */
9169 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009170read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171{
9172 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009173 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00009174 typval_T tv;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009175 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176
9177 if (!writing && (find_viminfo_parameter('!') != NULL))
9178 {
9179 tab = vim_strchr(virp->vir_line + 1, '\t');
9180 if (tab != NULL)
9181 {
9182 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009183 switch (*tab)
9184 {
9185 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009186#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009187 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009188#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009189 case 'D': type = VAR_DICT; break;
9190 case 'L': type = VAR_LIST; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009191 case 'B': type = VAR_BLOB; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009192 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194
9195 tab = vim_strchr(tab, '\t');
9196 if (tab != NULL)
9197 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009198 tv.v_type = type;
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009199 if (type == VAR_STRING || type == VAR_DICT
9200 || type == VAR_LIST || type == VAR_BLOB)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009201 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009203#ifdef FEAT_FLOAT
9204 else if (type == VAR_FLOAT)
9205 (void)string2float(tab + 1, &tv.vval.v_float);
9206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009208 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009209 if (type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009210 {
9211 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
9212
9213 if (etv == NULL)
9214 /* Failed to parse back the dict or list, use it as a
9215 * string. */
9216 tv.v_type = VAR_STRING;
9217 else
9218 {
9219 vim_free(tv.vval.v_string);
9220 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01009221 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009222 }
9223 }
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009224 else if (type == VAR_BLOB)
9225 {
9226 blob_T *blob = string2blob(tv.vval.v_string);
9227
9228 if (blob == NULL)
9229 // Failed to parse back the blob, use it as a string.
9230 tv.v_type = VAR_STRING;
9231 else
9232 {
9233 vim_free(tv.vval.v_string);
9234 tv.v_type = VAR_BLOB;
9235 tv.vval.v_blob = blob;
9236 }
9237 }
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009238
Bram Moolenaarb20e3342016-01-18 23:29:01 +01009239 /* when in a function use global variables */
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009240 save_funccal(&funccal_entry);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009241 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009242 restore_funccal();
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009243
9244 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009245 vim_free(tv.vval.v_string);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009246 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST ||
9247 tv.v_type == VAR_BLOB)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009248 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249 }
9250 }
9251 }
9252
9253 return viminfo_readline(virp);
9254}
9255
9256/*
9257 * Write global vars that start with a capital to the viminfo file
9258 */
9259 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009260write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261{
Bram Moolenaar33570922005-01-25 22:26:29 +00009262 hashitem_T *hi;
9263 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009264 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01009265 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009266 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009267 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009268 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269
9270 if (find_viminfo_parameter('!') == NULL)
9271 return;
9272
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02009273 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00009274
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009275 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009276 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009278 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009280 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009281 this_var = HI2DI(hi);
9282 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009283 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009284 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00009285 {
9286 case VAR_STRING: s = "STR"; break;
9287 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009288 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009289 case VAR_DICT: s = "DIC"; break;
9290 case VAR_LIST: s = "LIS"; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009291 case VAR_BLOB: s = "BLO"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009292 case VAR_SPECIAL: s = "XPL"; break;
9293
9294 case VAR_UNKNOWN:
9295 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009296 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01009297 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01009298 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01009299 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00009300 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009301 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009302 if (this_var->di_tv.v_type == VAR_SPECIAL)
9303 {
9304 sprintf((char *)numbuf, "%ld",
9305 (long)this_var->di_tv.vval.v_number);
9306 p = numbuf;
9307 tofree = NULL;
9308 }
9309 else
9310 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009311 if (p != NULL)
9312 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00009313 vim_free(tofree);
9314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315 }
9316 }
9317}
9318#endif
9319
9320#if defined(FEAT_SESSION) || defined(PROTO)
9321 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009322store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323{
Bram Moolenaar33570922005-01-25 22:26:29 +00009324 hashitem_T *hi;
9325 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009326 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327 char_u *p, *t;
9328
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009329 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009330 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009332 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009334 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009335 this_var = HI2DI(hi);
9336 if ((this_var->di_tv.v_type == VAR_NUMBER
9337 || this_var->di_tv.v_type == VAR_STRING)
9338 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009339 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009340 /* Escape special characters with a backslash. Turn a LF and
9341 * CR into \n and \r. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009342 p = vim_strsave_escaped(tv_get_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00009343 (char_u *)"\\\"\n\r");
9344 if (p == NULL) /* out of memory */
9345 break;
9346 for (t = p; *t != NUL; ++t)
9347 if (*t == '\n')
9348 *t = 'n';
9349 else if (*t == '\r')
9350 *t = 'r';
9351 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00009352 this_var->di_key,
9353 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9354 : ' ',
9355 p,
9356 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9357 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00009358 || put_eol(fd) == FAIL)
9359 {
9360 vim_free(p);
9361 return FAIL;
9362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363 vim_free(p);
9364 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009365#ifdef FEAT_FLOAT
9366 else if (this_var->di_tv.v_type == VAR_FLOAT
9367 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
9368 {
9369 float_T f = this_var->di_tv.vval.v_float;
9370 int sign = ' ';
9371
9372 if (f < 0)
9373 {
9374 f = -f;
9375 sign = '-';
9376 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01009377 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009378 this_var->di_key, sign, f) < 0)
9379 || put_eol(fd) == FAIL)
9380 return FAIL;
9381 }
9382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383 }
9384 }
9385 return OK;
9386}
9387#endif
9388
Bram Moolenaar661b1822005-07-28 22:36:45 +00009389/*
9390 * Display script name where an item was last set.
9391 * Should only be invoked when 'verbose' is non-zero.
9392 */
9393 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009394last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009395{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009396 char_u *p;
9397
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009398 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009399 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009400 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009401 if (p != NULL)
9402 {
9403 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01009404 msg_puts(_("\n\tLast set from "));
9405 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009406 if (script_ctx.sc_lnum > 0)
9407 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01009408 msg_puts(_(" line "));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009409 msg_outnum((long)script_ctx.sc_lnum);
9410 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009411 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009412 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009413 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00009414 }
9415}
9416
Bram Moolenaar61be3762019-03-19 23:04:17 +01009417/*
9418 * Reset v:option_new, v:option_old and v:option_type.
9419 */
Bram Moolenaar53744302015-07-17 17:38:22 +02009420 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009421reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02009422{
9423 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
9424 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
9425 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
9426}
9427
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009428/*
9429 * Prepare "gap" for an assert error and add the sourcing position.
9430 */
9431 void
9432prepare_assert_error(garray_T *gap)
9433{
9434 char buf[NUMBUFLEN];
9435
9436 ga_init2(gap, 1, 100);
9437 if (sourcing_name != NULL)
9438 {
9439 ga_concat(gap, sourcing_name);
9440 if (sourcing_lnum > 0)
9441 ga_concat(gap, (char_u *)" ");
9442 }
9443 if (sourcing_lnum > 0)
9444 {
9445 sprintf(buf, "line %ld", (long)sourcing_lnum);
9446 ga_concat(gap, (char_u *)buf);
9447 }
9448 if (sourcing_name != NULL || sourcing_lnum > 0)
9449 ga_concat(gap, (char_u *)": ");
9450}
9451
9452/*
9453 * Add an assert error to v:errors.
9454 */
9455 void
9456assert_error(garray_T *gap)
9457{
9458 struct vimvar *vp = &vimvars[VV_ERRORS];
9459
9460 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9461 /* Make sure v:errors is a list. */
9462 set_vim_var_list(VV_ERRORS, list_alloc());
9463 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9464}
9465
Bram Moolenaar65a54642018-04-28 16:56:53 +02009466 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009467assert_equal_common(typval_T *argvars, assert_type_T atype)
9468{
9469 garray_T ga;
9470
9471 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9472 != (atype == ASSERT_EQUAL))
9473 {
9474 prepare_assert_error(&ga);
9475 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9476 atype);
9477 assert_error(&ga);
9478 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009479 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009480 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009481 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009482}
9483
Bram Moolenaar65a54642018-04-28 16:56:53 +02009484 int
Bram Moolenaard96ff162018-02-18 22:13:29 +01009485assert_equalfile(typval_T *argvars)
9486{
9487 char_u buf1[NUMBUFLEN];
9488 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009489 char_u *fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
9490 char_u *fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009491 garray_T ga;
9492 FILE *fd1;
9493 FILE *fd2;
9494
9495 if (fname1 == NULL || fname2 == NULL)
Bram Moolenaar65a54642018-04-28 16:56:53 +02009496 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009497
9498 IObuff[0] = NUL;
9499 fd1 = mch_fopen((char *)fname1, READBIN);
9500 if (fd1 == NULL)
9501 {
9502 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname1);
9503 }
9504 else
9505 {
9506 fd2 = mch_fopen((char *)fname2, READBIN);
9507 if (fd2 == NULL)
9508 {
9509 fclose(fd1);
9510 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname2);
9511 }
9512 else
9513 {
9514 int c1, c2;
9515 long count = 0;
9516
9517 for (;;)
9518 {
9519 c1 = fgetc(fd1);
9520 c2 = fgetc(fd2);
9521 if (c1 == EOF)
9522 {
9523 if (c2 != EOF)
9524 STRCPY(IObuff, "first file is shorter");
9525 break;
9526 }
9527 else if (c2 == EOF)
9528 {
9529 STRCPY(IObuff, "second file is shorter");
9530 break;
9531 }
9532 else if (c1 != c2)
9533 {
9534 vim_snprintf((char *)IObuff, IOSIZE,
9535 "difference at byte %ld", count);
9536 break;
9537 }
9538 ++count;
9539 }
Bram Moolenaar30494182018-02-20 21:46:05 +01009540 fclose(fd1);
9541 fclose(fd2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009542 }
9543 }
9544 if (IObuff[0] != NUL)
9545 {
9546 prepare_assert_error(&ga);
9547 ga_concat(&ga, IObuff);
9548 assert_error(&ga);
9549 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009550 return 1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009551 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009552 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009553}
9554
Bram Moolenaar65a54642018-04-28 16:56:53 +02009555 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009556assert_match_common(typval_T *argvars, assert_type_T atype)
9557{
9558 garray_T ga;
9559 char_u buf1[NUMBUFLEN];
9560 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009561 char_u *pat = tv_get_string_buf_chk(&argvars[0], buf1);
9562 char_u *text = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009563
9564 if (pat == NULL || text == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009565 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009566 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
9567 {
9568 prepare_assert_error(&ga);
9569 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9570 atype);
9571 assert_error(&ga);
9572 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009573 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009574 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009575 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009576}
9577
Bram Moolenaar65a54642018-04-28 16:56:53 +02009578 int
Bram Moolenaar61c04492016-07-23 15:35:35 +02009579assert_inrange(typval_T *argvars)
9580{
9581 garray_T ga;
9582 int error = FALSE;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009583 char_u *tofree;
9584 char msg[200];
9585 char_u numbuf[NUMBUFLEN];
9586
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009587#ifdef FEAT_FLOAT
9588 if (argvars[0].v_type == VAR_FLOAT
9589 || argvars[1].v_type == VAR_FLOAT
9590 || argvars[2].v_type == VAR_FLOAT)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009591 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009592 float_T flower = tv_get_float(&argvars[0]);
9593 float_T fupper = tv_get_float(&argvars[1]);
9594 float_T factual = tv_get_float(&argvars[2]);
9595
9596 if (factual < flower || factual > fupper)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009597 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009598 prepare_assert_error(&ga);
9599 if (argvars[3].v_type != VAR_UNKNOWN)
9600 {
9601 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9602 vim_free(tofree);
9603 }
9604 else
9605 {
9606 vim_snprintf(msg, 200, "Expected range %g - %g, but got %g",
9607 flower, fupper, factual);
9608 ga_concat(&ga, (char_u *)msg);
9609 }
9610 assert_error(&ga);
9611 ga_clear(&ga);
9612 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009613 }
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009614 }
9615 else
9616#endif
9617 {
9618 varnumber_T lower = tv_get_number_chk(&argvars[0], &error);
9619 varnumber_T upper = tv_get_number_chk(&argvars[1], &error);
9620 varnumber_T actual = tv_get_number_chk(&argvars[2], &error);
9621
9622 if (error)
9623 return 0;
9624 if (actual < lower || actual > upper)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009625 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009626 prepare_assert_error(&ga);
9627 if (argvars[3].v_type != VAR_UNKNOWN)
9628 {
9629 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9630 vim_free(tofree);
9631 }
9632 else
9633 {
9634 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
Bram Moolenaar61c04492016-07-23 15:35:35 +02009635 (long)lower, (long)upper, (long)actual);
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009636 ga_concat(&ga, (char_u *)msg);
9637 }
9638 assert_error(&ga);
9639 ga_clear(&ga);
9640 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009641 }
Bram Moolenaar61c04492016-07-23 15:35:35 +02009642 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009643 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009644}
9645
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009646/*
9647 * Common for assert_true() and assert_false().
Bram Moolenaar65a54642018-04-28 16:56:53 +02009648 * Return non-zero for failure.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009649 */
Bram Moolenaar65a54642018-04-28 16:56:53 +02009650 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009651assert_bool(typval_T *argvars, int isTrue)
9652{
9653 int error = FALSE;
9654 garray_T ga;
9655
9656 if (argvars[0].v_type == VAR_SPECIAL
9657 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar65a54642018-04-28 16:56:53 +02009658 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009659 if (argvars[0].v_type != VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009660 || (tv_get_number_chk(&argvars[0], &error) == 0) == isTrue
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009661 || error)
9662 {
9663 prepare_assert_error(&ga);
9664 fill_assert_error(&ga, &argvars[1],
9665 (char_u *)(isTrue ? "True" : "False"),
9666 NULL, &argvars[0], ASSERT_OTHER);
9667 assert_error(&ga);
9668 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009669 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009670 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009671 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009672}
9673
Bram Moolenaar65a54642018-04-28 16:56:53 +02009674 int
Bram Moolenaar42205552017-03-18 19:42:22 +01009675assert_report(typval_T *argvars)
9676{
9677 garray_T ga;
9678
9679 prepare_assert_error(&ga);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009680 ga_concat(&ga, tv_get_string(&argvars[0]));
Bram Moolenaar42205552017-03-18 19:42:22 +01009681 assert_error(&ga);
9682 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009683 return 1;
Bram Moolenaar42205552017-03-18 19:42:22 +01009684}
9685
Bram Moolenaar65a54642018-04-28 16:56:53 +02009686 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009687assert_exception(typval_T *argvars)
9688{
9689 garray_T ga;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009690 char_u *error = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009691
9692 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9693 {
9694 prepare_assert_error(&ga);
9695 ga_concat(&ga, (char_u *)"v:exception is not set");
9696 assert_error(&ga);
9697 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009698 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009699 }
9700 else if (error != NULL
9701 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9702 {
9703 prepare_assert_error(&ga);
9704 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9705 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9706 assert_error(&ga);
9707 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009708 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009709 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009710 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009711}
9712
Bram Moolenaar65a54642018-04-28 16:56:53 +02009713 int
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009714assert_beeps(typval_T *argvars)
9715{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009716 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009717 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009718 int ret = 0;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009719
9720 called_vim_beep = FALSE;
9721 suppress_errthrow = TRUE;
9722 emsg_silent = FALSE;
9723 do_cmdline_cmd(cmd);
9724 if (!called_vim_beep)
9725 {
9726 prepare_assert_error(&ga);
9727 ga_concat(&ga, (char_u *)"command did not beep: ");
9728 ga_concat(&ga, cmd);
9729 assert_error(&ga);
9730 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009731 ret = 1;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009732 }
9733
9734 suppress_errthrow = FALSE;
9735 emsg_on_display = FALSE;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009736 return ret;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009737}
9738
Bram Moolenaar25190db2019-05-04 15:05:28 +02009739 static void
9740assert_append_cmd_or_arg(garray_T *gap, typval_T *argvars, char_u *cmd)
9741{
9742 char_u *tofree;
9743 char_u numbuf[NUMBUFLEN];
9744
9745 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9746 {
9747 ga_concat(gap, echo_string(&argvars[2], &tofree, numbuf, 0));
9748 vim_free(tofree);
9749 }
9750 else
9751 ga_concat(gap, cmd);
9752}
9753
Bram Moolenaar65a54642018-04-28 16:56:53 +02009754 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009755assert_fails(typval_T *argvars)
9756{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009757 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009758 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009759 int ret = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009760
9761 called_emsg = FALSE;
9762 suppress_errthrow = TRUE;
9763 emsg_silent = TRUE;
9764 do_cmdline_cmd(cmd);
9765 if (!called_emsg)
9766 {
9767 prepare_assert_error(&ga);
9768 ga_concat(&ga, (char_u *)"command did not fail: ");
Bram Moolenaar25190db2019-05-04 15:05:28 +02009769 assert_append_cmd_or_arg(&ga, argvars, cmd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009770 assert_error(&ga);
9771 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009772 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009773 }
9774 else if (argvars[1].v_type != VAR_UNKNOWN)
9775 {
9776 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009777 char *error = (char *)tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009778
9779 if (error == NULL
9780 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9781 {
9782 prepare_assert_error(&ga);
9783 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9784 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaar25190db2019-05-04 15:05:28 +02009785 ga_concat(&ga, (char_u *)": ");
9786 assert_append_cmd_or_arg(&ga, argvars, cmd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009787 assert_error(&ga);
9788 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009789 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009790 }
9791 }
9792
9793 called_emsg = FALSE;
9794 suppress_errthrow = FALSE;
9795 emsg_silent = FALSE;
9796 emsg_on_display = FALSE;
9797 set_vim_var_string(VV_ERRMSG, NULL, 0);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009798 return ret;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009799}
9800
9801/*
Bram Moolenaar86576712019-01-25 20:48:33 +01009802 * Append "p[clen]" to "gap", escaping unprintable characters.
9803 * Changes NL to \n, CR to \r, etc.
9804 */
9805 static void
9806ga_concat_esc(garray_T *gap, char_u *p, int clen)
9807{
9808 char_u buf[NUMBUFLEN];
9809
9810 if (clen > 1)
9811 {
9812 mch_memmove(buf, p, clen);
9813 buf[clen] = NUL;
9814 ga_concat(gap, buf);
9815 }
9816 else switch (*p)
9817 {
9818 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9819 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9820 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9821 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9822 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9823 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9824 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9825 default:
9826 if (*p < ' ')
9827 {
9828 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9829 ga_concat(gap, buf);
9830 }
9831 else
9832 ga_append(gap, *p);
9833 break;
9834 }
9835}
9836
9837/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009838 * Append "str" to "gap", escaping unprintable characters.
9839 * Changes NL to \n, CR to \r, etc.
9840 */
9841 static void
Bram Moolenaar86576712019-01-25 20:48:33 +01009842ga_concat_shorten_esc(garray_T *gap, char_u *str)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009843{
9844 char_u *p;
Bram Moolenaar86576712019-01-25 20:48:33 +01009845 char_u *s;
9846 int c;
9847 int clen;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009848 char_u buf[NUMBUFLEN];
Bram Moolenaar86576712019-01-25 20:48:33 +01009849 int same_len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009850
9851 if (str == NULL)
9852 {
9853 ga_concat(gap, (char_u *)"NULL");
9854 return;
9855 }
9856
9857 for (p = str; *p != NUL; ++p)
Bram Moolenaar86576712019-01-25 20:48:33 +01009858 {
9859 same_len = 1;
9860 s = p;
9861 c = mb_ptr2char_adv(&s);
9862 clen = s - p;
9863 while (*s != NUL && c == mb_ptr2char(s))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009864 {
Bram Moolenaar86576712019-01-25 20:48:33 +01009865 ++same_len;
9866 s += clen;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009867 }
Bram Moolenaar86576712019-01-25 20:48:33 +01009868 if (same_len > 20)
9869 {
9870 ga_concat(gap, (char_u *)"\\[");
9871 ga_concat_esc(gap, p, clen);
9872 ga_concat(gap, (char_u *)" occurs ");
9873 vim_snprintf((char *)buf, NUMBUFLEN, "%d", same_len);
9874 ga_concat(gap, buf);
9875 ga_concat(gap, (char_u *)" times]");
9876 p = s - 1;
9877 }
9878 else
9879 ga_concat_esc(gap, p, clen);
9880 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009881}
9882
9883/*
9884 * Fill "gap" with information about an assert error.
9885 */
9886 void
9887fill_assert_error(
9888 garray_T *gap,
9889 typval_T *opt_msg_tv,
9890 char_u *exp_str,
9891 typval_T *exp_tv,
9892 typval_T *got_tv,
9893 assert_type_T atype)
9894{
9895 char_u numbuf[NUMBUFLEN];
9896 char_u *tofree;
9897
9898 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9899 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009900 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9901 vim_free(tofree);
9902 ga_concat(gap, (char_u *)": ");
9903 }
9904
9905 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9906 ga_concat(gap, (char_u *)"Pattern ");
9907 else if (atype == ASSERT_NOTEQUAL)
9908 ga_concat(gap, (char_u *)"Expected not equal to ");
9909 else
9910 ga_concat(gap, (char_u *)"Expected ");
9911 if (exp_str == NULL)
9912 {
Bram Moolenaar86576712019-01-25 20:48:33 +01009913 ga_concat_shorten_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009914 vim_free(tofree);
9915 }
9916 else
Bram Moolenaar86576712019-01-25 20:48:33 +01009917 ga_concat_shorten_esc(gap, exp_str);
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009918 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009919 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009920 if (atype == ASSERT_MATCH)
9921 ga_concat(gap, (char_u *)" does not match ");
9922 else if (atype == ASSERT_NOTMATCH)
9923 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009924 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009925 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar86576712019-01-25 20:48:33 +01009926 ga_concat_shorten_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009927 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009928 }
9929}
9930
Bram Moolenaar31988702018-02-13 12:57:42 +01009931/*
9932 * Compare "typ1" and "typ2". Put the result in "typ1".
9933 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009934 int
9935typval_compare(
9936 typval_T *typ1, /* first operand */
9937 typval_T *typ2, /* second operand */
9938 exptype_T type, /* operator */
9939 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +01009940 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009941{
9942 int i;
9943 varnumber_T n1, n2;
9944 char_u *s1, *s2;
9945 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
9946
Bram Moolenaar31988702018-02-13 12:57:42 +01009947 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009948 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009949 /* For "is" a different type always means FALSE, for "notis"
9950 * it means TRUE. */
9951 n1 = (type == TYPE_NEQUAL);
9952 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009953 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
9954 {
9955 if (type_is)
9956 {
9957 n1 = (typ1->v_type == typ2->v_type
9958 && typ1->vval.v_blob == typ2->vval.v_blob);
9959 if (type == TYPE_NEQUAL)
9960 n1 = !n1;
9961 }
9962 else if (typ1->v_type != typ2->v_type
9963 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9964 {
9965 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009966 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009967 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009968 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009969 clear_tv(typ1);
9970 return FAIL;
9971 }
9972 else
9973 {
9974 // Compare two Blobs for being equal or unequal.
9975 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
9976 if (type == TYPE_NEQUAL)
9977 n1 = !n1;
9978 }
9979 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009980 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
9981 {
9982 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009983 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009984 n1 = (typ1->v_type == typ2->v_type
9985 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009986 if (type == TYPE_NEQUAL)
9987 n1 = !n1;
9988 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009989 else if (typ1->v_type != typ2->v_type
9990 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009991 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009992 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009993 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009994 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009995 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009996 clear_tv(typ1);
9997 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009998 }
9999 else
10000 {
Bram Moolenaar31988702018-02-13 12:57:42 +010010001 /* Compare two Lists for being equal or unequal. */
10002 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
10003 ic, FALSE);
10004 if (type == TYPE_NEQUAL)
10005 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010006 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010007 }
Bram Moolenaar31988702018-02-13 12:57:42 +010010008
10009 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
10010 {
10011 if (type_is)
10012 {
10013 n1 = (typ1->v_type == typ2->v_type
10014 && typ1->vval.v_dict == typ2->vval.v_dict);
10015 if (type == TYPE_NEQUAL)
10016 n1 = !n1;
10017 }
10018 else if (typ1->v_type != typ2->v_type
10019 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
10020 {
10021 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010022 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +010010023 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010024 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +010010025 clear_tv(typ1);
10026 return FAIL;
10027 }
10028 else
10029 {
10030 /* Compare two Dictionaries for being equal or unequal. */
10031 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
10032 ic, FALSE);
10033 if (type == TYPE_NEQUAL)
10034 n1 = !n1;
10035 }
10036 }
10037
10038 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
10039 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
10040 {
10041 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
10042 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010043 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +010010044 clear_tv(typ1);
10045 return FAIL;
10046 }
10047 if ((typ1->v_type == VAR_PARTIAL
10048 && typ1->vval.v_partial == NULL)
10049 || (typ2->v_type == VAR_PARTIAL
10050 && typ2->vval.v_partial == NULL))
10051 /* when a partial is NULL assume not equal */
10052 n1 = FALSE;
10053 else if (type_is)
10054 {
10055 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
10056 /* strings are considered the same if their value is
10057 * the same */
10058 n1 = tv_equal(typ1, typ2, ic, FALSE);
10059 else if (typ1->v_type == VAR_PARTIAL
10060 && typ2->v_type == VAR_PARTIAL)
10061 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
10062 else
10063 n1 = FALSE;
10064 }
10065 else
10066 n1 = tv_equal(typ1, typ2, ic, FALSE);
10067 if (type == TYPE_NEQUAL)
10068 n1 = !n1;
10069 }
10070
10071#ifdef FEAT_FLOAT
10072 /*
10073 * If one of the two variables is a float, compare as a float.
10074 * When using "=~" or "!~", always compare as string.
10075 */
10076 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
10077 && type != TYPE_MATCH && type != TYPE_NOMATCH)
10078 {
10079 float_T f1, f2;
10080
Bram Moolenaar38f08e72019-02-20 22:04:32 +010010081 f1 = tv_get_float(typ1);
10082 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +010010083 n1 = FALSE;
10084 switch (type)
10085 {
10086 case TYPE_EQUAL: n1 = (f1 == f2); break;
10087 case TYPE_NEQUAL: n1 = (f1 != f2); break;
10088 case TYPE_GREATER: n1 = (f1 > f2); break;
10089 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
10090 case TYPE_SMALLER: n1 = (f1 < f2); break;
10091 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
10092 case TYPE_UNKNOWN:
10093 case TYPE_MATCH:
10094 case TYPE_NOMATCH: break; /* avoid gcc warning */
10095 }
10096 }
10097#endif
10098
10099 /*
10100 * If one of the two variables is a number, compare as a number.
10101 * When using "=~" or "!~", always compare as string.
10102 */
10103 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
10104 && type != TYPE_MATCH && type != TYPE_NOMATCH)
10105 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010106 n1 = tv_get_number(typ1);
10107 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +010010108 switch (type)
10109 {
10110 case TYPE_EQUAL: n1 = (n1 == n2); break;
10111 case TYPE_NEQUAL: n1 = (n1 != n2); break;
10112 case TYPE_GREATER: n1 = (n1 > n2); break;
10113 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
10114 case TYPE_SMALLER: n1 = (n1 < n2); break;
10115 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
10116 case TYPE_UNKNOWN:
10117 case TYPE_MATCH:
10118 case TYPE_NOMATCH: break; /* avoid gcc warning */
10119 }
10120 }
10121 else
10122 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010123 s1 = tv_get_string_buf(typ1, buf1);
10124 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar31988702018-02-13 12:57:42 +010010125 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
10126 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
10127 else
10128 i = 0;
10129 n1 = FALSE;
10130 switch (type)
10131 {
10132 case TYPE_EQUAL: n1 = (i == 0); break;
10133 case TYPE_NEQUAL: n1 = (i != 0); break;
10134 case TYPE_GREATER: n1 = (i > 0); break;
10135 case TYPE_GEQUAL: n1 = (i >= 0); break;
10136 case TYPE_SMALLER: n1 = (i < 0); break;
10137 case TYPE_SEQUAL: n1 = (i <= 0); break;
10138
10139 case TYPE_MATCH:
10140 case TYPE_NOMATCH:
10141 n1 = pattern_match(s2, s1, ic);
10142 if (type == TYPE_NOMATCH)
10143 n1 = !n1;
10144 break;
10145
10146 case TYPE_UNKNOWN: break; /* avoid gcc warning */
10147 }
10148 }
10149 clear_tv(typ1);
10150 typ1->v_type = VAR_NUMBER;
10151 typ1->vval.v_number = n1;
10152
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010153 return OK;
10154}
10155
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010156 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010157typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010158{
10159 char_u *tofree;
10160 char_u numbuf[NUMBUFLEN];
10161 char_u *ret = NULL;
10162
10163 if (arg == NULL)
10164 return vim_strsave((char_u *)"(does not exist)");
10165 ret = tv2string(arg, &tofree, numbuf, 0);
10166 /* Make a copy if we have a value but it's not in allocated memory. */
10167 if (ret != NULL && tofree == NULL)
10168 ret = vim_strsave(ret);
10169 return ret;
10170}
10171
10172 int
10173var_exists(char_u *var)
10174{
10175 char_u *name;
10176 char_u *tofree;
10177 typval_T tv;
10178 int len = 0;
10179 int n = FALSE;
10180
10181 /* get_name_len() takes care of expanding curly braces */
10182 name = var;
10183 len = get_name_len(&var, &tofree, TRUE, FALSE);
10184 if (len > 0)
10185 {
10186 if (tofree != NULL)
10187 name = tofree;
10188 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
10189 if (n)
10190 {
10191 /* handle d.key, l[idx], f(expr) */
10192 n = (handle_subscript(&var, &tv, TRUE, FALSE) == OK);
10193 if (n)
10194 clear_tv(&tv);
10195 }
10196 }
10197 if (*var != NUL)
10198 n = FALSE;
10199
10200 vim_free(tofree);
10201 return n;
10202}
10203
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204#endif /* FEAT_EVAL */
10205
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010207#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208
Bram Moolenaar4f974752019-02-17 17:44:42 +010010209#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210/*
10211 * Functions for ":8" filename modifier: get 8.3 version of a filename.
10212 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213
10214/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010215 * Get the short path (8.3) for the filename in "fnamep".
10216 * Only works for a valid file name.
10217 * When the path gets longer "fnamep" is changed and the allocated buffer
10218 * is put in "bufp".
10219 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
10220 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221 */
10222 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010223get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010225 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226 char_u *newbuf;
10227
10228 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010229 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230 if (l > len - 1)
10231 {
10232 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010233 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234 newbuf = vim_strnsave(*fnamep, l);
10235 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010236 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237
10238 vim_free(*bufp);
10239 *fnamep = *bufp = newbuf;
10240
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010241 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010242 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010243 }
10244
10245 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010246 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247}
10248
10249/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010250 * Get the short path (8.3) for the filename in "fname". The converted
10251 * path is returned in "bufp".
10252 *
10253 * Some of the directories specified in "fname" may not exist. This function
10254 * will shorten the existing directories at the beginning of the path and then
10255 * append the remaining non-existing path.
10256 *
10257 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020010258 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010259 * bufp - Pointer to an allocated buffer for the filename.
10260 * fnamelen - Length of the filename pointed to by fname
10261 *
10262 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263 */
10264 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010265shortpath_for_invalid_fname(
10266 char_u **fname,
10267 char_u **bufp,
10268 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010270 char_u *short_fname, *save_fname, *pbuf_unused;
10271 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010273 int old_len, len;
10274 int new_len, sfx_len;
10275 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276
10277 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010278 old_len = *fnamelen;
10279 save_fname = vim_strnsave(*fname, old_len);
10280 pbuf_unused = NULL;
10281 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010283 endp = save_fname + old_len - 1; /* Find the end of the copy */
10284 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010286 /*
10287 * Try shortening the supplied path till it succeeds by removing one
10288 * directory at a time from the tail of the path.
10289 */
10290 len = 0;
10291 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010293 /* go back one path-separator */
10294 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
10295 --endp;
10296 if (endp <= save_fname)
10297 break; /* processed the complete path */
10298
10299 /*
10300 * Replace the path separator with a NUL and try to shorten the
10301 * resulting path.
10302 */
10303 ch = *endp;
10304 *endp = 0;
10305 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010306 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010307 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
10308 {
10309 retval = FAIL;
10310 goto theend;
10311 }
10312 *endp = ch; /* preserve the string */
10313
10314 if (len > 0)
10315 break; /* successfully shortened the path */
10316
10317 /* failed to shorten the path. Skip the path separator */
10318 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319 }
10320
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010321 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010323 /*
10324 * Succeeded in shortening the path. Now concatenate the shortened
10325 * path with the remaining path at the tail.
10326 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010328 /* Compute the length of the new path. */
10329 sfx_len = (int)(save_endp - endp) + 1;
10330 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010332 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010334 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010336 /* There is not enough space in the currently allocated string,
10337 * copy it to a buffer big enough. */
10338 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010340 {
10341 retval = FAIL;
10342 goto theend;
10343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344 }
10345 else
10346 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010347 /* Transfer short_fname to the main buffer (it's big enough),
10348 * unless get_short_pathname() did its work in-place. */
10349 *fname = *bufp = save_fname;
10350 if (short_fname != save_fname)
10351 vim_strncpy(save_fname, short_fname, len);
10352 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010354
10355 /* concat the not-shortened part of the path */
10356 vim_strncpy(*fname + len, endp, sfx_len);
10357 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010359
10360theend:
10361 vim_free(pbuf_unused);
10362 vim_free(save_fname);
10363
10364 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365}
10366
10367/*
10368 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010369 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370 */
10371 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010372shortpath_for_partial(
10373 char_u **fnamep,
10374 char_u **bufp,
10375 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376{
10377 int sepcount, len, tflen;
10378 char_u *p;
10379 char_u *pbuf, *tfname;
10380 int hasTilde;
10381
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010382 /* Count up the path separators from the RHS.. so we know which part
10383 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010385 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386 if (vim_ispathsep(*p))
10387 ++sepcount;
10388
10389 /* Need full path first (use expand_env() to remove a "~/") */
10390 hasTilde = (**fnamep == '~');
10391 if (hasTilde)
10392 pbuf = tfname = expand_env_save(*fnamep);
10393 else
10394 pbuf = tfname = FullName_save(*fnamep, FALSE);
10395
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010396 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010397
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010398 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
10399 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400
10401 if (len == 0)
10402 {
10403 /* Don't have a valid filename, so shorten the rest of the
10404 * path if we can. This CAN give us invalid 8.3 filenames, but
10405 * there's not a lot of point in guessing what it might be.
10406 */
10407 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010408 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
10409 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410 }
10411
10412 /* Count the paths backward to find the beginning of the desired string. */
10413 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010414 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010415 if (has_mbyte)
10416 p -= mb_head_off(tfname, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010417 if (vim_ispathsep(*p))
10418 {
10419 if (sepcount == 0 || (hasTilde && sepcount == 1))
10420 break;
10421 else
10422 sepcount --;
10423 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425 if (hasTilde)
10426 {
10427 --p;
10428 if (p >= tfname)
10429 *p = '~';
10430 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010431 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432 }
10433 else
10434 ++p;
10435
10436 /* Copy in the string - p indexes into tfname - allocated at pbuf */
10437 vim_free(*bufp);
10438 *fnamelen = (int)STRLEN(p);
10439 *bufp = pbuf;
10440 *fnamep = p;
10441
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010442 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443}
Bram Moolenaar4f974752019-02-17 17:44:42 +010010444#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445
10446/*
10447 * Adjust a filename, according to a string of modifiers.
10448 * *fnamep must be NUL terminated when called. When returning, the length is
10449 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010450 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451 * When there is an error, *fnamep is set to NULL.
10452 */
10453 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010454modify_fname(
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010455 char_u *src, // string with modifiers
10456 int tilde_file, // "~" is a file name, not $HOME
10457 int *usedlen, // characters after src that are used
10458 char_u **fnamep, // file name so far
10459 char_u **bufp, // buffer for allocated file name or NULL
10460 int *fnamelen) // length of fnamep
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461{
10462 int valid = 0;
10463 char_u *tail;
10464 char_u *s, *p, *pbuf;
10465 char_u dirname[MAXPATHL];
10466 int c;
10467 int has_fullname = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010468#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010469 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010470 int has_shortname = 0;
10471#endif
10472
10473repeat:
10474 /* ":p" - full path/file_name */
10475 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
10476 {
10477 has_fullname = 1;
10478
10479 valid |= VALID_PATH;
10480 *usedlen += 2;
10481
10482 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
10483 if ((*fnamep)[0] == '~'
10484#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
10485 && ((*fnamep)[1] == '/'
10486# ifdef BACKSLASH_IN_FILENAME
10487 || (*fnamep)[1] == '\\'
10488# endif
10489 || (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010491 && !(tilde_file && (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492 )
10493 {
10494 *fnamep = expand_env_save(*fnamep);
10495 vim_free(*bufp); /* free any allocated file name */
10496 *bufp = *fnamep;
10497 if (*fnamep == NULL)
10498 return -1;
10499 }
10500
10501 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010502 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503 {
10504 if (vim_ispathsep(*p)
10505 && p[1] == '.'
10506 && (p[2] == NUL
10507 || vim_ispathsep(p[2])
10508 || (p[2] == '.'
10509 && (p[3] == NUL || vim_ispathsep(p[3])))))
10510 break;
10511 }
10512
10513 /* FullName_save() is slow, don't use it when not needed. */
10514 if (*p != NUL || !vim_isAbsName(*fnamep))
10515 {
10516 *fnamep = FullName_save(*fnamep, *p != NUL);
10517 vim_free(*bufp); /* free any allocated file name */
10518 *bufp = *fnamep;
10519 if (*fnamep == NULL)
10520 return -1;
10521 }
10522
Bram Moolenaar4f974752019-02-17 17:44:42 +010010523#ifdef MSWIN
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010524# if _WIN32_WINNT >= 0x0500
10525 if (vim_strchr(*fnamep, '~') != NULL)
10526 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010527 // Expand 8.3 filename to full path. Needed to make sure the same
10528 // file does not have two different names.
10529 // Note: problem does not occur if _WIN32_WINNT < 0x0500.
10530 WCHAR *wfname = enc_to_utf16(*fnamep, NULL);
10531 WCHAR buf[_MAX_PATH];
10532
10533 if (wfname != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010534 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010535 if (GetLongPathNameW(wfname, buf, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010536 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010537 char_u *p = utf16_to_enc(buf, NULL);
10538
10539 if (p != NULL)
10540 {
10541 vim_free(*bufp); // free any allocated file name
10542 *bufp = *fnamep = p;
10543 }
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010544 }
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010545 vim_free(wfname);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010546 }
10547 }
10548# endif
10549#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550 /* Append a path separator to a directory. */
10551 if (mch_isdir(*fnamep))
10552 {
10553 /* Make room for one or two extra characters. */
10554 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
10555 vim_free(*bufp); /* free any allocated file name */
10556 *bufp = *fnamep;
10557 if (*fnamep == NULL)
10558 return -1;
10559 add_pathsep(*fnamep);
10560 }
10561 }
10562
10563 /* ":." - path relative to the current directory */
10564 /* ":~" - path relative to the home directory */
10565 /* ":8" - shortname path - postponed till after */
10566 while (src[*usedlen] == ':'
10567 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
10568 {
10569 *usedlen += 2;
10570 if (c == '8')
10571 {
Bram Moolenaar4f974752019-02-17 17:44:42 +010010572#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573 has_shortname = 1; /* Postpone this. */
10574#endif
10575 continue;
10576 }
10577 pbuf = NULL;
10578 /* Need full path first (use expand_env() to remove a "~/") */
10579 if (!has_fullname)
10580 {
10581 if (c == '.' && **fnamep == '~')
10582 p = pbuf = expand_env_save(*fnamep);
10583 else
10584 p = pbuf = FullName_save(*fnamep, FALSE);
10585 }
10586 else
10587 p = *fnamep;
10588
10589 has_fullname = 0;
10590
10591 if (p != NULL)
10592 {
10593 if (c == '.')
10594 {
10595 mch_dirname(dirname, MAXPATHL);
10596 s = shorten_fname(p, dirname);
10597 if (s != NULL)
10598 {
10599 *fnamep = s;
10600 if (pbuf != NULL)
10601 {
10602 vim_free(*bufp); /* free any allocated file name */
10603 *bufp = pbuf;
10604 pbuf = NULL;
10605 }
10606 }
10607 }
10608 else
10609 {
10610 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
10611 /* Only replace it when it starts with '~' */
10612 if (*dirname == '~')
10613 {
10614 s = vim_strsave(dirname);
10615 if (s != NULL)
10616 {
10617 *fnamep = s;
10618 vim_free(*bufp);
10619 *bufp = s;
10620 }
10621 }
10622 }
10623 vim_free(pbuf);
10624 }
10625 }
10626
10627 tail = gettail(*fnamep);
10628 *fnamelen = (int)STRLEN(*fnamep);
10629
10630 /* ":h" - head, remove "/file_name", can be repeated */
10631 /* Don't remove the first "/" or "c:\" */
10632 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
10633 {
10634 valid |= VALID_HEAD;
10635 *usedlen += 2;
10636 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010637 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010638 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010639 *fnamelen = (int)(tail - *fnamep);
10640#ifdef VMS
10641 if (*fnamelen > 0)
10642 *fnamelen += 1; /* the path separator is part of the path */
10643#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010644 if (*fnamelen == 0)
10645 {
10646 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
10647 p = vim_strsave((char_u *)".");
10648 if (p == NULL)
10649 return -1;
10650 vim_free(*bufp);
10651 *bufp = *fnamep = tail = p;
10652 *fnamelen = 1;
10653 }
10654 else
10655 {
10656 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010657 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659 }
10660
10661 /* ":8" - shortname */
10662 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
10663 {
10664 *usedlen += 2;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010665#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666 has_shortname = 1;
10667#endif
10668 }
10669
Bram Moolenaar4f974752019-02-17 17:44:42 +010010670#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010671 /*
10672 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673 */
10674 if (has_shortname)
10675 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010676 /* Copy the string if it is shortened by :h and when it wasn't copied
10677 * yet, because we are going to change it in place. Avoids changing
10678 * the buffer name for "%:8". */
10679 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680 {
10681 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020010682 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683 return -1;
10684 vim_free(*bufp);
10685 *bufp = *fnamep = p;
10686 }
10687
10688 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020010689 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690 if (!has_fullname && !vim_isAbsName(*fnamep))
10691 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010692 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693 return -1;
10694 }
10695 else
10696 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010697 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010698
Bram Moolenaardc935552011-08-17 15:23:23 +020010699 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010701 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702 return -1;
10703
10704 if (l == 0)
10705 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010706 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010708 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709 return -1;
10710 }
10711 *fnamelen = l;
10712 }
10713 }
Bram Moolenaar4f974752019-02-17 17:44:42 +010010714#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715
10716 /* ":t" - tail, just the basename */
10717 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
10718 {
10719 *usedlen += 2;
10720 *fnamelen -= (int)(tail - *fnamep);
10721 *fnamep = tail;
10722 }
10723
10724 /* ":e" - extension, can be repeated */
10725 /* ":r" - root, without extension, can be repeated */
10726 while (src[*usedlen] == ':'
10727 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
10728 {
10729 /* find a '.' in the tail:
10730 * - for second :e: before the current fname
10731 * - otherwise: The last '.'
10732 */
10733 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
10734 s = *fnamep - 2;
10735 else
10736 s = *fnamep + *fnamelen - 1;
10737 for ( ; s > tail; --s)
10738 if (s[0] == '.')
10739 break;
10740 if (src[*usedlen + 1] == 'e') /* :e */
10741 {
10742 if (s > tail)
10743 {
10744 *fnamelen += (int)(*fnamep - (s + 1));
10745 *fnamep = s + 1;
10746#ifdef VMS
10747 /* cut version from the extension */
10748 s = *fnamep + *fnamelen - 1;
10749 for ( ; s > *fnamep; --s)
10750 if (s[0] == ';')
10751 break;
10752 if (s > *fnamep)
10753 *fnamelen = s - *fnamep;
10754#endif
10755 }
10756 else if (*fnamep <= tail)
10757 *fnamelen = 0;
10758 }
10759 else /* :r */
10760 {
10761 if (s > tail) /* remove one extension */
10762 *fnamelen = (int)(s - *fnamep);
10763 }
10764 *usedlen += 2;
10765 }
10766
10767 /* ":s?pat?foo?" - substitute */
10768 /* ":gs?pat?foo?" - global substitute */
10769 if (src[*usedlen] == ':'
10770 && (src[*usedlen + 1] == 's'
10771 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
10772 {
10773 char_u *str;
10774 char_u *pat;
10775 char_u *sub;
10776 int sep;
10777 char_u *flags;
10778 int didit = FALSE;
10779
10780 flags = (char_u *)"";
10781 s = src + *usedlen + 2;
10782 if (src[*usedlen + 1] == 'g')
10783 {
10784 flags = (char_u *)"g";
10785 ++s;
10786 }
10787
10788 sep = *s++;
10789 if (sep)
10790 {
10791 /* find end of pattern */
10792 p = vim_strchr(s, sep);
10793 if (p != NULL)
10794 {
10795 pat = vim_strnsave(s, (int)(p - s));
10796 if (pat != NULL)
10797 {
10798 s = p + 1;
10799 /* find end of substitution */
10800 p = vim_strchr(s, sep);
10801 if (p != NULL)
10802 {
10803 sub = vim_strnsave(s, (int)(p - s));
10804 str = vim_strnsave(*fnamep, *fnamelen);
10805 if (sub != NULL && str != NULL)
10806 {
10807 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010808 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010809 if (s != NULL)
10810 {
10811 *fnamep = s;
10812 *fnamelen = (int)STRLEN(s);
10813 vim_free(*bufp);
10814 *bufp = s;
10815 didit = TRUE;
10816 }
10817 }
10818 vim_free(sub);
10819 vim_free(str);
10820 }
10821 vim_free(pat);
10822 }
10823 }
10824 /* after using ":s", repeat all the modifiers */
10825 if (didit)
10826 goto repeat;
10827 }
10828 }
10829
Bram Moolenaar26df0922014-02-23 23:39:13 +010010830 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10831 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010832 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010833 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010834 if (c != NUL)
10835 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010836 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010837 if (c != NUL)
10838 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010839 if (p == NULL)
10840 return -1;
10841 vim_free(*bufp);
10842 *bufp = *fnamep = p;
10843 *fnamelen = (int)STRLEN(p);
10844 *usedlen += 2;
10845 }
10846
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847 return valid;
10848}
10849
10850/*
10851 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010852 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853 * "flags" can be "g" to do a global substitute.
10854 * Returns an allocated string, NULL for error.
10855 */
10856 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010857do_string_sub(
10858 char_u *str,
10859 char_u *pat,
10860 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010861 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010862 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010863{
10864 int sublen;
10865 regmatch_T regmatch;
10866 int i;
10867 int do_all;
10868 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010869 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870 garray_T ga;
10871 char_u *ret;
10872 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010873 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874
10875 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
10876 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010877 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878
10879 ga_init2(&ga, 1, 200);
10880
10881 do_all = (flags[0] == 'g');
10882
10883 regmatch.rm_ic = p_ic;
10884 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10885 if (regmatch.regprog != NULL)
10886 {
10887 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010888 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
10890 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010010891 /* Skip empty match except for first match. */
10892 if (regmatch.startp[0] == regmatch.endp[0])
10893 {
10894 if (zero_width == regmatch.startp[0])
10895 {
10896 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020010897 i = MB_PTR2LEN(tail);
10898 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
10899 (size_t)i);
10900 ga.ga_len += i;
10901 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010902 continue;
10903 }
10904 zero_width = regmatch.startp[0];
10905 }
10906
Bram Moolenaar071d4272004-06-13 20:20:40 +000010907 /*
10908 * Get some space for a temporary buffer to do the substitution
10909 * into. It will contain:
10910 * - The text up to where the match is.
10911 * - The substituted text.
10912 * - The text after the match.
10913 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010914 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010010915 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
10917 {
10918 ga_clear(&ga);
10919 break;
10920 }
10921
10922 /* copy the text up to where the match is */
10923 i = (int)(regmatch.startp[0] - tail);
10924 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10925 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010926 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010927 + ga.ga_len + i, TRUE, TRUE, FALSE);
10928 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010929 tail = regmatch.endp[0];
10930 if (*tail == NUL)
10931 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932 if (!do_all)
10933 break;
10934 }
10935
10936 if (ga.ga_data != NULL)
10937 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10938
Bram Moolenaar473de612013-06-08 18:19:48 +020010939 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940 }
10941
10942 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10943 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010944 if (p_cpo == empty_option)
10945 p_cpo = save_cpo;
10946 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010947 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010948 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010949
10950 return ret;
10951}
10952
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010953 static int
10954filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10955{
10956 typval_T rettv;
10957 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010958 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010959
10960 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10961 argv[0] = vimvars[VV_KEY].vv_tv;
10962 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010963 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10964 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010965 if (map)
10966 {
10967 /* map(): replace the list item value */
10968 clear_tv(tv);
10969 rettv.v_lock = 0;
10970 *tv = rettv;
10971 }
10972 else
10973 {
10974 int error = FALSE;
10975
10976 /* filter(): when expr is zero remove the item */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010977 *remp = (tv_get_number_chk(&rettv, &error) == 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010978 clear_tv(&rettv);
10979 /* On type error, nothing has been removed; return FAIL to stop the
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010980 * loop. The error message was given by tv_get_number_chk(). */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010981 if (error)
10982 goto theend;
10983 }
10984 retval = OK;
10985theend:
10986 clear_tv(&vimvars[VV_VAL].vv_tv);
10987 return retval;
10988}
10989
10990
10991/*
10992 * Implementation of map() and filter().
10993 */
10994 void
10995filter_map(typval_T *argvars, typval_T *rettv, int map)
10996{
10997 typval_T *expr;
10998 listitem_T *li, *nli;
10999 list_T *l = NULL;
11000 dictitem_T *di;
11001 hashtab_T *ht;
11002 hashitem_T *hi;
11003 dict_T *d = NULL;
11004 typval_T save_val;
11005 typval_T save_key;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010011006 blob_T *b = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011007 int rem;
11008 int todo;
11009 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
11010 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
11011 : N_("filter() argument"));
11012 int save_did_emsg;
11013 int idx = 0;
11014
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010011015 if (argvars[0].v_type == VAR_BLOB)
11016 {
11017 if ((b = argvars[0].vval.v_blob) == NULL)
11018 return;
11019 }
11020 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011021 {
11022 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011023 || (!map && var_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011024 return;
11025 }
11026 else if (argvars[0].v_type == VAR_DICT)
11027 {
11028 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011029 || (!map && var_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011030 return;
11031 }
11032 else
11033 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011034 semsg(_(e_listdictarg), ermsg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011035 return;
11036 }
11037
11038 expr = &argvars[1];
11039 /* On type errors, the preceding call has already displayed an error
11040 * message. Avoid a misleading error message for an empty string that
11041 * was not passed as argument. */
11042 if (expr->v_type != VAR_UNKNOWN)
11043 {
11044 prepare_vimvar(VV_VAL, &save_val);
11045
11046 /* We reset "did_emsg" to be able to detect whether an error
11047 * occurred during evaluation of the expression. */
11048 save_did_emsg = did_emsg;
11049 did_emsg = FALSE;
11050
11051 prepare_vimvar(VV_KEY, &save_key);
11052 if (argvars[0].v_type == VAR_DICT)
11053 {
11054 vimvars[VV_KEY].vv_type = VAR_STRING;
11055
11056 ht = &d->dv_hashtab;
11057 hash_lock(ht);
11058 todo = (int)ht->ht_used;
11059 for (hi = ht->ht_array; todo > 0; ++hi)
11060 {
11061 if (!HASHITEM_EMPTY(hi))
11062 {
11063 int r;
11064
11065 --todo;
11066 di = HI2DI(hi);
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011067 if (map && (var_check_lock(di->di_tv.v_lock,
11068 arg_errmsg, TRUE)
11069 || var_check_ro(di->di_flags,
11070 arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011071 break;
11072 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
11073 r = filter_map_one(&di->di_tv, expr, map, &rem);
11074 clear_tv(&vimvars[VV_KEY].vv_tv);
11075 if (r == FAIL || did_emsg)
11076 break;
11077 if (!map && rem)
11078 {
11079 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11080 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
11081 break;
11082 dictitem_remove(d, di);
11083 }
11084 }
11085 }
11086 hash_unlock(ht);
11087 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010011088 else if (argvars[0].v_type == VAR_BLOB)
11089 {
11090 int i;
11091 typval_T tv;
11092
11093 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11094 for (i = 0; i < b->bv_ga.ga_len; i++)
11095 {
11096 tv.v_type = VAR_NUMBER;
11097 tv.vval.v_number = blob_get(b, i);
11098 vimvars[VV_KEY].vv_nr = idx;
11099 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg)
11100 break;
11101 if (tv.v_type != VAR_NUMBER)
11102 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011103 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010011104 return;
11105 }
11106 tv.v_type = VAR_NUMBER;
11107 blob_set(b, i, tv.vval.v_number);
11108 if (!map && rem)
11109 {
11110 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
11111
11112 mch_memmove(p + idx, p + i + 1,
11113 (size_t)b->bv_ga.ga_len - i - 1);
11114 --b->bv_ga.ga_len;
11115 --i;
11116 }
11117 }
11118 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011119 else
11120 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +010011121 // argvars[0].v_type == VAR_LIST
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011122 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11123
11124 for (li = l->lv_first; li != NULL; li = nli)
11125 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011126 if (map && var_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011127 break;
11128 nli = li->li_next;
11129 vimvars[VV_KEY].vv_nr = idx;
11130 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
11131 || did_emsg)
11132 break;
11133 if (!map && rem)
11134 listitem_remove(l, li);
11135 ++idx;
11136 }
11137 }
11138
11139 restore_vimvar(VV_KEY, &save_key);
11140 restore_vimvar(VV_VAL, &save_val);
11141
11142 did_emsg |= save_did_emsg;
11143 }
11144
11145 copy_tv(&argvars[0], rettv);
11146}
11147
Bram Moolenaar071d4272004-06-13 20:20:40 +000011148#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */