blob: 5452f45437d7ace82641ce6eaea0e88e1b76974b [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar33570922005-01-25 22:26:29 +000023#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026static char *e_undefvar = N_("E121: Undefined variable: %s");
27static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000028static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
29static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar92124a32005-06-17 22:03:40 +000030static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020031#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020032static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020033#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000034
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010035#define NAMESPACE_CHAR (char_u *)"abglstvw"
36
Bram Moolenaar230bb3f2013-04-24 14:07:45 +020037static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +000038#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +000039
40/*
Bram Moolenaar532c7802005-01-27 14:44:31 +000041 * Old Vim variables such as "v:version" are also available without the "v:".
42 * Also in functions. We need a special hashtable for them.
43 */
Bram Moolenaar4debb442005-06-01 21:57:40 +000044static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +000045
46/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000047 * When recursively copying lists and dicts we need to remember which ones we
48 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000049 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000050 */
51static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020052
Bram Moolenaard9fba312005-06-26 22:34:35 +000053/*
Bram Moolenaar33570922005-01-25 22:26:29 +000054 * Array to hold the hashtab with variables local to each sourced script.
55 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 */
Bram Moolenaar33570922005-01-25 22:26:29 +000057typedef struct
58{
59 dictitem_T sv_var;
60 dict_T sv_dict;
61} scriptvar_T;
62
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020063static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
64#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
65#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +000066
67static int echo_attr = 0; /* attributes used for ":echo" */
68
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000069/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000070static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
71
Bram Moolenaar071d4272004-06-13 20:20:40 +000072/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000073 * Info used by a ":for" loop.
74 */
Bram Moolenaar33570922005-01-25 22:26:29 +000075typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000076{
77 int fi_semicolon; /* TRUE if ending in '; var]' */
78 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +000079 listwatch_T fi_lw; /* keep an eye on the item used. */
80 list_T *fi_list; /* list being used */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010081 int fi_bi; /* index of blob */
82 blob_T *fi_blob; /* blob being used */
Bram Moolenaar33570922005-01-25 22:26:29 +000083} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000084
Bram Moolenaara7043832005-01-21 11:56:39 +000085
86/*
Bram Moolenaar33570922005-01-25 22:26:29 +000087 * Array to hold the value of v: variables.
88 * The value is in a dictitem, so that it can also be used in the v: scope.
89 * The reason to use this table anyway is for very quick access to the
90 * variables with the VV_ defines.
91 */
Bram Moolenaar33570922005-01-25 22:26:29 +000092
93/* values for vv_flags: */
94#define VV_COMPAT 1 /* compatible, also used without "v:" */
95#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000096#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +000097
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +010098#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +000099
100static struct vimvar
101{
102 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100103 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000104 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
105} vimvars[VV_LEN] =
106{
107 /*
108 * The order here must match the VV_ defines in vim.h!
109 * Initializing a union does not work, leave tv.vval empty to get zero's.
110 */
111 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
112 {VV_NAME("count1", VAR_NUMBER), VV_RO},
113 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
114 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
115 {VV_NAME("warningmsg", VAR_STRING), 0},
116 {VV_NAME("statusmsg", VAR_STRING), 0},
117 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
118 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
119 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
120 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
121 {VV_NAME("termresponse", VAR_STRING), VV_RO},
122 {VV_NAME("fname", VAR_STRING), VV_RO},
123 {VV_NAME("lang", VAR_STRING), VV_RO},
124 {VV_NAME("lc_time", VAR_STRING), VV_RO},
125 {VV_NAME("ctype", VAR_STRING), VV_RO},
126 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
127 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
128 {VV_NAME("fname_in", VAR_STRING), VV_RO},
129 {VV_NAME("fname_out", VAR_STRING), VV_RO},
130 {VV_NAME("fname_new", VAR_STRING), VV_RO},
131 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
132 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
133 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
134 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
135 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
136 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
137 {VV_NAME("progname", VAR_STRING), VV_RO},
138 {VV_NAME("servername", VAR_STRING), VV_RO},
139 {VV_NAME("dying", VAR_NUMBER), VV_RO},
140 {VV_NAME("exception", VAR_STRING), VV_RO},
141 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
142 {VV_NAME("register", VAR_STRING), VV_RO},
143 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
144 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000145 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
146 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000147 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000148 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
149 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000150 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
151 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
Bram Moolenaarc9721bd2016-06-04 17:41:03 +0200152 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000153 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
154 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
155 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000156 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000157 {VV_NAME("swapname", VAR_STRING), VV_RO},
158 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000159 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200160 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000161 {VV_NAME("mouse_win", VAR_NUMBER), 0},
Bram Moolenaar511972d2016-06-04 18:09:59 +0200162 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000163 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
164 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000165 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000166 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100167 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000168 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200169 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200170 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200171 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200172 {VV_NAME("option_new", VAR_STRING), VV_RO},
173 {VV_NAME("option_old", VAR_STRING), VV_RO},
174 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100175 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100176 {VV_NAME("false", VAR_SPECIAL), VV_RO},
177 {VV_NAME("true", VAR_SPECIAL), VV_RO},
178 {VV_NAME("null", VAR_SPECIAL), VV_RO},
179 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100180 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200181 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaarf562e722016-07-19 17:25:25 +0200182 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
183 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
184 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
185 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
186 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
187 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
188 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
189 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
190 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
191 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100192 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200193 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
194 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
Bram Moolenaarf3af54e2017-08-30 14:53:06 +0200195 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
196 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
197 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100198 {VV_NAME("event", VAR_DICT), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000199};
200
201/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000202#define vv_type vv_di.di_tv.v_type
203#define vv_nr vv_di.di_tv.vval.v_number
204#define vv_float vv_di.di_tv.vval.v_float
205#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000206#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200207#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100208#define vv_blob vv_di.di_tv.vval.v_blob
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000209#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000210
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200211static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000212#define vimvarht vimvardict.dv_hashtab
213
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100214static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
215static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
216static char_u *skip_var_one(char_u *arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100217static void list_glob_vars(int *first);
218static void list_buf_vars(int *first);
219static void list_win_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100220static void list_tab_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100221static void list_vim_vars(int *first);
222static void list_script_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100223static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
224static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100225static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
226static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100227static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
228static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
229static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
230static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000231
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100232static int eval2(char_u **arg, typval_T *rettv, int evaluate);
233static int eval3(char_u **arg, typval_T *rettv, int evaluate);
234static int eval4(char_u **arg, typval_T *rettv, int evaluate);
235static int eval5(char_u **arg, typval_T *rettv, int evaluate);
236static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
237static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000238
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100239static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
240static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100241static int free_unref_items(int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100242static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100243static int get_env_len(char_u **arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100244static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200245static void check_vars(char_u *name, int len);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100246static typval_T *alloc_string_tv(char_u *string);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100247static void delete_var(hashtab_T *ht, hashitem_T *hi);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100248static void list_one_var(dictitem_T *v, char *prefix, int *first);
249static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
Bram Moolenaar05c00c02019-02-11 22:00:11 +0100250static int tv_check_lock(typval_T *tv, char_u *name, int use_gettext);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100251static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200252
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200253/* for VIM_VERSION_ defines */
254#include "version.h"
255
Bram Moolenaare21c1582019-03-02 11:57:09 +0100256/*
257 * Return "n1" divided by "n2", taking care of dividing by zero.
258 */
259 static varnumber_T
260num_divide(varnumber_T n1, varnumber_T n2)
261{
262 varnumber_T result;
263
264 if (n2 == 0) // give an error message?
265 {
266 if (n1 == 0)
267 result = VARNUM_MIN; // similar to NaN
268 else if (n1 < 0)
269 result = -VARNUM_MAX;
270 else
271 result = VARNUM_MAX;
272 }
273 else
274 result = n1 / n2;
275
276 return result;
277}
278
279/*
280 * Return "n1" modulus "n2", taking care of dividing by zero.
281 */
282 static varnumber_T
283num_modulus(varnumber_T n1, varnumber_T n2)
284{
285 // Give an error when n2 is 0?
286 return (n2 == 0) ? 0 : (n1 % n2);
287}
288
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100289
290#if defined(EBCDIC) || defined(PROTO)
291/*
292 * Compare struct fst by function name.
293 */
294 static int
295compare_func_name(const void *s1, const void *s2)
296{
297 struct fst *p1 = (struct fst *)s1;
298 struct fst *p2 = (struct fst *)s2;
299
300 return STRCMP(p1->f_name, p2->f_name);
301}
302
303/*
304 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100305 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100306 * On machines using EBCDIC we have to sort it.
307 */
308 static void
309sortFunctions(void)
310{
311 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
312
313 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
314}
315#endif
316
317
Bram Moolenaar33570922005-01-25 22:26:29 +0000318/*
319 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000320 */
321 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100322eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000323{
Bram Moolenaar33570922005-01-25 22:26:29 +0000324 int i;
325 struct vimvar *p;
326
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200327 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
328 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200329 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000330 hash_init(&compat_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200331 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000332
333 for (i = 0; i < VV_LEN; ++i)
334 {
335 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200336 if (STRLEN(p->vv_name) > 16)
337 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100338 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200339 getout(1);
340 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000341 STRCPY(p->vv_di.di_key, p->vv_name);
342 if (p->vv_flags & VV_RO)
343 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
344 else if (p->vv_flags & VV_RO_SBX)
345 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
346 else
347 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000348
349 /* add to v: scope dict, unless the value is not always available */
350 if (p->vv_type != VAR_UNKNOWN)
351 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000352 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000353 /* add to compat scope dict */
354 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000355 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100356 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
357
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000358 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100359 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100360 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100361 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100362 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100363
364 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
365 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
366 set_vim_var_nr(VV_NONE, VVAL_NONE);
367 set_vim_var_nr(VV_NULL, VVAL_NULL);
368
Bram Moolenaarf562e722016-07-19 17:25:25 +0200369 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
370 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
371 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
372 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
373 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
374 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
375 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
376 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
377 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
378 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100379 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
Bram Moolenaarf562e722016-07-19 17:25:25 +0200380
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200381 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200382
383#ifdef EBCDIC
384 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100385 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200386 */
387 sortFunctions();
388#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000389}
390
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000391#if defined(EXITFREE) || defined(PROTO)
392 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100393eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000394{
395 int i;
396 struct vimvar *p;
397
398 for (i = 0; i < VV_LEN; ++i)
399 {
400 p = &vimvars[i];
401 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100402 VIM_CLEAR(p->vv_str);
Bram Moolenaard812df62008-11-09 12:46:09 +0000403 else if (p->vv_di.di_tv.v_type == VAR_LIST)
404 {
405 list_unref(p->vv_list);
406 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000407 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000408 }
409 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000410 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000411 hash_clear(&compat_hashtab);
412
Bram Moolenaard9fba312005-06-26 22:34:35 +0000413 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100414# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200415 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100416# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000417
418 /* global variables */
419 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000420
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000421 /* autoloaded script names */
422 ga_clear_strings(&ga_loaded);
423
Bram Moolenaarcca74132013-09-25 21:00:28 +0200424 /* Script-local variables. First clear all the variables and in a second
425 * loop free the scriptvar_T, because a variable in one script might hold
426 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200427 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200428 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200429 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200430 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200431 ga_clear(&ga_scripts);
432
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200433 // functions need to be freed before gargabe collecting, otherwise local
434 // variables might be freed twice.
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000435 free_all_functions();
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200436
437 // unreferenced lists and dicts
438 (void)garbage_collect(FALSE);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000439}
440#endif
441
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442
443/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 * Set an internal variable to a string value. Creates the variable if it does
445 * not already exist.
446 */
447 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100448set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000450 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000451 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452
453 val = vim_strsave(value);
454 if (val != NULL)
455 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000456 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000457 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000459 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000460 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 }
462 }
463}
464
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000465static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200466#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000467static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000468static char_u *redir_endp = NULL;
469static char_u *redir_varname = NULL;
470
471/*
472 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100473 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000474 * Returns OK if successfully completed the setup. FAIL otherwise.
475 */
476 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100477var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000478{
479 int save_emsg;
480 int err;
481 typval_T tv;
482
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000483 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000484 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000485 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100486 emsg(_(e_invarg));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000487 return FAIL;
488 }
489
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000490 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000491 redir_varname = vim_strsave(name);
492 if (redir_varname == NULL)
493 return FAIL;
494
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200495 redir_lval = ALLOC_CLEAR_ONE(lval_T);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000496 if (redir_lval == NULL)
497 {
498 var_redir_stop();
499 return FAIL;
500 }
501
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000502 /* The output is stored in growarray "redir_ga" until redirection ends. */
503 ga_init2(&redir_ga, (int)sizeof(char), 500);
504
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000505 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100506 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000507 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000508 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
509 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200510 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000511 if (redir_endp != NULL && *redir_endp != NUL)
512 /* Trailing characters are present after the variable name */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100513 emsg(_(e_trailing));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000514 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100515 emsg(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000516 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000517 var_redir_stop();
518 return FAIL;
519 }
520
521 /* check if we can write to the variable: set it to or append an empty
522 * string */
523 save_emsg = did_emsg;
524 did_emsg = FALSE;
525 tv.v_type = VAR_STRING;
526 tv.vval.v_string = (char_u *)"";
527 if (append)
528 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
529 else
530 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200531 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000532 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000533 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000534 if (err)
535 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000536 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000537 var_redir_stop();
538 return FAIL;
539 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000540
541 return OK;
542}
543
544/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000545 * Append "value[value_len]" to the variable set by var_redir_start().
546 * The actual appending is postponed until redirection ends, because the value
547 * appended may in fact be the string we write to, changing it may cause freed
548 * memory to be used:
549 * :redir => foo
550 * :let foo
551 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000552 */
553 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100554var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000555{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000556 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000557
558 if (redir_lval == NULL)
559 return;
560
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000561 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000562 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000563 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000564 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000565
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000566 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000567 {
568 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000569 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000570 }
571 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000572 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000573}
574
575/*
576 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000577 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000578 */
579 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100580var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000581{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000582 typval_T tv;
583
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200584 if (EVALCMD_BUSY)
585 {
586 redir_lval = NULL;
587 return;
588 }
589
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000590 if (redir_lval != NULL)
591 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000592 /* If there was no error: assign the text to the variable. */
593 if (redir_endp != NULL)
594 {
595 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
596 tv.v_type = VAR_STRING;
597 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200598 /* Call get_lval() again, if it's inside a Dict or List it may
599 * have changed. */
600 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100601 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200602 if (redir_endp != NULL && redir_lval->ll_name != NULL)
603 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
604 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000605 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000606
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000607 /* free the collected output */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100608 VIM_CLEAR(redir_ga.ga_data);
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000609
Bram Moolenaard23a8232018-02-10 18:45:26 +0100610 VIM_CLEAR(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000611 }
Bram Moolenaard23a8232018-02-10 18:45:26 +0100612 VIM_CLEAR(redir_varname);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000613}
614
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100616eval_charconvert(
617 char_u *enc_from,
618 char_u *enc_to,
619 char_u *fname_from,
620 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621{
622 int err = FALSE;
623
624 set_vim_var_string(VV_CC_FROM, enc_from, -1);
625 set_vim_var_string(VV_CC_TO, enc_to, -1);
626 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
627 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
628 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
629 err = TRUE;
630 set_vim_var_string(VV_CC_FROM, NULL, -1);
631 set_vim_var_string(VV_CC_TO, NULL, -1);
632 set_vim_var_string(VV_FNAME_IN, NULL, -1);
633 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
634
635 if (err)
636 return FAIL;
637 return OK;
638}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639
640# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
641 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100642eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643{
644 int err = FALSE;
645
646 set_vim_var_string(VV_FNAME_IN, fname, -1);
647 set_vim_var_string(VV_CMDARG, args, -1);
648 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
649 err = TRUE;
650 set_vim_var_string(VV_FNAME_IN, NULL, -1);
651 set_vim_var_string(VV_CMDARG, NULL, -1);
652
653 if (err)
654 {
655 mch_remove(fname);
656 return FAIL;
657 }
658 return OK;
659}
660# endif
661
662# if defined(FEAT_DIFF) || defined(PROTO)
663 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100664eval_diff(
665 char_u *origfile,
666 char_u *newfile,
667 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668{
669 int err = FALSE;
670
671 set_vim_var_string(VV_FNAME_IN, origfile, -1);
672 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
673 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
674 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
675 set_vim_var_string(VV_FNAME_IN, NULL, -1);
676 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
677 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
678}
679
680 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100681eval_patch(
682 char_u *origfile,
683 char_u *difffile,
684 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685{
686 int err;
687
688 set_vim_var_string(VV_FNAME_IN, origfile, -1);
689 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
690 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
691 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
692 set_vim_var_string(VV_FNAME_IN, NULL, -1);
693 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
694 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
695}
696# endif
697
698/*
699 * Top level evaluation function, returning a boolean.
700 * Sets "error" to TRUE if there was an error.
701 * Return TRUE or FALSE.
702 */
703 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100704eval_to_bool(
705 char_u *arg,
706 int *error,
707 char_u **nextcmd,
708 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709{
Bram Moolenaar33570922005-01-25 22:26:29 +0000710 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200711 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712
713 if (skip)
714 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000715 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 else
718 {
719 *error = FALSE;
720 if (!skip)
721 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100722 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000723 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 }
725 }
726 if (skip)
727 --emsg_skip;
728
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200729 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730}
731
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100732/*
733 * Call eval1() and give an error message if not done at a lower level.
734 */
735 static int
736eval1_emsg(char_u **arg, typval_T *rettv, int evaluate)
737{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100738 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100739 int ret;
740 int did_emsg_before = did_emsg;
741 int called_emsg_before = called_emsg;
742
743 ret = eval1(arg, rettv, evaluate);
744 if (ret == FAIL)
745 {
746 // Report the invalid expression unless the expression evaluation has
747 // been cancelled due to an aborting error, an interrupt, or an
748 // exception, or we already gave a more specific error.
749 // Also check called_emsg for when using assert_fails().
750 if (!aborting() && did_emsg == did_emsg_before
751 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100752 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100753 }
754 return ret;
755}
756
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200757 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100758eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
759{
760 char_u *s;
761 int dummy;
762 char_u buf[NUMBUFLEN];
763
764 if (expr->v_type == VAR_FUNC)
765 {
766 s = expr->vval.v_string;
767 if (s == NULL || *s == NUL)
768 return FAIL;
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200769 if (call_func(s, -1, rettv, argc, argv, NULL,
Bram Moolenaar48570482017-10-30 21:48:41 +0100770 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
771 return FAIL;
772 }
773 else if (expr->v_type == VAR_PARTIAL)
774 {
775 partial_T *partial = expr->vval.v_partial;
776
777 s = partial_name(partial);
778 if (s == NULL || *s == NUL)
779 return FAIL;
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200780 if (call_func(s, -1, rettv, argc, argv, NULL,
Bram Moolenaar48570482017-10-30 21:48:41 +0100781 0L, 0L, &dummy, TRUE, partial, NULL) == FAIL)
782 return FAIL;
783 }
784 else
785 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100786 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100787 if (s == NULL)
788 return FAIL;
789 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100790 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100791 return FAIL;
792 if (*s != NUL) /* check for trailing chars after expr */
793 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200794 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100795 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100796 return FAIL;
797 }
798 }
799 return OK;
800}
801
802/*
803 * Like eval_to_bool() but using a typval_T instead of a string.
804 * Works for string, funcref and partial.
805 */
806 int
807eval_expr_to_bool(typval_T *expr, int *error)
808{
809 typval_T rettv;
810 int res;
811
812 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
813 {
814 *error = TRUE;
815 return FALSE;
816 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100817 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100818 clear_tv(&rettv);
819 return res;
820}
821
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822/*
823 * Top level evaluation function, returning a string. If "skip" is TRUE,
824 * only parsing to "nextcmd" is done, without reporting errors. Return
825 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
826 */
827 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100828eval_to_string_skip(
829 char_u *arg,
830 char_u **nextcmd,
831 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832{
Bram Moolenaar33570922005-01-25 22:26:29 +0000833 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 char_u *retval;
835
836 if (skip)
837 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000838 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 retval = NULL;
840 else
841 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100842 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000843 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 }
845 if (skip)
846 --emsg_skip;
847
848 return retval;
849}
850
851/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000852 * Skip over an expression at "*pp".
853 * Return FAIL for an error, OK otherwise.
854 */
855 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100856skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000857{
Bram Moolenaar33570922005-01-25 22:26:29 +0000858 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000859
860 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000861 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000862}
863
864/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000866 * When "convert" is TRUE convert a List into a sequence of lines and convert
867 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 * Return pointer to allocated memory, or NULL for failure.
869 */
870 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100871eval_to_string(
872 char_u *arg,
873 char_u **nextcmd,
874 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875{
Bram Moolenaar33570922005-01-25 22:26:29 +0000876 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000878 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000879#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000880 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000883 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 retval = NULL;
885 else
886 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000887 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000888 {
889 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000890 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200891 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200892 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200893 if (tv.vval.v_list->lv_len > 0)
894 ga_append(&ga, NL);
895 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000896 ga_append(&ga, NUL);
897 retval = (char_u *)ga.ga_data;
898 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000899#ifdef FEAT_FLOAT
900 else if (convert && tv.v_type == VAR_FLOAT)
901 {
902 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
903 retval = vim_strsave(numbuf);
904 }
905#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000906 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100907 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000908 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 }
910
911 return retval;
912}
913
914/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000915 * Call eval_to_string() without using current local variables and using
916 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 */
918 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100919eval_to_string_safe(
920 char_u *arg,
921 char_u **nextcmd,
922 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923{
924 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200925 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200927 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000928 if (use_sandbox)
929 ++sandbox;
930 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000931 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000932 if (use_sandbox)
933 --sandbox;
934 --textlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200935 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 return retval;
937}
938
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939/*
940 * Top level evaluation function, returning a number.
941 * Evaluates "expr" silently.
942 * Returns -1 for an error.
943 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200944 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100945eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946{
Bram Moolenaar33570922005-01-25 22:26:29 +0000947 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200948 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000949 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950
951 ++emsg_off;
952
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000953 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 retval = -1;
955 else
956 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100957 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000958 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 }
960 --emsg_off;
961
962 return retval;
963}
964
Bram Moolenaara40058a2005-07-11 22:42:07 +0000965/*
966 * Prepare v: variable "idx" to be used.
967 * Save the current typeval in "save_tv".
968 * When not used yet add the variable to the v: hashtable.
969 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200970 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100971prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000972{
973 *save_tv = vimvars[idx].vv_tv;
974 if (vimvars[idx].vv_type == VAR_UNKNOWN)
975 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
976}
977
978/*
979 * Restore v: variable "idx" to typeval "save_tv".
980 * When no longer defined, remove the variable from the v: hashtable.
981 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200982 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100983restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000984{
985 hashitem_T *hi;
986
Bram Moolenaara40058a2005-07-11 22:42:07 +0000987 vimvars[idx].vv_tv = *save_tv;
988 if (vimvars[idx].vv_type == VAR_UNKNOWN)
989 {
990 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
991 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100992 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +0000993 else
994 hash_remove(&vimvarht, hi);
995 }
996}
997
Bram Moolenaar3c56a962006-03-12 22:19:04 +0000998#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000999/*
1000 * Evaluate an expression to a list with suggestions.
1001 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001002 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001003 */
1004 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001005eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001006{
1007 typval_T save_val;
1008 typval_T rettv;
1009 list_T *list = NULL;
1010 char_u *p = skipwhite(expr);
1011
1012 /* Set "v:val" to the bad word. */
1013 prepare_vimvar(VV_VAL, &save_val);
1014 vimvars[VV_VAL].vv_type = VAR_STRING;
1015 vimvars[VV_VAL].vv_str = badword;
1016 if (p_verbose == 0)
1017 ++emsg_off;
1018
1019 if (eval1(&p, &rettv, TRUE) == OK)
1020 {
1021 if (rettv.v_type != VAR_LIST)
1022 clear_tv(&rettv);
1023 else
1024 list = rettv.vval.v_list;
1025 }
1026
1027 if (p_verbose == 0)
1028 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001029 restore_vimvar(VV_VAL, &save_val);
1030
1031 return list;
1032}
1033
1034/*
1035 * "list" is supposed to contain two items: a word and a number. Return the
1036 * word in "pp" and the number as the return value.
1037 * Return -1 if anything isn't right.
1038 * Used to get the good word and score from the eval_spell_expr() result.
1039 */
1040 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001041get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001042{
1043 listitem_T *li;
1044
1045 li = list->lv_first;
1046 if (li == NULL)
1047 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001048 *pp = tv_get_string(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001049
1050 li = li->li_next;
1051 if (li == NULL)
1052 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001053 return (int)tv_get_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001054}
1055#endif
1056
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001057/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001058 * Top level evaluation function.
1059 * Returns an allocated typval_T with the result.
1060 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001061 */
1062 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001063eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001064{
1065 typval_T *tv;
1066
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001067 tv = ALLOC_ONE(typval_T);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001068 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001069 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001070
1071 return tv;
1072}
1073
1074
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001076 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +02001077 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
1078 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001079 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001081 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001082call_vim_function(
1083 char_u *func,
1084 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +02001085 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001086 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 int doesrange;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001089 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001091 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001092 ret = call_func(func, -1, rettv, argc, argv, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001094 &doesrange, TRUE, NULL, NULL);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001095 if (ret == FAIL)
1096 clear_tv(rettv);
1097
1098 return ret;
1099}
1100
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001101/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001102 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001103 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001104 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1105 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001106 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001107 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001108call_func_retnr(
1109 char_u *func,
1110 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001111 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001112{
1113 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001114 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001115
Bram Moolenaarded27a12018-08-01 19:06:03 +02001116 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001117 return -1;
1118
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001119 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001120 clear_tv(&rettv);
1121 return retval;
1122}
1123
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001124#if defined(FEAT_CMDL_COMPL) \
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001125 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1126
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001127# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001128/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001129 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001130 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001131 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1132 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001133 */
1134 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001135call_func_retstr(
1136 char_u *func,
1137 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001138 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001139{
1140 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001141 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001142
Bram Moolenaarded27a12018-08-01 19:06:03 +02001143 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001144 return NULL;
1145
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001146 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001147 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 return retval;
1149}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001150# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001151
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001152/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001153 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001154 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1155 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001156 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001157 */
1158 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001159call_func_retlist(
1160 char_u *func,
1161 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001162 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001163{
1164 typval_T rettv;
1165
Bram Moolenaarded27a12018-08-01 19:06:03 +02001166 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001167 return NULL;
1168
1169 if (rettv.v_type != VAR_LIST)
1170 {
1171 clear_tv(&rettv);
1172 return NULL;
1173 }
1174
1175 return rettv.vval.v_list;
1176}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177#endif
1178
Bram Moolenaar05159a02005-02-26 23:04:13 +00001179
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180#ifdef FEAT_FOLDING
1181/*
1182 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1183 * it in "*cp". Doesn't give error messages.
1184 */
1185 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001186eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187{
Bram Moolenaar33570922005-01-25 22:26:29 +00001188 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001189 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001191 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1192 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193
1194 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001195 if (use_sandbox)
1196 ++sandbox;
1197 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001199 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 retval = 0;
1201 else
1202 {
1203 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001204 if (tv.v_type == VAR_NUMBER)
1205 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001206 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 retval = 0;
1208 else
1209 {
1210 /* If the result is a string, check if there is a non-digit before
1211 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001212 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 if (!VIM_ISDIGIT(*s) && *s != '-')
1214 *cp = *s++;
1215 retval = atol((char *)s);
1216 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001217 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 }
1219 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001220 if (use_sandbox)
1221 --sandbox;
1222 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001224 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225}
1226#endif
1227
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228/*
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001229 * Get a list of lines from a HERE document. The here document is a list of
1230 * lines surrounded by a marker.
1231 * cmd << {marker}
1232 * {line1}
1233 * {line2}
1234 * ....
1235 * {marker}
1236 *
1237 * The {marker} is a string. If the optional 'trim' word is supplied before the
1238 * marker, then the leading indentation before the lines (matching the
1239 * indentation in the 'cmd' line) is stripped.
1240 * Returns a List with {lines} or NULL.
1241 */
1242 static list_T *
1243heredoc_get(exarg_T *eap, char_u *cmd)
1244{
1245 char_u *theline;
1246 char_u *marker;
1247 list_T *l;
1248 char_u *p;
1249 int indent_len = 0;
1250
1251 if (eap->getline == NULL)
1252 {
1253 emsg(_("E991: cannot use =<< here"));
1254 return NULL;
1255 }
1256
1257 // Check for the optional 'trim' word before the marker
1258 cmd = skipwhite(cmd);
1259 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
1260 {
1261 cmd = skipwhite(cmd + 4);
1262
1263 // Trim the indentation from all the lines in the here document
1264 // The amount of indentation trimmed is the same as the indentation of
1265 // the :let command line.
1266 p = *eap->cmdlinep;
1267 while (VIM_ISWHITE(*p))
1268 {
1269 p++;
1270 indent_len++;
1271 }
1272 }
1273
1274 // The marker is the next word. Default marker is "."
1275 if (*cmd != NUL && *cmd != '"')
1276 {
1277 marker = skipwhite(cmd);
1278 p = skiptowhite(marker);
1279 if (*skipwhite(p) != NUL && *skipwhite(p) != '"')
1280 {
1281 emsg(_(e_trailing));
1282 return NULL;
1283 }
1284 *p = NUL;
1285 }
1286 else
1287 marker = (char_u *)".";
1288
1289 l = list_alloc();
1290 if (l == NULL)
1291 return NULL;
1292
1293 for (;;)
1294 {
1295 int i = 0;
1296
1297 theline = eap->getline(NUL, eap->cookie, 0);
1298 if (theline != NULL && indent_len > 0)
1299 {
1300 // trim the indent matching the first line
1301 if (STRNCMP(theline, *eap->cmdlinep, indent_len) == 0)
1302 i = indent_len;
1303 }
1304
1305 if (theline == NULL)
1306 {
1307 semsg(_("E990: Missing end marker '%s'"), marker);
1308 break;
1309 }
1310 if (STRCMP(marker, theline + i) == 0)
1311 {
1312 vim_free(theline);
1313 break;
1314 }
1315
1316 if (list_append_string(l, theline + i, -1) == FAIL)
1317 break;
1318 vim_free(theline);
1319 }
1320
1321 return l;
1322}
1323
1324/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001325 * ":let" list all variable values
1326 * ":let var1 var2" list variable values
1327 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001328 * ":let var += expr" assignment command.
1329 * ":let var -= expr" assignment command.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001330 * ":let var *= expr" assignment command.
1331 * ":let var /= expr" assignment command.
1332 * ":let var %= expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001333 * ":let var .= expr" assignment command.
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001334 * ":let var ..= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001335 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 */
1337 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001338ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339{
1340 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001341 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001342 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001344 int var_count = 0;
1345 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001346 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001347 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001348 int first = TRUE;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001349 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350
Bram Moolenaardb552d602006-03-23 22:59:57 +00001351 argend = skip_var_list(arg, &var_count, &semicolon);
1352 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001353 return;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001354 if (argend > arg && argend[-1] == '.') // for var.='str'
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001355 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001356 expr = skipwhite(argend);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001357 concat = expr[0] == '.'
1358 && ((expr[1] == '=' && current_sctx.sc_version < 2)
1359 || (expr[1] == '.' && expr[2] == '='));
1360 if (*expr != '=' && !((vim_strchr((char_u *)"+-*/%", *expr) != NULL
1361 && expr[1] == '=') || concat))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001363 /*
1364 * ":let" without "=": list variables
1365 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001366 if (*arg == '[')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001367 emsg(_(e_invarg));
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001368 else if (expr[0] == '.')
1369 emsg(_("E985: .= is not supported with script version 2"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001370 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001371 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001372 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001373 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001374 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001375 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001376 list_glob_vars(&first);
1377 list_buf_vars(&first);
1378 list_win_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001379 list_tab_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001380 list_script_vars(&first);
1381 list_func_vars(&first);
1382 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 eap->nextcmd = check_nextcmd(arg);
1385 }
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001386 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
1387 {
1388 list_T *l;
1389
1390 // HERE document
1391 l = heredoc_get(eap, expr + 3);
1392 if (l != NULL)
1393 {
1394 rettv_list_set(&rettv, l);
1395 op[0] = '=';
1396 op[1] = NUL;
1397 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
1398 op);
1399 clear_tv(&rettv);
1400 }
1401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 else
1403 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001404 op[0] = '=';
1405 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001406 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001407 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001408 if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001409 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001410 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001411 if (expr[0] == '.' && expr[1] == '.') // ..=
1412 ++expr;
1413 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001414 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001415 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001416 else
1417 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001418
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 if (eap->skip)
1420 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001421 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 if (eap->skip)
1423 {
1424 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001425 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 --emsg_skip;
1427 }
1428 else if (i != FAIL)
1429 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001430 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001431 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001432 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 }
1434 }
1435}
1436
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001437/*
1438 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1439 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001440 * When "nextchars" is not NULL it points to a string with characters that
1441 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1442 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001443 * Returns OK or FAIL;
1444 */
1445 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001446ex_let_vars(
1447 char_u *arg_start,
1448 typval_T *tv,
1449 int copy, /* copy values from "tv", don't move */
1450 int semicolon, /* from skip_var_list() */
1451 int var_count, /* from skip_var_list() */
1452 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001453{
1454 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001455 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001456 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001457 listitem_T *item;
1458 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001459
1460 if (*arg != '[')
1461 {
1462 /*
1463 * ":let var = expr" or ":for var in list"
1464 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001465 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001466 return FAIL;
1467 return OK;
1468 }
1469
1470 /*
1471 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1472 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001473 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001474 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001475 emsg(_(e_listreq));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001476 return FAIL;
1477 }
1478
1479 i = list_len(l);
1480 if (semicolon == 0 && var_count < i)
1481 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001482 emsg(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001483 return FAIL;
1484 }
1485 if (var_count - semicolon > i)
1486 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001487 emsg(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001488 return FAIL;
1489 }
1490
1491 item = l->lv_first;
1492 while (*arg != ']')
1493 {
1494 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001495 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001496 item = item->li_next;
1497 if (arg == NULL)
1498 return FAIL;
1499
1500 arg = skipwhite(arg);
1501 if (*arg == ';')
1502 {
1503 /* Put the rest of the list (may be empty) in the var after ';'.
1504 * Create a new list for this. */
1505 l = list_alloc();
1506 if (l == NULL)
1507 return FAIL;
1508 while (item != NULL)
1509 {
1510 list_append_tv(l, &item->li_tv);
1511 item = item->li_next;
1512 }
1513
1514 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001515 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001516 ltv.vval.v_list = l;
1517 l->lv_refcount = 1;
1518
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001519 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1520 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001521 clear_tv(&ltv);
1522 if (arg == NULL)
1523 return FAIL;
1524 break;
1525 }
1526 else if (*arg != ',' && *arg != ']')
1527 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001528 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001529 return FAIL;
1530 }
1531 }
1532
1533 return OK;
1534}
1535
1536/*
1537 * Skip over assignable variable "var" or list of variables "[var, var]".
1538 * Used for ":let varvar = expr" and ":for varvar in expr".
1539 * For "[var, var]" increment "*var_count" for each variable.
1540 * for "[var, var; var]" set "semicolon".
1541 * Return NULL for an error.
1542 */
1543 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001544skip_var_list(
1545 char_u *arg,
1546 int *var_count,
1547 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001548{
1549 char_u *p, *s;
1550
1551 if (*arg == '[')
1552 {
1553 /* "[var, var]": find the matching ']'. */
1554 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001555 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001556 {
1557 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1558 s = skip_var_one(p);
1559 if (s == p)
1560 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001561 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001562 return NULL;
1563 }
1564 ++*var_count;
1565
1566 p = skipwhite(s);
1567 if (*p == ']')
1568 break;
1569 else if (*p == ';')
1570 {
1571 if (*semicolon == 1)
1572 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001573 emsg(_("Double ; in list of variables"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001574 return NULL;
1575 }
1576 *semicolon = 1;
1577 }
1578 else if (*p != ',')
1579 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001580 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001581 return NULL;
1582 }
1583 }
1584 return p + 1;
1585 }
1586 else
1587 return skip_var_one(arg);
1588}
1589
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001590/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001591 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001592 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001593 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001594 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001595skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001596{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001597 if (*arg == '@' && arg[1] != NUL)
1598 return arg + 2;
1599 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1600 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001601}
1602
Bram Moolenaara7043832005-01-21 11:56:39 +00001603/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001604 * List variables for hashtab "ht" with prefix "prefix".
1605 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001606 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001607 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001608list_hashtable_vars(
1609 hashtab_T *ht,
Bram Moolenaar32526b32019-01-19 17:43:09 +01001610 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01001611 int empty,
1612 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001613{
Bram Moolenaar33570922005-01-25 22:26:29 +00001614 hashitem_T *hi;
1615 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001616 int todo;
Bram Moolenaarf86db782018-10-25 13:31:37 +02001617 char_u buf[IOSIZE];
Bram Moolenaara7043832005-01-21 11:56:39 +00001618
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001619 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001620 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1621 {
1622 if (!HASHITEM_EMPTY(hi))
1623 {
1624 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001625 di = HI2DI(hi);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001626
1627 // apply :filter /pat/ to variable name
Bram Moolenaar32526b32019-01-19 17:43:09 +01001628 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1629 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001630 if (message_filtered(buf))
1631 continue;
1632
Bram Moolenaar33570922005-01-25 22:26:29 +00001633 if (empty || di->di_tv.v_type != VAR_STRING
1634 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001635 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001636 }
1637 }
1638}
1639
1640/*
1641 * List global variables.
1642 */
1643 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001644list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001645{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001646 list_hashtable_vars(&globvarht, "", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001647}
1648
1649/*
1650 * List buffer variables.
1651 */
1652 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001653list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001654{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001655 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001656}
1657
1658/*
1659 * List window variables.
1660 */
1661 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001662list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001663{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001664 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001665}
1666
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001667/*
1668 * List tab page variables.
1669 */
1670 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001671list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001672{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001673 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001674}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001675
Bram Moolenaara7043832005-01-21 11:56:39 +00001676/*
1677 * List Vim variables.
1678 */
1679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001680list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001681{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001682 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001683}
1684
1685/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001686 * List script-local variables, if there is a script.
1687 */
1688 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001689list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001690{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001691 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
1692 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
Bram Moolenaar32526b32019-01-19 17:43:09 +01001693 "s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001694}
1695
1696/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001697 * List variables in "arg".
1698 */
1699 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001700list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001701{
1702 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001703 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001704 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001705 char_u *name_start;
1706 char_u *arg_subsc;
1707 char_u *tofree;
1708 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001709
1710 while (!ends_excmd(*arg) && !got_int)
1711 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001712 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001713 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001714 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001715 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001716 {
1717 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001718 emsg(_(e_trailing));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001719 break;
1720 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001721 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001722 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001723 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001724 /* get_name_len() takes care of expanding curly braces */
1725 name_start = name = arg;
1726 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1727 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001728 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001729 /* This is mainly to keep test 49 working: when expanding
1730 * curly braces fails overrule the exception error message. */
1731 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001732 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001733 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001734 semsg(_(e_invarg2), arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001735 break;
1736 }
1737 error = TRUE;
1738 }
1739 else
1740 {
1741 if (tofree != NULL)
1742 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001743 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001744 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001745 else
1746 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001747 /* handle d.key, l[idx], f(expr) */
1748 arg_subsc = arg;
1749 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001750 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001751 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001752 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001753 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001754 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001755 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001756 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001757 case 'g': list_glob_vars(first); break;
1758 case 'b': list_buf_vars(first); break;
1759 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001760 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001761 case 'v': list_vim_vars(first); break;
1762 case 's': list_script_vars(first); break;
1763 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001764 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001765 semsg(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001766 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001767 }
1768 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001769 {
1770 char_u numbuf[NUMBUFLEN];
1771 char_u *tf;
1772 int c;
1773 char_u *s;
1774
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001775 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001776 c = *arg;
1777 *arg = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001778 list_one_var_a("",
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001779 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001780 tv.v_type,
1781 s == NULL ? (char_u *)"" : s,
1782 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001783 *arg = c;
1784 vim_free(tf);
1785 }
1786 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001787 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001788 }
1789 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001790
1791 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001792 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001793
1794 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001795 }
1796
1797 return arg;
1798}
1799
1800/*
1801 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1802 * Returns a pointer to the char just after the var name.
1803 * Returns NULL if there is an error.
1804 */
1805 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001806ex_let_one(
1807 char_u *arg, /* points to variable name */
1808 typval_T *tv, /* value to assign to variable */
1809 int copy, /* copy value from "tv" */
1810 char_u *endchars, /* valid chars after variable name or NULL */
1811 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001812{
1813 int c1;
1814 char_u *name;
1815 char_u *p;
1816 char_u *arg_end = NULL;
1817 int len;
1818 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001819 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001820
1821 /*
1822 * ":let $VAR = expr": Set environment variable.
1823 */
1824 if (*arg == '$')
1825 {
1826 /* Find the end of the name. */
1827 ++arg;
1828 name = arg;
1829 len = get_env_len(&arg);
1830 if (len == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001831 semsg(_(e_invarg2), name - 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001832 else
1833 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001834 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001835 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001836 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001837 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001838 emsg(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001839 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001840 {
1841 c1 = name[len];
1842 name[len] = NUL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001843 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001844 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001845 {
1846 int mustfree = FALSE;
1847 char_u *s = vim_getenv(name, &mustfree);
1848
1849 if (s != NULL)
1850 {
1851 p = tofree = concat_str(s, p);
1852 if (mustfree)
1853 vim_free(s);
1854 }
1855 }
1856 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001857 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001858 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001859 if (STRICMP(name, "HOME") == 0)
1860 init_homedir();
1861 else if (didset_vim && STRICMP(name, "VIM") == 0)
1862 didset_vim = FALSE;
1863 else if (didset_vimruntime
1864 && STRICMP(name, "VIMRUNTIME") == 0)
1865 didset_vimruntime = FALSE;
1866 arg_end = arg;
1867 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001868 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001869 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001870 }
1871 }
1872 }
1873
1874 /*
1875 * ":let &option = expr": Set option value.
1876 * ":let &l:option = expr": Set local option value.
1877 * ":let &g:option = expr": Set global option value.
1878 */
1879 else if (*arg == '&')
1880 {
1881 /* Find the end of the name. */
1882 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001883 if (p == NULL || (endchars != NULL
1884 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001885 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001886 else
1887 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001888 long n;
1889 int opt_type;
1890 long numval;
1891 char_u *stringval = NULL;
1892 char_u *s;
1893
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894 c1 = *p;
1895 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001896
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001897 n = (long)tv_get_number(tv);
1898 s = tv_get_string_chk(tv); /* != NULL if number or string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001899 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001900 {
1901 opt_type = get_option_value(arg, &numval,
1902 &stringval, opt_flags);
1903 if ((opt_type == 1 && *op == '.')
1904 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001905 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001906 semsg(_(e_letwrong), op);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001907 s = NULL; // don't set the value
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001908 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001909 else
1910 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001911 if (opt_type == 1) // number
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001912 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001913 switch (*op)
1914 {
1915 case '+': n = numval + n; break;
1916 case '-': n = numval - n; break;
1917 case '*': n = numval * n; break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001918 case '/': n = (long)num_divide(numval, n); break;
1919 case '%': n = (long)num_modulus(numval, n); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001920 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001921 }
Bram Moolenaarff697e62019-02-12 22:28:33 +01001922 else if (opt_type == 0 && stringval != NULL) // string
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001923 {
1924 s = concat_str(stringval, s);
1925 vim_free(stringval);
1926 stringval = s;
1927 }
1928 }
1929 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001930 if (s != NULL)
1931 {
1932 set_option_value(arg, n, s, opt_flags);
1933 arg_end = p;
1934 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001935 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001936 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001937 }
1938 }
1939
1940 /*
1941 * ":let @r = expr": Set register contents.
1942 */
1943 else if (*arg == '@')
1944 {
1945 ++arg;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001946 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001947 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001948 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001949 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001950 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001951 else
1952 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001953 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001954 char_u *s;
1955
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001956 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001957 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001958 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02001959 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001960 if (s != NULL)
1961 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001962 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001963 vim_free(s);
1964 }
1965 }
1966 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001967 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001968 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001969 arg_end = arg + 1;
1970 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001971 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001972 }
1973 }
1974
1975 /*
1976 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001977 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001978 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001979 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001980 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001981 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001982
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001983 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001984 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001985 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001986 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001987 emsg(_(e_letunexp));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001988 else
1989 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001990 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001991 arg_end = p;
1992 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001993 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001994 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001995 }
1996
1997 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001998 semsg(_(e_invarg2), arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001999
2000 return arg_end;
2001}
2002
2003/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002004 * Get an lval: variable, Dict item or List item that can be assigned a value
2005 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2006 * "name.key", "name.key[expr]" etc.
2007 * Indexing only works if "name" is an existing List or Dictionary.
2008 * "name" points to the start of the name.
2009 * If "rettv" is not NULL it points to the value to be assigned.
2010 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2011 * wrong; must end in space or cmd separator.
2012 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002013 * flags:
2014 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01002015 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002016 * GLV_NO_AUTOLOAD: do not use script autoloading
2017 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002018 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002019 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002020 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002021 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002022 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002023get_lval(
2024 char_u *name,
2025 typval_T *rettv,
2026 lval_T *lp,
2027 int unlet,
2028 int skip,
2029 int flags, /* GLV_ values */
2030 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002031{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002032 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002033 char_u *expr_start, *expr_end;
2034 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002035 dictitem_T *v;
2036 typval_T var1;
2037 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002038 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002039 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002040 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002041 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002042 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002043 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002044
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002045 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002046 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002047
2048 if (skip)
2049 {
2050 /* When skipping just find the end of the name. */
2051 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002052 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002053 }
2054
2055 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002056 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002057 if (expr_start != NULL)
2058 {
2059 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002060 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002061 && *p != '[' && *p != '.')
2062 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002063 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002064 return NULL;
2065 }
2066
2067 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2068 if (lp->ll_exp_name == NULL)
2069 {
2070 /* Report an invalid expression in braces, unless the
2071 * expression evaluation has been cancelled due to an
2072 * aborting error, an interrupt, or an exception. */
2073 if (!aborting() && !quiet)
2074 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002075 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002076 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002077 return NULL;
2078 }
2079 }
2080 lp->ll_name = lp->ll_exp_name;
2081 }
2082 else
2083 lp->ll_name = name;
2084
2085 /* Without [idx] or .key we are done. */
2086 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2087 return p;
2088
2089 cc = *p;
2090 *p = NUL;
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002091 /* Only pass &ht when we would write to the variable, it prevents autoload
2092 * as well. */
2093 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
2094 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002095 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002096 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002097 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002098 if (v == NULL)
2099 return NULL;
2100
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002101 /*
2102 * Loop until no more [idx] or .key is following.
2103 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002104 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002105 var1.v_type = VAR_UNKNOWN;
2106 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002107 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002108 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002109 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2110 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002111 && lp->ll_tv->vval.v_dict != NULL)
2112 && !(lp->ll_tv->v_type == VAR_BLOB
2113 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002114 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002115 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002116 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002117 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002118 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002119 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002120 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002121 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002122 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002123 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002124 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002125
Bram Moolenaar8c711452005-01-14 21:53:12 +00002126 len = -1;
2127 if (*p == '.')
2128 {
2129 key = p + 1;
2130 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2131 ;
2132 if (len == 0)
2133 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002134 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002135 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002136 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002137 }
2138 p = key + len;
2139 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002140 else
2141 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002142 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002143 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002144 if (*p == ':')
2145 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002146 else
2147 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002148 empty1 = FALSE;
2149 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002150 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002151 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002152 {
2153 /* not a number or string */
2154 clear_tv(&var1);
2155 return NULL;
2156 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002157 }
2158
2159 /* Optionally get the second index [ :expr]. */
2160 if (*p == ':')
2161 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002162 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002163 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002164 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002165 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002166 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002167 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002168 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002169 if (rettv != NULL
2170 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002171 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002172 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002173 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002174 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002175 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002176 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002177 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002178 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002179 }
2180 p = skipwhite(p + 1);
2181 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002182 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002183 else
2184 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002185 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002186 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2187 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002188 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002189 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002190 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002191 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002192 {
2193 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002194 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002195 clear_tv(&var2);
2196 return NULL;
2197 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002198 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002199 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002200 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002201 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002202 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002203
Bram Moolenaar8c711452005-01-14 21:53:12 +00002204 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002205 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002206 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002207 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002208 clear_tv(&var1);
2209 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002210 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002211 }
2212
2213 /* Skip to past ']'. */
2214 ++p;
2215 }
2216
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002217 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002218 {
2219 if (len == -1)
2220 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002221 /* "[key]": get key from "var1" */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002222 key = tv_get_string_chk(&var1); /* is number or string */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002223 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002224 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002225 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002226 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002227 }
2228 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002229 lp->ll_list = NULL;
2230 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002231 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002232
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002233 /* When assigning to a scope dictionary check that a function and
2234 * variable name is valid (only variable name unless it is l: or
2235 * g: dictionary). Disallow overwriting a builtin function. */
2236 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002237 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002238 int prevval;
2239 int wrong;
2240
2241 if (len != -1)
2242 {
2243 prevval = key[len];
2244 key[len] = NUL;
2245 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002246 else
2247 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002248 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2249 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002250 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002251 || !valid_varname(key);
2252 if (len != -1)
2253 key[len] = prevval;
2254 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002255 return NULL;
2256 }
2257
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002258 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002259 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01002260 // Can't add "v:" or "a:" variable.
2261 if (lp->ll_dict == &vimvardict
2262 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002263 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002264 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01002265 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002266 return NULL;
2267 }
2268
Bram Moolenaar31b81602019-02-10 22:14:27 +01002269 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002270 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002271 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002272 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002273 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002274 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002275 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002276 }
2277 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002278 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002279 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002280 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002281 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002282 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002283 p = NULL;
2284 break;
2285 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002286 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002287 else if ((flags & GLV_READ_ONLY) == 0
2288 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002289 {
2290 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002291 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002292 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002293
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002294 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002295 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002296 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002297 else if (lp->ll_tv->v_type == VAR_BLOB)
2298 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002299 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
2300
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002301 /*
2302 * Get the number and item for the only or first index of the List.
2303 */
2304 if (empty1)
2305 lp->ll_n1 = 0;
2306 else
2307 // is number or string
2308 lp->ll_n1 = (long)tv_get_number(&var1);
2309 clear_tv(&var1);
2310
2311 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002312 || lp->ll_n1 > bloblen
2313 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002314 {
2315 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002316 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002317 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002318 return NULL;
2319 }
2320 if (lp->ll_range && !lp->ll_empty2)
2321 {
2322 lp->ll_n2 = (long)tv_get_number(&var2);
2323 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002324 if (lp->ll_n2 < 0
2325 || lp->ll_n2 >= bloblen
2326 || lp->ll_n2 < lp->ll_n1)
2327 {
2328 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002329 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002330 return NULL;
2331 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002332 }
2333 lp->ll_blob = lp->ll_tv->vval.v_blob;
2334 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01002335 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002336 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002337 else
2338 {
2339 /*
2340 * Get the number and item for the only or first index of the List.
2341 */
2342 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002343 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002344 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002345 /* is number or string */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002346 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002347 clear_tv(&var1);
2348
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002349 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002350 lp->ll_list = lp->ll_tv->vval.v_list;
2351 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2352 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002353 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002354 if (lp->ll_n1 < 0)
2355 {
2356 lp->ll_n1 = 0;
2357 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2358 }
2359 }
2360 if (lp->ll_li == NULL)
2361 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002362 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002363 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002364 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002365 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002366 }
2367
2368 /*
2369 * May need to find the item or absolute index for the second
2370 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002371 * When no index given: "lp->ll_empty2" is TRUE.
2372 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002373 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002374 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002375 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002376 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002377 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002378 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002379 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002380 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002381 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002382 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002383 {
2384 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002385 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002386 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002387 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002388 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002389 }
2390
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002391 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2392 if (lp->ll_n1 < 0)
2393 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2394 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002395 {
2396 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002397 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002398 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002399 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002400 }
2401
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002402 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002403 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002404 }
2405
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002406 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002407 return p;
2408}
2409
2410/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002411 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002412 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002413 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002414clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002415{
2416 vim_free(lp->ll_exp_name);
2417 vim_free(lp->ll_newkey);
2418}
2419
2420/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002421 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002422 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01002423 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
2424 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002425 */
2426 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002427set_var_lval(
2428 lval_T *lp,
2429 char_u *endp,
2430 typval_T *rettv,
2431 int copy,
2432 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002433{
2434 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002435 listitem_T *ri;
2436 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002437
2438 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002439 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002440 cc = *endp;
2441 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002442 if (lp->ll_blob != NULL)
2443 {
2444 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002445
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002446 if (op != NULL && *op != '=')
2447 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002448 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002449 return;
2450 }
2451
2452 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2453 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002454 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002455
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002456 if (lp->ll_empty2)
2457 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
2458
2459 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002460 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002461 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002462 return;
2463 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002464 if (lp->ll_empty2)
2465 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002466
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002467 ir = 0;
2468 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
2469 blob_set(lp->ll_blob, il,
2470 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002471 }
2472 else
2473 {
2474 val = (int)tv_get_number_chk(rettv, &error);
2475 if (!error)
2476 {
2477 garray_T *gap = &lp->ll_blob->bv_ga;
2478
2479 // Allow for appending a byte. Setting a byte beyond
2480 // the end is an error otherwise.
2481 if (lp->ll_n1 < gap->ga_len
2482 || (lp->ll_n1 == gap->ga_len
2483 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
2484 {
2485 blob_set(lp->ll_blob, lp->ll_n1, val);
2486 if (lp->ll_n1 == gap->ga_len)
2487 ++gap->ga_len;
2488 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002489 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002490 }
2491 }
2492 }
2493 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002494 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002495 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002496
Bram Moolenaarff697e62019-02-12 22:28:33 +01002497 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01002498 di = NULL;
2499 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2500 &tv, &di, TRUE, FALSE) == OK)
2501 {
2502 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002503 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2504 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01002505 && tv_op(&tv, rettv, op) == OK)
2506 set_var(lp->ll_name, &tv, FALSE);
2507 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002508 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002509 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002510 else
2511 set_var(lp->ll_name, rettv, copy);
2512 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002514 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002515 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002516 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002517 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518 else if (lp->ll_range)
2519 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002520 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002521 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002522
2523 /*
2524 * Check whether any of the list items is locked
2525 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002526 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002527 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002528 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002529 return;
2530 ri = ri->li_next;
2531 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2532 break;
2533 ll_li = ll_li->li_next;
2534 ++ll_n1;
2535 }
2536
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 /*
2538 * Assign the List values to the list items.
2539 */
2540 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002541 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002542 if (op != NULL && *op != '=')
2543 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2544 else
2545 {
2546 clear_tv(&lp->ll_li->li_tv);
2547 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2548 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549 ri = ri->li_next;
2550 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2551 break;
2552 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002553 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002555 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002556 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557 ri = NULL;
2558 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002559 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002560 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 lp->ll_li = lp->ll_li->li_next;
2562 ++lp->ll_n1;
2563 }
2564 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002565 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002566 else if (lp->ll_empty2
2567 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002569 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 }
2571 else
2572 {
2573 /*
2574 * Assign to a List or Dictionary item.
2575 */
2576 if (lp->ll_newkey != NULL)
2577 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002578 if (op != NULL && *op != '=')
2579 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002580 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002581 return;
2582 }
2583
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002584 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002585 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002586 if (di == NULL)
2587 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002588 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2589 {
2590 vim_free(di);
2591 return;
2592 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002593 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002594 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002595 else if (op != NULL && *op != '=')
2596 {
2597 tv_op(lp->ll_tv, rettv, op);
2598 return;
2599 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002600 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002602
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 /*
2604 * Assign the value to the variable or list item.
2605 */
2606 if (copy)
2607 copy_tv(rettv, lp->ll_tv);
2608 else
2609 {
2610 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002611 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002613 }
2614 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002615}
2616
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002617/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01002618 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2619 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002620 * Returns OK or FAIL.
2621 */
2622 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002623tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002624{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002625 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002626 char_u numbuf[NUMBUFLEN];
2627 char_u *s;
2628
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002629 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2630 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2631 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002632 {
2633 switch (tv1->v_type)
2634 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002635 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002636 case VAR_DICT:
2637 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002638 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002639 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002640 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002641 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002642 break;
2643
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002644 case VAR_BLOB:
2645 if (*op != '+' || tv2->v_type != VAR_BLOB)
2646 break;
2647 // BLOB += BLOB
2648 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2649 {
2650 blob_T *b1 = tv1->vval.v_blob;
2651 blob_T *b2 = tv2->vval.v_blob;
2652 int i, len = blob_len(b2);
2653 for (i = 0; i < len; i++)
2654 ga_append(&b1->bv_ga, blob_get(b2, i));
2655 }
2656 return OK;
2657
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002658 case VAR_LIST:
2659 if (*op != '+' || tv2->v_type != VAR_LIST)
2660 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002661 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002662 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2663 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2664 return OK;
2665
2666 case VAR_NUMBER:
2667 case VAR_STRING:
2668 if (tv2->v_type == VAR_LIST)
2669 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002670 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002671 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002672 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002673 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002674#ifdef FEAT_FLOAT
2675 if (tv2->v_type == VAR_FLOAT)
2676 {
2677 float_T f = n;
2678
Bram Moolenaarff697e62019-02-12 22:28:33 +01002679 if (*op == '%')
2680 break;
2681 switch (*op)
2682 {
2683 case '+': f += tv2->vval.v_float; break;
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 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002688 clear_tv(tv1);
2689 tv1->v_type = VAR_FLOAT;
2690 tv1->vval.v_float = f;
2691 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002692 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002693#endif
2694 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002695 switch (*op)
2696 {
2697 case '+': n += tv_get_number(tv2); break;
2698 case '-': n -= tv_get_number(tv2); break;
2699 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01002700 case '/': n = num_divide(n, tv_get_number(tv2)); break;
2701 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002702 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002703 clear_tv(tv1);
2704 tv1->v_type = VAR_NUMBER;
2705 tv1->vval.v_number = n;
2706 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002707 }
2708 else
2709 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002710 if (tv2->v_type == VAR_FLOAT)
2711 break;
2712
Bram Moolenaarff697e62019-02-12 22:28:33 +01002713 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002714 s = tv_get_string(tv1);
2715 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002716 clear_tv(tv1);
2717 tv1->v_type = VAR_STRING;
2718 tv1->vval.v_string = s;
2719 }
2720 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002721
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002722 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002723#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002724 {
2725 float_T f;
2726
Bram Moolenaarff697e62019-02-12 22:28:33 +01002727 if (*op == '%' || *op == '.'
2728 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002729 && tv2->v_type != VAR_NUMBER
2730 && tv2->v_type != VAR_STRING))
2731 break;
2732 if (tv2->v_type == VAR_FLOAT)
2733 f = tv2->vval.v_float;
2734 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002735 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01002736 switch (*op)
2737 {
2738 case '+': tv1->vval.v_float += f; break;
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 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002743 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002744#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002745 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002746 }
2747 }
2748
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002749 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002750 return FAIL;
2751}
2752
2753/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002754 * Evaluate the expression used in a ":for var in expr" command.
2755 * "arg" points to "var".
2756 * Set "*errp" to TRUE for an error, FALSE otherwise;
2757 * Return a pointer that holds the info. Null when there is an error.
2758 */
2759 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002760eval_for_line(
2761 char_u *arg,
2762 int *errp,
2763 char_u **nextcmdp,
2764 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002765{
Bram Moolenaar33570922005-01-25 22:26:29 +00002766 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002767 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002768 typval_T tv;
2769 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002770
2771 *errp = TRUE; /* default: there is an error */
2772
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002773 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002774 if (fi == NULL)
2775 return NULL;
2776
2777 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2778 if (expr == NULL)
2779 return fi;
2780
2781 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002782 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002783 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002784 emsg(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002785 return fi;
2786 }
2787
2788 if (skip)
2789 ++emsg_skip;
2790 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2791 {
2792 *errp = FALSE;
2793 if (!skip)
2794 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002795 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002796 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002797 l = tv.vval.v_list;
2798 if (l == NULL)
2799 {
2800 // a null list is like an empty list: do nothing
2801 clear_tv(&tv);
2802 }
2803 else
2804 {
2805 // No need to increment the refcount, it's already set for
2806 // the list being used in "tv".
2807 fi->fi_list = l;
2808 list_add_watch(l, &fi->fi_lw);
2809 fi->fi_lw.lw_item = l->lv_first;
2810 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002811 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002812 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002813 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002814 fi->fi_bi = 0;
2815 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002816 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002817 typval_T btv;
2818
2819 // Make a copy, so that the iteration still works when the
2820 // blob is changed.
2821 blob_copy(&tv, &btv);
2822 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002823 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002824 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002825 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002826 else
2827 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002828 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002829 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002830 }
2831 }
2832 }
2833 if (skip)
2834 --emsg_skip;
2835
2836 return fi;
2837}
2838
2839/*
2840 * Use the first item in a ":for" list. Advance to the next.
2841 * Assign the values to the variable (list). "arg" points to the first one.
2842 * Return TRUE when a valid item was found, FALSE when at end of list or
2843 * something wrong.
2844 */
2845 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002846next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002847{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002848 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002849 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002850 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002851
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002852 if (fi->fi_blob != NULL)
2853 {
2854 typval_T tv;
2855
2856 if (fi->fi_bi >= blob_len(fi->fi_blob))
2857 return FALSE;
2858 tv.v_type = VAR_NUMBER;
2859 tv.v_lock = VAR_FIXED;
2860 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2861 ++fi->fi_bi;
2862 return ex_let_vars(arg, &tv, TRUE,
2863 fi->fi_semicolon, fi->fi_varcount, NULL) == OK;
2864 }
2865
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002866 item = fi->fi_lw.lw_item;
2867 if (item == NULL)
2868 result = FALSE;
2869 else
2870 {
2871 fi->fi_lw.lw_item = item->li_next;
2872 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2873 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2874 }
2875 return result;
2876}
2877
2878/*
2879 * Free the structure used to store info used by ":for".
2880 */
2881 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002882free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002883{
Bram Moolenaar33570922005-01-25 22:26:29 +00002884 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002885
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002886 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002887 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002888 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002889 list_unref(fi->fi_list);
2890 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002891 if (fi != NULL && fi->fi_blob != NULL)
2892 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002893 vim_free(fi);
2894}
2895
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2897
2898 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002899set_context_for_expression(
2900 expand_T *xp,
2901 char_u *arg,
2902 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903{
2904 int got_eq = FALSE;
2905 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002906 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002908 if (cmdidx == CMD_let)
2909 {
2910 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002911 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002912 {
2913 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002914 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002915 {
2916 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002917 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002918 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002919 break;
2920 }
2921 return;
2922 }
2923 }
2924 else
2925 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2926 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 while ((xp->xp_pattern = vim_strpbrk(arg,
2928 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2929 {
2930 c = *xp->xp_pattern;
2931 if (c == '&')
2932 {
2933 c = xp->xp_pattern[1];
2934 if (c == '&')
2935 {
2936 ++xp->xp_pattern;
2937 xp->xp_context = cmdidx != CMD_let || got_eq
2938 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2939 }
2940 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002941 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002943 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2944 xp->xp_pattern += 2;
2945
2946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 }
2948 else if (c == '$')
2949 {
2950 /* environment variable */
2951 xp->xp_context = EXPAND_ENV_VARS;
2952 }
2953 else if (c == '=')
2954 {
2955 got_eq = TRUE;
2956 xp->xp_context = EXPAND_EXPRESSION;
2957 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002958 else if (c == '#'
2959 && xp->xp_context == EXPAND_EXPRESSION)
2960 {
2961 /* Autoload function/variable contains '#'. */
2962 break;
2963 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002964 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 && xp->xp_context == EXPAND_FUNCTIONS
2966 && vim_strchr(xp->xp_pattern, '(') == NULL)
2967 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002968 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 break;
2970 }
2971 else if (cmdidx != CMD_let || got_eq)
2972 {
2973 if (c == '"') /* string */
2974 {
2975 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2976 if (c == '\\' && xp->xp_pattern[1] != NUL)
2977 ++xp->xp_pattern;
2978 xp->xp_context = EXPAND_NOTHING;
2979 }
2980 else if (c == '\'') /* literal string */
2981 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002982 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2984 /* skip */ ;
2985 xp->xp_context = EXPAND_NOTHING;
2986 }
2987 else if (c == '|')
2988 {
2989 if (xp->xp_pattern[1] == '|')
2990 {
2991 ++xp->xp_pattern;
2992 xp->xp_context = EXPAND_EXPRESSION;
2993 }
2994 else
2995 xp->xp_context = EXPAND_COMMANDS;
2996 }
2997 else
2998 xp->xp_context = EXPAND_EXPRESSION;
2999 }
3000 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003001 /* Doesn't look like something valid, expand as an expression
3002 * anyway. */
3003 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 arg = xp->xp_pattern;
3005 if (*arg != NUL)
3006 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3007 /* skip */ ;
3008 }
3009 xp->xp_pattern = arg;
3010}
3011
3012#endif /* FEAT_CMDL_COMPL */
3013
3014/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 * ":unlet[!] var1 ... " command.
3016 */
3017 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003018ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003020 ex_unletlock(eap, eap->arg, 0);
3021}
3022
3023/*
3024 * ":lockvar" and ":unlockvar" commands
3025 */
3026 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003027ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003028{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003030 int deep = 2;
3031
3032 if (eap->forceit)
3033 deep = -1;
3034 else if (vim_isdigit(*arg))
3035 {
3036 deep = getdigits(&arg);
3037 arg = skipwhite(arg);
3038 }
3039
3040 ex_unletlock(eap, arg, deep);
3041}
3042
3043/*
3044 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3045 */
3046 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003047ex_unletlock(
3048 exarg_T *eap,
3049 char_u *argstart,
3050 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003051{
3052 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003055 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056
3057 do
3058 {
Bram Moolenaar137374f2018-05-13 15:59:50 +02003059 if (*arg == '$')
3060 {
3061 char_u *name = ++arg;
3062
3063 if (get_env_len(&arg) == 0)
3064 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003065 semsg(_(e_invarg2), name - 1);
Bram Moolenaar137374f2018-05-13 15:59:50 +02003066 return;
3067 }
3068 vim_unsetenv(name);
3069 arg = skipwhite(arg);
3070 continue;
3071 }
3072
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003073 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003074 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003075 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003076 if (lv.ll_name == NULL)
3077 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003078 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003079 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003081 if (name_end != NULL)
3082 {
3083 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003084 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003085 }
3086 if (!(eap->skip || error))
3087 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 break;
3089 }
3090
3091 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003092 {
3093 if (eap->cmdidx == CMD_unlet)
3094 {
3095 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3096 error = TRUE;
3097 }
3098 else
3099 {
3100 if (do_lock_var(&lv, name_end, deep,
3101 eap->cmdidx == CMD_lockvar) == FAIL)
3102 error = TRUE;
3103 }
3104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003106 if (!eap->skip)
3107 clear_lval(&lv);
3108
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 arg = skipwhite(name_end);
3110 } while (!ends_excmd(*arg));
3111
3112 eap->nextcmd = check_nextcmd(arg);
3113}
3114
Bram Moolenaar8c711452005-01-14 21:53:12 +00003115 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003116do_unlet_var(
3117 lval_T *lp,
3118 char_u *name_end,
3119 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003120{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003121 int ret = OK;
3122 int cc;
3123
3124 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003125 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003126 cc = *name_end;
3127 *name_end = NUL;
3128
3129 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003130 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003131 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003132 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003133 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003134 else if ((lp->ll_list != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003135 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003136 || (lp->ll_dict != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003137 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003138 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003139 else if (lp->ll_range)
3140 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003141 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003142 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003143 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003144
3145 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3146 {
3147 li = ll_li->li_next;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003148 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003149 return FAIL;
3150 ll_li = li;
3151 ++ll_n1;
3152 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003153
3154 /* Delete a range of List items. */
3155 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3156 {
3157 li = lp->ll_li->li_next;
3158 listitem_remove(lp->ll_list, lp->ll_li);
3159 lp->ll_li = li;
3160 ++lp->ll_n1;
3161 }
3162 }
3163 else
3164 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003165 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003166 /* unlet a List item. */
3167 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003168 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003169 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003170 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003171 }
3172
3173 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003174}
3175
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176/*
3177 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003178 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 */
3180 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003181do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182{
Bram Moolenaar33570922005-01-25 22:26:29 +00003183 hashtab_T *ht;
3184 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003185 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003186 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003187 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188
Bram Moolenaar33570922005-01-25 22:26:29 +00003189 ht = find_var_ht(name, &varname);
3190 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003192 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003193 if (d == NULL)
3194 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003195 if (ht == &globvarht)
3196 d = &globvardict;
3197 else if (ht == &compat_hashtab)
3198 d = &vimvardict;
3199 else
3200 {
3201 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3202 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3203 }
3204 if (d == NULL)
3205 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003206 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003207 return FAIL;
3208 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003209 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003210 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003211 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003212 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003213 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003214 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003215 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003216 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003217 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003218 || var_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003219 return FAIL;
3220
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003221 delete_var(ht, hi);
3222 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003225 if (forceit)
3226 return OK;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003227 semsg(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 return FAIL;
3229}
3230
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003231/*
3232 * Lock or unlock variable indicated by "lp".
3233 * "deep" is the levels to go (-1 for unlimited);
3234 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3235 */
3236 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003237do_lock_var(
3238 lval_T *lp,
3239 char_u *name_end,
3240 int deep,
3241 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003242{
3243 int ret = OK;
3244 int cc;
3245 dictitem_T *di;
3246
3247 if (deep == 0) /* nothing to do */
3248 return OK;
3249
3250 if (lp->ll_tv == NULL)
3251 {
3252 cc = *name_end;
3253 *name_end = NUL;
3254
3255 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003256 di = find_var(lp->ll_name, NULL, TRUE);
3257 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003258 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01003259 else if ((di->di_flags & DI_FLAGS_FIX)
3260 && di->di_tv.v_type != VAR_DICT
3261 && di->di_tv.v_type != VAR_LIST)
3262 /* For historic reasons this error is not given for a list or dict.
3263 * E.g., the b: dict could be locked/unlocked. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003264 semsg(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003265 else
3266 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01003267 if (lock)
3268 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003269 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01003270 di->di_flags &= ~DI_FLAGS_LOCK;
3271 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003272 }
3273 *name_end = cc;
3274 }
3275 else if (lp->ll_range)
3276 {
3277 listitem_T *li = lp->ll_li;
3278
3279 /* (un)lock a range of List items. */
3280 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3281 {
3282 item_lock(&li->li_tv, deep, lock);
3283 li = li->li_next;
3284 ++lp->ll_n1;
3285 }
3286 }
3287 else if (lp->ll_list != NULL)
3288 /* (un)lock a List item. */
3289 item_lock(&lp->ll_li->li_tv, deep, lock);
3290 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003291 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003292 item_lock(&lp->ll_di->di_tv, deep, lock);
3293
3294 return ret;
3295}
3296
3297/*
3298 * Lock or unlock an item. "deep" is nr of levels to go.
3299 */
3300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003301item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003302{
3303 static int recurse = 0;
3304 list_T *l;
3305 listitem_T *li;
3306 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003307 blob_T *b;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003308 hashitem_T *hi;
3309 int todo;
3310
3311 if (recurse >= DICT_MAXNEST)
3312 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003313 emsg(_("E743: variable nested too deep for (un)lock"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003314 return;
3315 }
3316 if (deep == 0)
3317 return;
3318 ++recurse;
3319
3320 /* lock/unlock the item itself */
3321 if (lock)
3322 tv->v_lock |= VAR_LOCKED;
3323 else
3324 tv->v_lock &= ~VAR_LOCKED;
3325
3326 switch (tv->v_type)
3327 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003328 case VAR_UNKNOWN:
3329 case VAR_NUMBER:
3330 case VAR_STRING:
3331 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003332 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003333 case VAR_FLOAT:
3334 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003335 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003336 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003337 break;
3338
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003339 case VAR_BLOB:
3340 if ((b = tv->vval.v_blob) != NULL)
3341 {
3342 if (lock)
3343 b->bv_lock |= VAR_LOCKED;
3344 else
3345 b->bv_lock &= ~VAR_LOCKED;
3346 }
3347 break;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003348 case VAR_LIST:
3349 if ((l = tv->vval.v_list) != NULL)
3350 {
3351 if (lock)
3352 l->lv_lock |= VAR_LOCKED;
3353 else
3354 l->lv_lock &= ~VAR_LOCKED;
3355 if (deep < 0 || deep > 1)
3356 /* recursive: lock/unlock the items the List contains */
3357 for (li = l->lv_first; li != NULL; li = li->li_next)
3358 item_lock(&li->li_tv, deep - 1, lock);
3359 }
3360 break;
3361 case VAR_DICT:
3362 if ((d = tv->vval.v_dict) != NULL)
3363 {
3364 if (lock)
3365 d->dv_lock |= VAR_LOCKED;
3366 else
3367 d->dv_lock &= ~VAR_LOCKED;
3368 if (deep < 0 || deep > 1)
3369 {
3370 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003371 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003372 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3373 {
3374 if (!HASHITEM_EMPTY(hi))
3375 {
3376 --todo;
3377 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3378 }
3379 }
3380 }
3381 }
3382 }
3383 --recurse;
3384}
3385
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3387/*
3388 * Delete all "menutrans_" variables.
3389 */
3390 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003391del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392{
Bram Moolenaar33570922005-01-25 22:26:29 +00003393 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003394 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395
Bram Moolenaar33570922005-01-25 22:26:29 +00003396 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003397 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003398 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003399 {
3400 if (!HASHITEM_EMPTY(hi))
3401 {
3402 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003403 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3404 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003405 }
3406 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003407 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408}
3409#endif
3410
3411#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3412
3413/*
3414 * Local string buffer for the next two functions to store a variable name
3415 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3416 * get_user_var_name().
3417 */
3418
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419static char_u *varnamebuf = NULL;
3420static int varnamebuflen = 0;
3421
3422/*
3423 * Function to concatenate a prefix and a variable name.
3424 */
3425 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003426cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427{
3428 int len;
3429
3430 len = (int)STRLEN(name) + 3;
3431 if (len > varnamebuflen)
3432 {
3433 vim_free(varnamebuf);
3434 len += 10; /* some additional space */
3435 varnamebuf = alloc(len);
3436 if (varnamebuf == NULL)
3437 {
3438 varnamebuflen = 0;
3439 return NULL;
3440 }
3441 varnamebuflen = len;
3442 }
3443 *varnamebuf = prefix;
3444 varnamebuf[1] = ':';
3445 STRCPY(varnamebuf + 2, name);
3446 return varnamebuf;
3447}
3448
3449/*
3450 * Function given to ExpandGeneric() to obtain the list of user defined
3451 * (global/buffer/window/built-in) variable names.
3452 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003454get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003456 static long_u gdone;
3457 static long_u bdone;
3458 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003459 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003460 static int vidx;
3461 static hashitem_T *hi;
3462 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463
3464 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003465 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003466 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003467 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003468 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003469
3470 /* Global variables */
3471 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003473 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003474 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003475 else
3476 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003477 while (HASHITEM_EMPTY(hi))
3478 ++hi;
3479 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3480 return cat_prefix_varname('g', hi->hi_key);
3481 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003483
3484 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003485 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003486 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003488 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003489 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003490 else
3491 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003492 while (HASHITEM_EMPTY(hi))
3493 ++hi;
3494 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003496
3497 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003498 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003499 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003501 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003502 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003503 else
3504 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003505 while (HASHITEM_EMPTY(hi))
3506 ++hi;
3507 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003509
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003510 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003511 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003512 if (tdone < ht->ht_used)
3513 {
3514 if (tdone++ == 0)
3515 hi = ht->ht_array;
3516 else
3517 ++hi;
3518 while (HASHITEM_EMPTY(hi))
3519 ++hi;
3520 return cat_prefix_varname('t', hi->hi_key);
3521 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003522
Bram Moolenaar33570922005-01-25 22:26:29 +00003523 /* v: variables */
3524 if (vidx < VV_LEN)
3525 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526
Bram Moolenaard23a8232018-02-10 18:45:26 +01003527 VIM_CLEAR(varnamebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 varnamebuflen = 0;
3529 return NULL;
3530}
3531
3532#endif /* FEAT_CMDL_COMPL */
3533
3534/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003535 * Return TRUE if "pat" matches "text".
3536 * Does not use 'cpo' and always uses 'magic'.
3537 */
3538 static int
3539pattern_match(char_u *pat, char_u *text, int ic)
3540{
3541 int matches = FALSE;
3542 char_u *save_cpo;
3543 regmatch_T regmatch;
3544
3545 /* avoid 'l' flag in 'cpoptions' */
3546 save_cpo = p_cpo;
3547 p_cpo = (char_u *)"";
3548 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3549 if (regmatch.regprog != NULL)
3550 {
3551 regmatch.rm_ic = ic;
3552 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3553 vim_regfree(regmatch.regprog);
3554 }
3555 p_cpo = save_cpo;
3556 return matches;
3557}
3558
3559/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003561 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3563 */
3564
3565/*
3566 * Handle zero level expression.
3567 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003568 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003569 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 * Return OK or FAIL.
3571 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003572 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003573eval0(
3574 char_u *arg,
3575 typval_T *rettv,
3576 char_u **nextcmd,
3577 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578{
3579 int ret;
3580 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003581 int did_emsg_before = did_emsg;
3582 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583
3584 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003585 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 if (ret == FAIL || !ends_excmd(*p))
3587 {
3588 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003589 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 /*
3591 * Report the invalid expression unless the expression evaluation has
3592 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003593 * exception, or we already gave a more specific error.
3594 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003596 if (!aborting() && did_emsg == did_emsg_before
3597 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003598 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 ret = FAIL;
3600 }
3601 if (nextcmd != NULL)
3602 *nextcmd = check_nextcmd(p);
3603
3604 return ret;
3605}
3606
3607/*
3608 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003609 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 *
3611 * "arg" must point to the first non-white of the expression.
3612 * "arg" is advanced to the next non-white after the recognized expression.
3613 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003614 * Note: "rettv.v_lock" is not set.
3615 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 * Return OK or FAIL.
3617 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003618 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003619eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620{
3621 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003622 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623
3624 /*
3625 * Get the first variable.
3626 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003627 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 return FAIL;
3629
3630 if ((*arg)[0] == '?')
3631 {
3632 result = FALSE;
3633 if (evaluate)
3634 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003635 int error = FALSE;
3636
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003637 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003639 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003640 if (error)
3641 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 }
3643
3644 /*
3645 * Get the second variable.
3646 */
3647 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003648 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 return FAIL;
3650
3651 /*
3652 * Check for the ":".
3653 */
3654 if ((*arg)[0] != ':')
3655 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003656 emsg(_("E109: Missing ':' after '?'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003658 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 return FAIL;
3660 }
3661
3662 /*
3663 * Get the third variable.
3664 */
3665 *arg = skipwhite(*arg + 1);
3666 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3667 {
3668 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003669 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 return FAIL;
3671 }
3672 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003673 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 }
3675
3676 return OK;
3677}
3678
3679/*
3680 * Handle first level expression:
3681 * expr2 || expr2 || expr2 logical OR
3682 *
3683 * "arg" must point to the first non-white of the expression.
3684 * "arg" is advanced to the next non-white after the recognized expression.
3685 *
3686 * Return OK or FAIL.
3687 */
3688 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003689eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690{
Bram Moolenaar33570922005-01-25 22:26:29 +00003691 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 long result;
3693 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003694 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695
3696 /*
3697 * Get the first variable.
3698 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003699 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 return FAIL;
3701
3702 /*
3703 * Repeat until there is no following "||".
3704 */
3705 first = TRUE;
3706 result = FALSE;
3707 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3708 {
3709 if (evaluate && first)
3710 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003711 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003713 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003714 if (error)
3715 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 first = FALSE;
3717 }
3718
3719 /*
3720 * Get the second variable.
3721 */
3722 *arg = skipwhite(*arg + 2);
3723 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3724 return FAIL;
3725
3726 /*
3727 * Compute the result.
3728 */
3729 if (evaluate && !result)
3730 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003731 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003733 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003734 if (error)
3735 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 }
3737 if (evaluate)
3738 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003739 rettv->v_type = VAR_NUMBER;
3740 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 }
3742 }
3743
3744 return OK;
3745}
3746
3747/*
3748 * Handle second level expression:
3749 * expr3 && expr3 && expr3 logical AND
3750 *
3751 * "arg" must point to the first non-white of the expression.
3752 * "arg" is advanced to the next non-white after the recognized expression.
3753 *
3754 * Return OK or FAIL.
3755 */
3756 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003757eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758{
Bram Moolenaar33570922005-01-25 22:26:29 +00003759 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 long result;
3761 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003762 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763
3764 /*
3765 * Get the first variable.
3766 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003767 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 return FAIL;
3769
3770 /*
3771 * Repeat until there is no following "&&".
3772 */
3773 first = TRUE;
3774 result = TRUE;
3775 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3776 {
3777 if (evaluate && first)
3778 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003779 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003781 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003782 if (error)
3783 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 first = FALSE;
3785 }
3786
3787 /*
3788 * Get the second variable.
3789 */
3790 *arg = skipwhite(*arg + 2);
3791 if (eval4(arg, &var2, evaluate && result) == FAIL)
3792 return FAIL;
3793
3794 /*
3795 * Compute the result.
3796 */
3797 if (evaluate && result)
3798 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003799 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003801 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003802 if (error)
3803 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 }
3805 if (evaluate)
3806 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003807 rettv->v_type = VAR_NUMBER;
3808 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 }
3810 }
3811
3812 return OK;
3813}
3814
3815/*
3816 * Handle third level expression:
3817 * var1 == var2
3818 * var1 =~ var2
3819 * var1 != var2
3820 * var1 !~ var2
3821 * var1 > var2
3822 * var1 >= var2
3823 * var1 < var2
3824 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003825 * var1 is var2
3826 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 *
3828 * "arg" must point to the first non-white of the expression.
3829 * "arg" is advanced to the next non-white after the recognized expression.
3830 *
3831 * Return OK or FAIL.
3832 */
3833 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003834eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835{
Bram Moolenaar33570922005-01-25 22:26:29 +00003836 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 char_u *p;
3838 int i;
3839 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003840 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843
3844 /*
3845 * Get the first variable.
3846 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003847 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 return FAIL;
3849
3850 p = *arg;
3851 switch (p[0])
3852 {
3853 case '=': if (p[1] == '=')
3854 type = TYPE_EQUAL;
3855 else if (p[1] == '~')
3856 type = TYPE_MATCH;
3857 break;
3858 case '!': if (p[1] == '=')
3859 type = TYPE_NEQUAL;
3860 else if (p[1] == '~')
3861 type = TYPE_NOMATCH;
3862 break;
3863 case '>': if (p[1] != '=')
3864 {
3865 type = TYPE_GREATER;
3866 len = 1;
3867 }
3868 else
3869 type = TYPE_GEQUAL;
3870 break;
3871 case '<': if (p[1] != '=')
3872 {
3873 type = TYPE_SMALLER;
3874 len = 1;
3875 }
3876 else
3877 type = TYPE_SEQUAL;
3878 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003879 case 'i': if (p[1] == 's')
3880 {
3881 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3882 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003883 i = p[len];
3884 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003885 {
3886 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3887 type_is = TRUE;
3888 }
3889 }
3890 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 }
3892
3893 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003894 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 */
3896 if (type != TYPE_UNKNOWN)
3897 {
3898 /* extra question mark appended: ignore case */
3899 if (p[len] == '?')
3900 {
3901 ic = TRUE;
3902 ++len;
3903 }
3904 /* extra '#' appended: match case */
3905 else if (p[len] == '#')
3906 {
3907 ic = FALSE;
3908 ++len;
3909 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003910 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 else
3912 ic = p_ic;
3913
3914 /*
3915 * Get the second variable.
3916 */
3917 *arg = skipwhite(p + len);
3918 if (eval5(arg, &var2, evaluate) == FAIL)
3919 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003920 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 return FAIL;
3922 }
Bram Moolenaar31988702018-02-13 12:57:42 +01003923 if (evaluate)
3924 {
3925 int ret = typval_compare(rettv, &var2, type, type_is, ic);
3926
3927 clear_tv(&var2);
3928 return ret;
3929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 }
3931
3932 return OK;
3933}
3934
3935/*
3936 * Handle fourth level expression:
3937 * + number addition
3938 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003939 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003940 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 *
3942 * "arg" must point to the first non-white of the expression.
3943 * "arg" is advanced to the next non-white after the recognized expression.
3944 *
3945 * Return OK or FAIL.
3946 */
3947 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003948eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949{
Bram Moolenaar33570922005-01-25 22:26:29 +00003950 typval_T var2;
3951 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003953 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003954#ifdef FEAT_FLOAT
3955 float_T f1 = 0, f2 = 0;
3956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 char_u *s1, *s2;
3958 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3959 char_u *p;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003960 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961
3962 /*
3963 * Get the first variable.
3964 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003965 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 return FAIL;
3967
3968 /*
3969 * Repeat computing, until no '+', '-' or '.' is following.
3970 */
3971 for (;;)
3972 {
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003973 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 op = **arg;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003975 concat = op == '.'
3976 && (*(*arg + 1) == '.' || current_sctx.sc_version < 2);
3977 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 break;
3979
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003980 if ((op != '+' || (rettv->v_type != VAR_LIST
3981 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003982#ifdef FEAT_FLOAT
3983 && (op == '.' || rettv->v_type != VAR_FLOAT)
3984#endif
3985 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003986 {
3987 /* For "list + ...", an illegal use of the first operand as
3988 * a number cannot be determined before evaluating the 2nd
3989 * operand: if this is also a list, all is ok.
3990 * For "something . ...", "something - ..." or "non-list + ...",
3991 * we know that the first operand needs to be a string or number
3992 * without evaluating the 2nd operand. So check before to avoid
3993 * side effects after an error. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003994 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003995 {
3996 clear_tv(rettv);
3997 return FAIL;
3998 }
3999 }
4000
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 /*
4002 * Get the second variable.
4003 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02004004 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
4005 ++*arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004007 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004009 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 return FAIL;
4011 }
4012
4013 if (evaluate)
4014 {
4015 /*
4016 * Compute the result.
4017 */
4018 if (op == '.')
4019 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004020 s1 = tv_get_string_buf(rettv, buf1); /* already checked */
4021 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004022 if (s2 == NULL) /* type error ? */
4023 {
4024 clear_tv(rettv);
4025 clear_tv(&var2);
4026 return FAIL;
4027 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004028 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004029 clear_tv(rettv);
4030 rettv->v_type = VAR_STRING;
4031 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004033 else if (op == '+' && rettv->v_type == VAR_BLOB
4034 && var2.v_type == VAR_BLOB)
4035 {
4036 blob_T *b1 = rettv->vval.v_blob;
4037 blob_T *b2 = var2.vval.v_blob;
4038 blob_T *b = blob_alloc();
4039 int i;
4040
4041 if (b != NULL)
4042 {
4043 for (i = 0; i < blob_len(b1); i++)
4044 ga_append(&b->bv_ga, blob_get(b1, i));
4045 for (i = 0; i < blob_len(b2); i++)
4046 ga_append(&b->bv_ga, blob_get(b2, i));
4047
4048 clear_tv(rettv);
4049 rettv_blob_set(rettv, b);
4050 }
4051 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004052 else if (op == '+' && rettv->v_type == VAR_LIST
4053 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004054 {
4055 /* concatenate Lists */
4056 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4057 &var3) == FAIL)
4058 {
4059 clear_tv(rettv);
4060 clear_tv(&var2);
4061 return FAIL;
4062 }
4063 clear_tv(rettv);
4064 *rettv = var3;
4065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 else
4067 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004068 int error = FALSE;
4069
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004070#ifdef FEAT_FLOAT
4071 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004072 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004073 f1 = rettv->vval.v_float;
4074 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004075 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004076 else
4077#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004078 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004079 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004080 if (error)
4081 {
4082 /* This can only happen for "list + non-list". For
4083 * "non-list + ..." or "something - ...", we returned
4084 * before evaluating the 2nd operand. */
4085 clear_tv(rettv);
4086 return FAIL;
4087 }
4088#ifdef FEAT_FLOAT
4089 if (var2.v_type == VAR_FLOAT)
4090 f1 = n1;
4091#endif
4092 }
4093#ifdef FEAT_FLOAT
4094 if (var2.v_type == VAR_FLOAT)
4095 {
4096 f2 = var2.vval.v_float;
4097 n2 = 0;
4098 }
4099 else
4100#endif
4101 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004102 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004103 if (error)
4104 {
4105 clear_tv(rettv);
4106 clear_tv(&var2);
4107 return FAIL;
4108 }
4109#ifdef FEAT_FLOAT
4110 if (rettv->v_type == VAR_FLOAT)
4111 f2 = n2;
4112#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004113 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004114 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004115
4116#ifdef FEAT_FLOAT
4117 /* If there is a float on either side the result is a float. */
4118 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4119 {
4120 if (op == '+')
4121 f1 = f1 + f2;
4122 else
4123 f1 = f1 - f2;
4124 rettv->v_type = VAR_FLOAT;
4125 rettv->vval.v_float = f1;
4126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004128#endif
4129 {
4130 if (op == '+')
4131 n1 = n1 + n2;
4132 else
4133 n1 = n1 - n2;
4134 rettv->v_type = VAR_NUMBER;
4135 rettv->vval.v_number = n1;
4136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004138 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 }
4140 }
4141 return OK;
4142}
4143
4144/*
4145 * Handle fifth level expression:
4146 * * number multiplication
4147 * / number division
4148 * % number modulo
4149 *
4150 * "arg" must point to the first non-white of the expression.
4151 * "arg" is advanced to the next non-white after the recognized expression.
4152 *
4153 * Return OK or FAIL.
4154 */
4155 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004156eval6(
4157 char_u **arg,
4158 typval_T *rettv,
4159 int evaluate,
4160 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161{
Bram Moolenaar33570922005-01-25 22:26:29 +00004162 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004164 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004165#ifdef FEAT_FLOAT
4166 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02004167 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004168#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004169 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170
4171 /*
4172 * Get the first variable.
4173 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004174 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 return FAIL;
4176
4177 /*
4178 * Repeat computing, until no '*', '/' or '%' is following.
4179 */
4180 for (;;)
4181 {
4182 op = **arg;
4183 if (op != '*' && op != '/' && op != '%')
4184 break;
4185
4186 if (evaluate)
4187 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004188#ifdef FEAT_FLOAT
4189 if (rettv->v_type == VAR_FLOAT)
4190 {
4191 f1 = rettv->vval.v_float;
4192 use_float = TRUE;
4193 n1 = 0;
4194 }
4195 else
4196#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004197 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004198 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004199 if (error)
4200 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 }
4202 else
4203 n1 = 0;
4204
4205 /*
4206 * Get the second variable.
4207 */
4208 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004209 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 return FAIL;
4211
4212 if (evaluate)
4213 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004214#ifdef FEAT_FLOAT
4215 if (var2.v_type == VAR_FLOAT)
4216 {
4217 if (!use_float)
4218 {
4219 f1 = n1;
4220 use_float = TRUE;
4221 }
4222 f2 = var2.vval.v_float;
4223 n2 = 0;
4224 }
4225 else
4226#endif
4227 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004228 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004229 clear_tv(&var2);
4230 if (error)
4231 return FAIL;
4232#ifdef FEAT_FLOAT
4233 if (use_float)
4234 f2 = n2;
4235#endif
4236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237
4238 /*
4239 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004240 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004242#ifdef FEAT_FLOAT
4243 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004245 if (op == '*')
4246 f1 = f1 * f2;
4247 else if (op == '/')
4248 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004249# ifdef VMS
4250 /* VMS crashes on divide by zero, work around it */
4251 if (f2 == 0.0)
4252 {
4253 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004254 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004255 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004256 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004257 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004258 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004259 }
4260 else
4261 f1 = f1 / f2;
4262# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004263 /* We rely on the floating point library to handle divide
4264 * by zero to result in "inf" and not a crash. */
4265 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004266# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004269 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004270 emsg(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004271 return FAIL;
4272 }
4273 rettv->v_type = VAR_FLOAT;
4274 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 }
4276 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004279 if (op == '*')
4280 n1 = n1 * n2;
4281 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01004282 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01004284 n1 = num_modulus(n1, n2);
4285
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004286 rettv->v_type = VAR_NUMBER;
4287 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 }
4290 }
4291
4292 return OK;
4293}
4294
4295/*
4296 * Handle sixth level expression:
4297 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004298 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004299 * "string" string constant
4300 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 * &option-name option value
4302 * @r register contents
4303 * identifier variable value
4304 * function() function call
4305 * $VAR environment variable
4306 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004307 * [expr, expr] List
4308 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 *
4310 * Also handle:
4311 * ! in front logical NOT
4312 * - in front unary minus
4313 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004314 * trailing [] subscript in String or List
4315 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 *
4317 * "arg" must point to the first non-white of the expression.
4318 * "arg" is advanced to the next non-white after the recognized expression.
4319 *
4320 * Return OK or FAIL.
4321 */
4322 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004323eval7(
4324 char_u **arg,
4325 typval_T *rettv,
4326 int evaluate,
4327 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004329 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 int len;
4331 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 char_u *start_leader, *end_leader;
4333 int ret = OK;
4334 char_u *alias;
4335
4336 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004337 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004338 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004340 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341
4342 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004343 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 */
4345 start_leader = *arg;
4346 while (**arg == '!' || **arg == '-' || **arg == '+')
4347 *arg = skipwhite(*arg + 1);
4348 end_leader = *arg;
4349
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004350 if (**arg == '.' && (!isdigit(*(*arg + 1))
4351#ifdef FEAT_FLOAT
4352 || current_sctx.sc_version < 2
4353#endif
4354 ))
4355 {
4356 semsg(_(e_invexpr2), *arg);
4357 ++*arg;
4358 return FAIL;
4359 }
4360
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 switch (**arg)
4362 {
4363 /*
4364 * Number constant.
4365 */
4366 case '0':
4367 case '1':
4368 case '2':
4369 case '3':
4370 case '4':
4371 case '5':
4372 case '6':
4373 case '7':
4374 case '8':
4375 case '9':
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004376 case '.':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004377 {
4378#ifdef FEAT_FLOAT
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004379 char_u *p;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004380 int get_float = FALSE;
4381
4382 /* We accept a float when the format matches
4383 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004384 * strict to avoid backwards compatibility problems.
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004385 * With script version 2 and later the leading digit can be
4386 * omitted.
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004387 * Don't look for a float after the "." operator, so that
4388 * ":let vers = 1.2.3" doesn't fail. */
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004389 if (**arg == '.')
4390 p = *arg;
4391 else
4392 p = skipdigits(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004393 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004395 get_float = TRUE;
4396 p = skipdigits(p + 2);
4397 if (*p == 'e' || *p == 'E')
4398 {
4399 ++p;
4400 if (*p == '-' || *p == '+')
4401 ++p;
4402 if (!vim_isdigit(*p))
4403 get_float = FALSE;
4404 else
4405 p = skipdigits(p + 1);
4406 }
4407 if (ASCII_ISALPHA(*p) || *p == '.')
4408 get_float = FALSE;
4409 }
4410 if (get_float)
4411 {
4412 float_T f;
4413
4414 *arg += string2float(*arg, &f);
4415 if (evaluate)
4416 {
4417 rettv->v_type = VAR_FLOAT;
4418 rettv->vval.v_float = f;
4419 }
4420 }
4421 else
4422#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004423 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004424 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004425 char_u *bp;
Bram Moolenaare519dfd2019-01-13 15:42:02 +01004426 blob_T *blob = NULL; // init for gcc
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004427
4428 // Blob constant: 0z0123456789abcdef
4429 if (evaluate)
4430 blob = blob_alloc();
4431 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
4432 {
4433 if (!vim_isxdigit(bp[1]))
4434 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004435 if (blob != NULL)
4436 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004437 emsg(_("E973: Blob literal should have an even number of hex characters"));
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004438 ga_clear(&blob->bv_ga);
4439 VIM_CLEAR(blob);
4440 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004441 ret = FAIL;
4442 break;
4443 }
4444 if (blob != NULL)
4445 ga_append(&blob->bv_ga,
4446 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
Bram Moolenaar4131fd52019-01-17 16:32:53 +01004447 if (bp[2] == '.' && vim_isxdigit(bp[3]))
4448 ++bp;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004449 }
4450 if (blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004451 rettv_blob_set(rettv, blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004452 *arg = bp;
4453 }
4454 else
4455 {
4456 // decimal, hex or octal number
Bram Moolenaar16e9b852019-05-19 19:59:35 +02004457 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0, TRUE);
4458 if (len == 0)
4459 {
4460 semsg(_(e_invexpr2), *arg);
4461 ret = FAIL;
4462 break;
4463 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004464 *arg += len;
4465 if (evaluate)
4466 {
4467 rettv->v_type = VAR_NUMBER;
4468 rettv->vval.v_number = n;
4469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 }
4471 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473
4474 /*
4475 * String constant: "string".
4476 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004477 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 break;
4479
4480 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004481 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004483 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004484 break;
4485
4486 /*
4487 * List: [expr, expr]
4488 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004489 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 break;
4491
4492 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004493 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004494 * Dictionary: {key: val, key: val}
4495 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004496 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4497 if (ret == NOTDONE)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004498 ret = dict_get_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004499 break;
4500
4501 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004502 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004504 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 break;
4506
4507 /*
4508 * Environment variable: $VAR.
4509 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004510 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 break;
4512
4513 /*
4514 * Register contents: @r.
4515 */
4516 case '@': ++*arg;
4517 if (evaluate)
4518 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004519 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004520 rettv->vval.v_string = get_reg_contents(**arg,
4521 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 }
4523 if (**arg != NUL)
4524 ++*arg;
4525 break;
4526
4527 /*
4528 * nested expression: (expression).
4529 */
4530 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004531 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 if (**arg == ')')
4533 ++*arg;
4534 else if (ret == OK)
4535 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004536 emsg(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004537 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 ret = FAIL;
4539 }
4540 break;
4541
Bram Moolenaar8c711452005-01-14 21:53:12 +00004542 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 break;
4544 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004545
4546 if (ret == NOTDONE)
4547 {
4548 /*
4549 * Must be a variable or function name.
4550 * Can also be a curly-braces kind of name: {expr}.
4551 */
4552 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004553 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004554 if (alias != NULL)
4555 s = alias;
4556
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004557 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004558 ret = FAIL;
4559 else
4560 {
4561 if (**arg == '(') /* recursive! */
4562 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004563 partial_T *partial;
4564
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004565 if (!evaluate)
4566 check_vars(s, len);
4567
Bram Moolenaar8c711452005-01-14 21:53:12 +00004568 /* If "s" is the name of a variable of type VAR_FUNC
4569 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004570 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004571
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004572 /* Need to make a copy, in case evaluating the arguments makes
4573 * the name invalid. */
4574 s = vim_strsave(s);
4575 if (s == NULL)
4576 ret = FAIL;
4577 else
4578 /* Invoke the function. */
4579 ret = get_func_tv(s, len, rettv, arg,
4580 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4581 &len, evaluate, partial, NULL);
4582 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004583
4584 /* If evaluate is FALSE rettv->v_type was not set in
4585 * get_func_tv, but it's needed in handle_subscript() to parse
4586 * what follows. So set it here. */
4587 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4588 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004589 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004590 rettv->v_type = VAR_FUNC;
4591 }
4592
Bram Moolenaar8c711452005-01-14 21:53:12 +00004593 /* Stop the expression evaluation when immediately
4594 * aborting on error, or when an interrupt occurred or
4595 * an exception was thrown but not caught. */
4596 if (aborting())
4597 {
4598 if (ret == OK)
4599 clear_tv(rettv);
4600 ret = FAIL;
4601 }
4602 }
4603 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004604 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004605 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004606 {
4607 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004608 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004609 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004610 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004611 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004612 }
4613
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 *arg = skipwhite(*arg);
4615
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004616 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4617 * expr(expr). */
4618 if (ret == OK)
4619 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620
4621 /*
4622 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4623 */
4624 if (ret == OK && evaluate && end_leader > start_leader)
4625 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004626 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004627 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004628#ifdef FEAT_FLOAT
4629 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004630
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004631 if (rettv->v_type == VAR_FLOAT)
4632 f = rettv->vval.v_float;
4633 else
4634#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004635 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004636 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004638 clear_tv(rettv);
4639 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004641 else
4642 {
4643 while (end_leader > start_leader)
4644 {
4645 --end_leader;
4646 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004647 {
4648#ifdef FEAT_FLOAT
4649 if (rettv->v_type == VAR_FLOAT)
4650 f = !f;
4651 else
4652#endif
4653 val = !val;
4654 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004655 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004656 {
4657#ifdef FEAT_FLOAT
4658 if (rettv->v_type == VAR_FLOAT)
4659 f = -f;
4660 else
4661#endif
4662 val = -val;
4663 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004664 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004665#ifdef FEAT_FLOAT
4666 if (rettv->v_type == VAR_FLOAT)
4667 {
4668 clear_tv(rettv);
4669 rettv->vval.v_float = f;
4670 }
4671 else
4672#endif
4673 {
4674 clear_tv(rettv);
4675 rettv->v_type = VAR_NUMBER;
4676 rettv->vval.v_number = val;
4677 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 }
4680
4681 return ret;
4682}
4683
4684/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004685 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4686 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004687 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4688 */
4689 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004690eval_index(
4691 char_u **arg,
4692 typval_T *rettv,
4693 int evaluate,
4694 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004695{
4696 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004697 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004698 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004699 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004700 long len = -1;
4701 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004702 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004703 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004704
Bram Moolenaara03f2332016-02-06 18:09:59 +01004705 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004706 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004707 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004708 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004709 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004710 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004711 return FAIL;
4712 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004713#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004714 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004715 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004716 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004717#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004718 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004719 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004720 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004721 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004722 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004723 return FAIL;
4724 case VAR_UNKNOWN:
4725 if (evaluate)
4726 return FAIL;
4727 /* FALLTHROUGH */
4728
4729 case VAR_STRING:
4730 case VAR_NUMBER:
4731 case VAR_LIST:
4732 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004733 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004734 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004735 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004736
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004737 init_tv(&var1);
4738 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004739 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004740 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004741 /*
4742 * dict.name
4743 */
4744 key = *arg + 1;
4745 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4746 ;
4747 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004748 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004749 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004750 }
4751 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004752 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004753 /*
4754 * something[idx]
4755 *
4756 * Get the (first) variable from inside the [].
4757 */
4758 *arg = skipwhite(*arg + 1);
4759 if (**arg == ':')
4760 empty1 = TRUE;
4761 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4762 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004763 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004764 {
4765 /* not a number or string */
4766 clear_tv(&var1);
4767 return FAIL;
4768 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004769
4770 /*
4771 * Get the second variable from inside the [:].
4772 */
4773 if (**arg == ':')
4774 {
4775 range = TRUE;
4776 *arg = skipwhite(*arg + 1);
4777 if (**arg == ']')
4778 empty2 = TRUE;
4779 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4780 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004781 if (!empty1)
4782 clear_tv(&var1);
4783 return FAIL;
4784 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004785 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004786 {
4787 /* not a number or string */
4788 if (!empty1)
4789 clear_tv(&var1);
4790 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004791 return FAIL;
4792 }
4793 }
4794
4795 /* Check for the ']'. */
4796 if (**arg != ']')
4797 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004798 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004799 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004800 clear_tv(&var1);
4801 if (range)
4802 clear_tv(&var2);
4803 return FAIL;
4804 }
4805 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004806 }
4807
4808 if (evaluate)
4809 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004810 n1 = 0;
4811 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004812 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004813 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004814 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004815 }
4816 if (range)
4817 {
4818 if (empty2)
4819 n2 = -1;
4820 else
4821 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004822 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004823 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004824 }
4825 }
4826
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004827 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004828 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004829 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004830 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004831 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004832 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004833 case VAR_SPECIAL:
4834 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004835 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004836 break; /* not evaluating, skipping over subscript */
4837
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004838 case VAR_NUMBER:
4839 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004840 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004841 len = (long)STRLEN(s);
4842 if (range)
4843 {
4844 /* The resulting variable is a substring. If the indexes
4845 * are out of range the result is empty. */
4846 if (n1 < 0)
4847 {
4848 n1 = len + n1;
4849 if (n1 < 0)
4850 n1 = 0;
4851 }
4852 if (n2 < 0)
4853 n2 = len + n2;
4854 else if (n2 >= len)
4855 n2 = len;
4856 if (n1 >= len || n2 < 0 || n1 > n2)
4857 s = NULL;
4858 else
4859 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4860 }
4861 else
4862 {
4863 /* The resulting variable is a string of a single
4864 * character. If the index is too big or negative the
4865 * result is empty. */
4866 if (n1 >= len || n1 < 0)
4867 s = NULL;
4868 else
4869 s = vim_strnsave(s + n1, 1);
4870 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004871 clear_tv(rettv);
4872 rettv->v_type = VAR_STRING;
4873 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004874 break;
4875
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004876 case VAR_BLOB:
4877 len = blob_len(rettv->vval.v_blob);
4878 if (range)
4879 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01004880 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004881 // are out of range the result is empty.
4882 if (n1 < 0)
4883 {
4884 n1 = len + n1;
4885 if (n1 < 0)
4886 n1 = 0;
4887 }
4888 if (n2 < 0)
4889 n2 = len + n2;
4890 else if (n2 >= len)
4891 n2 = len - 1;
4892 if (n1 >= len || n2 < 0 || n1 > n2)
4893 {
4894 clear_tv(rettv);
4895 rettv->v_type = VAR_BLOB;
4896 rettv->vval.v_blob = NULL;
4897 }
4898 else
4899 {
4900 blob_T *blob = blob_alloc();
4901
4902 if (blob != NULL)
4903 {
4904 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
4905 {
4906 blob_free(blob);
4907 return FAIL;
4908 }
4909 blob->bv_ga.ga_len = n2 - n1 + 1;
4910 for (i = n1; i <= n2; i++)
4911 blob_set(blob, i - n1,
4912 blob_get(rettv->vval.v_blob, i));
4913
4914 clear_tv(rettv);
4915 rettv_blob_set(rettv, blob);
4916 }
4917 }
4918 }
4919 else
4920 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01004921 // The resulting variable is a byte value.
4922 // If the index is too big or negative that is an error.
4923 if (n1 < 0)
4924 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004925 if (n1 < len && n1 >= 0)
4926 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01004927 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004928
4929 clear_tv(rettv);
4930 rettv->v_type = VAR_NUMBER;
4931 rettv->vval.v_number = v;
4932 }
4933 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004934 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004935 }
4936 break;
4937
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004938 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004939 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004940 if (n1 < 0)
4941 n1 = len + n1;
4942 if (!empty1 && (n1 < 0 || n1 >= len))
4943 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004944 /* For a range we allow invalid values and return an empty
4945 * list. A list index out of range is an error. */
4946 if (!range)
4947 {
4948 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004949 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004950 return FAIL;
4951 }
4952 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004953 }
4954 if (range)
4955 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004956 list_T *l;
4957 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004958
4959 if (n2 < 0)
4960 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004961 else if (n2 >= len)
4962 n2 = len - 1;
4963 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004964 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004965 l = list_alloc();
4966 if (l == NULL)
4967 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004968 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004969 n1 <= n2; ++n1)
4970 {
4971 if (list_append_tv(l, &item->li_tv) == FAIL)
4972 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004973 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004974 return FAIL;
4975 }
4976 item = item->li_next;
4977 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004978 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004979 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004980 }
4981 else
4982 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004983 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004984 clear_tv(rettv);
4985 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004986 }
4987 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004988
4989 case VAR_DICT:
4990 if (range)
4991 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004992 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004993 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004994 if (len == -1)
4995 clear_tv(&var1);
4996 return FAIL;
4997 }
4998 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004999 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005000
5001 if (len == -1)
5002 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005003 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005004 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005005 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005006 clear_tv(&var1);
5007 return FAIL;
5008 }
5009 }
5010
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005011 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005012
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005013 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005014 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005015 if (len == -1)
5016 clear_tv(&var1);
5017 if (item == NULL)
5018 return FAIL;
5019
5020 copy_tv(&item->di_tv, &var1);
5021 clear_tv(rettv);
5022 *rettv = var1;
5023 }
5024 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005025 }
5026 }
5027
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005028 return OK;
5029}
5030
5031/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 * Get an option value.
5033 * "arg" points to the '&' or '+' before the option name.
5034 * "arg" is advanced to character after the option name.
5035 * Return OK or FAIL.
5036 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005037 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005038get_option_tv(
5039 char_u **arg,
5040 typval_T *rettv, /* when NULL, only check if option exists */
5041 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042{
5043 char_u *option_end;
5044 long numval;
5045 char_u *stringval;
5046 int opt_type;
5047 int c;
5048 int working = (**arg == '+'); /* has("+option") */
5049 int ret = OK;
5050 int opt_flags;
5051
5052 /*
5053 * Isolate the option name and find its value.
5054 */
5055 option_end = find_option_end(arg, &opt_flags);
5056 if (option_end == NULL)
5057 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005058 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005059 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 return FAIL;
5061 }
5062
5063 if (!evaluate)
5064 {
5065 *arg = option_end;
5066 return OK;
5067 }
5068
5069 c = *option_end;
5070 *option_end = NUL;
5071 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005072 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073
5074 if (opt_type == -3) /* invalid name */
5075 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005076 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005077 semsg(_("E113: Unknown option: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 ret = FAIL;
5079 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005080 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 {
5082 if (opt_type == -2) /* hidden string option */
5083 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005084 rettv->v_type = VAR_STRING;
5085 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 }
5087 else if (opt_type == -1) /* hidden number option */
5088 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005089 rettv->v_type = VAR_NUMBER;
5090 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 }
5092 else if (opt_type == 1) /* number option */
5093 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005094 rettv->v_type = VAR_NUMBER;
5095 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 }
5097 else /* string option */
5098 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005099 rettv->v_type = VAR_STRING;
5100 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 }
5102 }
5103 else if (working && (opt_type == -2 || opt_type == -1))
5104 ret = FAIL;
5105
5106 *option_end = c; /* put back for error messages */
5107 *arg = option_end;
5108
5109 return ret;
5110}
5111
5112/*
5113 * Allocate a variable for a string constant.
5114 * Return OK or FAIL.
5115 */
5116 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005117get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118{
5119 char_u *p;
5120 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 int extra = 0;
5122
5123 /*
5124 * Find the end of the string, skipping backslashed characters.
5125 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005126 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 {
5128 if (*p == '\\' && p[1] != NUL)
5129 {
5130 ++p;
5131 /* A "\<x>" form occupies at least 4 characters, and produces up
5132 * to 6 characters: reserve space for 2 extra */
5133 if (*p == '<')
5134 extra += 2;
5135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 }
5137
5138 if (*p != '"')
5139 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005140 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 return FAIL;
5142 }
5143
5144 /* If only parsing, set *arg and return here */
5145 if (!evaluate)
5146 {
5147 *arg = p + 1;
5148 return OK;
5149 }
5150
5151 /*
5152 * Copy the string into allocated memory, handling backslashed
5153 * characters.
5154 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005155 name = alloc(p - *arg + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 if (name == NULL)
5157 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005158 rettv->v_type = VAR_STRING;
5159 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160
Bram Moolenaar8c711452005-01-14 21:53:12 +00005161 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 {
5163 if (*p == '\\')
5164 {
5165 switch (*++p)
5166 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005167 case 'b': *name++ = BS; ++p; break;
5168 case 'e': *name++ = ESC; ++p; break;
5169 case 'f': *name++ = FF; ++p; break;
5170 case 'n': *name++ = NL; ++p; break;
5171 case 'r': *name++ = CAR; ++p; break;
5172 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173
5174 case 'X': /* hex: "\x1", "\x12" */
5175 case 'x':
5176 case 'u': /* Unicode: "\u0023" */
5177 case 'U':
5178 if (vim_isxdigit(p[1]))
5179 {
5180 int n, nr;
5181 int c = toupper(*p);
5182
5183 if (c == 'X')
5184 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005185 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005187 else
5188 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 nr = 0;
5190 while (--n >= 0 && vim_isxdigit(p[1]))
5191 {
5192 ++p;
5193 nr = (nr << 4) + hex2nr(*p);
5194 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005195 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 /* For "\u" store the number according to
5197 * 'encoding'. */
5198 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005199 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005201 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 break;
5204
5205 /* octal: "\1", "\12", "\123" */
5206 case '0':
5207 case '1':
5208 case '2':
5209 case '3':
5210 case '4':
5211 case '5':
5212 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005213 case '7': *name = *p++ - '0';
5214 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005216 *name = (*name << 3) + *p++ - '0';
5217 if (*p >= '0' && *p <= '7')
5218 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005220 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 break;
5222
5223 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005224 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 if (extra != 0)
5226 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005227 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 break;
5229 }
5230 /* FALLTHROUGH */
5231
Bram Moolenaar8c711452005-01-14 21:53:12 +00005232 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 break;
5234 }
5235 }
5236 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005237 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005240 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02005241 if (*p != NUL) /* just in case */
5242 ++p;
5243 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 return OK;
5246}
5247
5248/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005249 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 * Return OK or FAIL.
5251 */
5252 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005253get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254{
5255 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005256 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005257 int reduce = 0;
5258
5259 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005260 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005261 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005262 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005263 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005264 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005265 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005266 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005267 break;
5268 ++reduce;
5269 ++p;
5270 }
5271 }
5272
Bram Moolenaar8c711452005-01-14 21:53:12 +00005273 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005274 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005275 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005276 return FAIL;
5277 }
5278
Bram Moolenaar8c711452005-01-14 21:53:12 +00005279 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005280 if (!evaluate)
5281 {
5282 *arg = p + 1;
5283 return OK;
5284 }
5285
5286 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005287 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005288 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005289 str = alloc((p - *arg) - reduce);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005290 if (str == NULL)
5291 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005292 rettv->v_type = VAR_STRING;
5293 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005294
Bram Moolenaar8c711452005-01-14 21:53:12 +00005295 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005296 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005297 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005298 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005299 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005300 break;
5301 ++p;
5302 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005303 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005304 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005305 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005306 *arg = p + 1;
5307
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005308 return OK;
5309}
5310
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005311/*
5312 * Return the function name of the partial.
5313 */
5314 char_u *
5315partial_name(partial_T *pt)
5316{
5317 if (pt->pt_name != NULL)
5318 return pt->pt_name;
5319 return pt->pt_func->uf_name;
5320}
5321
Bram Moolenaarddecc252016-04-06 22:59:37 +02005322 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005323partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005324{
5325 int i;
5326
5327 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005328 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005329 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005330 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005331 if (pt->pt_name != NULL)
5332 {
5333 func_unref(pt->pt_name);
5334 vim_free(pt->pt_name);
5335 }
5336 else
5337 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005338 vim_free(pt);
5339}
5340
5341/*
5342 * Unreference a closure: decrement the reference count and free it when it
5343 * becomes zero.
5344 */
5345 void
5346partial_unref(partial_T *pt)
5347{
5348 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005349 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005350}
5351
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005352static int tv_equal_recurse_limit;
5353
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005354 static int
5355func_equal(
5356 typval_T *tv1,
5357 typval_T *tv2,
5358 int ic) /* ignore case */
5359{
5360 char_u *s1, *s2;
5361 dict_T *d1, *d2;
5362 int a1, a2;
5363 int i;
5364
5365 /* empty and NULL function name considered the same */
5366 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005367 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005368 if (s1 != NULL && *s1 == NUL)
5369 s1 = NULL;
5370 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005371 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005372 if (s2 != NULL && *s2 == NUL)
5373 s2 = NULL;
5374 if (s1 == NULL || s2 == NULL)
5375 {
5376 if (s1 != s2)
5377 return FALSE;
5378 }
5379 else if (STRCMP(s1, s2) != 0)
5380 return FALSE;
5381
5382 /* empty dict and NULL dict is different */
5383 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5384 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5385 if (d1 == NULL || d2 == NULL)
5386 {
5387 if (d1 != d2)
5388 return FALSE;
5389 }
5390 else if (!dict_equal(d1, d2, ic, TRUE))
5391 return FALSE;
5392
5393 /* empty list and no list considered the same */
5394 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5395 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5396 if (a1 != a2)
5397 return FALSE;
5398 for (i = 0; i < a1; ++i)
5399 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5400 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5401 return FALSE;
5402
5403 return TRUE;
5404}
5405
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005406/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005407 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005408 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005409 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005410 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005411 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005412tv_equal(
5413 typval_T *tv1,
5414 typval_T *tv2,
5415 int ic, /* ignore case */
5416 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005417{
5418 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005419 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005420 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005421 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005422
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005423 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005424 * recursiveness to a limit. We guess they are equal then.
5425 * A fixed limit has the problem of still taking an awful long time.
5426 * Reduce the limit every time running into it. That should work fine for
5427 * deeply linked structures that are not recursively linked and catch
5428 * recursiveness quickly. */
5429 if (!recursive)
5430 tv_equal_recurse_limit = 1000;
5431 if (recursive_cnt >= tv_equal_recurse_limit)
5432 {
5433 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005434 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005435 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005436
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005437 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5438 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005439 if ((tv1->v_type == VAR_FUNC
5440 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5441 && (tv2->v_type == VAR_FUNC
5442 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5443 {
5444 ++recursive_cnt;
5445 r = func_equal(tv1, tv2, ic);
5446 --recursive_cnt;
5447 return r;
5448 }
5449
5450 if (tv1->v_type != tv2->v_type)
5451 return FALSE;
5452
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005453 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005454 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005455 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005456 ++recursive_cnt;
5457 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5458 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005459 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005460
5461 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005462 ++recursive_cnt;
5463 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5464 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005465 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005466
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005467 case VAR_BLOB:
5468 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
5469
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005470 case VAR_NUMBER:
5471 return tv1->vval.v_number == tv2->vval.v_number;
5472
5473 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005474 s1 = tv_get_string_buf(tv1, buf1);
5475 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005476 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005477
5478 case VAR_SPECIAL:
5479 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005480
5481 case VAR_FLOAT:
5482#ifdef FEAT_FLOAT
5483 return tv1->vval.v_float == tv2->vval.v_float;
5484#endif
5485 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005486#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005487 return tv1->vval.v_job == tv2->vval.v_job;
5488#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005489 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005490#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005491 return tv1->vval.v_channel == tv2->vval.v_channel;
5492#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005493 case VAR_FUNC:
5494 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005495 case VAR_UNKNOWN:
5496 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005497 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005498
Bram Moolenaara03f2332016-02-06 18:09:59 +01005499 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5500 * does not equal anything, not even itself. */
5501 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005502}
5503
5504/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005505 * Return the next (unique) copy ID.
5506 * Used for serializing nested structures.
5507 */
5508 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005509get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005510{
5511 current_copyID += COPYID_INC;
5512 return current_copyID;
5513}
5514
5515/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005516 * Garbage collection for lists and dictionaries.
5517 *
5518 * We use reference counts to be able to free most items right away when they
5519 * are no longer used. But for composite items it's possible that it becomes
5520 * unused while the reference count is > 0: When there is a recursive
5521 * reference. Example:
5522 * :let l = [1, 2, 3]
5523 * :let d = {9: l}
5524 * :let l[1] = d
5525 *
5526 * Since this is quite unusual we handle this with garbage collection: every
5527 * once in a while find out which lists and dicts are not referenced from any
5528 * variable.
5529 *
5530 * Here is a good reference text about garbage collection (refers to Python
5531 * but it applies to all reference-counting mechanisms):
5532 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005533 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005534
5535/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005536 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005537 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005538 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005539 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005540 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005541garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005542{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005543 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005544 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005545 buf_T *buf;
5546 win_T *wp;
5547 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005548 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005549 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005550
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005551 if (!testing)
5552 {
5553 /* Only do this once. */
5554 want_garbage_collect = FALSE;
5555 may_garbage_collect = FALSE;
5556 garbage_collect_at_exit = FALSE;
5557 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005558
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005559 /* We advance by two because we add one for items referenced through
5560 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005561 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005562
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005563 /*
5564 * 1. Go through all accessible variables and mark all lists and dicts
5565 * with copyID.
5566 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005567
5568 /* Don't free variables in the previous_funccal list unless they are only
5569 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005570 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005571 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005572
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005573 /* script-local variables */
5574 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005575 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005576
5577 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005578 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005579 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5580 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005581
5582 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005583 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005584 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5585 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005586 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005587 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5588 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005589#ifdef FEAT_TEXT_PROP
5590 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
5591 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5592 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005593 FOR_ALL_TABPAGES(tp)
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02005594 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005595 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5596 NULL, NULL);
5597#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005598
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005599 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005600 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005601 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5602 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005603 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005604 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005605
5606 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005607 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005608
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005609 /* named functions (matters for closures) */
5610 abort = abort || set_ref_in_functions(copyID);
5611
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005612 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005613 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005614
Bram Moolenaard812df62008-11-09 12:46:09 +00005615 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005616 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005617
Bram Moolenaar1dced572012-04-05 16:54:08 +02005618#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005619 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005620#endif
5621
Bram Moolenaardb913952012-06-29 12:54:53 +02005622#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005623 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005624#endif
5625
5626#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005627 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005628#endif
5629
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005630#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005631 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005632 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005633#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005634#ifdef FEAT_NETBEANS_INTG
5635 abort = abort || set_ref_in_nb_channel(copyID);
5636#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005637
Bram Moolenaare3188e22016-05-31 21:13:04 +02005638#ifdef FEAT_TIMERS
5639 abort = abort || set_ref_in_timer(copyID);
5640#endif
5641
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005642#ifdef FEAT_QUICKFIX
5643 abort = abort || set_ref_in_quickfix(copyID);
5644#endif
5645
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005646#ifdef FEAT_TERMINAL
5647 abort = abort || set_ref_in_term(copyID);
5648#endif
5649
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005650 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005651 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005652 /*
5653 * 2. Free lists and dictionaries that are not referenced.
5654 */
5655 did_free = free_unref_items(copyID);
5656
5657 /*
5658 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005659 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005660 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005661 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005662 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005663 else if (p_verbose > 0)
5664 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005665 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005666 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005667
5668 return did_free;
5669}
5670
5671/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005672 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005673 */
5674 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005675free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005676{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005677 int did_free = FALSE;
5678
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005679 /* Let all "free" functions know that we are here. This means no
5680 * dictionaries, lists, channels or jobs are to be freed, because we will
5681 * do that here. */
5682 in_free_unref_items = TRUE;
5683
5684 /*
5685 * PASS 1: free the contents of the items. We don't free the items
5686 * themselves yet, so that it is possible to decrement refcount counters
5687 */
5688
Bram Moolenaarcd524592016-07-17 14:57:05 +02005689 /* Go through the list of dicts and free items without the copyID. */
5690 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005691
Bram Moolenaarda861d62016-07-17 15:46:27 +02005692 /* Go through the list of lists and free items without the copyID. */
5693 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005694
5695#ifdef FEAT_JOB_CHANNEL
5696 /* Go through the list of jobs and free items without the copyID. This
5697 * must happen before doing channels, because jobs refer to channels, but
5698 * the reference from the channel to the job isn't tracked. */
5699 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5700
5701 /* Go through the list of channels and free items without the copyID. */
5702 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5703#endif
5704
5705 /*
5706 * PASS 2: free the items themselves.
5707 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005708 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005709 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005710
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005711#ifdef FEAT_JOB_CHANNEL
5712 /* Go through the list of jobs and free items without the copyID. This
5713 * must happen before doing channels, because jobs refer to channels, but
5714 * the reference from the channel to the job isn't tracked. */
5715 free_unused_jobs(copyID, COPYID_MASK);
5716
5717 /* Go through the list of channels and free items without the copyID. */
5718 free_unused_channels(copyID, COPYID_MASK);
5719#endif
5720
5721 in_free_unref_items = FALSE;
5722
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005723 return did_free;
5724}
5725
5726/*
5727 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005728 * "list_stack" is used to add lists to be marked. Can be NULL.
5729 *
5730 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005731 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005732 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005733set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005734{
5735 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005736 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005737 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005738 hashtab_T *cur_ht;
5739 ht_stack_T *ht_stack = NULL;
5740 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005741
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005742 cur_ht = ht;
5743 for (;;)
5744 {
5745 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005746 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005747 /* Mark each item in the hashtab. If the item contains a hashtab
5748 * it is added to ht_stack, if it contains a list it is added to
5749 * list_stack. */
5750 todo = (int)cur_ht->ht_used;
5751 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5752 if (!HASHITEM_EMPTY(hi))
5753 {
5754 --todo;
5755 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5756 &ht_stack, list_stack);
5757 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005758 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005759
5760 if (ht_stack == NULL)
5761 break;
5762
5763 /* take an item from the stack */
5764 cur_ht = ht_stack->ht;
5765 tempitem = ht_stack;
5766 ht_stack = ht_stack->prev;
5767 free(tempitem);
5768 }
5769
5770 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005771}
5772
5773/*
5774 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005775 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5776 *
5777 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005778 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005779 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005780set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005781{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005782 listitem_T *li;
5783 int abort = FALSE;
5784 list_T *cur_l;
5785 list_stack_T *list_stack = NULL;
5786 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005787
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005788 cur_l = l;
5789 for (;;)
5790 {
5791 if (!abort)
5792 /* Mark each item in the list. If the item contains a hashtab
5793 * it is added to ht_stack, if it contains a list it is added to
5794 * list_stack. */
5795 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5796 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5797 ht_stack, &list_stack);
5798 if (list_stack == NULL)
5799 break;
5800
5801 /* take an item from the stack */
5802 cur_l = list_stack->list;
5803 tempitem = list_stack;
5804 list_stack = list_stack->prev;
5805 free(tempitem);
5806 }
5807
5808 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005809}
5810
5811/*
5812 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005813 * "list_stack" is used to add lists to be marked. Can be NULL.
5814 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5815 *
5816 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005817 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005818 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005819set_ref_in_item(
5820 typval_T *tv,
5821 int copyID,
5822 ht_stack_T **ht_stack,
5823 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005824{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005825 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005826
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005827 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005828 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005829 dict_T *dd = tv->vval.v_dict;
5830
Bram Moolenaara03f2332016-02-06 18:09:59 +01005831 if (dd != NULL && dd->dv_copyID != copyID)
5832 {
5833 /* Didn't see this dict yet. */
5834 dd->dv_copyID = copyID;
5835 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005836 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005837 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5838 }
5839 else
5840 {
5841 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5842 if (newitem == NULL)
5843 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005844 else
5845 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005846 newitem->ht = &dd->dv_hashtab;
5847 newitem->prev = *ht_stack;
5848 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005849 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005850 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005851 }
5852 }
5853 else if (tv->v_type == VAR_LIST)
5854 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005855 list_T *ll = tv->vval.v_list;
5856
Bram Moolenaara03f2332016-02-06 18:09:59 +01005857 if (ll != NULL && ll->lv_copyID != copyID)
5858 {
5859 /* Didn't see this list yet. */
5860 ll->lv_copyID = copyID;
5861 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005862 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005863 abort = set_ref_in_list(ll, copyID, ht_stack);
5864 }
5865 else
5866 {
5867 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005868 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005869 if (newitem == NULL)
5870 abort = TRUE;
5871 else
5872 {
5873 newitem->list = ll;
5874 newitem->prev = *list_stack;
5875 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005876 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005877 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005878 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005879 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005880 else if (tv->v_type == VAR_FUNC)
5881 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005882 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005883 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005884 else if (tv->v_type == VAR_PARTIAL)
5885 {
5886 partial_T *pt = tv->vval.v_partial;
5887 int i;
5888
5889 /* A partial does not have a copyID, because it cannot contain itself.
5890 */
5891 if (pt != NULL)
5892 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005893 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005894
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005895 if (pt->pt_dict != NULL)
5896 {
5897 typval_T dtv;
5898
5899 dtv.v_type = VAR_DICT;
5900 dtv.vval.v_dict = pt->pt_dict;
5901 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5902 }
5903
5904 for (i = 0; i < pt->pt_argc; ++i)
5905 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5906 ht_stack, list_stack);
5907 }
5908 }
5909#ifdef FEAT_JOB_CHANNEL
5910 else if (tv->v_type == VAR_JOB)
5911 {
5912 job_T *job = tv->vval.v_job;
5913 typval_T dtv;
5914
5915 if (job != NULL && job->jv_copyID != copyID)
5916 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005917 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005918 if (job->jv_channel != NULL)
5919 {
5920 dtv.v_type = VAR_CHANNEL;
5921 dtv.vval.v_channel = job->jv_channel;
5922 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5923 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005924 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005925 {
5926 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005927 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005928 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5929 }
5930 }
5931 }
5932 else if (tv->v_type == VAR_CHANNEL)
5933 {
5934 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005935 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005936 typval_T dtv;
5937 jsonq_T *jq;
5938 cbq_T *cq;
5939
5940 if (ch != NULL && ch->ch_copyID != copyID)
5941 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005942 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005943 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005944 {
5945 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5946 jq = jq->jq_next)
5947 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5948 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5949 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005950 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005951 {
5952 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005953 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005954 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5955 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005956 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005957 {
5958 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005959 dtv.vval.v_partial =
5960 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005961 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5962 }
5963 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005964 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005965 {
5966 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005967 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005968 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5969 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005970 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005971 {
5972 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005973 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005974 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5975 }
5976 }
5977 }
5978#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005979 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005980}
5981
Bram Moolenaar17a13432016-01-24 14:22:10 +01005982 static char *
5983get_var_special_name(int nr)
5984{
5985 switch (nr)
5986 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005987 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005988 case VVAL_TRUE: return "v:true";
5989 case VVAL_NONE: return "v:none";
5990 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005991 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01005992 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01005993 return "42";
5994}
5995
Bram Moolenaar8c711452005-01-14 21:53:12 +00005996/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005997 * Return a string with the string representation of a variable.
5998 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005999 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006000 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02006001 * When both "echo_style" and "composite_val" are FALSE, put quotes around
6002 * stings as "string()", otherwise does not put quotes around strings, as
6003 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006004 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6005 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006006 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006007 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006008 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006009echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006010 typval_T *tv,
6011 char_u **tofree,
6012 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006013 int copyID,
6014 int echo_style,
6015 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006016 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006017{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006018 static int recurse = 0;
6019 char_u *r = NULL;
6020
Bram Moolenaar33570922005-01-25 22:26:29 +00006021 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006022 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006023 if (!did_echo_string_emsg)
6024 {
6025 /* Only give this message once for a recursive call to avoid
6026 * flooding the user with errors. And stop iterating over lists
6027 * and dicts. */
6028 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006029 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006030 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006031 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006032 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006033 }
6034 ++recurse;
6035
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006036 switch (tv->v_type)
6037 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006038 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006039 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006040 {
6041 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02006042 r = tv->vval.v_string;
6043 if (r == NULL)
6044 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006045 }
6046 else
6047 {
6048 *tofree = string_quote(tv->vval.v_string, FALSE);
6049 r = *tofree;
6050 }
6051 break;
6052
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006053 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006054 if (echo_style)
6055 {
6056 *tofree = NULL;
6057 r = tv->vval.v_string;
6058 }
6059 else
6060 {
6061 *tofree = string_quote(tv->vval.v_string, TRUE);
6062 r = *tofree;
6063 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006064 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006065
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006066 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006067 {
6068 partial_T *pt = tv->vval.v_partial;
6069 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006070 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006071 garray_T ga;
6072 int i;
6073 char_u *tf;
6074
6075 ga_init2(&ga, 1, 100);
6076 ga_concat(&ga, (char_u *)"function(");
6077 if (fname != NULL)
6078 {
6079 ga_concat(&ga, fname);
6080 vim_free(fname);
6081 }
6082 if (pt != NULL && pt->pt_argc > 0)
6083 {
6084 ga_concat(&ga, (char_u *)", [");
6085 for (i = 0; i < pt->pt_argc; ++i)
6086 {
6087 if (i > 0)
6088 ga_concat(&ga, (char_u *)", ");
6089 ga_concat(&ga,
6090 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6091 vim_free(tf);
6092 }
6093 ga_concat(&ga, (char_u *)"]");
6094 }
6095 if (pt != NULL && pt->pt_dict != NULL)
6096 {
6097 typval_T dtv;
6098
6099 ga_concat(&ga, (char_u *)", ");
6100 dtv.v_type = VAR_DICT;
6101 dtv.vval.v_dict = pt->pt_dict;
6102 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6103 vim_free(tf);
6104 }
6105 ga_concat(&ga, (char_u *)")");
6106
6107 *tofree = ga.ga_data;
6108 r = *tofree;
6109 break;
6110 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006111
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006112 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006113 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006114 break;
6115
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006116 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006117 if (tv->vval.v_list == NULL)
6118 {
6119 *tofree = NULL;
6120 r = NULL;
6121 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006122 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6123 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006124 {
6125 *tofree = NULL;
6126 r = (char_u *)"[...]";
6127 }
6128 else
6129 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006130 int old_copyID = tv->vval.v_list->lv_copyID;
6131
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006132 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006133 *tofree = list2string(tv, copyID, restore_copyID);
6134 if (restore_copyID)
6135 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006136 r = *tofree;
6137 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006138 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006139
Bram Moolenaar8c711452005-01-14 21:53:12 +00006140 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006141 if (tv->vval.v_dict == NULL)
6142 {
6143 *tofree = NULL;
6144 r = NULL;
6145 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006146 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6147 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006148 {
6149 *tofree = NULL;
6150 r = (char_u *)"{...}";
6151 }
6152 else
6153 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006154 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006155 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006156 *tofree = dict2string(tv, copyID, restore_copyID);
6157 if (restore_copyID)
6158 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006159 r = *tofree;
6160 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006161 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006162
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006163 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006164 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006165 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006166 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006167 break;
6168
Bram Moolenaar835dc632016-02-07 14:27:38 +01006169 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006170 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006171 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006172 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006173 if (composite_val)
6174 {
6175 *tofree = string_quote(r, FALSE);
6176 r = *tofree;
6177 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006178 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006179
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006180 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006181#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006182 *tofree = NULL;
6183 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6184 r = numbuf;
6185 break;
6186#endif
6187
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006188 case VAR_SPECIAL:
6189 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006190 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006191 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006192 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006193
Bram Moolenaar8502c702014-06-17 12:51:16 +02006194 if (--recurse == 0)
6195 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006196 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006197}
6198
6199/*
6200 * Return a string with the string representation of a variable.
6201 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6202 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006203 * Does not put quotes around strings, as ":echo" displays values.
6204 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6205 * May return NULL.
6206 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006207 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006208echo_string(
6209 typval_T *tv,
6210 char_u **tofree,
6211 char_u *numbuf,
6212 int copyID)
6213{
6214 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6215}
6216
6217/*
6218 * Return a string with the string representation of a variable.
6219 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6220 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006221 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006222 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006223 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02006224 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006225tv2string(
6226 typval_T *tv,
6227 char_u **tofree,
6228 char_u *numbuf,
6229 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006230{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006231 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006232}
6233
6234/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006235 * Return string "str" in ' quotes, doubling ' characters.
6236 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006237 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006238 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006239 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006240string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006241{
Bram Moolenaar33570922005-01-25 22:26:29 +00006242 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006243 char_u *p, *r, *s;
6244
Bram Moolenaar33570922005-01-25 22:26:29 +00006245 len = (function ? 13 : 3);
6246 if (str != NULL)
6247 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006248 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006249 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00006250 if (*p == '\'')
6251 ++len;
6252 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006253 s = r = alloc(len);
6254 if (r != NULL)
6255 {
6256 if (function)
6257 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006258 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006259 r += 10;
6260 }
6261 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006262 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006263 if (str != NULL)
6264 for (p = str; *p != NUL; )
6265 {
6266 if (*p == '\'')
6267 *r++ = '\'';
6268 MB_COPY_CHAR(p, r);
6269 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006270 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006271 if (function)
6272 *r++ = ')';
6273 *r++ = NUL;
6274 }
6275 return s;
6276}
6277
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006278#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006279/*
6280 * Convert the string "text" to a floating point number.
6281 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
6282 * this always uses a decimal point.
6283 * Returns the length of the text that was consumed.
6284 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006285 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006286string2float(
6287 char_u *text,
6288 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006289{
6290 char *s = (char *)text;
6291 float_T f;
6292
Bram Moolenaar62473612017-01-08 19:25:40 +01006293 /* MS-Windows does not deal with "inf" and "nan" properly. */
6294 if (STRNICMP(text, "inf", 3) == 0)
6295 {
6296 *value = INFINITY;
6297 return 3;
6298 }
6299 if (STRNICMP(text, "-inf", 3) == 0)
6300 {
6301 *value = -INFINITY;
6302 return 4;
6303 }
6304 if (STRNICMP(text, "nan", 3) == 0)
6305 {
6306 *value = NAN;
6307 return 3;
6308 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006309 f = strtod(s, &s);
6310 *value = f;
6311 return (int)((char_u *)s - text);
6312}
6313#endif
6314
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006315/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 * Get the value of an environment variable.
6317 * "arg" is pointing to the '$'. It is advanced to after the name.
6318 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006319 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 */
6321 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006322get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323{
6324 char_u *string = NULL;
6325 int len;
6326 int cc;
6327 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006328 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329
6330 ++*arg;
6331 name = *arg;
6332 len = get_env_len(arg);
6333 if (evaluate)
6334 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006335 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006336 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006337
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006338 cc = name[len];
6339 name[len] = NUL;
6340 /* first try vim_getenv(), fast for normal environment vars */
6341 string = vim_getenv(name, &mustfree);
6342 if (string != NULL && *string != NUL)
6343 {
6344 if (!mustfree)
6345 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006347 else
6348 {
6349 if (mustfree)
6350 vim_free(string);
6351
6352 /* next try expanding things like $VIM and ${HOME} */
6353 string = expand_env_save(name - 1);
6354 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01006355 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006356 }
6357 name[len] = cc;
6358
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006359 rettv->v_type = VAR_STRING;
6360 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 }
6362
6363 return OK;
6364}
6365
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006366
6367
6368/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006370 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006372 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006373var2fpos(
6374 typval_T *varp,
6375 int dollar_lnum, /* TRUE when $ is last line */
6376 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006378 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006380 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381
Bram Moolenaara5525202006-03-02 22:52:09 +00006382 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006383 if (varp->v_type == VAR_LIST)
6384 {
6385 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006386 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006387 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006388 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006389
6390 l = varp->vval.v_list;
6391 if (l == NULL)
6392 return NULL;
6393
6394 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006395 pos.lnum = list_find_nr(l, 0L, &error);
6396 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006397 return NULL; /* invalid line number */
6398
6399 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006400 pos.col = list_find_nr(l, 1L, &error);
6401 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006402 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006403 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006404
6405 /* We accept "$" for the column number: last column. */
6406 li = list_find(l, 1L);
6407 if (li != NULL && li->li_tv.v_type == VAR_STRING
6408 && li->li_tv.vval.v_string != NULL
6409 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6410 pos.col = len + 1;
6411
Bram Moolenaara5525202006-03-02 22:52:09 +00006412 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006413 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006414 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006415 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006416
Bram Moolenaara5525202006-03-02 22:52:09 +00006417 /* Get the virtual offset. Defaults to zero. */
6418 pos.coladd = list_find_nr(l, 2L, &error);
6419 if (error)
6420 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006421
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006422 return &pos;
6423 }
6424
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006425 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006426 if (name == NULL)
6427 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006428 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006430 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6431 {
6432 if (VIsual_active)
6433 return &VIsual;
6434 return &curwin->w_cursor;
6435 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006436 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006438 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6440 return NULL;
6441 return pp;
6442 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006443
Bram Moolenaara5525202006-03-02 22:52:09 +00006444 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006445
Bram Moolenaar477933c2007-07-17 14:32:23 +00006446 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006447 {
6448 pos.col = 0;
6449 if (name[1] == '0') /* "w0": first visible line */
6450 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006451 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006452 /* In silent Ex mode topline is zero, but that's not a valid line
6453 * number; use one instead. */
6454 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006455 return &pos;
6456 }
6457 else if (name[1] == '$') /* "w$": last visible line */
6458 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006459 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006460 /* In silent Ex mode botline is zero, return zero then. */
6461 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006462 return &pos;
6463 }
6464 }
6465 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006467 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 {
6469 pos.lnum = curbuf->b_ml.ml_line_count;
6470 pos.col = 0;
6471 }
6472 else
6473 {
6474 pos.lnum = curwin->w_cursor.lnum;
6475 pos.col = (colnr_T)STRLEN(ml_get_curline());
6476 }
6477 return &pos;
6478 }
6479 return NULL;
6480}
6481
6482/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006483 * Convert list in "arg" into a position and optional file number.
6484 * When "fnump" is NULL there is no file number, only 3 items.
6485 * Note that the column is passed on as-is, the caller may want to decrement
6486 * it to use 1 for the first column.
6487 * Return FAIL when conversion is not possible, doesn't check the position for
6488 * validity.
6489 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006490 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006491list2fpos(
6492 typval_T *arg,
6493 pos_T *posp,
6494 int *fnump,
6495 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006496{
6497 list_T *l = arg->vval.v_list;
6498 long i = 0;
6499 long n;
6500
Bram Moolenaar493c1782014-05-28 14:34:46 +02006501 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6502 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006503 if (arg->v_type != VAR_LIST
6504 || l == NULL
6505 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006506 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006507 return FAIL;
6508
6509 if (fnump != NULL)
6510 {
6511 n = list_find_nr(l, i++, NULL); /* fnum */
6512 if (n < 0)
6513 return FAIL;
6514 if (n == 0)
6515 n = curbuf->b_fnum; /* current buffer */
6516 *fnump = n;
6517 }
6518
6519 n = list_find_nr(l, i++, NULL); /* lnum */
6520 if (n < 0)
6521 return FAIL;
6522 posp->lnum = n;
6523
6524 n = list_find_nr(l, i++, NULL); /* col */
6525 if (n < 0)
6526 return FAIL;
6527 posp->col = n;
6528
Bram Moolenaar493c1782014-05-28 14:34:46 +02006529 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006530 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006531 posp->coladd = 0;
6532 else
6533 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006534
Bram Moolenaar493c1782014-05-28 14:34:46 +02006535 if (curswantp != NULL)
6536 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6537
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006538 return OK;
6539}
6540
6541/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 * Get the length of an environment variable name.
6543 * Advance "arg" to the first character after the name.
6544 * Return 0 for error.
6545 */
6546 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006547get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548{
6549 char_u *p;
6550 int len;
6551
6552 for (p = *arg; vim_isIDc(*p); ++p)
6553 ;
6554 if (p == *arg) /* no name found */
6555 return 0;
6556
6557 len = (int)(p - *arg);
6558 *arg = p;
6559 return len;
6560}
6561
6562/*
6563 * Get the length of the name of a function or internal variable.
6564 * "arg" is advanced to the first non-white character after the name.
6565 * Return 0 if something is wrong.
6566 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006567 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006568get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569{
6570 char_u *p;
6571 int len;
6572
6573 /* Find the end of the name. */
6574 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006575 {
6576 if (*p == ':')
6577 {
6578 /* "s:" is start of "s:var", but "n:" is not and can be used in
6579 * slice "[n:]". Also "xx:" is not a namespace. */
6580 len = (int)(p - *arg);
6581 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6582 || len > 1)
6583 break;
6584 }
6585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 if (p == *arg) /* no name found */
6587 return 0;
6588
6589 len = (int)(p - *arg);
6590 *arg = skipwhite(p);
6591
6592 return len;
6593}
6594
6595/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006596 * Get the length of the name of a variable or function.
6597 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006599 * Return -1 if curly braces expansion failed.
6600 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 * If the name contains 'magic' {}'s, expand them and return the
6602 * expanded name in an allocated string via 'alias' - caller must free.
6603 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006604 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006605get_name_len(
6606 char_u **arg,
6607 char_u **alias,
6608 int evaluate,
6609 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610{
6611 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 char_u *p;
6613 char_u *expr_start;
6614 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615
6616 *alias = NULL; /* default to no alias */
6617
6618 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6619 && (*arg)[2] == (int)KE_SNR)
6620 {
6621 /* hard coded <SNR>, already translated */
6622 *arg += 3;
6623 return get_id_len(arg) + 3;
6624 }
6625 len = eval_fname_script(*arg);
6626 if (len > 0)
6627 {
6628 /* literal "<SID>", "s:" or "<SNR>" */
6629 *arg += len;
6630 }
6631
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006633 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006635 p = find_name_end(*arg, &expr_start, &expr_end,
6636 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 if (expr_start != NULL)
6638 {
6639 char_u *temp_string;
6640
6641 if (!evaluate)
6642 {
6643 len += (int)(p - *arg);
6644 *arg = skipwhite(p);
6645 return len;
6646 }
6647
6648 /*
6649 * Include any <SID> etc in the expanded string:
6650 * Thus the -len here.
6651 */
6652 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6653 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006654 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 *alias = temp_string;
6656 *arg = skipwhite(p);
6657 return (int)STRLEN(temp_string);
6658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659
6660 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006661 // Only give an error when there is something, otherwise it will be
6662 // reported at a higher level.
6663 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006664 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665
6666 return len;
6667}
6668
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006669/*
6670 * Find the end of a variable or function name, taking care of magic braces.
6671 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6672 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006673 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006674 * Return a pointer to just after the name. Equal to "arg" if there is no
6675 * valid name.
6676 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006677 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006678find_name_end(
6679 char_u *arg,
6680 char_u **expr_start,
6681 char_u **expr_end,
6682 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006684 int mb_nest = 0;
6685 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006687 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006689 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006691 *expr_start = NULL;
6692 *expr_end = NULL;
6693 }
6694
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006695 /* Quick check for valid starting character. */
6696 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6697 return arg;
6698
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006699 for (p = arg; *p != NUL
6700 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006701 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006702 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006703 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006704 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006705 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006706 if (*p == '\'')
6707 {
6708 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006709 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006710 ;
6711 if (*p == NUL)
6712 break;
6713 }
6714 else if (*p == '"')
6715 {
6716 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006717 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006718 if (*p == '\\' && p[1] != NUL)
6719 ++p;
6720 if (*p == NUL)
6721 break;
6722 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006723 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6724 {
6725 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006726 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006727 len = (int)(p - arg);
6728 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006729 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006730 break;
6731 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006732
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006733 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006735 if (*p == '[')
6736 ++br_nest;
6737 else if (*p == ']')
6738 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006740
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006741 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006743 if (*p == '{')
6744 {
6745 mb_nest++;
6746 if (expr_start != NULL && *expr_start == NULL)
6747 *expr_start = p;
6748 }
6749 else if (*p == '}')
6750 {
6751 mb_nest--;
6752 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6753 *expr_end = p;
6754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 }
6757
6758 return p;
6759}
6760
6761/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006762 * Expands out the 'magic' {}'s in a variable/function name.
6763 * Note that this can call itself recursively, to deal with
6764 * constructs like foo{bar}{baz}{bam}
6765 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6766 * "in_start" ^
6767 * "expr_start" ^
6768 * "expr_end" ^
6769 * "in_end" ^
6770 *
6771 * Returns a new allocated string, which the caller must free.
6772 * Returns NULL for failure.
6773 */
6774 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006775make_expanded_name(
6776 char_u *in_start,
6777 char_u *expr_start,
6778 char_u *expr_end,
6779 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006780{
6781 char_u c1;
6782 char_u *retval = NULL;
6783 char_u *temp_result;
6784 char_u *nextcmd = NULL;
6785
6786 if (expr_end == NULL || in_end == NULL)
6787 return NULL;
6788 *expr_start = NUL;
6789 *expr_end = NUL;
6790 c1 = *in_end;
6791 *in_end = NUL;
6792
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006793 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006794 if (temp_result != NULL && nextcmd == NULL)
6795 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006796 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6797 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006798 if (retval != NULL)
6799 {
6800 STRCPY(retval, in_start);
6801 STRCAT(retval, temp_result);
6802 STRCAT(retval, expr_end + 1);
6803 }
6804 }
6805 vim_free(temp_result);
6806
6807 *in_end = c1; /* put char back for error messages */
6808 *expr_start = '{';
6809 *expr_end = '}';
6810
6811 if (retval != NULL)
6812 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006813 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006814 if (expr_start != NULL)
6815 {
6816 /* Further expansion! */
6817 temp_result = make_expanded_name(retval, expr_start,
6818 expr_end, temp_result);
6819 vim_free(retval);
6820 retval = temp_result;
6821 }
6822 }
6823
6824 return retval;
6825}
6826
6827/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006829 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006831 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006832eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006834 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6835}
6836
6837/*
6838 * Return TRUE if character "c" can be used as the first character in a
6839 * variable or function name (excluding '{' and '}').
6840 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006841 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006842eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006843{
6844 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845}
6846
6847/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 * Set number v: variable to "val".
6849 */
6850 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006851set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006853 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854}
6855
6856/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006857 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006859 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006860get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006862 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863}
6864
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006865/*
6866 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01006867 * If the String variable has never been set, return an empty string.
6868 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006869 */
6870 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006871get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006872{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006873 return tv_get_string(&vimvars[idx].vv_tv);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006874}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006875
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006877 * Get List v: variable value. Caller must take care of reference count when
6878 * needed.
6879 */
6880 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006881get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006882{
6883 return vimvars[idx].vv_list;
6884}
6885
6886/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006887 * Get Dict v: variable value. Caller must take care of reference count when
6888 * needed.
6889 */
6890 dict_T *
6891get_vim_var_dict(int idx)
6892{
6893 return vimvars[idx].vv_dict;
6894}
6895
6896/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006897 * Set v:char to character "c".
6898 */
6899 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006900set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006901{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006902 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006903
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006904 if (has_mbyte)
6905 buf[(*mb_char2bytes)(c, buf)] = NUL;
6906 else
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006907 {
6908 buf[0] = c;
6909 buf[1] = NUL;
6910 }
6911 set_vim_var_string(VV_CHAR, buf, -1);
6912}
6913
6914/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006915 * Set v:count to "count" and v:count1 to "count1".
6916 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 */
6918 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006919set_vcount(
6920 long count,
6921 long count1,
6922 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006924 if (set_prevcount)
6925 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006926 vimvars[VV_COUNT].vv_nr = count;
6927 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928}
6929
6930/*
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02006931 * Save variables that might be changed as a side effect. Used when executing
6932 * a timer callback.
6933 */
6934 void
6935save_vimvars(vimvars_save_T *vvsave)
6936{
6937 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
6938 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
6939 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
6940}
6941
6942/*
6943 * Restore variables saved by save_vimvars().
6944 */
6945 void
6946restore_vimvars(vimvars_save_T *vvsave)
6947{
6948 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
6949 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
6950 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
6951}
6952
6953/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 * Set string v: variable to a copy of "val".
6955 */
6956 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006957set_vim_var_string(
6958 int idx,
6959 char_u *val,
6960 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961{
Bram Moolenaara542c682016-01-31 16:28:04 +01006962 clear_tv(&vimvars[idx].vv_di.di_tv);
6963 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006965 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006967 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006969 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970}
6971
6972/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006973 * Set List v: variable to "val".
6974 */
6975 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006976set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006977{
Bram Moolenaara542c682016-01-31 16:28:04 +01006978 clear_tv(&vimvars[idx].vv_di.di_tv);
6979 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006980 vimvars[idx].vv_list = val;
6981 if (val != NULL)
6982 ++val->lv_refcount;
6983}
6984
6985/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006986 * Set Dictionary v: variable to "val".
6987 */
6988 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006989set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006990{
Bram Moolenaara542c682016-01-31 16:28:04 +01006991 clear_tv(&vimvars[idx].vv_di.di_tv);
6992 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006993 vimvars[idx].vv_dict = val;
6994 if (val != NULL)
6995 {
6996 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006997 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02006998 }
6999}
7000
7001/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 * Set v:register if needed.
7003 */
7004 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007005set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006{
7007 char_u regname;
7008
7009 if (c == 0 || c == ' ')
7010 regname = '"';
7011 else
7012 regname = c;
7013 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007014 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 set_vim_var_string(VV_REG, &regname, 1);
7016}
7017
7018/*
7019 * Get or set v:exception. If "oldval" == NULL, return the current value.
7020 * Otherwise, restore the value to "oldval" and return NULL.
7021 * Must always be called in pairs to save and restore v:exception! Does not
7022 * take care of memory allocations.
7023 */
7024 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007025v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026{
7027 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007028 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029
Bram Moolenaare9a41262005-01-15 22:18:47 +00007030 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 return NULL;
7032}
7033
7034/*
7035 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
7036 * Otherwise, restore the value to "oldval" and return NULL.
7037 * Must always be called in pairs to save and restore v:throwpoint! Does not
7038 * take care of memory allocations.
7039 */
7040 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007041v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042{
7043 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007044 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045
Bram Moolenaare9a41262005-01-15 22:18:47 +00007046 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 return NULL;
7048}
7049
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050/*
7051 * Set v:cmdarg.
7052 * If "eap" != NULL, use "eap" to generate the value and return the old value.
7053 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
7054 * Must always be called in pairs!
7055 */
7056 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007057set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058{
7059 char_u *oldval;
7060 char_u *newval;
7061 unsigned len;
7062
Bram Moolenaare9a41262005-01-15 22:18:47 +00007063 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007064 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007066 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007067 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007068 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 }
7070
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007071 if (eap->force_bin == FORCE_BIN)
7072 len = 6;
7073 else if (eap->force_bin == FORCE_NOBIN)
7074 len = 8;
7075 else
7076 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007077
7078 if (eap->read_edit)
7079 len += 7;
7080
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007081 if (eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02007082 len += 10; /* " ++ff=unix" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007083 if (eap->force_enc != 0)
7084 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02007085 if (eap->bad_char != 0)
7086 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007087
7088 newval = alloc(len + 1);
7089 if (newval == NULL)
7090 return NULL;
7091
7092 if (eap->force_bin == FORCE_BIN)
7093 sprintf((char *)newval, " ++bin");
7094 else if (eap->force_bin == FORCE_NOBIN)
7095 sprintf((char *)newval, " ++nobin");
7096 else
7097 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007098
7099 if (eap->read_edit)
7100 STRCAT(newval, " ++edit");
7101
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007102 if (eap->force_ff != 0)
7103 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
Bram Moolenaar333b80a2018-04-04 22:57:29 +02007104 eap->force_ff == 'u' ? "unix"
7105 : eap->force_ff == 'd' ? "dos"
7106 : "mac");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007107 if (eap->force_enc != 0)
7108 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
7109 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02007110 if (eap->bad_char == BAD_KEEP)
7111 STRCPY(newval + STRLEN(newval), " ++bad=keep");
7112 else if (eap->bad_char == BAD_DROP)
7113 STRCPY(newval + STRLEN(newval), " ++bad=drop");
7114 else if (eap->bad_char != 0)
7115 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007116 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007117 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119
7120/*
7121 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01007122 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007124 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007125get_var_tv(
7126 char_u *name,
7127 int len, /* length of "name" */
7128 typval_T *rettv, /* NULL when only checking existence */
7129 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
7130 int verbose, /* may give error message */
7131 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132{
7133 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007134 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007135 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137
7138 /* truncate the name, so that we can use strcmp() */
7139 cc = name[len];
7140 name[len] = NUL;
7141
7142 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 * Check for user-defined variables.
7144 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01007145 v = find_var(name, NULL, no_autoload);
7146 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007148 tv = &v->di_tv;
7149 if (dip != NULL)
7150 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 }
7152
Bram Moolenaare9a41262005-01-15 22:18:47 +00007153 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007155 if (rettv != NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007156 semsg(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 ret = FAIL;
7158 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007159 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007160 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161
7162 name[len] = cc;
7163
7164 return ret;
7165}
7166
7167/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007168 * Check if variable "name[len]" is a local variable or an argument.
7169 * If so, "*eval_lavars_used" is set to TRUE.
7170 */
7171 static void
7172check_vars(char_u *name, int len)
7173{
7174 int cc;
7175 char_u *varname;
7176 hashtab_T *ht;
7177
7178 if (eval_lavars_used == NULL)
7179 return;
7180
7181 /* truncate the name, so that we can use strcmp() */
7182 cc = name[len];
7183 name[len] = NUL;
7184
7185 ht = find_var_ht(name, &varname);
7186 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
7187 {
7188 if (find_var(name, NULL, TRUE) != NULL)
7189 *eval_lavars_used = TRUE;
7190 }
7191
7192 name[len] = cc;
7193}
7194
7195/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007196 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
7197 * Also handle function call with Funcref variable: func(expr)
7198 * Can all be combined: dict.func(expr)[idx]['func'](expr)
7199 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007200 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007201handle_subscript(
7202 char_u **arg,
7203 typval_T *rettv,
7204 int evaluate, /* do more than finding the end */
7205 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007206{
7207 int ret = OK;
7208 dict_T *selfdict = NULL;
7209 char_u *s;
7210 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007211 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007212
7213 while (ret == OK
7214 && (**arg == '['
7215 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007216 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7217 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01007218 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007219 {
7220 if (**arg == '(')
7221 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007222 partial_T *pt = NULL;
7223
Bram Moolenaard9fba312005-06-26 22:34:35 +00007224 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007225 if (evaluate)
7226 {
7227 functv = *rettv;
7228 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007229
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007230 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007231 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007232 {
7233 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007234 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007235 }
7236 else
7237 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007238 }
7239 else
7240 s = (char_u *)"";
Bram Moolenaar6ed88192019-05-11 18:37:44 +02007241 ret = get_func_tv(s, -1, rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00007242 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007243 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007244
7245 /* Clear the funcref afterwards, so that deleting it while
7246 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007247 if (evaluate)
7248 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007249
7250 /* Stop the expression evaluation when immediately aborting on
7251 * error, or when an interrupt occurred or an exception was thrown
7252 * but not caught. */
7253 if (aborting())
7254 {
7255 if (ret == OK)
7256 clear_tv(rettv);
7257 ret = FAIL;
7258 }
7259 dict_unref(selfdict);
7260 selfdict = NULL;
7261 }
7262 else /* **arg == '[' || **arg == '.' */
7263 {
7264 dict_unref(selfdict);
7265 if (rettv->v_type == VAR_DICT)
7266 {
7267 selfdict = rettv->vval.v_dict;
7268 if (selfdict != NULL)
7269 ++selfdict->dv_refcount;
7270 }
7271 else
7272 selfdict = NULL;
7273 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
7274 {
7275 clear_tv(rettv);
7276 ret = FAIL;
7277 }
7278 }
7279 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007280
Bram Moolenaar1d429612016-05-24 15:44:17 +02007281 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
7282 * Don't do this when "Func" is already a partial that was bound
7283 * explicitly (pt_auto is FALSE). */
7284 if (selfdict != NULL
7285 && (rettv->v_type == VAR_FUNC
7286 || (rettv->v_type == VAR_PARTIAL
7287 && (rettv->vval.v_partial->pt_auto
7288 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007289 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007290
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007291 dict_unref(selfdict);
7292 return ret;
7293}
7294
7295/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007296 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007297 * value).
7298 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01007299 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007300alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007301{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007302 return ALLOC_CLEAR_ONE(typval_T);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007303}
7304
7305/*
7306 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 * The string "s" must have been allocated, it is consumed.
7308 * Return NULL for out of memory, the variable otherwise.
7309 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007310 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007311alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312{
Bram Moolenaar33570922005-01-25 22:26:29 +00007313 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007315 rettv = alloc_tv();
7316 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007318 rettv->v_type = VAR_STRING;
7319 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 }
7321 else
7322 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007323 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324}
7325
7326/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007327 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007329 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007330free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331{
7332 if (varp != NULL)
7333 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007334 switch (varp->v_type)
7335 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007336 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007337 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007338 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007339 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007340 vim_free(varp->vval.v_string);
7341 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007342 case VAR_PARTIAL:
7343 partial_unref(varp->vval.v_partial);
7344 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007345 case VAR_BLOB:
7346 blob_unref(varp->vval.v_blob);
7347 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007348 case VAR_LIST:
7349 list_unref(varp->vval.v_list);
7350 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007351 case VAR_DICT:
7352 dict_unref(varp->vval.v_dict);
7353 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007354 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007355#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007356 job_unref(varp->vval.v_job);
7357 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007358#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007359 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007360#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007361 channel_unref(varp->vval.v_channel);
7362 break;
7363#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007364 case VAR_NUMBER:
7365 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007366 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007367 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007368 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 vim_free(varp);
7371 }
7372}
7373
7374/*
7375 * Free the memory for a variable value and set the value to NULL or 0.
7376 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007377 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007378clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379{
7380 if (varp != NULL)
7381 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007382 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007384 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007385 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007386 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007387 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007388 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007389 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007390 case VAR_PARTIAL:
7391 partial_unref(varp->vval.v_partial);
7392 varp->vval.v_partial = NULL;
7393 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007394 case VAR_BLOB:
7395 blob_unref(varp->vval.v_blob);
7396 varp->vval.v_blob = NULL;
7397 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007398 case VAR_LIST:
7399 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007400 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007401 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007402 case VAR_DICT:
7403 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007404 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007405 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007406 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007407 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007408 varp->vval.v_number = 0;
7409 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007410 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007411#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007412 varp->vval.v_float = 0.0;
7413 break;
7414#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007415 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007416#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007417 job_unref(varp->vval.v_job);
7418 varp->vval.v_job = NULL;
7419#endif
7420 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007421 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007422#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007423 channel_unref(varp->vval.v_channel);
7424 varp->vval.v_channel = NULL;
7425#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007426 case VAR_UNKNOWN:
7427 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007429 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 }
7431}
7432
7433/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007434 * Set the value of a variable to NULL without freeing items.
7435 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007436 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007437init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007438{
7439 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007440 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007441}
7442
7443/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 * Get the number value of a variable.
7445 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007446 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007447 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007448 * caller of incompatible types: it sets *denote to TRUE if "denote"
7449 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007451 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007452tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007454 int error = FALSE;
7455
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007456 return tv_get_number_chk(varp, &error); /* return 0L on error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007457}
7458
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007459 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007460tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007461{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007462 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007464 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007466 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007467 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007468 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007469#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007470 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007471 break;
7472#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007473 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007474 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007475 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007476 break;
7477 case VAR_STRING:
7478 if (varp->vval.v_string != NULL)
7479 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02007480 STR2NR_ALL, &n, NULL, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007481 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007482 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007483 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007484 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007485 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007486 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007487 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007488 case VAR_SPECIAL:
7489 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7490 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007491 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007492#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007493 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007494 break;
7495#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007496 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007497#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007498 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007499 break;
7500#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007501 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007502 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007503 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007504 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007505 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007506 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007508 if (denote == NULL) /* useful for values that must be unsigned */
7509 n = -1;
7510 else
7511 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007512 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513}
7514
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007515#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007516 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007517tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007518{
7519 switch (varp->v_type)
7520 {
7521 case VAR_NUMBER:
7522 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007523 case VAR_FLOAT:
7524 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007525 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007526 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007527 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007528 break;
7529 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007530 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007531 break;
7532 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007533 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007534 break;
7535 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007536 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007537 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007538 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007539 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007540 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007541 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007542# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007543 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007544 break;
7545# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007546 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007547# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007548 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007549 break;
7550# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007551 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007552 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007553 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007554 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007555 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007556 break;
7557 }
7558 return 0;
7559}
7560#endif
7561
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 * Get the string value of a variable.
7564 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007565 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7566 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 * If the String variable has never been set, return an empty string.
7568 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007569 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007570 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007572 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007573tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574{
7575 static char_u mybuf[NUMBUFLEN];
7576
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007577 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578}
7579
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007580 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007581tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007583 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007584
7585 return res != NULL ? res : (char_u *)"";
7586}
7587
Bram Moolenaar7d647822014-04-05 21:28:56 +02007588/*
7589 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7590 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007591 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007592tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007593{
7594 static char_u mybuf[NUMBUFLEN];
7595
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007596 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007597}
7598
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007599 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007600tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007601{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007602 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007604 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007605 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007606 (long_long_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007607 return buf;
7608 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007609 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007610 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007611 break;
7612 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007613 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007614 break;
7615 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007616 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007617 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007618 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007619#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007620 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007621 break;
7622#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007623 case VAR_STRING:
7624 if (varp->vval.v_string != NULL)
7625 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007626 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007627 case VAR_SPECIAL:
7628 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7629 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007630 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007631 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007632 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007633 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007634#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007635 {
7636 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007637 char *status;
7638
7639 if (job == NULL)
7640 return (char_u *)"no process";
7641 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007642 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007643 : "run";
7644# ifdef UNIX
7645 vim_snprintf((char *)buf, NUMBUFLEN,
7646 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01007647# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007648 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007649 "process %ld %s",
7650 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007651 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007652# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007653 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007654 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7655# endif
7656 return buf;
7657 }
7658#endif
7659 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007660 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007661#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007662 {
7663 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007664 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007665
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007666 if (channel == NULL)
7667 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7668 else
7669 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007670 "channel %d %s", channel->ch_id, status);
7671 return buf;
7672 }
7673#endif
7674 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007675 case VAR_UNKNOWN:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007676 emsg(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007677 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007679 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680}
7681
7682/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007683 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
7684 * string() on Dict, List, etc.
7685 */
7686 char_u *
7687tv_stringify(typval_T *varp, char_u *buf)
7688{
7689 if (varp->v_type == VAR_LIST
7690 || varp->v_type == VAR_DICT
7691 || varp->v_type == VAR_FUNC
7692 || varp->v_type == VAR_PARTIAL
7693 || varp->v_type == VAR_FLOAT)
7694 {
7695 typval_T tmp;
7696
7697 f_string(varp, &tmp);
7698 tv_get_string_buf(&tmp, buf);
7699 clear_tv(varp);
7700 *varp = tmp;
7701 return tmp.vval.v_string;
7702 }
7703 return tv_get_string_buf(varp, buf);
7704}
7705
7706/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 * Find variable "name" in the list of variables.
7708 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007709 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007710 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007711 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007713 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007714find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007717 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007718 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719
Bram Moolenaara7043832005-01-21 11:56:39 +00007720 ht = find_var_ht(name, &varname);
7721 if (htp != NULL)
7722 *htp = ht;
7723 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007725 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7726 if (ret != NULL)
7727 return ret;
7728
7729 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007730 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731}
7732
7733/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007734 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007735 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007737 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007738find_var_in_ht(
7739 hashtab_T *ht,
7740 int htname,
7741 char_u *varname,
7742 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007743{
Bram Moolenaar33570922005-01-25 22:26:29 +00007744 hashitem_T *hi;
7745
7746 if (*varname == NUL)
7747 {
7748 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007749 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007750 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007751 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007752 case 'g': return &globvars_var;
7753 case 'v': return &vimvars_var;
7754 case 'b': return &curbuf->b_bufvar;
7755 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007756 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007757 case 'l': return get_funccal_local_var();
7758 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007759 }
7760 return NULL;
7761 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007762
7763 hi = hash_find(ht, varname);
7764 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007765 {
7766 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007767 * worked find the variable again. Don't auto-load a script if it was
7768 * loaded already, otherwise it would be loaded every time when
7769 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007770 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007771 {
7772 /* Note: script_autoload() may make "hi" invalid. It must either
7773 * be obtained again or not used. */
7774 if (!script_autoload(varname, FALSE) || aborting())
7775 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007776 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007777 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007778 if (HASHITEM_EMPTY(hi))
7779 return NULL;
7780 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007781 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007782}
7783
7784/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007785 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007786 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007787 * Set "varname" to the start of name without ':'.
7788 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007789 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007790find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007792 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007793 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007794
Bram Moolenaar73627d02015-08-11 15:46:09 +02007795 if (name[0] == NUL)
7796 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 if (name[1] != ':')
7798 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007799 /* The name must not start with a colon or #. */
7800 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 return NULL;
7802 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007803
Bram Moolenaard2e716e2019-04-20 14:39:52 +02007804 // "version" is "v:version" in all scopes if scriptversion < 3.
7805 // Same for a few other variables marked with VV_COMPAT.
7806 if (current_sctx.sc_version < 3)
7807 {
7808 hi = hash_find(&compat_hashtab, name);
7809 if (!HASHITEM_EMPTY(hi))
7810 return &compat_hashtab;
7811 }
Bram Moolenaar532c7802005-01-27 14:44:31 +00007812
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007813 ht = get_funccal_local_ht();
7814 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007815 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007816 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 }
7818 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007819 if (*name == 'g') /* global variable */
7820 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007821 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7822 */
7823 if (vim_strchr(name + 2, ':') != NULL
7824 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007825 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007827 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007829 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007830 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007831 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007832 if (*name == 'v') /* v: variable */
7833 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007834 if (*name == 'a') /* a: function argument */
7835 return get_funccal_args_ht();
7836 if (*name == 'l') /* l: local function variable */
7837 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 if (*name == 's' /* script variable */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007839 && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
7840 return &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 return NULL;
7842}
7843
7844/*
7845 * Get the string value of a (global/local) variable.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007846 * Note: see tv_get_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 * Returns NULL when it doesn't exist.
7848 */
7849 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007850get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851{
Bram Moolenaar33570922005-01-25 22:26:29 +00007852 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007854 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 if (v == NULL)
7856 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007857 return tv_get_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858}
7859
7860/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007861 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 * sourcing this script and when executing functions defined in the script.
7863 */
7864 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007865new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866{
Bram Moolenaara7043832005-01-21 11:56:39 +00007867 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007868 hashtab_T *ht;
7869 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007870
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7872 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007873 /* Re-allocating ga_data means that an ht_array pointing to
7874 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007875 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007876 for (i = 1; i <= ga_scripts.ga_len; ++i)
7877 {
7878 ht = &SCRIPT_VARS(i);
7879 if (ht->ht_mask == HT_INIT_SIZE - 1)
7880 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007881 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007882 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007883 }
7884
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 while (ga_scripts.ga_len < id)
7886 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007887 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007888 ALLOC_CLEAR_ONE(scriptvar_T);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007889 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 }
7892 }
7893}
7894
7895/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007896 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7897 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 */
7899 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007900init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901{
Bram Moolenaar33570922005-01-25 22:26:29 +00007902 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007903 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007904 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007905 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007906 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007907 dict_var->di_tv.vval.v_dict = dict;
7908 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007909 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007910 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7911 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912}
7913
7914/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007915 * Unreference a dictionary initialized by init_var_dict().
7916 */
7917 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007918unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007919{
7920 /* Now the dict needs to be freed if no one else is using it, go back to
7921 * normal reference counting. */
7922 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7923 dict_unref(dict);
7924}
7925
7926/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007928 * Frees all allocated variables and the value they contain.
7929 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 */
7931 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007932vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007933{
7934 vars_clear_ext(ht, TRUE);
7935}
7936
7937/*
7938 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7939 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007940 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007941vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942{
Bram Moolenaara7043832005-01-21 11:56:39 +00007943 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007944 hashitem_T *hi;
7945 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946
Bram Moolenaar33570922005-01-25 22:26:29 +00007947 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007948 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007949 for (hi = ht->ht_array; todo > 0; ++hi)
7950 {
7951 if (!HASHITEM_EMPTY(hi))
7952 {
7953 --todo;
7954
Bram Moolenaar33570922005-01-25 22:26:29 +00007955 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007956 * ht_array might change then. hash_clear() takes care of it
7957 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007958 v = HI2DI(hi);
7959 if (free_val)
7960 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007961 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007962 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007963 }
7964 }
7965 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007966 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967}
7968
Bram Moolenaara7043832005-01-21 11:56:39 +00007969/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007970 * Delete a variable from hashtab "ht" at item "hi".
7971 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007972 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007974delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975{
Bram Moolenaar33570922005-01-25 22:26:29 +00007976 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007977
7978 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007979 clear_tv(&di->di_tv);
7980 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981}
7982
7983/*
7984 * List the value of one internal variable.
7985 */
7986 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01007987list_one_var(dictitem_T *v, char *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007989 char_u *tofree;
7990 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007991 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007992
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007993 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007994 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007995 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007996 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997}
7998
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008000list_one_var_a(
Bram Moolenaar32526b32019-01-19 17:43:09 +01008001 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01008002 char_u *name,
8003 int type,
8004 char_u *string,
8005 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006{
Bram Moolenaar31859182007-08-14 20:41:13 +00008007 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
8008 msg_start();
8009 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 if (name != NULL) /* "a:" vars don't have a name stored */
Bram Moolenaar32526b32019-01-19 17:43:09 +01008011 msg_puts((char *)name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 msg_putchar(' ');
8013 msg_advance(22);
8014 if (type == VAR_NUMBER)
8015 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008016 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008017 msg_putchar('*');
8018 else if (type == VAR_LIST)
8019 {
8020 msg_putchar('[');
8021 if (*string == '[')
8022 ++string;
8023 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008024 else if (type == VAR_DICT)
8025 {
8026 msg_putchar('{');
8027 if (*string == '{')
8028 ++string;
8029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 else
8031 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008032
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008034
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008035 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008036 msg_puts("()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00008037 if (*first)
8038 {
8039 msg_clr_eos();
8040 *first = FALSE;
8041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042}
8043
8044/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008045 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 * If the variable already exists, the value is updated.
8047 * Otherwise the variable is created.
8048 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008049 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008050set_var(
8051 char_u *name,
8052 typval_T *tv,
8053 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054{
Bram Moolenaar33570922005-01-25 22:26:29 +00008055 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008057 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008059 ht = find_var_ht(name, &varname);
8060 if (ht == NULL || *varname == NUL)
8061 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008062 semsg(_(e_illvar), name);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008063 return;
8064 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02008065 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008066
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008067 /* Search in parent scope which is possible to reference from lambda */
8068 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02008069 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008070
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008071 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
8072 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008073 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008074
Bram Moolenaar33570922005-01-25 22:26:29 +00008075 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008077 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02008078 if (var_check_ro(v->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008079 || var_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00008080 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008081
8082 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008083 * Handle setting internal v: variables separately where needed to
8084 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00008085 */
8086 if (ht == &vimvarht)
8087 {
8088 if (v->di_tv.v_type == VAR_STRING)
8089 {
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01008090 VIM_CLEAR(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008091 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01008092 {
8093 char_u *val = tv_get_string(tv);
8094
8095 // Careful: when assigning to v:errmsg and tv_get_string()
8096 // causes an error message the variable will alrady be set.
8097 if (v->di_tv.vval.v_string == NULL)
8098 v->di_tv.vval.v_string = vim_strsave(val);
8099 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008100 else
8101 {
8102 /* Take over the string to avoid an extra alloc/free. */
8103 v->di_tv.vval.v_string = tv->vval.v_string;
8104 tv->vval.v_string = NULL;
8105 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008106 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008107 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008108 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008109 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008110 v->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008111 if (STRCMP(varname, "searchforward") == 0)
8112 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01008113#ifdef FEAT_SEARCH_EXTRA
8114 else if (STRCMP(varname, "hlsearch") == 0)
8115 {
8116 no_hlsearch = !v->di_tv.vval.v_number;
8117 redraw_all_later(SOME_VALID);
8118 }
8119#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008120 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008121 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008122 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01008123 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008124 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01008125 return;
8126 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008127 }
8128
8129 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 }
8131 else /* add a new variable */
8132 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01008133 // Can't add "v:" or "a:" variable.
8134 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00008135 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008136 semsg(_(e_illvar), name);
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00008137 return;
8138 }
8139
Bram Moolenaar31b81602019-02-10 22:14:27 +01008140 // Make sure the variable name is valid.
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008141 if (!valid_varname(varname))
8142 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00008143
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008144 v = alloc(sizeof(dictitem_T) + STRLEN(varname));
Bram Moolenaara7043832005-01-21 11:56:39 +00008145 if (v == NULL)
8146 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008147 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00008148 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008150 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 return;
8152 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02008153 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 }
Bram Moolenaara7043832005-01-21 11:56:39 +00008155
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008156 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00008157 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00008158 else
8159 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008160 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008161 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008162 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00008163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164}
8165
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008166/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008167 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00008168 * Also give an error message.
8169 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008170 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008171var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00008172{
8173 if (flags & DI_FLAGS_RO)
8174 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008175 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008176 return TRUE;
8177 }
8178 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
8179 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008180 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008181 return TRUE;
8182 }
8183 return FALSE;
8184}
8185
8186/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008187 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
8188 * Also give an error message.
8189 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008190 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008191var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008192{
8193 if (flags & DI_FLAGS_FIX)
8194 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008195 semsg(_("E795: Cannot delete variable %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008196 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008197 return TRUE;
8198 }
8199 return FALSE;
8200}
8201
8202/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008203 * Check if a funcref is assigned to a valid variable name.
8204 * Return TRUE and give an error if not.
8205 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008206 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008207var_check_func_name(
8208 char_u *name, /* points to start of variable name */
8209 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008210{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02008211 /* Allow for w: b: s: and t:. */
8212 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008213 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
8214 ? name[2] : name[0]))
8215 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008216 semsg(_("E704: Funcref variable name must start with a capital: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008217 name);
8218 return TRUE;
8219 }
8220 /* Don't allow hiding a function. When "v" is not NULL we might be
8221 * assigning another function to the same var, the type is checked
8222 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02008223 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008224 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008225 semsg(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008226 name);
8227 return TRUE;
8228 }
8229 return FALSE;
8230}
8231
8232/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008233 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02008234 * Also give an error message, using "name" or _("name") when use_gettext is
8235 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008236 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008237 int
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008238var_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008239{
8240 if (lock & VAR_LOCKED)
8241 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008242 semsg(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008243 name == NULL ? (char_u *)_("Unknown")
8244 : use_gettext ? (char_u *)_(name)
8245 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008246 return TRUE;
8247 }
8248 if (lock & VAR_FIXED)
8249 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008250 semsg(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008251 name == NULL ? (char_u *)_("Unknown")
8252 : use_gettext ? (char_u *)_(name)
8253 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008254 return TRUE;
8255 }
8256 return FALSE;
8257}
8258
8259/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008260 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
8261 * Also give an error message, using "name" or _("name") when use_gettext is
8262 * TRUE.
8263 */
8264 static int
8265tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
8266{
8267 int lock = 0;
8268
8269 switch (tv->v_type)
8270 {
8271 case VAR_BLOB:
8272 if (tv->vval.v_blob != NULL)
8273 lock = tv->vval.v_blob->bv_lock;
8274 break;
8275 case VAR_LIST:
8276 if (tv->vval.v_list != NULL)
8277 lock = tv->vval.v_list->lv_lock;
8278 break;
8279 case VAR_DICT:
8280 if (tv->vval.v_dict != NULL)
8281 lock = tv->vval.v_dict->dv_lock;
8282 break;
8283 default:
8284 break;
8285 }
8286 return var_check_lock(tv->v_lock, name, use_gettext)
8287 || (lock != 0 && var_check_lock(lock, name, use_gettext));
8288}
8289
8290/*
8291 * Check if a variable name is valid.
8292 * Return FALSE and give an error if not.
8293 */
8294 int
8295valid_varname(char_u *varname)
8296{
8297 char_u *p;
8298
8299 for (p = varname; *p != NUL; ++p)
8300 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
8301 && *p != AUTOLOAD_CHAR)
8302 {
8303 semsg(_(e_illvar), varname);
8304 return FALSE;
8305 }
8306 return TRUE;
8307}
8308
8309/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008310 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008311 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008312 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008313 * It is OK for "from" and "to" to point to the same item. This is used to
8314 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008315 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008316 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008317copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008319 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008320 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008321 switch (from->v_type)
8322 {
8323 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008324 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008325 to->vval.v_number = from->vval.v_number;
8326 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008327 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008328#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008329 to->vval.v_float = from->vval.v_float;
8330 break;
8331#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008332 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008333#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01008334 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01008335 if (to->vval.v_job != NULL)
8336 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01008337 break;
8338#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01008339 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008340#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01008341 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01008342 if (to->vval.v_channel != NULL)
8343 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01008344 break;
8345#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008346 case VAR_STRING:
8347 case VAR_FUNC:
8348 if (from->vval.v_string == NULL)
8349 to->vval.v_string = NULL;
8350 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008351 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008352 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008353 if (from->v_type == VAR_FUNC)
8354 func_ref(to->vval.v_string);
8355 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008356 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008357 case VAR_PARTIAL:
8358 if (from->vval.v_partial == NULL)
8359 to->vval.v_partial = NULL;
8360 else
8361 {
8362 to->vval.v_partial = from->vval.v_partial;
8363 ++to->vval.v_partial->pt_refcount;
8364 }
8365 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008366 case VAR_BLOB:
8367 if (from->vval.v_blob == NULL)
8368 to->vval.v_blob = NULL;
8369 else
8370 {
8371 to->vval.v_blob = from->vval.v_blob;
8372 ++to->vval.v_blob->bv_refcount;
8373 }
8374 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008375 case VAR_LIST:
8376 if (from->vval.v_list == NULL)
8377 to->vval.v_list = NULL;
8378 else
8379 {
8380 to->vval.v_list = from->vval.v_list;
8381 ++to->vval.v_list->lv_refcount;
8382 }
8383 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008384 case VAR_DICT:
8385 if (from->vval.v_dict == NULL)
8386 to->vval.v_dict = NULL;
8387 else
8388 {
8389 to->vval.v_dict = from->vval.v_dict;
8390 ++to->vval.v_dict->dv_refcount;
8391 }
8392 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008393 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008394 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008395 break;
8396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397}
8398
8399/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008400 * Make a copy of an item.
8401 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008402 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
8403 * reference to an already copied list/dict can be used.
8404 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008405 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008406 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008407item_copy(
8408 typval_T *from,
8409 typval_T *to,
8410 int deep,
8411 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008412{
8413 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008414 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008415
Bram Moolenaar33570922005-01-25 22:26:29 +00008416 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008417 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008418 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008419 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008420 }
8421 ++recurse;
8422
8423 switch (from->v_type)
8424 {
8425 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008426 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008427 case VAR_STRING:
8428 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008429 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008430 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008431 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008432 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008433 copy_tv(from, to);
8434 break;
8435 case VAR_LIST:
8436 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008437 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008438 if (from->vval.v_list == NULL)
8439 to->vval.v_list = NULL;
8440 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8441 {
8442 /* use the copy made earlier */
8443 to->vval.v_list = from->vval.v_list->lv_copylist;
8444 ++to->vval.v_list->lv_refcount;
8445 }
8446 else
8447 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8448 if (to->vval.v_list == NULL)
8449 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008450 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008451 case VAR_BLOB:
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008452 ret = blob_copy(from, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008453 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008454 case VAR_DICT:
8455 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008456 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008457 if (from->vval.v_dict == NULL)
8458 to->vval.v_dict = NULL;
8459 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8460 {
8461 /* use the copy made earlier */
8462 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8463 ++to->vval.v_dict->dv_refcount;
8464 }
8465 else
8466 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8467 if (to->vval.v_dict == NULL)
8468 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008469 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008470 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008471 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008472 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008473 }
8474 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008475 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008476}
8477
8478/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008479 * This function is used by f_input() and f_inputdialog() functions. The third
8480 * argument to f_input() specifies the type of completion to use at the
8481 * prompt. The third argument to f_inputdialog() specifies the value to return
8482 * when the user cancels the prompt.
8483 */
8484 void
8485get_user_input(
8486 typval_T *argvars,
8487 typval_T *rettv,
8488 int inputdialog,
8489 int secret)
8490{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008491 char_u *prompt = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008492 char_u *p = NULL;
8493 int c;
8494 char_u buf[NUMBUFLEN];
8495 int cmd_silent_save = cmd_silent;
8496 char_u *defstr = (char_u *)"";
8497 int xp_type = EXPAND_NOTHING;
8498 char_u *xp_arg = NULL;
8499
8500 rettv->v_type = VAR_STRING;
8501 rettv->vval.v_string = NULL;
8502
8503#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008504 /* While starting up, there is no place to enter text. When running tests
8505 * with --not-a-term we assume feedkeys() will be used. */
8506 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008507 return;
8508#endif
8509
8510 cmd_silent = FALSE; /* Want to see the prompt. */
8511 if (prompt != NULL)
8512 {
8513 /* Only the part of the message after the last NL is considered as
8514 * prompt for the command line */
8515 p = vim_strrchr(prompt, '\n');
8516 if (p == NULL)
8517 p = prompt;
8518 else
8519 {
8520 ++p;
8521 c = *p;
8522 *p = NUL;
8523 msg_start();
8524 msg_clr_eos();
Bram Moolenaar32526b32019-01-19 17:43:09 +01008525 msg_puts_attr((char *)prompt, echo_attr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008526 msg_didout = FALSE;
8527 msg_starthere();
8528 *p = c;
8529 }
8530 cmdline_row = msg_row;
8531
8532 if (argvars[1].v_type != VAR_UNKNOWN)
8533 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008534 defstr = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008535 if (defstr != NULL)
8536 stuffReadbuffSpec(defstr);
8537
8538 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8539 {
8540 char_u *xp_name;
8541 int xp_namelen;
8542 long argt;
8543
8544 /* input() with a third argument: completion */
8545 rettv->vval.v_string = NULL;
8546
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008547 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008548 if (xp_name == NULL)
8549 return;
8550
8551 xp_namelen = (int)STRLEN(xp_name);
8552
8553 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8554 &xp_arg) == FAIL)
8555 return;
8556 }
8557 }
8558
8559 if (defstr != NULL)
8560 {
8561 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02008562
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008563 ex_normal_busy = 0;
8564 rettv->vval.v_string =
8565 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8566 xp_type, xp_arg);
8567 ex_normal_busy = save_ex_normal_busy;
8568 }
8569 if (inputdialog && rettv->vval.v_string == NULL
8570 && argvars[1].v_type != VAR_UNKNOWN
8571 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008572 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008573 &argvars[2], buf));
8574
8575 vim_free(xp_arg);
8576
8577 /* since the user typed this, no need to wait for return */
8578 need_wait_return = FALSE;
8579 msg_didout = FALSE;
8580 }
8581 cmd_silent = cmd_silent_save;
8582}
8583
8584/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 * ":echo expr1 ..." print each argument separated with a space, add a
8586 * newline at the end.
8587 * ":echon expr1 ..." print each argument plain.
8588 */
8589 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008590ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591{
8592 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008593 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008594 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 char_u *p;
8596 int needclr = TRUE;
8597 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008598 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01008599 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008600 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601
8602 if (eap->skip)
8603 ++emsg_skip;
8604 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8605 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008606 /* If eval1() causes an error message the text from the command may
8607 * still need to be cleared. E.g., "echo 22,44". */
8608 need_clr_eos = needclr;
8609
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008611 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 {
8613 /*
8614 * Report the invalid expression unless the expression evaluation
8615 * has been cancelled due to an aborting error, an interrupt, or an
8616 * exception.
8617 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008618 if (!aborting() && did_emsg == did_emsg_before
8619 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008620 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008621 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 break;
8623 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008624 need_clr_eos = FALSE;
8625
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 if (!eap->skip)
8627 {
8628 if (atstart)
8629 {
8630 atstart = FALSE;
8631 /* Call msg_start() after eval1(), evaluating the expression
8632 * may cause a message to appear. */
8633 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008634 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008635 /* Mark the saved text as finishing the line, so that what
8636 * follows is displayed on a new line when scrolling back
8637 * at the more prompt. */
8638 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 }
8642 else if (eap->cmdidx == CMD_echo)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008643 msg_puts_attr(" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008644 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008645 if (p != NULL)
8646 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008648 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008650 if (*p != TAB && needclr)
8651 {
8652 /* remove any text still there from the command */
8653 msg_clr_eos();
8654 needclr = FALSE;
8655 }
8656 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 }
8658 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008659 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008660 if (has_mbyte)
8661 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008662 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008663
8664 (void)msg_outtrans_len_attr(p, i, echo_attr);
8665 p += i - 1;
8666 }
8667 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008668 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008671 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008673 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 arg = skipwhite(arg);
8675 }
8676 eap->nextcmd = check_nextcmd(arg);
8677
8678 if (eap->skip)
8679 --emsg_skip;
8680 else
8681 {
8682 /* remove text that may still be there from the command */
8683 if (needclr)
8684 msg_clr_eos();
8685 if (eap->cmdidx == CMD_echo)
8686 msg_end();
8687 }
8688}
8689
8690/*
8691 * ":echohl {name}".
8692 */
8693 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008694ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008696 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697}
8698
8699/*
8700 * ":execute expr1 ..." execute the result of an expression.
8701 * ":echomsg expr1 ..." Print a message
8702 * ":echoerr expr1 ..." Print an error
8703 * Each gets spaces around each argument and a newline at the end for
8704 * echo commands
8705 */
8706 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008707ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708{
8709 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008710 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 int ret = OK;
8712 char_u *p;
8713 garray_T ga;
8714 int len;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01008715 int save_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716
8717 ga_init2(&ga, 1, 80);
8718
8719 if (eap->skip)
8720 ++emsg_skip;
8721 while (*arg != NUL && *arg != '|' && *arg != '\n')
8722 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01008723 ret = eval1_emsg(&arg, &rettv, !eap->skip);
8724 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726
8727 if (!eap->skip)
8728 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008729 char_u buf[NUMBUFLEN];
8730
8731 if (eap->cmdidx == CMD_execute)
8732 p = tv_get_string_buf(&rettv, buf);
8733 else
8734 p = tv_stringify(&rettv, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 len = (int)STRLEN(p);
8736 if (ga_grow(&ga, len + 2) == FAIL)
8737 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008738 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 ret = FAIL;
8740 break;
8741 }
8742 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 ga.ga_len += len;
8746 }
8747
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008748 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 arg = skipwhite(arg);
8750 }
8751
8752 if (ret != FAIL && ga.ga_data != NULL)
8753 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008754 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8755 {
8756 /* Mark the already saved text as finishing the line, so that what
8757 * follows is displayed on a new line when scrolling back at the
8758 * more prompt. */
8759 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008760 }
8761
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008763 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01008764 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008765 out_flush();
8766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 else if (eap->cmdidx == CMD_echoerr)
8768 {
8769 /* We don't want to abort following commands, restore did_emsg. */
8770 save_did_emsg = did_emsg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008771 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 if (!force_abort)
8773 did_emsg = save_did_emsg;
8774 }
8775 else if (eap->cmdidx == CMD_execute)
8776 do_cmdline((char_u *)ga.ga_data,
8777 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8778 }
8779
8780 ga_clear(&ga);
8781
8782 if (eap->skip)
8783 --emsg_skip;
8784
8785 eap->nextcmd = check_nextcmd(arg);
8786}
8787
8788/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008789 * Find window specified by "vp" in tabpage "tp".
8790 */
8791 win_T *
8792find_win_by_nr(
8793 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01008794 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008795{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008796 win_T *wp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008797 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008798
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008799 if (nr < 0)
8800 return NULL;
8801 if (nr == 0)
8802 return curwin;
8803
Bram Moolenaar29323592016-07-24 22:04:11 +02008804 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8805 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008806 if (nr >= LOWEST_WIN_ID)
8807 {
8808 if (wp->w_id == nr)
8809 return wp;
8810 }
8811 else if (--nr <= 0)
8812 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008813 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008814 if (nr >= LOWEST_WIN_ID)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008815 {
8816#ifdef FEAT_TEXT_PROP
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02008817 // check tab-local popup windows
8818 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008819 if (wp->w_id == nr)
8820 return wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02008821 // check global popup windows
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008822 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
8823 if (wp->w_id == nr)
8824 return wp;
8825#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008826 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008827 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008828 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008829}
8830
8831/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02008832 * Find a window: When using a Window ID in any tab page, when using a number
8833 * in the current tab page.
8834 */
8835 win_T *
8836find_win_by_nr_or_id(typval_T *vp)
8837{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008838 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02008839
8840 if (nr >= LOWEST_WIN_ID)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01008841 return win_id2wp(tv_get_number(vp));
Bram Moolenaare6e39892018-10-25 12:32:11 +02008842 return find_win_by_nr(vp, NULL);
8843}
8844
8845/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008846 * Find window specified by "wvp" in tabpage "tvp".
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008847 * Returns the tab page in 'ptp'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008848 */
8849 win_T *
8850find_tabwin(
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008851 typval_T *wvp, // VAR_UNKNOWN for current window
8852 typval_T *tvp, // VAR_UNKNOWN for current tab page
8853 tabpage_T **ptp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008854{
8855 win_T *wp = NULL;
8856 tabpage_T *tp = NULL;
8857 long n;
8858
8859 if (wvp->v_type != VAR_UNKNOWN)
8860 {
8861 if (tvp->v_type != VAR_UNKNOWN)
8862 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008863 n = (long)tv_get_number(tvp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008864 if (n >= 0)
8865 tp = find_tabpage(n);
8866 }
8867 else
8868 tp = curtab;
8869
8870 if (tp != NULL)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008871 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008872 wp = find_win_by_nr(wvp, tp);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008873 if (wp == NULL && wvp->v_type == VAR_NUMBER
8874 && wvp->vval.v_number != -1)
8875 // A window with the specified number is not found
8876 tp = NULL;
8877 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008878 }
8879 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008880 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008881 wp = curwin;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008882 tp = curtab;
8883 }
8884
8885 if (ptp != NULL)
8886 *ptp = tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008887
8888 return wp;
8889}
8890
8891/*
8892 * getwinvar() and gettabwinvar()
8893 */
8894 void
8895getwinvar(
8896 typval_T *argvars,
8897 typval_T *rettv,
8898 int off) /* 1 for gettabwinvar() */
8899{
8900 win_T *win;
8901 char_u *varname;
8902 dictitem_T *v;
8903 tabpage_T *tp = NULL;
8904 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008905 win_T *oldcurwin;
8906 tabpage_T *oldtabpage;
8907 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008908
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008909 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008910 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008911 else
8912 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008913 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008914 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008915 ++emsg_off;
8916
8917 rettv->v_type = VAR_STRING;
8918 rettv->vval.v_string = NULL;
8919
8920 if (win != NULL && varname != NULL)
8921 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008922 /* Set curwin to be our win, temporarily. Also set the tabpage,
8923 * otherwise the window is not valid. Only do this when needed,
8924 * autocommands get blocked. */
8925 need_switch_win = !(tp == curtab && win == curwin);
8926 if (!need_switch_win
8927 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008928 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008929 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008930 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008931 if (varname[1] == NUL)
8932 {
8933 /* get all window-local options in a dict */
8934 dict_T *opts = get_winbuf_options(FALSE);
8935
8936 if (opts != NULL)
8937 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008938 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02008939 done = TRUE;
8940 }
8941 }
8942 else if (get_option_tv(&varname, rettv, 1) == OK)
8943 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008944 done = TRUE;
8945 }
8946 else
8947 {
8948 /* Look up the variable. */
8949 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8950 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8951 varname, FALSE);
8952 if (v != NULL)
8953 {
8954 copy_tv(&v->di_tv, rettv);
8955 done = TRUE;
8956 }
8957 }
8958 }
8959
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008960 if (need_switch_win)
8961 /* restore previous notion of curwin */
8962 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008963 }
8964
8965 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8966 /* use the default return value */
8967 copy_tv(&argvars[off + 2], rettv);
8968
8969 --emsg_off;
8970}
8971
8972/*
8973 * "setwinvar()" and "settabwinvar()" functions
8974 */
8975 void
8976setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8977{
8978 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008979 win_T *save_curwin;
8980 tabpage_T *save_curtab;
8981 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008982 char_u *varname, *winvarname;
8983 typval_T *varp;
8984 char_u nbuf[NUMBUFLEN];
8985 tabpage_T *tp = NULL;
8986
Bram Moolenaare0fb7d12019-02-12 20:48:10 +01008987 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008988 return;
8989
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008990 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008991 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008992 else
8993 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008994 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008995 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008996 varp = &argvars[off + 2];
8997
8998 if (win != NULL && varname != NULL && varp != NULL)
8999 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009000 need_switch_win = !(tp == curtab && win == curwin);
9001 if (!need_switch_win
9002 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009003 {
9004 if (*varname == '&')
9005 {
9006 long numval;
9007 char_u *strval;
9008 int error = FALSE;
9009
9010 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009011 numval = (long)tv_get_number_chk(varp, &error);
9012 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009013 if (!error && strval != NULL)
9014 set_option_value(varname, numval, strval, OPT_LOCAL);
9015 }
9016 else
9017 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02009018 winvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009019 if (winvarname != NULL)
9020 {
9021 STRCPY(winvarname, "w:");
9022 STRCPY(winvarname + 2, varname);
9023 set_var(winvarname, varp, TRUE);
9024 vim_free(winvarname);
9025 }
9026 }
9027 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009028 if (need_switch_win)
9029 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009030 }
9031}
9032
9033/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
9035 * "arg" points to the "&" or '+' when called, to "option" when returning.
9036 * Returns NULL when no option name found. Otherwise pointer to the char
9037 * after the option name.
9038 */
9039 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009040find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041{
9042 char_u *p = *arg;
9043
9044 ++p;
9045 if (*p == 'g' && p[1] == ':')
9046 {
9047 *opt_flags = OPT_GLOBAL;
9048 p += 2;
9049 }
9050 else if (*p == 'l' && p[1] == ':')
9051 {
9052 *opt_flags = OPT_LOCAL;
9053 p += 2;
9054 }
9055 else
9056 *opt_flags = 0;
9057
9058 if (!ASCII_ISALPHA(*p))
9059 return NULL;
9060 *arg = p;
9061
9062 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
9063 p += 4; /* termcap option */
9064 else
9065 while (ASCII_ISALPHA(*p))
9066 ++p;
9067 return p;
9068}
9069
9070/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009071 * Return the autoload script name for a function or variable name.
9072 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02009074 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009075autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02009076{
Bram Moolenaara1544c02013-05-30 12:35:52 +02009077 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009078 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02009079
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009080 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaar964b3742019-05-24 18:54:09 +02009081 scriptname = alloc(STRLEN(name) + 14);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009082 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02009083 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009084 STRCPY(scriptname, "autoload/");
9085 STRCAT(scriptname, name);
9086 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
9087 STRCAT(scriptname, ".vim");
9088 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
9089 *p = '/';
9090 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009091}
9092
9093/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009094 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009095 * Return TRUE if a package was loaded.
9096 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009097 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009098script_autoload(
9099 char_u *name,
9100 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009101{
9102 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009103 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009104 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009105 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009106
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009107 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00009108 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009109 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009110 return FALSE;
9111
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009112 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009113
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009114 /* Find the name in the list of previously loaded package names. Skip
9115 * "autoload/", it's always the same. */
9116 for (i = 0; i < ga_loaded.ga_len; ++i)
9117 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
9118 break;
9119 if (!reload && i < ga_loaded.ga_len)
9120 ret = FALSE; /* was loaded already */
9121 else
9122 {
9123 /* Remember the name if it wasn't loaded already. */
9124 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
9125 {
9126 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
9127 tofree = NULL;
9128 }
9129
9130 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009131 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009132 ret = TRUE;
9133 }
9134
9135 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009136 return ret;
9137}
9138
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
9140typedef enum
9141{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009142 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
9143 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
9144 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145} var_flavour_T;
9146
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01009148var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149{
9150 char_u *p = varname;
9151
9152 if (ASCII_ISUPPER(*p))
9153 {
9154 while (*(++p))
9155 if (ASCII_ISLOWER(*p))
9156 return VAR_FLAVOUR_SESSION;
9157 return VAR_FLAVOUR_VIMINFO;
9158 }
9159 else
9160 return VAR_FLAVOUR_DEFAULT;
9161}
9162#endif
9163
9164#if defined(FEAT_VIMINFO) || defined(PROTO)
9165/*
9166 * Restore global vars that start with a capital from the viminfo file
9167 */
9168 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009169read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170{
9171 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009172 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00009173 typval_T tv;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009174 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175
9176 if (!writing && (find_viminfo_parameter('!') != NULL))
9177 {
9178 tab = vim_strchr(virp->vir_line + 1, '\t');
9179 if (tab != NULL)
9180 {
9181 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009182 switch (*tab)
9183 {
9184 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009185#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009186 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009187#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009188 case 'D': type = VAR_DICT; break;
9189 case 'L': type = VAR_LIST; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009190 case 'B': type = VAR_BLOB; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009191 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193
9194 tab = vim_strchr(tab, '\t');
9195 if (tab != NULL)
9196 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009197 tv.v_type = type;
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009198 if (type == VAR_STRING || type == VAR_DICT
9199 || type == VAR_LIST || type == VAR_BLOB)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009200 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009202#ifdef FEAT_FLOAT
9203 else if (type == VAR_FLOAT)
9204 (void)string2float(tab + 1, &tv.vval.v_float);
9205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009207 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009208 if (type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009209 {
9210 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
9211
9212 if (etv == NULL)
9213 /* Failed to parse back the dict or list, use it as a
9214 * string. */
9215 tv.v_type = VAR_STRING;
9216 else
9217 {
9218 vim_free(tv.vval.v_string);
9219 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01009220 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009221 }
9222 }
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009223 else if (type == VAR_BLOB)
9224 {
9225 blob_T *blob = string2blob(tv.vval.v_string);
9226
9227 if (blob == NULL)
9228 // Failed to parse back the blob, use it as a string.
9229 tv.v_type = VAR_STRING;
9230 else
9231 {
9232 vim_free(tv.vval.v_string);
9233 tv.v_type = VAR_BLOB;
9234 tv.vval.v_blob = blob;
9235 }
9236 }
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009237
Bram Moolenaarb20e3342016-01-18 23:29:01 +01009238 /* when in a function use global variables */
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009239 save_funccal(&funccal_entry);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009240 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009241 restore_funccal();
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009242
9243 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009244 vim_free(tv.vval.v_string);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009245 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST ||
9246 tv.v_type == VAR_BLOB)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009247 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009248 }
9249 }
9250 }
9251
9252 return viminfo_readline(virp);
9253}
9254
9255/*
9256 * Write global vars that start with a capital to the viminfo file
9257 */
9258 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009259write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260{
Bram Moolenaar33570922005-01-25 22:26:29 +00009261 hashitem_T *hi;
9262 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009263 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01009264 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009265 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009266 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009267 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268
9269 if (find_viminfo_parameter('!') == NULL)
9270 return;
9271
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02009272 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00009273
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009274 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009275 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009277 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009279 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009280 this_var = HI2DI(hi);
9281 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009282 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009283 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00009284 {
9285 case VAR_STRING: s = "STR"; break;
9286 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009287 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009288 case VAR_DICT: s = "DIC"; break;
9289 case VAR_LIST: s = "LIS"; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009290 case VAR_BLOB: s = "BLO"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009291 case VAR_SPECIAL: s = "XPL"; break;
9292
9293 case VAR_UNKNOWN:
9294 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009295 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01009296 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01009297 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01009298 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00009299 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009300 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009301 if (this_var->di_tv.v_type == VAR_SPECIAL)
9302 {
9303 sprintf((char *)numbuf, "%ld",
9304 (long)this_var->di_tv.vval.v_number);
9305 p = numbuf;
9306 tofree = NULL;
9307 }
9308 else
9309 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009310 if (p != NULL)
9311 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00009312 vim_free(tofree);
9313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314 }
9315 }
9316}
9317#endif
9318
9319#if defined(FEAT_SESSION) || defined(PROTO)
9320 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009321store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322{
Bram Moolenaar33570922005-01-25 22:26:29 +00009323 hashitem_T *hi;
9324 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009325 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326 char_u *p, *t;
9327
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009328 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009329 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009331 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009333 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009334 this_var = HI2DI(hi);
9335 if ((this_var->di_tv.v_type == VAR_NUMBER
9336 || this_var->di_tv.v_type == VAR_STRING)
9337 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009338 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009339 /* Escape special characters with a backslash. Turn a LF and
9340 * CR into \n and \r. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009341 p = vim_strsave_escaped(tv_get_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00009342 (char_u *)"\\\"\n\r");
9343 if (p == NULL) /* out of memory */
9344 break;
9345 for (t = p; *t != NUL; ++t)
9346 if (*t == '\n')
9347 *t = 'n';
9348 else if (*t == '\r')
9349 *t = 'r';
9350 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00009351 this_var->di_key,
9352 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9353 : ' ',
9354 p,
9355 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9356 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00009357 || put_eol(fd) == FAIL)
9358 {
9359 vim_free(p);
9360 return FAIL;
9361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362 vim_free(p);
9363 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009364#ifdef FEAT_FLOAT
9365 else if (this_var->di_tv.v_type == VAR_FLOAT
9366 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
9367 {
9368 float_T f = this_var->di_tv.vval.v_float;
9369 int sign = ' ';
9370
9371 if (f < 0)
9372 {
9373 f = -f;
9374 sign = '-';
9375 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01009376 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009377 this_var->di_key, sign, f) < 0)
9378 || put_eol(fd) == FAIL)
9379 return FAIL;
9380 }
9381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382 }
9383 }
9384 return OK;
9385}
9386#endif
9387
Bram Moolenaar661b1822005-07-28 22:36:45 +00009388/*
9389 * Display script name where an item was last set.
9390 * Should only be invoked when 'verbose' is non-zero.
9391 */
9392 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009393last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009394{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009395 char_u *p;
9396
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009397 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009398 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009399 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009400 if (p != NULL)
9401 {
9402 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01009403 msg_puts(_("\n\tLast set from "));
9404 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009405 if (script_ctx.sc_lnum > 0)
9406 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01009407 msg_puts(_(" line "));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009408 msg_outnum((long)script_ctx.sc_lnum);
9409 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009410 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009411 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009412 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00009413 }
9414}
9415
Bram Moolenaar61be3762019-03-19 23:04:17 +01009416/*
9417 * Reset v:option_new, v:option_old and v:option_type.
9418 */
Bram Moolenaar53744302015-07-17 17:38:22 +02009419 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009420reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02009421{
9422 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
9423 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
9424 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
9425}
9426
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009427/*
9428 * Prepare "gap" for an assert error and add the sourcing position.
9429 */
9430 void
9431prepare_assert_error(garray_T *gap)
9432{
9433 char buf[NUMBUFLEN];
9434
9435 ga_init2(gap, 1, 100);
9436 if (sourcing_name != NULL)
9437 {
9438 ga_concat(gap, sourcing_name);
9439 if (sourcing_lnum > 0)
9440 ga_concat(gap, (char_u *)" ");
9441 }
9442 if (sourcing_lnum > 0)
9443 {
9444 sprintf(buf, "line %ld", (long)sourcing_lnum);
9445 ga_concat(gap, (char_u *)buf);
9446 }
9447 if (sourcing_name != NULL || sourcing_lnum > 0)
9448 ga_concat(gap, (char_u *)": ");
9449}
9450
9451/*
9452 * Add an assert error to v:errors.
9453 */
9454 void
9455assert_error(garray_T *gap)
9456{
9457 struct vimvar *vp = &vimvars[VV_ERRORS];
9458
9459 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9460 /* Make sure v:errors is a list. */
9461 set_vim_var_list(VV_ERRORS, list_alloc());
9462 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9463}
9464
Bram Moolenaar65a54642018-04-28 16:56:53 +02009465 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009466assert_equal_common(typval_T *argvars, assert_type_T atype)
9467{
9468 garray_T ga;
9469
9470 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9471 != (atype == ASSERT_EQUAL))
9472 {
9473 prepare_assert_error(&ga);
9474 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9475 atype);
9476 assert_error(&ga);
9477 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009478 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009479 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009480 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009481}
9482
Bram Moolenaar65a54642018-04-28 16:56:53 +02009483 int
Bram Moolenaard96ff162018-02-18 22:13:29 +01009484assert_equalfile(typval_T *argvars)
9485{
9486 char_u buf1[NUMBUFLEN];
9487 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009488 char_u *fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
9489 char_u *fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009490 garray_T ga;
9491 FILE *fd1;
9492 FILE *fd2;
9493
9494 if (fname1 == NULL || fname2 == NULL)
Bram Moolenaar65a54642018-04-28 16:56:53 +02009495 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009496
9497 IObuff[0] = NUL;
9498 fd1 = mch_fopen((char *)fname1, READBIN);
9499 if (fd1 == NULL)
9500 {
9501 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname1);
9502 }
9503 else
9504 {
9505 fd2 = mch_fopen((char *)fname2, READBIN);
9506 if (fd2 == NULL)
9507 {
9508 fclose(fd1);
9509 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname2);
9510 }
9511 else
9512 {
9513 int c1, c2;
9514 long count = 0;
9515
9516 for (;;)
9517 {
9518 c1 = fgetc(fd1);
9519 c2 = fgetc(fd2);
9520 if (c1 == EOF)
9521 {
9522 if (c2 != EOF)
9523 STRCPY(IObuff, "first file is shorter");
9524 break;
9525 }
9526 else if (c2 == EOF)
9527 {
9528 STRCPY(IObuff, "second file is shorter");
9529 break;
9530 }
9531 else if (c1 != c2)
9532 {
9533 vim_snprintf((char *)IObuff, IOSIZE,
9534 "difference at byte %ld", count);
9535 break;
9536 }
9537 ++count;
9538 }
Bram Moolenaar30494182018-02-20 21:46:05 +01009539 fclose(fd1);
9540 fclose(fd2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009541 }
9542 }
9543 if (IObuff[0] != NUL)
9544 {
9545 prepare_assert_error(&ga);
9546 ga_concat(&ga, IObuff);
9547 assert_error(&ga);
9548 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009549 return 1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009550 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009551 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009552}
9553
Bram Moolenaar65a54642018-04-28 16:56:53 +02009554 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009555assert_match_common(typval_T *argvars, assert_type_T atype)
9556{
9557 garray_T ga;
9558 char_u buf1[NUMBUFLEN];
9559 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009560 char_u *pat = tv_get_string_buf_chk(&argvars[0], buf1);
9561 char_u *text = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009562
9563 if (pat == NULL || text == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009564 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009565 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
9566 {
9567 prepare_assert_error(&ga);
9568 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9569 atype);
9570 assert_error(&ga);
9571 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009572 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009573 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009574 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009575}
9576
Bram Moolenaar65a54642018-04-28 16:56:53 +02009577 int
Bram Moolenaar61c04492016-07-23 15:35:35 +02009578assert_inrange(typval_T *argvars)
9579{
9580 garray_T ga;
9581 int error = FALSE;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009582 char_u *tofree;
9583 char msg[200];
9584 char_u numbuf[NUMBUFLEN];
9585
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009586#ifdef FEAT_FLOAT
9587 if (argvars[0].v_type == VAR_FLOAT
9588 || argvars[1].v_type == VAR_FLOAT
9589 || argvars[2].v_type == VAR_FLOAT)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009590 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009591 float_T flower = tv_get_float(&argvars[0]);
9592 float_T fupper = tv_get_float(&argvars[1]);
9593 float_T factual = tv_get_float(&argvars[2]);
9594
9595 if (factual < flower || factual > fupper)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009596 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009597 prepare_assert_error(&ga);
9598 if (argvars[3].v_type != VAR_UNKNOWN)
9599 {
9600 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9601 vim_free(tofree);
9602 }
9603 else
9604 {
9605 vim_snprintf(msg, 200, "Expected range %g - %g, but got %g",
9606 flower, fupper, factual);
9607 ga_concat(&ga, (char_u *)msg);
9608 }
9609 assert_error(&ga);
9610 ga_clear(&ga);
9611 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009612 }
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009613 }
9614 else
9615#endif
9616 {
9617 varnumber_T lower = tv_get_number_chk(&argvars[0], &error);
9618 varnumber_T upper = tv_get_number_chk(&argvars[1], &error);
9619 varnumber_T actual = tv_get_number_chk(&argvars[2], &error);
9620
9621 if (error)
9622 return 0;
9623 if (actual < lower || actual > upper)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009624 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009625 prepare_assert_error(&ga);
9626 if (argvars[3].v_type != VAR_UNKNOWN)
9627 {
9628 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9629 vim_free(tofree);
9630 }
9631 else
9632 {
9633 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
Bram Moolenaar61c04492016-07-23 15:35:35 +02009634 (long)lower, (long)upper, (long)actual);
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009635 ga_concat(&ga, (char_u *)msg);
9636 }
9637 assert_error(&ga);
9638 ga_clear(&ga);
9639 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009640 }
Bram Moolenaar61c04492016-07-23 15:35:35 +02009641 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009642 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009643}
9644
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009645/*
9646 * Common for assert_true() and assert_false().
Bram Moolenaar65a54642018-04-28 16:56:53 +02009647 * Return non-zero for failure.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009648 */
Bram Moolenaar65a54642018-04-28 16:56:53 +02009649 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009650assert_bool(typval_T *argvars, int isTrue)
9651{
9652 int error = FALSE;
9653 garray_T ga;
9654
9655 if (argvars[0].v_type == VAR_SPECIAL
9656 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar65a54642018-04-28 16:56:53 +02009657 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009658 if (argvars[0].v_type != VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009659 || (tv_get_number_chk(&argvars[0], &error) == 0) == isTrue
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009660 || error)
9661 {
9662 prepare_assert_error(&ga);
9663 fill_assert_error(&ga, &argvars[1],
9664 (char_u *)(isTrue ? "True" : "False"),
9665 NULL, &argvars[0], ASSERT_OTHER);
9666 assert_error(&ga);
9667 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009668 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009669 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009670 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009671}
9672
Bram Moolenaar65a54642018-04-28 16:56:53 +02009673 int
Bram Moolenaar42205552017-03-18 19:42:22 +01009674assert_report(typval_T *argvars)
9675{
9676 garray_T ga;
9677
9678 prepare_assert_error(&ga);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009679 ga_concat(&ga, tv_get_string(&argvars[0]));
Bram Moolenaar42205552017-03-18 19:42:22 +01009680 assert_error(&ga);
9681 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009682 return 1;
Bram Moolenaar42205552017-03-18 19:42:22 +01009683}
9684
Bram Moolenaar65a54642018-04-28 16:56:53 +02009685 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009686assert_exception(typval_T *argvars)
9687{
9688 garray_T ga;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009689 char_u *error = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009690
9691 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9692 {
9693 prepare_assert_error(&ga);
9694 ga_concat(&ga, (char_u *)"v:exception is not set");
9695 assert_error(&ga);
9696 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009697 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009698 }
9699 else if (error != NULL
9700 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9701 {
9702 prepare_assert_error(&ga);
9703 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9704 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9705 assert_error(&ga);
9706 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009707 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009708 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009709 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009710}
9711
Bram Moolenaar65a54642018-04-28 16:56:53 +02009712 int
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009713assert_beeps(typval_T *argvars)
9714{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009715 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009716 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009717 int ret = 0;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009718
9719 called_vim_beep = FALSE;
9720 suppress_errthrow = TRUE;
9721 emsg_silent = FALSE;
9722 do_cmdline_cmd(cmd);
9723 if (!called_vim_beep)
9724 {
9725 prepare_assert_error(&ga);
9726 ga_concat(&ga, (char_u *)"command did not beep: ");
9727 ga_concat(&ga, cmd);
9728 assert_error(&ga);
9729 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009730 ret = 1;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009731 }
9732
9733 suppress_errthrow = FALSE;
9734 emsg_on_display = FALSE;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009735 return ret;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009736}
9737
Bram Moolenaar25190db2019-05-04 15:05:28 +02009738 static void
9739assert_append_cmd_or_arg(garray_T *gap, typval_T *argvars, char_u *cmd)
9740{
9741 char_u *tofree;
9742 char_u numbuf[NUMBUFLEN];
9743
9744 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9745 {
9746 ga_concat(gap, echo_string(&argvars[2], &tofree, numbuf, 0));
9747 vim_free(tofree);
9748 }
9749 else
9750 ga_concat(gap, cmd);
9751}
9752
Bram Moolenaar65a54642018-04-28 16:56:53 +02009753 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009754assert_fails(typval_T *argvars)
9755{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009756 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009757 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009758 int ret = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009759
9760 called_emsg = FALSE;
9761 suppress_errthrow = TRUE;
9762 emsg_silent = TRUE;
9763 do_cmdline_cmd(cmd);
9764 if (!called_emsg)
9765 {
9766 prepare_assert_error(&ga);
9767 ga_concat(&ga, (char_u *)"command did not fail: ");
Bram Moolenaar25190db2019-05-04 15:05:28 +02009768 assert_append_cmd_or_arg(&ga, argvars, cmd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009769 assert_error(&ga);
9770 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009771 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009772 }
9773 else if (argvars[1].v_type != VAR_UNKNOWN)
9774 {
9775 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009776 char *error = (char *)tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009777
9778 if (error == NULL
9779 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9780 {
9781 prepare_assert_error(&ga);
9782 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9783 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaar25190db2019-05-04 15:05:28 +02009784 ga_concat(&ga, (char_u *)": ");
9785 assert_append_cmd_or_arg(&ga, argvars, cmd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009786 assert_error(&ga);
9787 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009788 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009789 }
9790 }
9791
9792 called_emsg = FALSE;
9793 suppress_errthrow = FALSE;
9794 emsg_silent = FALSE;
9795 emsg_on_display = FALSE;
9796 set_vim_var_string(VV_ERRMSG, NULL, 0);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009797 return ret;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009798}
9799
9800/*
Bram Moolenaar86576712019-01-25 20:48:33 +01009801 * Append "p[clen]" to "gap", escaping unprintable characters.
9802 * Changes NL to \n, CR to \r, etc.
9803 */
9804 static void
9805ga_concat_esc(garray_T *gap, char_u *p, int clen)
9806{
9807 char_u buf[NUMBUFLEN];
9808
9809 if (clen > 1)
9810 {
9811 mch_memmove(buf, p, clen);
9812 buf[clen] = NUL;
9813 ga_concat(gap, buf);
9814 }
9815 else switch (*p)
9816 {
9817 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9818 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9819 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9820 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9821 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9822 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9823 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9824 default:
9825 if (*p < ' ')
9826 {
9827 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9828 ga_concat(gap, buf);
9829 }
9830 else
9831 ga_append(gap, *p);
9832 break;
9833 }
9834}
9835
9836/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009837 * Append "str" to "gap", escaping unprintable characters.
9838 * Changes NL to \n, CR to \r, etc.
9839 */
9840 static void
Bram Moolenaar86576712019-01-25 20:48:33 +01009841ga_concat_shorten_esc(garray_T *gap, char_u *str)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009842{
9843 char_u *p;
Bram Moolenaar86576712019-01-25 20:48:33 +01009844 char_u *s;
9845 int c;
9846 int clen;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009847 char_u buf[NUMBUFLEN];
Bram Moolenaar86576712019-01-25 20:48:33 +01009848 int same_len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009849
9850 if (str == NULL)
9851 {
9852 ga_concat(gap, (char_u *)"NULL");
9853 return;
9854 }
9855
9856 for (p = str; *p != NUL; ++p)
Bram Moolenaar86576712019-01-25 20:48:33 +01009857 {
9858 same_len = 1;
9859 s = p;
9860 c = mb_ptr2char_adv(&s);
9861 clen = s - p;
9862 while (*s != NUL && c == mb_ptr2char(s))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009863 {
Bram Moolenaar86576712019-01-25 20:48:33 +01009864 ++same_len;
9865 s += clen;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009866 }
Bram Moolenaar86576712019-01-25 20:48:33 +01009867 if (same_len > 20)
9868 {
9869 ga_concat(gap, (char_u *)"\\[");
9870 ga_concat_esc(gap, p, clen);
9871 ga_concat(gap, (char_u *)" occurs ");
9872 vim_snprintf((char *)buf, NUMBUFLEN, "%d", same_len);
9873 ga_concat(gap, buf);
9874 ga_concat(gap, (char_u *)" times]");
9875 p = s - 1;
9876 }
9877 else
9878 ga_concat_esc(gap, p, clen);
9879 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009880}
9881
9882/*
9883 * Fill "gap" with information about an assert error.
9884 */
9885 void
9886fill_assert_error(
9887 garray_T *gap,
9888 typval_T *opt_msg_tv,
9889 char_u *exp_str,
9890 typval_T *exp_tv,
9891 typval_T *got_tv,
9892 assert_type_T atype)
9893{
9894 char_u numbuf[NUMBUFLEN];
9895 char_u *tofree;
9896
9897 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9898 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009899 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9900 vim_free(tofree);
9901 ga_concat(gap, (char_u *)": ");
9902 }
9903
9904 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9905 ga_concat(gap, (char_u *)"Pattern ");
9906 else if (atype == ASSERT_NOTEQUAL)
9907 ga_concat(gap, (char_u *)"Expected not equal to ");
9908 else
9909 ga_concat(gap, (char_u *)"Expected ");
9910 if (exp_str == NULL)
9911 {
Bram Moolenaar86576712019-01-25 20:48:33 +01009912 ga_concat_shorten_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009913 vim_free(tofree);
9914 }
9915 else
Bram Moolenaar86576712019-01-25 20:48:33 +01009916 ga_concat_shorten_esc(gap, exp_str);
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009917 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009918 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009919 if (atype == ASSERT_MATCH)
9920 ga_concat(gap, (char_u *)" does not match ");
9921 else if (atype == ASSERT_NOTMATCH)
9922 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009923 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009924 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar86576712019-01-25 20:48:33 +01009925 ga_concat_shorten_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009926 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009927 }
9928}
9929
Bram Moolenaar31988702018-02-13 12:57:42 +01009930/*
9931 * Compare "typ1" and "typ2". Put the result in "typ1".
9932 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009933 int
9934typval_compare(
9935 typval_T *typ1, /* first operand */
9936 typval_T *typ2, /* second operand */
9937 exptype_T type, /* operator */
9938 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +01009939 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009940{
9941 int i;
9942 varnumber_T n1, n2;
9943 char_u *s1, *s2;
9944 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
9945
Bram Moolenaar31988702018-02-13 12:57:42 +01009946 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009947 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009948 /* For "is" a different type always means FALSE, for "notis"
9949 * it means TRUE. */
9950 n1 = (type == TYPE_NEQUAL);
9951 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009952 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
9953 {
9954 if (type_is)
9955 {
9956 n1 = (typ1->v_type == typ2->v_type
9957 && typ1->vval.v_blob == typ2->vval.v_blob);
9958 if (type == TYPE_NEQUAL)
9959 n1 = !n1;
9960 }
9961 else if (typ1->v_type != typ2->v_type
9962 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9963 {
9964 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009965 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009966 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009967 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009968 clear_tv(typ1);
9969 return FAIL;
9970 }
9971 else
9972 {
9973 // Compare two Blobs for being equal or unequal.
9974 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
9975 if (type == TYPE_NEQUAL)
9976 n1 = !n1;
9977 }
9978 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009979 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
9980 {
9981 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009982 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009983 n1 = (typ1->v_type == typ2->v_type
9984 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009985 if (type == TYPE_NEQUAL)
9986 n1 = !n1;
9987 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009988 else if (typ1->v_type != typ2->v_type
9989 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009990 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009991 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009992 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009993 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009994 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009995 clear_tv(typ1);
9996 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009997 }
9998 else
9999 {
Bram Moolenaar31988702018-02-13 12:57:42 +010010000 /* Compare two Lists for being equal or unequal. */
10001 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
10002 ic, FALSE);
10003 if (type == TYPE_NEQUAL)
10004 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010005 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010006 }
Bram Moolenaar31988702018-02-13 12:57:42 +010010007
10008 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
10009 {
10010 if (type_is)
10011 {
10012 n1 = (typ1->v_type == typ2->v_type
10013 && typ1->vval.v_dict == typ2->vval.v_dict);
10014 if (type == TYPE_NEQUAL)
10015 n1 = !n1;
10016 }
10017 else if (typ1->v_type != typ2->v_type
10018 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
10019 {
10020 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010021 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +010010022 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010023 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +010010024 clear_tv(typ1);
10025 return FAIL;
10026 }
10027 else
10028 {
10029 /* Compare two Dictionaries for being equal or unequal. */
10030 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
10031 ic, FALSE);
10032 if (type == TYPE_NEQUAL)
10033 n1 = !n1;
10034 }
10035 }
10036
10037 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
10038 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
10039 {
10040 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
10041 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010042 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +010010043 clear_tv(typ1);
10044 return FAIL;
10045 }
10046 if ((typ1->v_type == VAR_PARTIAL
10047 && typ1->vval.v_partial == NULL)
10048 || (typ2->v_type == VAR_PARTIAL
10049 && typ2->vval.v_partial == NULL))
10050 /* when a partial is NULL assume not equal */
10051 n1 = FALSE;
10052 else if (type_is)
10053 {
10054 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
10055 /* strings are considered the same if their value is
10056 * the same */
10057 n1 = tv_equal(typ1, typ2, ic, FALSE);
10058 else if (typ1->v_type == VAR_PARTIAL
10059 && typ2->v_type == VAR_PARTIAL)
10060 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
10061 else
10062 n1 = FALSE;
10063 }
10064 else
10065 n1 = tv_equal(typ1, typ2, ic, FALSE);
10066 if (type == TYPE_NEQUAL)
10067 n1 = !n1;
10068 }
10069
10070#ifdef FEAT_FLOAT
10071 /*
10072 * If one of the two variables is a float, compare as a float.
10073 * When using "=~" or "!~", always compare as string.
10074 */
10075 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
10076 && type != TYPE_MATCH && type != TYPE_NOMATCH)
10077 {
10078 float_T f1, f2;
10079
Bram Moolenaar38f08e72019-02-20 22:04:32 +010010080 f1 = tv_get_float(typ1);
10081 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +010010082 n1 = FALSE;
10083 switch (type)
10084 {
10085 case TYPE_EQUAL: n1 = (f1 == f2); break;
10086 case TYPE_NEQUAL: n1 = (f1 != f2); break;
10087 case TYPE_GREATER: n1 = (f1 > f2); break;
10088 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
10089 case TYPE_SMALLER: n1 = (f1 < f2); break;
10090 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
10091 case TYPE_UNKNOWN:
10092 case TYPE_MATCH:
10093 case TYPE_NOMATCH: break; /* avoid gcc warning */
10094 }
10095 }
10096#endif
10097
10098 /*
10099 * If one of the two variables is a number, compare as a number.
10100 * When using "=~" or "!~", always compare as string.
10101 */
10102 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
10103 && type != TYPE_MATCH && type != TYPE_NOMATCH)
10104 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010105 n1 = tv_get_number(typ1);
10106 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +010010107 switch (type)
10108 {
10109 case TYPE_EQUAL: n1 = (n1 == n2); break;
10110 case TYPE_NEQUAL: n1 = (n1 != n2); break;
10111 case TYPE_GREATER: n1 = (n1 > n2); break;
10112 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
10113 case TYPE_SMALLER: n1 = (n1 < n2); break;
10114 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
10115 case TYPE_UNKNOWN:
10116 case TYPE_MATCH:
10117 case TYPE_NOMATCH: break; /* avoid gcc warning */
10118 }
10119 }
10120 else
10121 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010122 s1 = tv_get_string_buf(typ1, buf1);
10123 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar31988702018-02-13 12:57:42 +010010124 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
10125 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
10126 else
10127 i = 0;
10128 n1 = FALSE;
10129 switch (type)
10130 {
10131 case TYPE_EQUAL: n1 = (i == 0); break;
10132 case TYPE_NEQUAL: n1 = (i != 0); break;
10133 case TYPE_GREATER: n1 = (i > 0); break;
10134 case TYPE_GEQUAL: n1 = (i >= 0); break;
10135 case TYPE_SMALLER: n1 = (i < 0); break;
10136 case TYPE_SEQUAL: n1 = (i <= 0); break;
10137
10138 case TYPE_MATCH:
10139 case TYPE_NOMATCH:
10140 n1 = pattern_match(s2, s1, ic);
10141 if (type == TYPE_NOMATCH)
10142 n1 = !n1;
10143 break;
10144
10145 case TYPE_UNKNOWN: break; /* avoid gcc warning */
10146 }
10147 }
10148 clear_tv(typ1);
10149 typ1->v_type = VAR_NUMBER;
10150 typ1->vval.v_number = n1;
10151
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010152 return OK;
10153}
10154
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010155 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010156typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010157{
10158 char_u *tofree;
10159 char_u numbuf[NUMBUFLEN];
10160 char_u *ret = NULL;
10161
10162 if (arg == NULL)
10163 return vim_strsave((char_u *)"(does not exist)");
10164 ret = tv2string(arg, &tofree, numbuf, 0);
10165 /* Make a copy if we have a value but it's not in allocated memory. */
10166 if (ret != NULL && tofree == NULL)
10167 ret = vim_strsave(ret);
10168 return ret;
10169}
10170
10171 int
10172var_exists(char_u *var)
10173{
10174 char_u *name;
10175 char_u *tofree;
10176 typval_T tv;
10177 int len = 0;
10178 int n = FALSE;
10179
10180 /* get_name_len() takes care of expanding curly braces */
10181 name = var;
10182 len = get_name_len(&var, &tofree, TRUE, FALSE);
10183 if (len > 0)
10184 {
10185 if (tofree != NULL)
10186 name = tofree;
10187 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
10188 if (n)
10189 {
10190 /* handle d.key, l[idx], f(expr) */
10191 n = (handle_subscript(&var, &tv, TRUE, FALSE) == OK);
10192 if (n)
10193 clear_tv(&tv);
10194 }
10195 }
10196 if (*var != NUL)
10197 n = FALSE;
10198
10199 vim_free(tofree);
10200 return n;
10201}
10202
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203#endif /* FEAT_EVAL */
10204
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010206#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207
Bram Moolenaar4f974752019-02-17 17:44:42 +010010208#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209/*
10210 * Functions for ":8" filename modifier: get 8.3 version of a filename.
10211 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212
10213/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010214 * Get the short path (8.3) for the filename in "fnamep".
10215 * Only works for a valid file name.
10216 * When the path gets longer "fnamep" is changed and the allocated buffer
10217 * is put in "bufp".
10218 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
10219 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220 */
10221 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010222get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010224 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225 char_u *newbuf;
10226
10227 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010228 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229 if (l > len - 1)
10230 {
10231 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010232 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233 newbuf = vim_strnsave(*fnamep, l);
10234 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010235 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236
10237 vim_free(*bufp);
10238 *fnamep = *bufp = newbuf;
10239
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010240 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010241 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242 }
10243
10244 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010245 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246}
10247
10248/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010249 * Get the short path (8.3) for the filename in "fname". The converted
10250 * path is returned in "bufp".
10251 *
10252 * Some of the directories specified in "fname" may not exist. This function
10253 * will shorten the existing directories at the beginning of the path and then
10254 * append the remaining non-existing path.
10255 *
10256 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020010257 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010258 * bufp - Pointer to an allocated buffer for the filename.
10259 * fnamelen - Length of the filename pointed to by fname
10260 *
10261 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262 */
10263 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010264shortpath_for_invalid_fname(
10265 char_u **fname,
10266 char_u **bufp,
10267 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010268{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010269 char_u *short_fname, *save_fname, *pbuf_unused;
10270 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010272 int old_len, len;
10273 int new_len, sfx_len;
10274 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275
10276 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010277 old_len = *fnamelen;
10278 save_fname = vim_strnsave(*fname, old_len);
10279 pbuf_unused = NULL;
10280 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010282 endp = save_fname + old_len - 1; /* Find the end of the copy */
10283 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010285 /*
10286 * Try shortening the supplied path till it succeeds by removing one
10287 * directory at a time from the tail of the path.
10288 */
10289 len = 0;
10290 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010292 /* go back one path-separator */
10293 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
10294 --endp;
10295 if (endp <= save_fname)
10296 break; /* processed the complete path */
10297
10298 /*
10299 * Replace the path separator with a NUL and try to shorten the
10300 * resulting path.
10301 */
10302 ch = *endp;
10303 *endp = 0;
10304 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010305 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010306 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
10307 {
10308 retval = FAIL;
10309 goto theend;
10310 }
10311 *endp = ch; /* preserve the string */
10312
10313 if (len > 0)
10314 break; /* successfully shortened the path */
10315
10316 /* failed to shorten the path. Skip the path separator */
10317 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318 }
10319
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010320 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010322 /*
10323 * Succeeded in shortening the path. Now concatenate the shortened
10324 * path with the remaining path at the tail.
10325 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010327 /* Compute the length of the new path. */
10328 sfx_len = (int)(save_endp - endp) + 1;
10329 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010331 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010333 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010334 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010335 /* There is not enough space in the currently allocated string,
10336 * copy it to a buffer big enough. */
10337 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010339 {
10340 retval = FAIL;
10341 goto theend;
10342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343 }
10344 else
10345 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010346 /* Transfer short_fname to the main buffer (it's big enough),
10347 * unless get_short_pathname() did its work in-place. */
10348 *fname = *bufp = save_fname;
10349 if (short_fname != save_fname)
10350 vim_strncpy(save_fname, short_fname, len);
10351 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010353
10354 /* concat the not-shortened part of the path */
10355 vim_strncpy(*fname + len, endp, sfx_len);
10356 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010358
10359theend:
10360 vim_free(pbuf_unused);
10361 vim_free(save_fname);
10362
10363 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364}
10365
10366/*
10367 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010368 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369 */
10370 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010371shortpath_for_partial(
10372 char_u **fnamep,
10373 char_u **bufp,
10374 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375{
10376 int sepcount, len, tflen;
10377 char_u *p;
10378 char_u *pbuf, *tfname;
10379 int hasTilde;
10380
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010381 /* Count up the path separators from the RHS.. so we know which part
10382 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010384 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385 if (vim_ispathsep(*p))
10386 ++sepcount;
10387
10388 /* Need full path first (use expand_env() to remove a "~/") */
10389 hasTilde = (**fnamep == '~');
10390 if (hasTilde)
10391 pbuf = tfname = expand_env_save(*fnamep);
10392 else
10393 pbuf = tfname = FullName_save(*fnamep, FALSE);
10394
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010395 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010397 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
10398 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399
10400 if (len == 0)
10401 {
10402 /* Don't have a valid filename, so shorten the rest of the
10403 * path if we can. This CAN give us invalid 8.3 filenames, but
10404 * there's not a lot of point in guessing what it might be.
10405 */
10406 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010407 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
10408 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409 }
10410
10411 /* Count the paths backward to find the beginning of the desired string. */
10412 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010413 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010414 if (has_mbyte)
10415 p -= mb_head_off(tfname, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416 if (vim_ispathsep(*p))
10417 {
10418 if (sepcount == 0 || (hasTilde && sepcount == 1))
10419 break;
10420 else
10421 sepcount --;
10422 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424 if (hasTilde)
10425 {
10426 --p;
10427 if (p >= tfname)
10428 *p = '~';
10429 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010430 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431 }
10432 else
10433 ++p;
10434
10435 /* Copy in the string - p indexes into tfname - allocated at pbuf */
10436 vim_free(*bufp);
10437 *fnamelen = (int)STRLEN(p);
10438 *bufp = pbuf;
10439 *fnamep = p;
10440
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010441 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442}
Bram Moolenaar4f974752019-02-17 17:44:42 +010010443#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444
10445/*
10446 * Adjust a filename, according to a string of modifiers.
10447 * *fnamep must be NUL terminated when called. When returning, the length is
10448 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010449 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450 * When there is an error, *fnamep is set to NULL.
10451 */
10452 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010453modify_fname(
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010454 char_u *src, // string with modifiers
10455 int tilde_file, // "~" is a file name, not $HOME
10456 int *usedlen, // characters after src that are used
10457 char_u **fnamep, // file name so far
10458 char_u **bufp, // buffer for allocated file name or NULL
10459 int *fnamelen) // length of fnamep
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460{
10461 int valid = 0;
10462 char_u *tail;
10463 char_u *s, *p, *pbuf;
10464 char_u dirname[MAXPATHL];
10465 int c;
10466 int has_fullname = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010467#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010468 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469 int has_shortname = 0;
10470#endif
10471
10472repeat:
10473 /* ":p" - full path/file_name */
10474 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
10475 {
10476 has_fullname = 1;
10477
10478 valid |= VALID_PATH;
10479 *usedlen += 2;
10480
10481 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
10482 if ((*fnamep)[0] == '~'
10483#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
10484 && ((*fnamep)[1] == '/'
10485# ifdef BACKSLASH_IN_FILENAME
10486 || (*fnamep)[1] == '\\'
10487# endif
10488 || (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010490 && !(tilde_file && (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010491 )
10492 {
10493 *fnamep = expand_env_save(*fnamep);
10494 vim_free(*bufp); /* free any allocated file name */
10495 *bufp = *fnamep;
10496 if (*fnamep == NULL)
10497 return -1;
10498 }
10499
10500 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010501 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502 {
10503 if (vim_ispathsep(*p)
10504 && p[1] == '.'
10505 && (p[2] == NUL
10506 || vim_ispathsep(p[2])
10507 || (p[2] == '.'
10508 && (p[3] == NUL || vim_ispathsep(p[3])))))
10509 break;
10510 }
10511
10512 /* FullName_save() is slow, don't use it when not needed. */
10513 if (*p != NUL || !vim_isAbsName(*fnamep))
10514 {
10515 *fnamep = FullName_save(*fnamep, *p != NUL);
10516 vim_free(*bufp); /* free any allocated file name */
10517 *bufp = *fnamep;
10518 if (*fnamep == NULL)
10519 return -1;
10520 }
10521
Bram Moolenaar4f974752019-02-17 17:44:42 +010010522#ifdef MSWIN
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010523# if _WIN32_WINNT >= 0x0500
10524 if (vim_strchr(*fnamep, '~') != NULL)
10525 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010526 // Expand 8.3 filename to full path. Needed to make sure the same
10527 // file does not have two different names.
10528 // Note: problem does not occur if _WIN32_WINNT < 0x0500.
10529 WCHAR *wfname = enc_to_utf16(*fnamep, NULL);
10530 WCHAR buf[_MAX_PATH];
10531
10532 if (wfname != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010533 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010534 if (GetLongPathNameW(wfname, buf, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010535 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010536 char_u *p = utf16_to_enc(buf, NULL);
10537
10538 if (p != NULL)
10539 {
10540 vim_free(*bufp); // free any allocated file name
10541 *bufp = *fnamep = p;
10542 }
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010543 }
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010544 vim_free(wfname);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010545 }
10546 }
10547# endif
10548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549 /* Append a path separator to a directory. */
10550 if (mch_isdir(*fnamep))
10551 {
10552 /* Make room for one or two extra characters. */
10553 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
10554 vim_free(*bufp); /* free any allocated file name */
10555 *bufp = *fnamep;
10556 if (*fnamep == NULL)
10557 return -1;
10558 add_pathsep(*fnamep);
10559 }
10560 }
10561
10562 /* ":." - path relative to the current directory */
10563 /* ":~" - path relative to the home directory */
10564 /* ":8" - shortname path - postponed till after */
10565 while (src[*usedlen] == ':'
10566 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
10567 {
10568 *usedlen += 2;
10569 if (c == '8')
10570 {
Bram Moolenaar4f974752019-02-17 17:44:42 +010010571#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572 has_shortname = 1; /* Postpone this. */
10573#endif
10574 continue;
10575 }
10576 pbuf = NULL;
10577 /* Need full path first (use expand_env() to remove a "~/") */
10578 if (!has_fullname)
10579 {
10580 if (c == '.' && **fnamep == '~')
10581 p = pbuf = expand_env_save(*fnamep);
10582 else
10583 p = pbuf = FullName_save(*fnamep, FALSE);
10584 }
10585 else
10586 p = *fnamep;
10587
10588 has_fullname = 0;
10589
10590 if (p != NULL)
10591 {
10592 if (c == '.')
10593 {
10594 mch_dirname(dirname, MAXPATHL);
10595 s = shorten_fname(p, dirname);
10596 if (s != NULL)
10597 {
10598 *fnamep = s;
10599 if (pbuf != NULL)
10600 {
10601 vim_free(*bufp); /* free any allocated file name */
10602 *bufp = pbuf;
10603 pbuf = NULL;
10604 }
10605 }
10606 }
10607 else
10608 {
10609 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
10610 /* Only replace it when it starts with '~' */
10611 if (*dirname == '~')
10612 {
10613 s = vim_strsave(dirname);
10614 if (s != NULL)
10615 {
10616 *fnamep = s;
10617 vim_free(*bufp);
10618 *bufp = s;
10619 }
10620 }
10621 }
10622 vim_free(pbuf);
10623 }
10624 }
10625
10626 tail = gettail(*fnamep);
10627 *fnamelen = (int)STRLEN(*fnamep);
10628
10629 /* ":h" - head, remove "/file_name", can be repeated */
10630 /* Don't remove the first "/" or "c:\" */
10631 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
10632 {
10633 valid |= VALID_HEAD;
10634 *usedlen += 2;
10635 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010636 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010637 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010638 *fnamelen = (int)(tail - *fnamep);
10639#ifdef VMS
10640 if (*fnamelen > 0)
10641 *fnamelen += 1; /* the path separator is part of the path */
10642#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010643 if (*fnamelen == 0)
10644 {
10645 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
10646 p = vim_strsave((char_u *)".");
10647 if (p == NULL)
10648 return -1;
10649 vim_free(*bufp);
10650 *bufp = *fnamep = tail = p;
10651 *fnamelen = 1;
10652 }
10653 else
10654 {
10655 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010656 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658 }
10659
10660 /* ":8" - shortname */
10661 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
10662 {
10663 *usedlen += 2;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010664#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665 has_shortname = 1;
10666#endif
10667 }
10668
Bram Moolenaar4f974752019-02-17 17:44:42 +010010669#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010670 /*
10671 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672 */
10673 if (has_shortname)
10674 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010675 /* Copy the string if it is shortened by :h and when it wasn't copied
10676 * yet, because we are going to change it in place. Avoids changing
10677 * the buffer name for "%:8". */
10678 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679 {
10680 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020010681 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682 return -1;
10683 vim_free(*bufp);
10684 *bufp = *fnamep = p;
10685 }
10686
10687 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020010688 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689 if (!has_fullname && !vim_isAbsName(*fnamep))
10690 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010691 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010692 return -1;
10693 }
10694 else
10695 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010696 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697
Bram Moolenaardc935552011-08-17 15:23:23 +020010698 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010699 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010700 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701 return -1;
10702
10703 if (l == 0)
10704 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010705 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010707 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708 return -1;
10709 }
10710 *fnamelen = l;
10711 }
10712 }
Bram Moolenaar4f974752019-02-17 17:44:42 +010010713#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714
10715 /* ":t" - tail, just the basename */
10716 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
10717 {
10718 *usedlen += 2;
10719 *fnamelen -= (int)(tail - *fnamep);
10720 *fnamep = tail;
10721 }
10722
10723 /* ":e" - extension, can be repeated */
10724 /* ":r" - root, without extension, can be repeated */
10725 while (src[*usedlen] == ':'
10726 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
10727 {
10728 /* find a '.' in the tail:
10729 * - for second :e: before the current fname
10730 * - otherwise: The last '.'
10731 */
10732 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
10733 s = *fnamep - 2;
10734 else
10735 s = *fnamep + *fnamelen - 1;
10736 for ( ; s > tail; --s)
10737 if (s[0] == '.')
10738 break;
10739 if (src[*usedlen + 1] == 'e') /* :e */
10740 {
10741 if (s > tail)
10742 {
10743 *fnamelen += (int)(*fnamep - (s + 1));
10744 *fnamep = s + 1;
10745#ifdef VMS
10746 /* cut version from the extension */
10747 s = *fnamep + *fnamelen - 1;
10748 for ( ; s > *fnamep; --s)
10749 if (s[0] == ';')
10750 break;
10751 if (s > *fnamep)
10752 *fnamelen = s - *fnamep;
10753#endif
10754 }
10755 else if (*fnamep <= tail)
10756 *fnamelen = 0;
10757 }
10758 else /* :r */
10759 {
10760 if (s > tail) /* remove one extension */
10761 *fnamelen = (int)(s - *fnamep);
10762 }
10763 *usedlen += 2;
10764 }
10765
10766 /* ":s?pat?foo?" - substitute */
10767 /* ":gs?pat?foo?" - global substitute */
10768 if (src[*usedlen] == ':'
10769 && (src[*usedlen + 1] == 's'
10770 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
10771 {
10772 char_u *str;
10773 char_u *pat;
10774 char_u *sub;
10775 int sep;
10776 char_u *flags;
10777 int didit = FALSE;
10778
10779 flags = (char_u *)"";
10780 s = src + *usedlen + 2;
10781 if (src[*usedlen + 1] == 'g')
10782 {
10783 flags = (char_u *)"g";
10784 ++s;
10785 }
10786
10787 sep = *s++;
10788 if (sep)
10789 {
10790 /* find end of pattern */
10791 p = vim_strchr(s, sep);
10792 if (p != NULL)
10793 {
10794 pat = vim_strnsave(s, (int)(p - s));
10795 if (pat != NULL)
10796 {
10797 s = p + 1;
10798 /* find end of substitution */
10799 p = vim_strchr(s, sep);
10800 if (p != NULL)
10801 {
10802 sub = vim_strnsave(s, (int)(p - s));
10803 str = vim_strnsave(*fnamep, *fnamelen);
10804 if (sub != NULL && str != NULL)
10805 {
10806 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010807 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808 if (s != NULL)
10809 {
10810 *fnamep = s;
10811 *fnamelen = (int)STRLEN(s);
10812 vim_free(*bufp);
10813 *bufp = s;
10814 didit = TRUE;
10815 }
10816 }
10817 vim_free(sub);
10818 vim_free(str);
10819 }
10820 vim_free(pat);
10821 }
10822 }
10823 /* after using ":s", repeat all the modifiers */
10824 if (didit)
10825 goto repeat;
10826 }
10827 }
10828
Bram Moolenaar26df0922014-02-23 23:39:13 +010010829 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10830 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010831 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010832 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010833 if (c != NUL)
10834 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010835 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010836 if (c != NUL)
10837 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010838 if (p == NULL)
10839 return -1;
10840 vim_free(*bufp);
10841 *bufp = *fnamep = p;
10842 *fnamelen = (int)STRLEN(p);
10843 *usedlen += 2;
10844 }
10845
Bram Moolenaar071d4272004-06-13 20:20:40 +000010846 return valid;
10847}
10848
10849/*
10850 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010851 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852 * "flags" can be "g" to do a global substitute.
10853 * Returns an allocated string, NULL for error.
10854 */
10855 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010856do_string_sub(
10857 char_u *str,
10858 char_u *pat,
10859 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010860 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010861 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010862{
10863 int sublen;
10864 regmatch_T regmatch;
10865 int i;
10866 int do_all;
10867 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010868 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010869 garray_T ga;
10870 char_u *ret;
10871 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010872 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873
10874 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
10875 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010876 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877
10878 ga_init2(&ga, 1, 200);
10879
10880 do_all = (flags[0] == 'g');
10881
10882 regmatch.rm_ic = p_ic;
10883 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10884 if (regmatch.regprog != NULL)
10885 {
10886 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010887 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
10889 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010010890 /* Skip empty match except for first match. */
10891 if (regmatch.startp[0] == regmatch.endp[0])
10892 {
10893 if (zero_width == regmatch.startp[0])
10894 {
10895 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020010896 i = MB_PTR2LEN(tail);
10897 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
10898 (size_t)i);
10899 ga.ga_len += i;
10900 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010901 continue;
10902 }
10903 zero_width = regmatch.startp[0];
10904 }
10905
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906 /*
10907 * Get some space for a temporary buffer to do the substitution
10908 * into. It will contain:
10909 * - The text up to where the match is.
10910 * - The substituted text.
10911 * - The text after the match.
10912 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010913 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010010914 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
10916 {
10917 ga_clear(&ga);
10918 break;
10919 }
10920
10921 /* copy the text up to where the match is */
10922 i = (int)(regmatch.startp[0] - tail);
10923 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10924 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010925 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926 + ga.ga_len + i, TRUE, TRUE, FALSE);
10927 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010928 tail = regmatch.endp[0];
10929 if (*tail == NUL)
10930 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931 if (!do_all)
10932 break;
10933 }
10934
10935 if (ga.ga_data != NULL)
10936 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10937
Bram Moolenaar473de612013-06-08 18:19:48 +020010938 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939 }
10940
10941 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10942 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010943 if (p_cpo == empty_option)
10944 p_cpo = save_cpo;
10945 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010946 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010947 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948
10949 return ret;
10950}
10951
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010952 static int
10953filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10954{
10955 typval_T rettv;
10956 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010957 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010958
10959 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10960 argv[0] = vimvars[VV_KEY].vv_tv;
10961 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010962 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10963 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010964 if (map)
10965 {
10966 /* map(): replace the list item value */
10967 clear_tv(tv);
10968 rettv.v_lock = 0;
10969 *tv = rettv;
10970 }
10971 else
10972 {
10973 int error = FALSE;
10974
10975 /* filter(): when expr is zero remove the item */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010976 *remp = (tv_get_number_chk(&rettv, &error) == 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010977 clear_tv(&rettv);
10978 /* On type error, nothing has been removed; return FAIL to stop the
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010979 * loop. The error message was given by tv_get_number_chk(). */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010980 if (error)
10981 goto theend;
10982 }
10983 retval = OK;
10984theend:
10985 clear_tv(&vimvars[VV_VAL].vv_tv);
10986 return retval;
10987}
10988
10989
10990/*
10991 * Implementation of map() and filter().
10992 */
10993 void
10994filter_map(typval_T *argvars, typval_T *rettv, int map)
10995{
10996 typval_T *expr;
10997 listitem_T *li, *nli;
10998 list_T *l = NULL;
10999 dictitem_T *di;
11000 hashtab_T *ht;
11001 hashitem_T *hi;
11002 dict_T *d = NULL;
11003 typval_T save_val;
11004 typval_T save_key;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010011005 blob_T *b = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011006 int rem;
11007 int todo;
11008 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
11009 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
11010 : N_("filter() argument"));
11011 int save_did_emsg;
11012 int idx = 0;
11013
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010011014 if (argvars[0].v_type == VAR_BLOB)
11015 {
11016 if ((b = argvars[0].vval.v_blob) == NULL)
11017 return;
11018 }
11019 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011020 {
11021 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011022 || (!map && var_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011023 return;
11024 }
11025 else if (argvars[0].v_type == VAR_DICT)
11026 {
11027 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011028 || (!map && var_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011029 return;
11030 }
11031 else
11032 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011033 semsg(_(e_listdictarg), ermsg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011034 return;
11035 }
11036
11037 expr = &argvars[1];
11038 /* On type errors, the preceding call has already displayed an error
11039 * message. Avoid a misleading error message for an empty string that
11040 * was not passed as argument. */
11041 if (expr->v_type != VAR_UNKNOWN)
11042 {
11043 prepare_vimvar(VV_VAL, &save_val);
11044
11045 /* We reset "did_emsg" to be able to detect whether an error
11046 * occurred during evaluation of the expression. */
11047 save_did_emsg = did_emsg;
11048 did_emsg = FALSE;
11049
11050 prepare_vimvar(VV_KEY, &save_key);
11051 if (argvars[0].v_type == VAR_DICT)
11052 {
11053 vimvars[VV_KEY].vv_type = VAR_STRING;
11054
11055 ht = &d->dv_hashtab;
11056 hash_lock(ht);
11057 todo = (int)ht->ht_used;
11058 for (hi = ht->ht_array; todo > 0; ++hi)
11059 {
11060 if (!HASHITEM_EMPTY(hi))
11061 {
11062 int r;
11063
11064 --todo;
11065 di = HI2DI(hi);
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011066 if (map && (var_check_lock(di->di_tv.v_lock,
11067 arg_errmsg, TRUE)
11068 || var_check_ro(di->di_flags,
11069 arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011070 break;
11071 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
11072 r = filter_map_one(&di->di_tv, expr, map, &rem);
11073 clear_tv(&vimvars[VV_KEY].vv_tv);
11074 if (r == FAIL || did_emsg)
11075 break;
11076 if (!map && rem)
11077 {
11078 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11079 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
11080 break;
11081 dictitem_remove(d, di);
11082 }
11083 }
11084 }
11085 hash_unlock(ht);
11086 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010011087 else if (argvars[0].v_type == VAR_BLOB)
11088 {
11089 int i;
11090 typval_T tv;
11091
11092 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11093 for (i = 0; i < b->bv_ga.ga_len; i++)
11094 {
11095 tv.v_type = VAR_NUMBER;
11096 tv.vval.v_number = blob_get(b, i);
11097 vimvars[VV_KEY].vv_nr = idx;
11098 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg)
11099 break;
11100 if (tv.v_type != VAR_NUMBER)
11101 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011102 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010011103 return;
11104 }
11105 tv.v_type = VAR_NUMBER;
11106 blob_set(b, i, tv.vval.v_number);
11107 if (!map && rem)
11108 {
11109 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
11110
11111 mch_memmove(p + idx, p + i + 1,
11112 (size_t)b->bv_ga.ga_len - i - 1);
11113 --b->bv_ga.ga_len;
11114 --i;
11115 }
11116 }
11117 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011118 else
11119 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +010011120 // argvars[0].v_type == VAR_LIST
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011121 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11122
11123 for (li = l->lv_first; li != NULL; li = nli)
11124 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011125 if (map && var_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011126 break;
11127 nli = li->li_next;
11128 vimvars[VV_KEY].vv_nr = idx;
11129 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
11130 || did_emsg)
11131 break;
11132 if (!map && rem)
11133 listitem_remove(l, li);
11134 ++idx;
11135 }
11136 }
11137
11138 restore_vimvar(VV_KEY, &save_key);
11139 restore_vimvar(VV_VAL, &save_val);
11140
11141 did_emsg |= save_did_emsg;
11142 }
11143
11144 copy_tv(&argvars[0], rettv);
11145}
11146
Bram Moolenaar071d4272004-06-13 20:20:40 +000011147#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */