blob: 7fbd7702ddd781c0bdbd34e67378cb5935377897 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar33570922005-01-25 22:26:29 +000023#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026static char *e_undefvar = N_("E121: Undefined variable: %s");
27static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000028static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
29static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar92124a32005-06-17 22:03:40 +000030static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020031#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020032static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020033#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000034
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010035#define NAMESPACE_CHAR (char_u *)"abglstvw"
36
Bram Moolenaar230bb3f2013-04-24 14:07:45 +020037static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +000038#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +000039
40/*
Bram Moolenaar532c7802005-01-27 14:44:31 +000041 * Old Vim variables such as "v:version" are also available without the "v:".
42 * Also in functions. We need a special hashtable for them.
43 */
Bram Moolenaar4debb442005-06-01 21:57:40 +000044static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +000045
46/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000047 * When recursively copying lists and dicts we need to remember which ones we
48 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000049 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000050 */
51static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020052
Bram Moolenaard9fba312005-06-26 22:34:35 +000053/*
Bram Moolenaar33570922005-01-25 22:26:29 +000054 * Array to hold the hashtab with variables local to each sourced script.
55 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 */
Bram Moolenaar33570922005-01-25 22:26:29 +000057typedef struct
58{
59 dictitem_T sv_var;
60 dict_T sv_dict;
61} scriptvar_T;
62
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020063static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
64#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
65#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +000066
67static int echo_attr = 0; /* attributes used for ":echo" */
68
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000069/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000070static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
71
Bram Moolenaar071d4272004-06-13 20:20:40 +000072/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000073 * Info used by a ":for" loop.
74 */
Bram Moolenaar33570922005-01-25 22:26:29 +000075typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000076{
77 int fi_semicolon; /* TRUE if ending in '; var]' */
78 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +000079 listwatch_T fi_lw; /* keep an eye on the item used. */
80 list_T *fi_list; /* list being used */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010081 int fi_bi; /* index of blob */
82 blob_T *fi_blob; /* blob being used */
Bram Moolenaar33570922005-01-25 22:26:29 +000083} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000084
Bram Moolenaara7043832005-01-21 11:56:39 +000085
86/*
Bram Moolenaar33570922005-01-25 22:26:29 +000087 * Array to hold the value of v: variables.
88 * The value is in a dictitem, so that it can also be used in the v: scope.
89 * The reason to use this table anyway is for very quick access to the
90 * variables with the VV_ defines.
91 */
Bram Moolenaar33570922005-01-25 22:26:29 +000092
93/* values for vv_flags: */
94#define VV_COMPAT 1 /* compatible, also used without "v:" */
95#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000096#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +000097
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +010098#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +000099
100static struct vimvar
101{
102 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100103 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000104 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
105} vimvars[VV_LEN] =
106{
107 /*
108 * The order here must match the VV_ defines in vim.h!
109 * Initializing a union does not work, leave tv.vval empty to get zero's.
110 */
111 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
112 {VV_NAME("count1", VAR_NUMBER), VV_RO},
113 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
114 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
115 {VV_NAME("warningmsg", VAR_STRING), 0},
116 {VV_NAME("statusmsg", VAR_STRING), 0},
117 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
118 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
119 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
120 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
121 {VV_NAME("termresponse", VAR_STRING), VV_RO},
122 {VV_NAME("fname", VAR_STRING), VV_RO},
123 {VV_NAME("lang", VAR_STRING), VV_RO},
124 {VV_NAME("lc_time", VAR_STRING), VV_RO},
125 {VV_NAME("ctype", VAR_STRING), VV_RO},
126 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
127 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
128 {VV_NAME("fname_in", VAR_STRING), VV_RO},
129 {VV_NAME("fname_out", VAR_STRING), VV_RO},
130 {VV_NAME("fname_new", VAR_STRING), VV_RO},
131 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
132 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
133 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
134 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
135 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
136 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
137 {VV_NAME("progname", VAR_STRING), VV_RO},
138 {VV_NAME("servername", VAR_STRING), VV_RO},
139 {VV_NAME("dying", VAR_NUMBER), VV_RO},
140 {VV_NAME("exception", VAR_STRING), VV_RO},
141 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
142 {VV_NAME("register", VAR_STRING), VV_RO},
143 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
144 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000145 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
146 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000147 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000148 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
149 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000150 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
151 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
Bram Moolenaarc9721bd2016-06-04 17:41:03 +0200152 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000153 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
154 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
155 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000156 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000157 {VV_NAME("swapname", VAR_STRING), VV_RO},
158 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000159 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200160 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000161 {VV_NAME("mouse_win", VAR_NUMBER), 0},
Bram Moolenaar511972d2016-06-04 18:09:59 +0200162 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000163 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
164 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000165 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000166 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100167 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000168 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200169 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200170 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200171 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200172 {VV_NAME("option_new", VAR_STRING), VV_RO},
173 {VV_NAME("option_old", VAR_STRING), VV_RO},
174 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100175 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100176 {VV_NAME("false", VAR_SPECIAL), VV_RO},
177 {VV_NAME("true", VAR_SPECIAL), VV_RO},
178 {VV_NAME("null", VAR_SPECIAL), VV_RO},
179 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100180 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200181 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaarf562e722016-07-19 17:25:25 +0200182 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
183 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
184 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
185 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
186 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
187 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
188 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
189 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
190 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
191 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100192 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200193 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
194 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
Bram Moolenaarf3af54e2017-08-30 14:53:06 +0200195 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
196 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
197 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100198 {VV_NAME("event", VAR_DICT), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000199};
200
201/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000202#define vv_type vv_di.di_tv.v_type
203#define vv_nr vv_di.di_tv.vval.v_number
204#define vv_float vv_di.di_tv.vval.v_float
205#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000206#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200207#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100208#define vv_blob vv_di.di_tv.vval.v_blob
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000209#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000210
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200211static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000212#define vimvarht vimvardict.dv_hashtab
213
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100214static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
215static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
216static char_u *skip_var_one(char_u *arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100217static void list_glob_vars(int *first);
218static void list_buf_vars(int *first);
219static void list_win_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100220static void list_tab_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100221static void list_vim_vars(int *first);
222static void list_script_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100223static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
224static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100225static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
226static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100227static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
228static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
229static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
230static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000231
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100232static int eval2(char_u **arg, typval_T *rettv, int evaluate);
233static int eval3(char_u **arg, typval_T *rettv, int evaluate);
234static int eval4(char_u **arg, typval_T *rettv, int evaluate);
235static int eval5(char_u **arg, typval_T *rettv, int evaluate);
236static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
237static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000238
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100239static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
240static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100241static int free_unref_items(int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100242static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100243static int get_env_len(char_u **arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100244static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200245static void check_vars(char_u *name, int len);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100246static typval_T *alloc_string_tv(char_u *string);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100247static void delete_var(hashtab_T *ht, hashitem_T *hi);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100248static void list_one_var(dictitem_T *v, char *prefix, int *first);
249static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
Bram Moolenaar05c00c02019-02-11 22:00:11 +0100250static int tv_check_lock(typval_T *tv, char_u *name, int use_gettext);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100251static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200252
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200253/* for VIM_VERSION_ defines */
254#include "version.h"
255
Bram Moolenaare21c1582019-03-02 11:57:09 +0100256/*
257 * Return "n1" divided by "n2", taking care of dividing by zero.
258 */
259 static varnumber_T
260num_divide(varnumber_T n1, varnumber_T n2)
261{
262 varnumber_T result;
263
264 if (n2 == 0) // give an error message?
265 {
266 if (n1 == 0)
267 result = VARNUM_MIN; // similar to NaN
268 else if (n1 < 0)
269 result = -VARNUM_MAX;
270 else
271 result = VARNUM_MAX;
272 }
273 else
274 result = n1 / n2;
275
276 return result;
277}
278
279/*
280 * Return "n1" modulus "n2", taking care of dividing by zero.
281 */
282 static varnumber_T
283num_modulus(varnumber_T n1, varnumber_T n2)
284{
285 // Give an error when n2 is 0?
286 return (n2 == 0) ? 0 : (n1 % n2);
287}
288
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100289
290#if defined(EBCDIC) || defined(PROTO)
291/*
292 * Compare struct fst by function name.
293 */
294 static int
295compare_func_name(const void *s1, const void *s2)
296{
297 struct fst *p1 = (struct fst *)s1;
298 struct fst *p2 = (struct fst *)s2;
299
300 return STRCMP(p1->f_name, p2->f_name);
301}
302
303/*
304 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100305 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100306 * On machines using EBCDIC we have to sort it.
307 */
308 static void
309sortFunctions(void)
310{
311 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
312
313 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
314}
315#endif
316
317
Bram Moolenaar33570922005-01-25 22:26:29 +0000318/*
319 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000320 */
321 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100322eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000323{
Bram Moolenaar33570922005-01-25 22:26:29 +0000324 int i;
325 struct vimvar *p;
326
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200327 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
328 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200329 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000330 hash_init(&compat_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200331 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000332
333 for (i = 0; i < VV_LEN; ++i)
334 {
335 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200336 if (STRLEN(p->vv_name) > 16)
337 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100338 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200339 getout(1);
340 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000341 STRCPY(p->vv_di.di_key, p->vv_name);
342 if (p->vv_flags & VV_RO)
343 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
344 else if (p->vv_flags & VV_RO_SBX)
345 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
346 else
347 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000348
349 /* add to v: scope dict, unless the value is not always available */
350 if (p->vv_type != VAR_UNKNOWN)
351 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000352 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000353 /* add to compat scope dict */
354 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000355 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100356 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
357
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000358 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100359 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100360 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100361 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100362 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100363
364 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
365 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
366 set_vim_var_nr(VV_NONE, VVAL_NONE);
367 set_vim_var_nr(VV_NULL, VVAL_NULL);
368
Bram Moolenaarf562e722016-07-19 17:25:25 +0200369 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
370 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
371 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
372 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
373 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
374 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
375 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
376 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
377 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
378 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100379 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
Bram Moolenaarf562e722016-07-19 17:25:25 +0200380
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200381 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200382
383#ifdef EBCDIC
384 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100385 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200386 */
387 sortFunctions();
388#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000389}
390
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000391#if defined(EXITFREE) || defined(PROTO)
392 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100393eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000394{
395 int i;
396 struct vimvar *p;
397
398 for (i = 0; i < VV_LEN; ++i)
399 {
400 p = &vimvars[i];
401 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100402 VIM_CLEAR(p->vv_str);
Bram Moolenaard812df62008-11-09 12:46:09 +0000403 else if (p->vv_di.di_tv.v_type == VAR_LIST)
404 {
405 list_unref(p->vv_list);
406 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000407 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000408 }
409 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000410 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000411 hash_clear(&compat_hashtab);
412
Bram Moolenaard9fba312005-06-26 22:34:35 +0000413 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100414# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200415 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100416# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000417
418 /* global variables */
419 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000420
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000421 /* autoloaded script names */
422 ga_clear_strings(&ga_loaded);
423
Bram Moolenaarcca74132013-09-25 21:00:28 +0200424 /* Script-local variables. First clear all the variables and in a second
425 * loop free the scriptvar_T, because a variable in one script might hold
426 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200427 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200428 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200429 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200430 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200431 ga_clear(&ga_scripts);
432
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000433 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200434 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000435
436 /* functions */
437 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000438}
439#endif
440
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441
442/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 * Set an internal variable to a string value. Creates the variable if it does
444 * not already exist.
445 */
446 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100447set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000449 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000450 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451
452 val = vim_strsave(value);
453 if (val != NULL)
454 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000455 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000456 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000458 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000459 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 }
461 }
462}
463
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000464static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200465#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000466static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000467static char_u *redir_endp = NULL;
468static char_u *redir_varname = NULL;
469
470/*
471 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100472 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000473 * Returns OK if successfully completed the setup. FAIL otherwise.
474 */
475 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100476var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000477{
478 int save_emsg;
479 int err;
480 typval_T tv;
481
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000482 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000483 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000484 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100485 emsg(_(e_invarg));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000486 return FAIL;
487 }
488
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000489 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000490 redir_varname = vim_strsave(name);
491 if (redir_varname == NULL)
492 return FAIL;
493
Bram Moolenaar18a4ba22019-05-24 19:39:03 +0200494 redir_lval = (lval_T *)alloc_clear(sizeof(lval_T));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000495 if (redir_lval == NULL)
496 {
497 var_redir_stop();
498 return FAIL;
499 }
500
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000501 /* The output is stored in growarray "redir_ga" until redirection ends. */
502 ga_init2(&redir_ga, (int)sizeof(char), 500);
503
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000504 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100505 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000506 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000507 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
508 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200509 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000510 if (redir_endp != NULL && *redir_endp != NUL)
511 /* Trailing characters are present after the variable name */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100512 emsg(_(e_trailing));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000513 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100514 emsg(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000515 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000516 var_redir_stop();
517 return FAIL;
518 }
519
520 /* check if we can write to the variable: set it to or append an empty
521 * string */
522 save_emsg = did_emsg;
523 did_emsg = FALSE;
524 tv.v_type = VAR_STRING;
525 tv.vval.v_string = (char_u *)"";
526 if (append)
527 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
528 else
529 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200530 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000531 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000532 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000533 if (err)
534 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000535 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000536 var_redir_stop();
537 return FAIL;
538 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000539
540 return OK;
541}
542
543/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000544 * Append "value[value_len]" to the variable set by var_redir_start().
545 * The actual appending is postponed until redirection ends, because the value
546 * appended may in fact be the string we write to, changing it may cause freed
547 * memory to be used:
548 * :redir => foo
549 * :let foo
550 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000551 */
552 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100553var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000554{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000555 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000556
557 if (redir_lval == NULL)
558 return;
559
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000560 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000561 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000562 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000563 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000564
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000565 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000566 {
567 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000568 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000569 }
570 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000571 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000572}
573
574/*
575 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000576 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000577 */
578 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100579var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000580{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000581 typval_T tv;
582
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200583 if (EVALCMD_BUSY)
584 {
585 redir_lval = NULL;
586 return;
587 }
588
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000589 if (redir_lval != NULL)
590 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000591 /* If there was no error: assign the text to the variable. */
592 if (redir_endp != NULL)
593 {
594 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
595 tv.v_type = VAR_STRING;
596 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200597 /* Call get_lval() again, if it's inside a Dict or List it may
598 * have changed. */
599 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100600 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200601 if (redir_endp != NULL && redir_lval->ll_name != NULL)
602 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
603 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000604 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000605
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000606 /* free the collected output */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100607 VIM_CLEAR(redir_ga.ga_data);
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000608
Bram Moolenaard23a8232018-02-10 18:45:26 +0100609 VIM_CLEAR(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000610 }
Bram Moolenaard23a8232018-02-10 18:45:26 +0100611 VIM_CLEAR(redir_varname);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000612}
613
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100615eval_charconvert(
616 char_u *enc_from,
617 char_u *enc_to,
618 char_u *fname_from,
619 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620{
621 int err = FALSE;
622
623 set_vim_var_string(VV_CC_FROM, enc_from, -1);
624 set_vim_var_string(VV_CC_TO, enc_to, -1);
625 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
626 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
627 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
628 err = TRUE;
629 set_vim_var_string(VV_CC_FROM, NULL, -1);
630 set_vim_var_string(VV_CC_TO, NULL, -1);
631 set_vim_var_string(VV_FNAME_IN, NULL, -1);
632 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
633
634 if (err)
635 return FAIL;
636 return OK;
637}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638
639# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
640 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100641eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642{
643 int err = FALSE;
644
645 set_vim_var_string(VV_FNAME_IN, fname, -1);
646 set_vim_var_string(VV_CMDARG, args, -1);
647 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
648 err = TRUE;
649 set_vim_var_string(VV_FNAME_IN, NULL, -1);
650 set_vim_var_string(VV_CMDARG, NULL, -1);
651
652 if (err)
653 {
654 mch_remove(fname);
655 return FAIL;
656 }
657 return OK;
658}
659# endif
660
661# if defined(FEAT_DIFF) || defined(PROTO)
662 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100663eval_diff(
664 char_u *origfile,
665 char_u *newfile,
666 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667{
668 int err = FALSE;
669
670 set_vim_var_string(VV_FNAME_IN, origfile, -1);
671 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
672 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
673 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
674 set_vim_var_string(VV_FNAME_IN, NULL, -1);
675 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
676 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
677}
678
679 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100680eval_patch(
681 char_u *origfile,
682 char_u *difffile,
683 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684{
685 int err;
686
687 set_vim_var_string(VV_FNAME_IN, origfile, -1);
688 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
689 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
690 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
691 set_vim_var_string(VV_FNAME_IN, NULL, -1);
692 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
693 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
694}
695# endif
696
697/*
698 * Top level evaluation function, returning a boolean.
699 * Sets "error" to TRUE if there was an error.
700 * Return TRUE or FALSE.
701 */
702 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100703eval_to_bool(
704 char_u *arg,
705 int *error,
706 char_u **nextcmd,
707 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708{
Bram Moolenaar33570922005-01-25 22:26:29 +0000709 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200710 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711
712 if (skip)
713 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000714 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 else
717 {
718 *error = FALSE;
719 if (!skip)
720 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100721 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000722 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 }
724 }
725 if (skip)
726 --emsg_skip;
727
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200728 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729}
730
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100731/*
732 * Call eval1() and give an error message if not done at a lower level.
733 */
734 static int
735eval1_emsg(char_u **arg, typval_T *rettv, int evaluate)
736{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100737 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100738 int ret;
739 int did_emsg_before = did_emsg;
740 int called_emsg_before = called_emsg;
741
742 ret = eval1(arg, rettv, evaluate);
743 if (ret == FAIL)
744 {
745 // Report the invalid expression unless the expression evaluation has
746 // been cancelled due to an aborting error, an interrupt, or an
747 // exception, or we already gave a more specific error.
748 // Also check called_emsg for when using assert_fails().
749 if (!aborting() && did_emsg == did_emsg_before
750 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100751 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100752 }
753 return ret;
754}
755
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200756 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100757eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
758{
759 char_u *s;
760 int dummy;
761 char_u buf[NUMBUFLEN];
762
763 if (expr->v_type == VAR_FUNC)
764 {
765 s = expr->vval.v_string;
766 if (s == NULL || *s == NUL)
767 return FAIL;
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200768 if (call_func(s, -1, rettv, argc, argv, NULL,
Bram Moolenaar48570482017-10-30 21:48:41 +0100769 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
770 return FAIL;
771 }
772 else if (expr->v_type == VAR_PARTIAL)
773 {
774 partial_T *partial = expr->vval.v_partial;
775
776 s = partial_name(partial);
777 if (s == NULL || *s == NUL)
778 return FAIL;
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200779 if (call_func(s, -1, rettv, argc, argv, NULL,
Bram Moolenaar48570482017-10-30 21:48:41 +0100780 0L, 0L, &dummy, TRUE, partial, NULL) == FAIL)
781 return FAIL;
782 }
783 else
784 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100785 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100786 if (s == NULL)
787 return FAIL;
788 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100789 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100790 return FAIL;
791 if (*s != NUL) /* check for trailing chars after expr */
792 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200793 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100794 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100795 return FAIL;
796 }
797 }
798 return OK;
799}
800
801/*
802 * Like eval_to_bool() but using a typval_T instead of a string.
803 * Works for string, funcref and partial.
804 */
805 int
806eval_expr_to_bool(typval_T *expr, int *error)
807{
808 typval_T rettv;
809 int res;
810
811 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
812 {
813 *error = TRUE;
814 return FALSE;
815 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100816 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100817 clear_tv(&rettv);
818 return res;
819}
820
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821/*
822 * Top level evaluation function, returning a string. If "skip" is TRUE,
823 * only parsing to "nextcmd" is done, without reporting errors. Return
824 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
825 */
826 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100827eval_to_string_skip(
828 char_u *arg,
829 char_u **nextcmd,
830 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831{
Bram Moolenaar33570922005-01-25 22:26:29 +0000832 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 char_u *retval;
834
835 if (skip)
836 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000837 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 retval = NULL;
839 else
840 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100841 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000842 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 }
844 if (skip)
845 --emsg_skip;
846
847 return retval;
848}
849
850/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000851 * Skip over an expression at "*pp".
852 * Return FAIL for an error, OK otherwise.
853 */
854 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100855skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000856{
Bram Moolenaar33570922005-01-25 22:26:29 +0000857 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000858
859 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000860 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000861}
862
863/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000865 * When "convert" is TRUE convert a List into a sequence of lines and convert
866 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 * Return pointer to allocated memory, or NULL for failure.
868 */
869 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100870eval_to_string(
871 char_u *arg,
872 char_u **nextcmd,
873 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874{
Bram Moolenaar33570922005-01-25 22:26:29 +0000875 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000877 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000878#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000879 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000882 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 retval = NULL;
884 else
885 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000886 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000887 {
888 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000889 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200890 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200891 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200892 if (tv.vval.v_list->lv_len > 0)
893 ga_append(&ga, NL);
894 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000895 ga_append(&ga, NUL);
896 retval = (char_u *)ga.ga_data;
897 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000898#ifdef FEAT_FLOAT
899 else if (convert && tv.v_type == VAR_FLOAT)
900 {
901 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
902 retval = vim_strsave(numbuf);
903 }
904#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000905 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100906 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000907 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 }
909
910 return retval;
911}
912
913/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000914 * Call eval_to_string() without using current local variables and using
915 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 */
917 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100918eval_to_string_safe(
919 char_u *arg,
920 char_u **nextcmd,
921 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922{
923 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200924 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200926 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000927 if (use_sandbox)
928 ++sandbox;
929 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000930 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000931 if (use_sandbox)
932 --sandbox;
933 --textlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200934 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 return retval;
936}
937
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938/*
939 * Top level evaluation function, returning a number.
940 * Evaluates "expr" silently.
941 * Returns -1 for an error.
942 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200943 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100944eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945{
Bram Moolenaar33570922005-01-25 22:26:29 +0000946 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200947 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000948 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949
950 ++emsg_off;
951
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000952 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 retval = -1;
954 else
955 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100956 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000957 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 }
959 --emsg_off;
960
961 return retval;
962}
963
Bram Moolenaara40058a2005-07-11 22:42:07 +0000964/*
965 * Prepare v: variable "idx" to be used.
966 * Save the current typeval in "save_tv".
967 * When not used yet add the variable to the v: hashtable.
968 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200969 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100970prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000971{
972 *save_tv = vimvars[idx].vv_tv;
973 if (vimvars[idx].vv_type == VAR_UNKNOWN)
974 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
975}
976
977/*
978 * Restore v: variable "idx" to typeval "save_tv".
979 * When no longer defined, remove the variable from the v: hashtable.
980 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200981 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100982restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000983{
984 hashitem_T *hi;
985
Bram Moolenaara40058a2005-07-11 22:42:07 +0000986 vimvars[idx].vv_tv = *save_tv;
987 if (vimvars[idx].vv_type == VAR_UNKNOWN)
988 {
989 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
990 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100991 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +0000992 else
993 hash_remove(&vimvarht, hi);
994 }
995}
996
Bram Moolenaar3c56a962006-03-12 22:19:04 +0000997#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000998/*
999 * Evaluate an expression to a list with suggestions.
1000 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001001 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001002 */
1003 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001004eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001005{
1006 typval_T save_val;
1007 typval_T rettv;
1008 list_T *list = NULL;
1009 char_u *p = skipwhite(expr);
1010
1011 /* Set "v:val" to the bad word. */
1012 prepare_vimvar(VV_VAL, &save_val);
1013 vimvars[VV_VAL].vv_type = VAR_STRING;
1014 vimvars[VV_VAL].vv_str = badword;
1015 if (p_verbose == 0)
1016 ++emsg_off;
1017
1018 if (eval1(&p, &rettv, TRUE) == OK)
1019 {
1020 if (rettv.v_type != VAR_LIST)
1021 clear_tv(&rettv);
1022 else
1023 list = rettv.vval.v_list;
1024 }
1025
1026 if (p_verbose == 0)
1027 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001028 restore_vimvar(VV_VAL, &save_val);
1029
1030 return list;
1031}
1032
1033/*
1034 * "list" is supposed to contain two items: a word and a number. Return the
1035 * word in "pp" and the number as the return value.
1036 * Return -1 if anything isn't right.
1037 * Used to get the good word and score from the eval_spell_expr() result.
1038 */
1039 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001040get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001041{
1042 listitem_T *li;
1043
1044 li = list->lv_first;
1045 if (li == NULL)
1046 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001047 *pp = tv_get_string(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001048
1049 li = li->li_next;
1050 if (li == NULL)
1051 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001052 return (int)tv_get_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001053}
1054#endif
1055
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001056/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001057 * Top level evaluation function.
1058 * Returns an allocated typval_T with the result.
1059 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001060 */
1061 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001062eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001063{
1064 typval_T *tv;
1065
1066 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001067 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001068 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001069
1070 return tv;
1071}
1072
1073
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001075 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +02001076 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
1077 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001078 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001080 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001081call_vim_function(
1082 char_u *func,
1083 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +02001084 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001085 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 int doesrange;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001088 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001090 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001091 ret = call_func(func, -1, rettv, argc, argv, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001093 &doesrange, TRUE, NULL, NULL);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001094 if (ret == FAIL)
1095 clear_tv(rettv);
1096
1097 return ret;
1098}
1099
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001100/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001101 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001102 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001103 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1104 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001105 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001106 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001107call_func_retnr(
1108 char_u *func,
1109 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001110 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001111{
1112 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001113 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001114
Bram Moolenaarded27a12018-08-01 19:06:03 +02001115 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001116 return -1;
1117
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001118 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001119 clear_tv(&rettv);
1120 return retval;
1121}
1122
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001123#if defined(FEAT_CMDL_COMPL) \
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001124 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1125
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001126# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001127/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001128 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001129 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001130 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1131 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001132 */
1133 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001134call_func_retstr(
1135 char_u *func,
1136 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001137 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001138{
1139 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001140 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001141
Bram Moolenaarded27a12018-08-01 19:06:03 +02001142 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001143 return NULL;
1144
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001145 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001146 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 return retval;
1148}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001149# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001150
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001151/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001152 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001153 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1154 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001155 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001156 */
1157 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001158call_func_retlist(
1159 char_u *func,
1160 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001161 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001162{
1163 typval_T rettv;
1164
Bram Moolenaarded27a12018-08-01 19:06:03 +02001165 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001166 return NULL;
1167
1168 if (rettv.v_type != VAR_LIST)
1169 {
1170 clear_tv(&rettv);
1171 return NULL;
1172 }
1173
1174 return rettv.vval.v_list;
1175}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176#endif
1177
Bram Moolenaar05159a02005-02-26 23:04:13 +00001178
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179#ifdef FEAT_FOLDING
1180/*
1181 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1182 * it in "*cp". Doesn't give error messages.
1183 */
1184 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001185eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186{
Bram Moolenaar33570922005-01-25 22:26:29 +00001187 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001188 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001190 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1191 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192
1193 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001194 if (use_sandbox)
1195 ++sandbox;
1196 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001198 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 retval = 0;
1200 else
1201 {
1202 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001203 if (tv.v_type == VAR_NUMBER)
1204 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001205 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 retval = 0;
1207 else
1208 {
1209 /* If the result is a string, check if there is a non-digit before
1210 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001211 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 if (!VIM_ISDIGIT(*s) && *s != '-')
1213 *cp = *s++;
1214 retval = atol((char *)s);
1215 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001216 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 }
1218 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001219 if (use_sandbox)
1220 --sandbox;
1221 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001223 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224}
1225#endif
1226
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227/*
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001228 * Get a list of lines from a HERE document. The here document is a list of
1229 * lines surrounded by a marker.
1230 * cmd << {marker}
1231 * {line1}
1232 * {line2}
1233 * ....
1234 * {marker}
1235 *
1236 * The {marker} is a string. If the optional 'trim' word is supplied before the
1237 * marker, then the leading indentation before the lines (matching the
1238 * indentation in the 'cmd' line) is stripped.
1239 * Returns a List with {lines} or NULL.
1240 */
1241 static list_T *
1242heredoc_get(exarg_T *eap, char_u *cmd)
1243{
1244 char_u *theline;
1245 char_u *marker;
1246 list_T *l;
1247 char_u *p;
1248 int indent_len = 0;
1249
1250 if (eap->getline == NULL)
1251 {
1252 emsg(_("E991: cannot use =<< here"));
1253 return NULL;
1254 }
1255
1256 // Check for the optional 'trim' word before the marker
1257 cmd = skipwhite(cmd);
1258 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
1259 {
1260 cmd = skipwhite(cmd + 4);
1261
1262 // Trim the indentation from all the lines in the here document
1263 // The amount of indentation trimmed is the same as the indentation of
1264 // the :let command line.
1265 p = *eap->cmdlinep;
1266 while (VIM_ISWHITE(*p))
1267 {
1268 p++;
1269 indent_len++;
1270 }
1271 }
1272
1273 // The marker is the next word. Default marker is "."
1274 if (*cmd != NUL && *cmd != '"')
1275 {
1276 marker = skipwhite(cmd);
1277 p = skiptowhite(marker);
1278 if (*skipwhite(p) != NUL && *skipwhite(p) != '"')
1279 {
1280 emsg(_(e_trailing));
1281 return NULL;
1282 }
1283 *p = NUL;
1284 }
1285 else
1286 marker = (char_u *)".";
1287
1288 l = list_alloc();
1289 if (l == NULL)
1290 return NULL;
1291
1292 for (;;)
1293 {
1294 int i = 0;
1295
1296 theline = eap->getline(NUL, eap->cookie, 0);
1297 if (theline != NULL && indent_len > 0)
1298 {
1299 // trim the indent matching the first line
1300 if (STRNCMP(theline, *eap->cmdlinep, indent_len) == 0)
1301 i = indent_len;
1302 }
1303
1304 if (theline == NULL)
1305 {
1306 semsg(_("E990: Missing end marker '%s'"), marker);
1307 break;
1308 }
1309 if (STRCMP(marker, theline + i) == 0)
1310 {
1311 vim_free(theline);
1312 break;
1313 }
1314
1315 if (list_append_string(l, theline + i, -1) == FAIL)
1316 break;
1317 vim_free(theline);
1318 }
1319
1320 return l;
1321}
1322
1323/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001324 * ":let" list all variable values
1325 * ":let var1 var2" list variable values
1326 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001327 * ":let var += expr" assignment command.
1328 * ":let var -= expr" assignment command.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001329 * ":let var *= expr" assignment command.
1330 * ":let var /= expr" assignment command.
1331 * ":let var %= expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001332 * ":let var .= expr" assignment command.
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001333 * ":let var ..= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001334 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 */
1336 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001337ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338{
1339 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001340 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001341 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001343 int var_count = 0;
1344 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001345 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001346 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001347 int first = TRUE;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001348 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349
Bram Moolenaardb552d602006-03-23 22:59:57 +00001350 argend = skip_var_list(arg, &var_count, &semicolon);
1351 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001352 return;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001353 if (argend > arg && argend[-1] == '.') // for var.='str'
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001354 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001355 expr = skipwhite(argend);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001356 concat = expr[0] == '.'
1357 && ((expr[1] == '=' && current_sctx.sc_version < 2)
1358 || (expr[1] == '.' && expr[2] == '='));
1359 if (*expr != '=' && !((vim_strchr((char_u *)"+-*/%", *expr) != NULL
1360 && expr[1] == '=') || concat))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001362 /*
1363 * ":let" without "=": list variables
1364 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001365 if (*arg == '[')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001366 emsg(_(e_invarg));
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001367 else if (expr[0] == '.')
1368 emsg(_("E985: .= is not supported with script version 2"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001369 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001370 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001371 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001372 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001373 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001374 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001375 list_glob_vars(&first);
1376 list_buf_vars(&first);
1377 list_win_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001378 list_tab_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001379 list_script_vars(&first);
1380 list_func_vars(&first);
1381 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 eap->nextcmd = check_nextcmd(arg);
1384 }
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001385 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
1386 {
1387 list_T *l;
1388
1389 // HERE document
1390 l = heredoc_get(eap, expr + 3);
1391 if (l != NULL)
1392 {
1393 rettv_list_set(&rettv, l);
1394 op[0] = '=';
1395 op[1] = NUL;
1396 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
1397 op);
1398 clear_tv(&rettv);
1399 }
1400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 else
1402 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001403 op[0] = '=';
1404 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001405 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001406 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001407 if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001408 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001409 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001410 if (expr[0] == '.' && expr[1] == '.') // ..=
1411 ++expr;
1412 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001413 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001414 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001415 else
1416 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001417
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 if (eap->skip)
1419 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001420 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 if (eap->skip)
1422 {
1423 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001424 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 --emsg_skip;
1426 }
1427 else if (i != FAIL)
1428 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001429 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001430 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001431 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 }
1433 }
1434}
1435
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001436/*
1437 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1438 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001439 * When "nextchars" is not NULL it points to a string with characters that
1440 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1441 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001442 * Returns OK or FAIL;
1443 */
1444 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001445ex_let_vars(
1446 char_u *arg_start,
1447 typval_T *tv,
1448 int copy, /* copy values from "tv", don't move */
1449 int semicolon, /* from skip_var_list() */
1450 int var_count, /* from skip_var_list() */
1451 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001452{
1453 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001454 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001455 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001456 listitem_T *item;
1457 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001458
1459 if (*arg != '[')
1460 {
1461 /*
1462 * ":let var = expr" or ":for var in list"
1463 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001464 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001465 return FAIL;
1466 return OK;
1467 }
1468
1469 /*
1470 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1471 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001472 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001473 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001474 emsg(_(e_listreq));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001475 return FAIL;
1476 }
1477
1478 i = list_len(l);
1479 if (semicolon == 0 && var_count < i)
1480 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001481 emsg(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001482 return FAIL;
1483 }
1484 if (var_count - semicolon > i)
1485 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001486 emsg(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001487 return FAIL;
1488 }
1489
1490 item = l->lv_first;
1491 while (*arg != ']')
1492 {
1493 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001494 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001495 item = item->li_next;
1496 if (arg == NULL)
1497 return FAIL;
1498
1499 arg = skipwhite(arg);
1500 if (*arg == ';')
1501 {
1502 /* Put the rest of the list (may be empty) in the var after ';'.
1503 * Create a new list for this. */
1504 l = list_alloc();
1505 if (l == NULL)
1506 return FAIL;
1507 while (item != NULL)
1508 {
1509 list_append_tv(l, &item->li_tv);
1510 item = item->li_next;
1511 }
1512
1513 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001514 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001515 ltv.vval.v_list = l;
1516 l->lv_refcount = 1;
1517
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001518 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1519 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001520 clear_tv(&ltv);
1521 if (arg == NULL)
1522 return FAIL;
1523 break;
1524 }
1525 else if (*arg != ',' && *arg != ']')
1526 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001527 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001528 return FAIL;
1529 }
1530 }
1531
1532 return OK;
1533}
1534
1535/*
1536 * Skip over assignable variable "var" or list of variables "[var, var]".
1537 * Used for ":let varvar = expr" and ":for varvar in expr".
1538 * For "[var, var]" increment "*var_count" for each variable.
1539 * for "[var, var; var]" set "semicolon".
1540 * Return NULL for an error.
1541 */
1542 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001543skip_var_list(
1544 char_u *arg,
1545 int *var_count,
1546 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001547{
1548 char_u *p, *s;
1549
1550 if (*arg == '[')
1551 {
1552 /* "[var, var]": find the matching ']'. */
1553 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001554 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001555 {
1556 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1557 s = skip_var_one(p);
1558 if (s == p)
1559 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001560 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001561 return NULL;
1562 }
1563 ++*var_count;
1564
1565 p = skipwhite(s);
1566 if (*p == ']')
1567 break;
1568 else if (*p == ';')
1569 {
1570 if (*semicolon == 1)
1571 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001572 emsg(_("Double ; in list of variables"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001573 return NULL;
1574 }
1575 *semicolon = 1;
1576 }
1577 else if (*p != ',')
1578 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001579 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001580 return NULL;
1581 }
1582 }
1583 return p + 1;
1584 }
1585 else
1586 return skip_var_one(arg);
1587}
1588
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001589/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001590 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001591 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001592 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001593 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001594skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001595{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001596 if (*arg == '@' && arg[1] != NUL)
1597 return arg + 2;
1598 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1599 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001600}
1601
Bram Moolenaara7043832005-01-21 11:56:39 +00001602/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001603 * List variables for hashtab "ht" with prefix "prefix".
1604 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001605 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001606 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001607list_hashtable_vars(
1608 hashtab_T *ht,
Bram Moolenaar32526b32019-01-19 17:43:09 +01001609 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01001610 int empty,
1611 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001612{
Bram Moolenaar33570922005-01-25 22:26:29 +00001613 hashitem_T *hi;
1614 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001615 int todo;
Bram Moolenaarf86db782018-10-25 13:31:37 +02001616 char_u buf[IOSIZE];
Bram Moolenaara7043832005-01-21 11:56:39 +00001617
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001618 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001619 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1620 {
1621 if (!HASHITEM_EMPTY(hi))
1622 {
1623 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001624 di = HI2DI(hi);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001625
1626 // apply :filter /pat/ to variable name
Bram Moolenaar32526b32019-01-19 17:43:09 +01001627 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1628 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001629 if (message_filtered(buf))
1630 continue;
1631
Bram Moolenaar33570922005-01-25 22:26:29 +00001632 if (empty || di->di_tv.v_type != VAR_STRING
1633 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001634 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001635 }
1636 }
1637}
1638
1639/*
1640 * List global variables.
1641 */
1642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001643list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001644{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001645 list_hashtable_vars(&globvarht, "", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001646}
1647
1648/*
1649 * List buffer variables.
1650 */
1651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001652list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001653{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001654 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001655}
1656
1657/*
1658 * List window variables.
1659 */
1660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001661list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001662{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001663 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001664}
1665
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001666/*
1667 * List tab page variables.
1668 */
1669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001670list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001671{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001672 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001673}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001674
Bram Moolenaara7043832005-01-21 11:56:39 +00001675/*
1676 * List Vim variables.
1677 */
1678 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001679list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001680{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001681 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001682}
1683
1684/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001685 * List script-local variables, if there is a script.
1686 */
1687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001688list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001689{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001690 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
1691 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
Bram Moolenaar32526b32019-01-19 17:43:09 +01001692 "s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001693}
1694
1695/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001696 * List variables in "arg".
1697 */
1698 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001699list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001700{
1701 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001702 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001703 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001704 char_u *name_start;
1705 char_u *arg_subsc;
1706 char_u *tofree;
1707 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001708
1709 while (!ends_excmd(*arg) && !got_int)
1710 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001711 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001712 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001713 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001714 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001715 {
1716 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001717 emsg(_(e_trailing));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001718 break;
1719 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001720 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001721 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001722 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001723 /* get_name_len() takes care of expanding curly braces */
1724 name_start = name = arg;
1725 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1726 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001727 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001728 /* This is mainly to keep test 49 working: when expanding
1729 * curly braces fails overrule the exception error message. */
1730 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001731 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001732 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001733 semsg(_(e_invarg2), arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001734 break;
1735 }
1736 error = TRUE;
1737 }
1738 else
1739 {
1740 if (tofree != NULL)
1741 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001742 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001743 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001744 else
1745 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001746 /* handle d.key, l[idx], f(expr) */
1747 arg_subsc = arg;
1748 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001749 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001750 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001751 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001752 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001753 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001754 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001755 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001756 case 'g': list_glob_vars(first); break;
1757 case 'b': list_buf_vars(first); break;
1758 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001759 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001760 case 'v': list_vim_vars(first); break;
1761 case 's': list_script_vars(first); break;
1762 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001763 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001764 semsg(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001765 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001766 }
1767 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001768 {
1769 char_u numbuf[NUMBUFLEN];
1770 char_u *tf;
1771 int c;
1772 char_u *s;
1773
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001774 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001775 c = *arg;
1776 *arg = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001777 list_one_var_a("",
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001778 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001779 tv.v_type,
1780 s == NULL ? (char_u *)"" : s,
1781 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001782 *arg = c;
1783 vim_free(tf);
1784 }
1785 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001786 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001787 }
1788 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001789
1790 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001791 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001792
1793 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001794 }
1795
1796 return arg;
1797}
1798
1799/*
1800 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1801 * Returns a pointer to the char just after the var name.
1802 * Returns NULL if there is an error.
1803 */
1804 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001805ex_let_one(
1806 char_u *arg, /* points to variable name */
1807 typval_T *tv, /* value to assign to variable */
1808 int copy, /* copy value from "tv" */
1809 char_u *endchars, /* valid chars after variable name or NULL */
1810 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001811{
1812 int c1;
1813 char_u *name;
1814 char_u *p;
1815 char_u *arg_end = NULL;
1816 int len;
1817 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001818 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001819
1820 /*
1821 * ":let $VAR = expr": Set environment variable.
1822 */
1823 if (*arg == '$')
1824 {
1825 /* Find the end of the name. */
1826 ++arg;
1827 name = arg;
1828 len = get_env_len(&arg);
1829 if (len == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001830 semsg(_(e_invarg2), name - 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001831 else
1832 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001833 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001834 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001835 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001836 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001837 emsg(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001838 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001839 {
1840 c1 = name[len];
1841 name[len] = NUL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001842 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001843 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001844 {
1845 int mustfree = FALSE;
1846 char_u *s = vim_getenv(name, &mustfree);
1847
1848 if (s != NULL)
1849 {
1850 p = tofree = concat_str(s, p);
1851 if (mustfree)
1852 vim_free(s);
1853 }
1854 }
1855 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001856 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001857 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001858 if (STRICMP(name, "HOME") == 0)
1859 init_homedir();
1860 else if (didset_vim && STRICMP(name, "VIM") == 0)
1861 didset_vim = FALSE;
1862 else if (didset_vimruntime
1863 && STRICMP(name, "VIMRUNTIME") == 0)
1864 didset_vimruntime = FALSE;
1865 arg_end = arg;
1866 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001867 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001868 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001869 }
1870 }
1871 }
1872
1873 /*
1874 * ":let &option = expr": Set option value.
1875 * ":let &l:option = expr": Set local option value.
1876 * ":let &g:option = expr": Set global option value.
1877 */
1878 else if (*arg == '&')
1879 {
1880 /* Find the end of the name. */
1881 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001882 if (p == NULL || (endchars != NULL
1883 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001884 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001885 else
1886 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001887 long n;
1888 int opt_type;
1889 long numval;
1890 char_u *stringval = NULL;
1891 char_u *s;
1892
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893 c1 = *p;
1894 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001895
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001896 n = (long)tv_get_number(tv);
1897 s = tv_get_string_chk(tv); /* != NULL if number or string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001898 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001899 {
1900 opt_type = get_option_value(arg, &numval,
1901 &stringval, opt_flags);
1902 if ((opt_type == 1 && *op == '.')
1903 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001904 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001905 semsg(_(e_letwrong), op);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001906 s = NULL; // don't set the value
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001907 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001908 else
1909 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001910 if (opt_type == 1) // number
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001911 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001912 switch (*op)
1913 {
1914 case '+': n = numval + n; break;
1915 case '-': n = numval - n; break;
1916 case '*': n = numval * n; break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001917 case '/': n = (long)num_divide(numval, n); break;
1918 case '%': n = (long)num_modulus(numval, n); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001919 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001920 }
Bram Moolenaarff697e62019-02-12 22:28:33 +01001921 else if (opt_type == 0 && stringval != NULL) // string
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001922 {
1923 s = concat_str(stringval, s);
1924 vim_free(stringval);
1925 stringval = s;
1926 }
1927 }
1928 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001929 if (s != NULL)
1930 {
1931 set_option_value(arg, n, s, opt_flags);
1932 arg_end = p;
1933 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001934 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001935 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001936 }
1937 }
1938
1939 /*
1940 * ":let @r = expr": Set register contents.
1941 */
1942 else if (*arg == '@')
1943 {
1944 ++arg;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001945 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001946 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001947 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001948 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001949 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001950 else
1951 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001952 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001953 char_u *s;
1954
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001955 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001956 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001957 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02001958 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001959 if (s != NULL)
1960 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001961 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001962 vim_free(s);
1963 }
1964 }
1965 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001966 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001967 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001968 arg_end = arg + 1;
1969 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001970 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001971 }
1972 }
1973
1974 /*
1975 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001976 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001977 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001978 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001979 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001980 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001981
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001982 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001983 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001984 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001985 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001986 emsg(_(e_letunexp));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001987 else
1988 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001989 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001990 arg_end = p;
1991 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001992 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001993 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001994 }
1995
1996 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001997 semsg(_(e_invarg2), arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001998
1999 return arg_end;
2000}
2001
2002/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002003 * Get an lval: variable, Dict item or List item that can be assigned a value
2004 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2005 * "name.key", "name.key[expr]" etc.
2006 * Indexing only works if "name" is an existing List or Dictionary.
2007 * "name" points to the start of the name.
2008 * If "rettv" is not NULL it points to the value to be assigned.
2009 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2010 * wrong; must end in space or cmd separator.
2011 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002012 * flags:
2013 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01002014 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002015 * GLV_NO_AUTOLOAD: do not use script autoloading
2016 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002017 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002018 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002019 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002020 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002021 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002022get_lval(
2023 char_u *name,
2024 typval_T *rettv,
2025 lval_T *lp,
2026 int unlet,
2027 int skip,
2028 int flags, /* GLV_ values */
2029 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002030{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002031 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002032 char_u *expr_start, *expr_end;
2033 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002034 dictitem_T *v;
2035 typval_T var1;
2036 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002037 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002038 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002039 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002040 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002041 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002042 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002043
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002044 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002045 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002046
2047 if (skip)
2048 {
2049 /* When skipping just find the end of the name. */
2050 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002051 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002052 }
2053
2054 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002055 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002056 if (expr_start != NULL)
2057 {
2058 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002059 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002060 && *p != '[' && *p != '.')
2061 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002062 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002063 return NULL;
2064 }
2065
2066 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2067 if (lp->ll_exp_name == NULL)
2068 {
2069 /* Report an invalid expression in braces, unless the
2070 * expression evaluation has been cancelled due to an
2071 * aborting error, an interrupt, or an exception. */
2072 if (!aborting() && !quiet)
2073 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002074 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002075 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002076 return NULL;
2077 }
2078 }
2079 lp->ll_name = lp->ll_exp_name;
2080 }
2081 else
2082 lp->ll_name = name;
2083
2084 /* Without [idx] or .key we are done. */
2085 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2086 return p;
2087
2088 cc = *p;
2089 *p = NUL;
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002090 /* Only pass &ht when we would write to the variable, it prevents autoload
2091 * as well. */
2092 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
2093 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002094 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002095 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002096 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002097 if (v == NULL)
2098 return NULL;
2099
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002100 /*
2101 * Loop until no more [idx] or .key is following.
2102 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002103 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002104 var1.v_type = VAR_UNKNOWN;
2105 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002106 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002107 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002108 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2109 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002110 && lp->ll_tv->vval.v_dict != NULL)
2111 && !(lp->ll_tv->v_type == VAR_BLOB
2112 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002113 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002114 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002115 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002116 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002117 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002118 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002119 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002120 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002121 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002122 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002123 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002124
Bram Moolenaar8c711452005-01-14 21:53:12 +00002125 len = -1;
2126 if (*p == '.')
2127 {
2128 key = p + 1;
2129 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2130 ;
2131 if (len == 0)
2132 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002133 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002134 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002135 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002136 }
2137 p = key + len;
2138 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002139 else
2140 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002141 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002142 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002143 if (*p == ':')
2144 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002145 else
2146 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002147 empty1 = FALSE;
2148 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002149 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002150 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002151 {
2152 /* not a number or string */
2153 clear_tv(&var1);
2154 return NULL;
2155 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002156 }
2157
2158 /* Optionally get the second index [ :expr]. */
2159 if (*p == ':')
2160 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002161 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002162 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002163 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002164 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002165 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002166 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002167 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002168 if (rettv != NULL
2169 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002170 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002171 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002172 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002173 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002174 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002175 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002176 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002177 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002178 }
2179 p = skipwhite(p + 1);
2180 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002181 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002182 else
2183 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002184 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002185 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2186 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002187 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002188 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002189 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002190 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002191 {
2192 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002193 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002194 clear_tv(&var2);
2195 return NULL;
2196 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002197 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002198 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002199 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002200 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002201 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002202
Bram Moolenaar8c711452005-01-14 21:53:12 +00002203 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002204 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002205 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002206 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002207 clear_tv(&var1);
2208 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002209 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002210 }
2211
2212 /* Skip to past ']'. */
2213 ++p;
2214 }
2215
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002216 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002217 {
2218 if (len == -1)
2219 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002220 /* "[key]": get key from "var1" */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002221 key = tv_get_string_chk(&var1); /* is number or string */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002222 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002223 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002224 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002225 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002226 }
2227 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002228 lp->ll_list = NULL;
2229 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002230 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002231
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002232 /* When assigning to a scope dictionary check that a function and
2233 * variable name is valid (only variable name unless it is l: or
2234 * g: dictionary). Disallow overwriting a builtin function. */
2235 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002236 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002237 int prevval;
2238 int wrong;
2239
2240 if (len != -1)
2241 {
2242 prevval = key[len];
2243 key[len] = NUL;
2244 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002245 else
2246 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002247 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2248 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002249 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002250 || !valid_varname(key);
2251 if (len != -1)
2252 key[len] = prevval;
2253 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002254 return NULL;
2255 }
2256
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002257 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002258 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01002259 // Can't add "v:" or "a:" variable.
2260 if (lp->ll_dict == &vimvardict
2261 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002262 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002263 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01002264 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002265 return NULL;
2266 }
2267
Bram Moolenaar31b81602019-02-10 22:14:27 +01002268 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002269 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002270 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002271 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002272 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002273 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002274 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002275 }
2276 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002277 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002278 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002279 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002280 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002281 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002282 p = NULL;
2283 break;
2284 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002285 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002286 else if ((flags & GLV_READ_ONLY) == 0
2287 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002288 {
2289 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002290 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002291 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002292
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002293 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002294 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002295 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002296 else if (lp->ll_tv->v_type == VAR_BLOB)
2297 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002298 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
2299
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002300 /*
2301 * Get the number and item for the only or first index of the List.
2302 */
2303 if (empty1)
2304 lp->ll_n1 = 0;
2305 else
2306 // is number or string
2307 lp->ll_n1 = (long)tv_get_number(&var1);
2308 clear_tv(&var1);
2309
2310 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002311 || lp->ll_n1 > bloblen
2312 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002313 {
2314 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002315 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002316 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002317 return NULL;
2318 }
2319 if (lp->ll_range && !lp->ll_empty2)
2320 {
2321 lp->ll_n2 = (long)tv_get_number(&var2);
2322 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002323 if (lp->ll_n2 < 0
2324 || lp->ll_n2 >= bloblen
2325 || lp->ll_n2 < lp->ll_n1)
2326 {
2327 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002328 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002329 return NULL;
2330 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002331 }
2332 lp->ll_blob = lp->ll_tv->vval.v_blob;
2333 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01002334 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002335 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002336 else
2337 {
2338 /*
2339 * Get the number and item for the only or first index of the List.
2340 */
2341 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002342 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002343 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002344 /* is number or string */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002345 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002346 clear_tv(&var1);
2347
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002348 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002349 lp->ll_list = lp->ll_tv->vval.v_list;
2350 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2351 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002352 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002353 if (lp->ll_n1 < 0)
2354 {
2355 lp->ll_n1 = 0;
2356 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2357 }
2358 }
2359 if (lp->ll_li == NULL)
2360 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002361 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002362 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002363 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002364 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002365 }
2366
2367 /*
2368 * May need to find the item or absolute index for the second
2369 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002370 * When no index given: "lp->ll_empty2" is TRUE.
2371 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002372 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002373 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002374 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002375 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002376 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002377 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002378 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002379 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002380 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002381 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002382 {
2383 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002384 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002385 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002386 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002387 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002388 }
2389
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002390 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2391 if (lp->ll_n1 < 0)
2392 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2393 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002394 {
2395 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002396 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002397 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002398 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002399 }
2400
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002401 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002402 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002403 }
2404
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002405 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002406 return p;
2407}
2408
2409/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002410 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002411 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002412 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002413clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002414{
2415 vim_free(lp->ll_exp_name);
2416 vim_free(lp->ll_newkey);
2417}
2418
2419/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002420 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002421 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01002422 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
2423 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002424 */
2425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002426set_var_lval(
2427 lval_T *lp,
2428 char_u *endp,
2429 typval_T *rettv,
2430 int copy,
2431 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002432{
2433 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002434 listitem_T *ri;
2435 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002436
2437 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002438 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002439 cc = *endp;
2440 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002441 if (lp->ll_blob != NULL)
2442 {
2443 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002444
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002445 if (op != NULL && *op != '=')
2446 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002447 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002448 return;
2449 }
2450
2451 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2452 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002453 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002454
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002455 if (lp->ll_empty2)
2456 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
2457
2458 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002459 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002460 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002461 return;
2462 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002463 if (lp->ll_empty2)
2464 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002465
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002466 ir = 0;
2467 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
2468 blob_set(lp->ll_blob, il,
2469 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002470 }
2471 else
2472 {
2473 val = (int)tv_get_number_chk(rettv, &error);
2474 if (!error)
2475 {
2476 garray_T *gap = &lp->ll_blob->bv_ga;
2477
2478 // Allow for appending a byte. Setting a byte beyond
2479 // the end is an error otherwise.
2480 if (lp->ll_n1 < gap->ga_len
2481 || (lp->ll_n1 == gap->ga_len
2482 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
2483 {
2484 blob_set(lp->ll_blob, lp->ll_n1, val);
2485 if (lp->ll_n1 == gap->ga_len)
2486 ++gap->ga_len;
2487 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002488 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002489 }
2490 }
2491 }
2492 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002493 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002494 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002495
Bram Moolenaarff697e62019-02-12 22:28:33 +01002496 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01002497 di = NULL;
2498 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2499 &tv, &di, TRUE, FALSE) == OK)
2500 {
2501 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002502 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2503 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01002504 && tv_op(&tv, rettv, op) == OK)
2505 set_var(lp->ll_name, &tv, FALSE);
2506 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002507 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002508 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002509 else
2510 set_var(lp->ll_name, rettv, copy);
2511 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002513 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002514 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002515 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002516 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517 else if (lp->ll_range)
2518 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002519 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002520 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002521
2522 /*
2523 * Check whether any of the list items is locked
2524 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002525 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002526 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002527 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002528 return;
2529 ri = ri->li_next;
2530 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2531 break;
2532 ll_li = ll_li->li_next;
2533 ++ll_n1;
2534 }
2535
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 /*
2537 * Assign the List values to the list items.
2538 */
2539 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002540 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002541 if (op != NULL && *op != '=')
2542 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2543 else
2544 {
2545 clear_tv(&lp->ll_li->li_tv);
2546 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2547 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548 ri = ri->li_next;
2549 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2550 break;
2551 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002552 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002553 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002554 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002555 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 ri = NULL;
2557 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002558 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002559 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 lp->ll_li = lp->ll_li->li_next;
2561 ++lp->ll_n1;
2562 }
2563 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002564 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002565 else if (lp->ll_empty2
2566 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002568 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569 }
2570 else
2571 {
2572 /*
2573 * Assign to a List or Dictionary item.
2574 */
2575 if (lp->ll_newkey != NULL)
2576 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002577 if (op != NULL && *op != '=')
2578 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002579 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002580 return;
2581 }
2582
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002584 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 if (di == NULL)
2586 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002587 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2588 {
2589 vim_free(di);
2590 return;
2591 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002593 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002594 else if (op != NULL && *op != '=')
2595 {
2596 tv_op(lp->ll_tv, rettv, op);
2597 return;
2598 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002599 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002601
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 /*
2603 * Assign the value to the variable or list item.
2604 */
2605 if (copy)
2606 copy_tv(rettv, lp->ll_tv);
2607 else
2608 {
2609 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002610 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002612 }
2613 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002614}
2615
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002616/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01002617 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2618 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002619 * Returns OK or FAIL.
2620 */
2621 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002622tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002623{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002624 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002625 char_u numbuf[NUMBUFLEN];
2626 char_u *s;
2627
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002628 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2629 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2630 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002631 {
2632 switch (tv1->v_type)
2633 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002634 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002635 case VAR_DICT:
2636 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002637 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002638 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002639 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002640 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002641 break;
2642
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002643 case VAR_BLOB:
2644 if (*op != '+' || tv2->v_type != VAR_BLOB)
2645 break;
2646 // BLOB += BLOB
2647 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2648 {
2649 blob_T *b1 = tv1->vval.v_blob;
2650 blob_T *b2 = tv2->vval.v_blob;
2651 int i, len = blob_len(b2);
2652 for (i = 0; i < len; i++)
2653 ga_append(&b1->bv_ga, blob_get(b2, i));
2654 }
2655 return OK;
2656
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002657 case VAR_LIST:
2658 if (*op != '+' || tv2->v_type != VAR_LIST)
2659 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002660 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002661 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2662 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2663 return OK;
2664
2665 case VAR_NUMBER:
2666 case VAR_STRING:
2667 if (tv2->v_type == VAR_LIST)
2668 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002669 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002670 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002671 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002672 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002673#ifdef FEAT_FLOAT
2674 if (tv2->v_type == VAR_FLOAT)
2675 {
2676 float_T f = n;
2677
Bram Moolenaarff697e62019-02-12 22:28:33 +01002678 if (*op == '%')
2679 break;
2680 switch (*op)
2681 {
2682 case '+': f += tv2->vval.v_float; break;
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 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002687 clear_tv(tv1);
2688 tv1->v_type = VAR_FLOAT;
2689 tv1->vval.v_float = f;
2690 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002691 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002692#endif
2693 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002694 switch (*op)
2695 {
2696 case '+': n += tv_get_number(tv2); break;
2697 case '-': n -= tv_get_number(tv2); break;
2698 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01002699 case '/': n = num_divide(n, tv_get_number(tv2)); break;
2700 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002701 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002702 clear_tv(tv1);
2703 tv1->v_type = VAR_NUMBER;
2704 tv1->vval.v_number = n;
2705 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002706 }
2707 else
2708 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002709 if (tv2->v_type == VAR_FLOAT)
2710 break;
2711
Bram Moolenaarff697e62019-02-12 22:28:33 +01002712 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002713 s = tv_get_string(tv1);
2714 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002715 clear_tv(tv1);
2716 tv1->v_type = VAR_STRING;
2717 tv1->vval.v_string = s;
2718 }
2719 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002720
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002721 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002722#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002723 {
2724 float_T f;
2725
Bram Moolenaarff697e62019-02-12 22:28:33 +01002726 if (*op == '%' || *op == '.'
2727 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002728 && tv2->v_type != VAR_NUMBER
2729 && tv2->v_type != VAR_STRING))
2730 break;
2731 if (tv2->v_type == VAR_FLOAT)
2732 f = tv2->vval.v_float;
2733 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002734 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01002735 switch (*op)
2736 {
2737 case '+': tv1->vval.v_float += f; break;
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 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002742 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002743#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002744 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002745 }
2746 }
2747
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002748 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002749 return FAIL;
2750}
2751
2752/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002753 * Evaluate the expression used in a ":for var in expr" command.
2754 * "arg" points to "var".
2755 * Set "*errp" to TRUE for an error, FALSE otherwise;
2756 * Return a pointer that holds the info. Null when there is an error.
2757 */
2758 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002759eval_for_line(
2760 char_u *arg,
2761 int *errp,
2762 char_u **nextcmdp,
2763 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002764{
Bram Moolenaar33570922005-01-25 22:26:29 +00002765 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002766 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002767 typval_T tv;
2768 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002769
2770 *errp = TRUE; /* default: there is an error */
2771
Bram Moolenaar33570922005-01-25 22:26:29 +00002772 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002773 if (fi == NULL)
2774 return NULL;
2775
2776 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2777 if (expr == NULL)
2778 return fi;
2779
2780 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002781 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002782 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002783 emsg(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002784 return fi;
2785 }
2786
2787 if (skip)
2788 ++emsg_skip;
2789 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2790 {
2791 *errp = FALSE;
2792 if (!skip)
2793 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002794 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002795 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002796 l = tv.vval.v_list;
2797 if (l == NULL)
2798 {
2799 // a null list is like an empty list: do nothing
2800 clear_tv(&tv);
2801 }
2802 else
2803 {
2804 // No need to increment the refcount, it's already set for
2805 // the list being used in "tv".
2806 fi->fi_list = l;
2807 list_add_watch(l, &fi->fi_lw);
2808 fi->fi_lw.lw_item = l->lv_first;
2809 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002810 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002811 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002812 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002813 fi->fi_bi = 0;
2814 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002815 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002816 typval_T btv;
2817
2818 // Make a copy, so that the iteration still works when the
2819 // blob is changed.
2820 blob_copy(&tv, &btv);
2821 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002822 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002823 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002824 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002825 else
2826 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002827 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002828 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002829 }
2830 }
2831 }
2832 if (skip)
2833 --emsg_skip;
2834
2835 return fi;
2836}
2837
2838/*
2839 * Use the first item in a ":for" list. Advance to the next.
2840 * Assign the values to the variable (list). "arg" points to the first one.
2841 * Return TRUE when a valid item was found, FALSE when at end of list or
2842 * something wrong.
2843 */
2844 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002845next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002846{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002847 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002848 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002849 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002850
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002851 if (fi->fi_blob != NULL)
2852 {
2853 typval_T tv;
2854
2855 if (fi->fi_bi >= blob_len(fi->fi_blob))
2856 return FALSE;
2857 tv.v_type = VAR_NUMBER;
2858 tv.v_lock = VAR_FIXED;
2859 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2860 ++fi->fi_bi;
2861 return ex_let_vars(arg, &tv, TRUE,
2862 fi->fi_semicolon, fi->fi_varcount, NULL) == OK;
2863 }
2864
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002865 item = fi->fi_lw.lw_item;
2866 if (item == NULL)
2867 result = FALSE;
2868 else
2869 {
2870 fi->fi_lw.lw_item = item->li_next;
2871 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2872 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2873 }
2874 return result;
2875}
2876
2877/*
2878 * Free the structure used to store info used by ":for".
2879 */
2880 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002881free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002882{
Bram Moolenaar33570922005-01-25 22:26:29 +00002883 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002884
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002885 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002886 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002887 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002888 list_unref(fi->fi_list);
2889 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002890 if (fi != NULL && fi->fi_blob != NULL)
2891 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002892 vim_free(fi);
2893}
2894
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2896
2897 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002898set_context_for_expression(
2899 expand_T *xp,
2900 char_u *arg,
2901 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902{
2903 int got_eq = FALSE;
2904 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002905 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002907 if (cmdidx == CMD_let)
2908 {
2909 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002910 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002911 {
2912 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002913 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002914 {
2915 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002916 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002917 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002918 break;
2919 }
2920 return;
2921 }
2922 }
2923 else
2924 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2925 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 while ((xp->xp_pattern = vim_strpbrk(arg,
2927 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2928 {
2929 c = *xp->xp_pattern;
2930 if (c == '&')
2931 {
2932 c = xp->xp_pattern[1];
2933 if (c == '&')
2934 {
2935 ++xp->xp_pattern;
2936 xp->xp_context = cmdidx != CMD_let || got_eq
2937 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2938 }
2939 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002940 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002942 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2943 xp->xp_pattern += 2;
2944
2945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 }
2947 else if (c == '$')
2948 {
2949 /* environment variable */
2950 xp->xp_context = EXPAND_ENV_VARS;
2951 }
2952 else if (c == '=')
2953 {
2954 got_eq = TRUE;
2955 xp->xp_context = EXPAND_EXPRESSION;
2956 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002957 else if (c == '#'
2958 && xp->xp_context == EXPAND_EXPRESSION)
2959 {
2960 /* Autoload function/variable contains '#'. */
2961 break;
2962 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002963 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 && xp->xp_context == EXPAND_FUNCTIONS
2965 && vim_strchr(xp->xp_pattern, '(') == NULL)
2966 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002967 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 break;
2969 }
2970 else if (cmdidx != CMD_let || got_eq)
2971 {
2972 if (c == '"') /* string */
2973 {
2974 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2975 if (c == '\\' && xp->xp_pattern[1] != NUL)
2976 ++xp->xp_pattern;
2977 xp->xp_context = EXPAND_NOTHING;
2978 }
2979 else if (c == '\'') /* literal string */
2980 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002981 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2983 /* skip */ ;
2984 xp->xp_context = EXPAND_NOTHING;
2985 }
2986 else if (c == '|')
2987 {
2988 if (xp->xp_pattern[1] == '|')
2989 {
2990 ++xp->xp_pattern;
2991 xp->xp_context = EXPAND_EXPRESSION;
2992 }
2993 else
2994 xp->xp_context = EXPAND_COMMANDS;
2995 }
2996 else
2997 xp->xp_context = EXPAND_EXPRESSION;
2998 }
2999 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003000 /* Doesn't look like something valid, expand as an expression
3001 * anyway. */
3002 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 arg = xp->xp_pattern;
3004 if (*arg != NUL)
3005 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3006 /* skip */ ;
3007 }
3008 xp->xp_pattern = arg;
3009}
3010
3011#endif /* FEAT_CMDL_COMPL */
3012
3013/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 * ":unlet[!] var1 ... " command.
3015 */
3016 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003017ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003019 ex_unletlock(eap, eap->arg, 0);
3020}
3021
3022/*
3023 * ":lockvar" and ":unlockvar" commands
3024 */
3025 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003026ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003027{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003029 int deep = 2;
3030
3031 if (eap->forceit)
3032 deep = -1;
3033 else if (vim_isdigit(*arg))
3034 {
3035 deep = getdigits(&arg);
3036 arg = skipwhite(arg);
3037 }
3038
3039 ex_unletlock(eap, arg, deep);
3040}
3041
3042/*
3043 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3044 */
3045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003046ex_unletlock(
3047 exarg_T *eap,
3048 char_u *argstart,
3049 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003050{
3051 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003054 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055
3056 do
3057 {
Bram Moolenaar137374f2018-05-13 15:59:50 +02003058 if (*arg == '$')
3059 {
3060 char_u *name = ++arg;
3061
3062 if (get_env_len(&arg) == 0)
3063 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003064 semsg(_(e_invarg2), name - 1);
Bram Moolenaar137374f2018-05-13 15:59:50 +02003065 return;
3066 }
3067 vim_unsetenv(name);
3068 arg = skipwhite(arg);
3069 continue;
3070 }
3071
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003072 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003073 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003074 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003075 if (lv.ll_name == NULL)
3076 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003077 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003078 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003080 if (name_end != NULL)
3081 {
3082 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003083 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003084 }
3085 if (!(eap->skip || error))
3086 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 break;
3088 }
3089
3090 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003091 {
3092 if (eap->cmdidx == CMD_unlet)
3093 {
3094 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3095 error = TRUE;
3096 }
3097 else
3098 {
3099 if (do_lock_var(&lv, name_end, deep,
3100 eap->cmdidx == CMD_lockvar) == FAIL)
3101 error = TRUE;
3102 }
3103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003105 if (!eap->skip)
3106 clear_lval(&lv);
3107
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 arg = skipwhite(name_end);
3109 } while (!ends_excmd(*arg));
3110
3111 eap->nextcmd = check_nextcmd(arg);
3112}
3113
Bram Moolenaar8c711452005-01-14 21:53:12 +00003114 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003115do_unlet_var(
3116 lval_T *lp,
3117 char_u *name_end,
3118 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003119{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003120 int ret = OK;
3121 int cc;
3122
3123 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003124 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003125 cc = *name_end;
3126 *name_end = NUL;
3127
3128 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003129 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003130 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003131 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003132 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003133 else if ((lp->ll_list != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003134 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003135 || (lp->ll_dict != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003136 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003137 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003138 else if (lp->ll_range)
3139 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003140 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003141 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003142 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003143
3144 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3145 {
3146 li = ll_li->li_next;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003147 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003148 return FAIL;
3149 ll_li = li;
3150 ++ll_n1;
3151 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003152
3153 /* Delete a range of List items. */
3154 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3155 {
3156 li = lp->ll_li->li_next;
3157 listitem_remove(lp->ll_list, lp->ll_li);
3158 lp->ll_li = li;
3159 ++lp->ll_n1;
3160 }
3161 }
3162 else
3163 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003164 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003165 /* unlet a List item. */
3166 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003167 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003168 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003169 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003170 }
3171
3172 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003173}
3174
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175/*
3176 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003177 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 */
3179 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003180do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181{
Bram Moolenaar33570922005-01-25 22:26:29 +00003182 hashtab_T *ht;
3183 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003184 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003185 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003186 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187
Bram Moolenaar33570922005-01-25 22:26:29 +00003188 ht = find_var_ht(name, &varname);
3189 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003191 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003192 if (d == NULL)
3193 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003194 if (ht == &globvarht)
3195 d = &globvardict;
3196 else if (ht == &compat_hashtab)
3197 d = &vimvardict;
3198 else
3199 {
3200 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3201 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3202 }
3203 if (d == NULL)
3204 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003205 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003206 return FAIL;
3207 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003208 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003209 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003210 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003211 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003212 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003213 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003214 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003215 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003216 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003217 || var_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003218 return FAIL;
3219
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003220 delete_var(ht, hi);
3221 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003224 if (forceit)
3225 return OK;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003226 semsg(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 return FAIL;
3228}
3229
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003230/*
3231 * Lock or unlock variable indicated by "lp".
3232 * "deep" is the levels to go (-1 for unlimited);
3233 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3234 */
3235 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003236do_lock_var(
3237 lval_T *lp,
3238 char_u *name_end,
3239 int deep,
3240 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003241{
3242 int ret = OK;
3243 int cc;
3244 dictitem_T *di;
3245
3246 if (deep == 0) /* nothing to do */
3247 return OK;
3248
3249 if (lp->ll_tv == NULL)
3250 {
3251 cc = *name_end;
3252 *name_end = NUL;
3253
3254 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003255 di = find_var(lp->ll_name, NULL, TRUE);
3256 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003257 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01003258 else if ((di->di_flags & DI_FLAGS_FIX)
3259 && di->di_tv.v_type != VAR_DICT
3260 && di->di_tv.v_type != VAR_LIST)
3261 /* For historic reasons this error is not given for a list or dict.
3262 * E.g., the b: dict could be locked/unlocked. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003263 semsg(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003264 else
3265 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01003266 if (lock)
3267 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003268 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01003269 di->di_flags &= ~DI_FLAGS_LOCK;
3270 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003271 }
3272 *name_end = cc;
3273 }
3274 else if (lp->ll_range)
3275 {
3276 listitem_T *li = lp->ll_li;
3277
3278 /* (un)lock a range of List items. */
3279 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3280 {
3281 item_lock(&li->li_tv, deep, lock);
3282 li = li->li_next;
3283 ++lp->ll_n1;
3284 }
3285 }
3286 else if (lp->ll_list != NULL)
3287 /* (un)lock a List item. */
3288 item_lock(&lp->ll_li->li_tv, deep, lock);
3289 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003290 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003291 item_lock(&lp->ll_di->di_tv, deep, lock);
3292
3293 return ret;
3294}
3295
3296/*
3297 * Lock or unlock an item. "deep" is nr of levels to go.
3298 */
3299 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003300item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003301{
3302 static int recurse = 0;
3303 list_T *l;
3304 listitem_T *li;
3305 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003306 blob_T *b;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003307 hashitem_T *hi;
3308 int todo;
3309
3310 if (recurse >= DICT_MAXNEST)
3311 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003312 emsg(_("E743: variable nested too deep for (un)lock"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003313 return;
3314 }
3315 if (deep == 0)
3316 return;
3317 ++recurse;
3318
3319 /* lock/unlock the item itself */
3320 if (lock)
3321 tv->v_lock |= VAR_LOCKED;
3322 else
3323 tv->v_lock &= ~VAR_LOCKED;
3324
3325 switch (tv->v_type)
3326 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003327 case VAR_UNKNOWN:
3328 case VAR_NUMBER:
3329 case VAR_STRING:
3330 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003331 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003332 case VAR_FLOAT:
3333 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003334 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003335 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003336 break;
3337
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003338 case VAR_BLOB:
3339 if ((b = tv->vval.v_blob) != NULL)
3340 {
3341 if (lock)
3342 b->bv_lock |= VAR_LOCKED;
3343 else
3344 b->bv_lock &= ~VAR_LOCKED;
3345 }
3346 break;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003347 case VAR_LIST:
3348 if ((l = tv->vval.v_list) != NULL)
3349 {
3350 if (lock)
3351 l->lv_lock |= VAR_LOCKED;
3352 else
3353 l->lv_lock &= ~VAR_LOCKED;
3354 if (deep < 0 || deep > 1)
3355 /* recursive: lock/unlock the items the List contains */
3356 for (li = l->lv_first; li != NULL; li = li->li_next)
3357 item_lock(&li->li_tv, deep - 1, lock);
3358 }
3359 break;
3360 case VAR_DICT:
3361 if ((d = tv->vval.v_dict) != NULL)
3362 {
3363 if (lock)
3364 d->dv_lock |= VAR_LOCKED;
3365 else
3366 d->dv_lock &= ~VAR_LOCKED;
3367 if (deep < 0 || deep > 1)
3368 {
3369 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003370 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003371 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3372 {
3373 if (!HASHITEM_EMPTY(hi))
3374 {
3375 --todo;
3376 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3377 }
3378 }
3379 }
3380 }
3381 }
3382 --recurse;
3383}
3384
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3386/*
3387 * Delete all "menutrans_" variables.
3388 */
3389 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003390del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391{
Bram Moolenaar33570922005-01-25 22:26:29 +00003392 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003393 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394
Bram Moolenaar33570922005-01-25 22:26:29 +00003395 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003396 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003397 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003398 {
3399 if (!HASHITEM_EMPTY(hi))
3400 {
3401 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003402 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3403 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003404 }
3405 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003406 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407}
3408#endif
3409
3410#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3411
3412/*
3413 * Local string buffer for the next two functions to store a variable name
3414 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3415 * get_user_var_name().
3416 */
3417
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418static char_u *varnamebuf = NULL;
3419static int varnamebuflen = 0;
3420
3421/*
3422 * Function to concatenate a prefix and a variable name.
3423 */
3424 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003425cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426{
3427 int len;
3428
3429 len = (int)STRLEN(name) + 3;
3430 if (len > varnamebuflen)
3431 {
3432 vim_free(varnamebuf);
3433 len += 10; /* some additional space */
3434 varnamebuf = alloc(len);
3435 if (varnamebuf == NULL)
3436 {
3437 varnamebuflen = 0;
3438 return NULL;
3439 }
3440 varnamebuflen = len;
3441 }
3442 *varnamebuf = prefix;
3443 varnamebuf[1] = ':';
3444 STRCPY(varnamebuf + 2, name);
3445 return varnamebuf;
3446}
3447
3448/*
3449 * Function given to ExpandGeneric() to obtain the list of user defined
3450 * (global/buffer/window/built-in) variable names.
3451 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003453get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003455 static long_u gdone;
3456 static long_u bdone;
3457 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003458 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003459 static int vidx;
3460 static hashitem_T *hi;
3461 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462
3463 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003464 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003465 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003466 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003467 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003468
3469 /* Global variables */
3470 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003472 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003473 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003474 else
3475 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003476 while (HASHITEM_EMPTY(hi))
3477 ++hi;
3478 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3479 return cat_prefix_varname('g', hi->hi_key);
3480 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003482
3483 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003484 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003485 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003487 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003488 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003489 else
3490 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003491 while (HASHITEM_EMPTY(hi))
3492 ++hi;
3493 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003495
3496 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003497 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003498 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003500 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003501 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003502 else
3503 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003504 while (HASHITEM_EMPTY(hi))
3505 ++hi;
3506 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003508
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003509 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003510 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003511 if (tdone < ht->ht_used)
3512 {
3513 if (tdone++ == 0)
3514 hi = ht->ht_array;
3515 else
3516 ++hi;
3517 while (HASHITEM_EMPTY(hi))
3518 ++hi;
3519 return cat_prefix_varname('t', hi->hi_key);
3520 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003521
Bram Moolenaar33570922005-01-25 22:26:29 +00003522 /* v: variables */
3523 if (vidx < VV_LEN)
3524 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525
Bram Moolenaard23a8232018-02-10 18:45:26 +01003526 VIM_CLEAR(varnamebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 varnamebuflen = 0;
3528 return NULL;
3529}
3530
3531#endif /* FEAT_CMDL_COMPL */
3532
3533/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003534 * Return TRUE if "pat" matches "text".
3535 * Does not use 'cpo' and always uses 'magic'.
3536 */
3537 static int
3538pattern_match(char_u *pat, char_u *text, int ic)
3539{
3540 int matches = FALSE;
3541 char_u *save_cpo;
3542 regmatch_T regmatch;
3543
3544 /* avoid 'l' flag in 'cpoptions' */
3545 save_cpo = p_cpo;
3546 p_cpo = (char_u *)"";
3547 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3548 if (regmatch.regprog != NULL)
3549 {
3550 regmatch.rm_ic = ic;
3551 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3552 vim_regfree(regmatch.regprog);
3553 }
3554 p_cpo = save_cpo;
3555 return matches;
3556}
3557
3558/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003560 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3562 */
3563
3564/*
3565 * Handle zero level expression.
3566 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003567 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003568 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 * Return OK or FAIL.
3570 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003571 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003572eval0(
3573 char_u *arg,
3574 typval_T *rettv,
3575 char_u **nextcmd,
3576 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577{
3578 int ret;
3579 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003580 int did_emsg_before = did_emsg;
3581 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582
3583 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003584 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 if (ret == FAIL || !ends_excmd(*p))
3586 {
3587 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003588 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 /*
3590 * Report the invalid expression unless the expression evaluation has
3591 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003592 * exception, or we already gave a more specific error.
3593 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003595 if (!aborting() && did_emsg == did_emsg_before
3596 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003597 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 ret = FAIL;
3599 }
3600 if (nextcmd != NULL)
3601 *nextcmd = check_nextcmd(p);
3602
3603 return ret;
3604}
3605
3606/*
3607 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003608 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 *
3610 * "arg" must point to the first non-white of the expression.
3611 * "arg" is advanced to the next non-white after the recognized expression.
3612 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003613 * Note: "rettv.v_lock" is not set.
3614 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 * Return OK or FAIL.
3616 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003617 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003618eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619{
3620 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003621 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622
3623 /*
3624 * Get the first variable.
3625 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003626 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 return FAIL;
3628
3629 if ((*arg)[0] == '?')
3630 {
3631 result = FALSE;
3632 if (evaluate)
3633 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003634 int error = FALSE;
3635
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003636 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003638 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003639 if (error)
3640 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 }
3642
3643 /*
3644 * Get the second variable.
3645 */
3646 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003647 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 return FAIL;
3649
3650 /*
3651 * Check for the ":".
3652 */
3653 if ((*arg)[0] != ':')
3654 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003655 emsg(_("E109: Missing ':' after '?'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003657 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 return FAIL;
3659 }
3660
3661 /*
3662 * Get the third variable.
3663 */
3664 *arg = skipwhite(*arg + 1);
3665 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3666 {
3667 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003668 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 return FAIL;
3670 }
3671 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003672 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 }
3674
3675 return OK;
3676}
3677
3678/*
3679 * Handle first level expression:
3680 * expr2 || expr2 || expr2 logical OR
3681 *
3682 * "arg" must point to the first non-white of the expression.
3683 * "arg" is advanced to the next non-white after the recognized expression.
3684 *
3685 * Return OK or FAIL.
3686 */
3687 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003688eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689{
Bram Moolenaar33570922005-01-25 22:26:29 +00003690 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 long result;
3692 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003693 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694
3695 /*
3696 * Get the first variable.
3697 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003698 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 return FAIL;
3700
3701 /*
3702 * Repeat until there is no following "||".
3703 */
3704 first = TRUE;
3705 result = FALSE;
3706 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3707 {
3708 if (evaluate && first)
3709 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003710 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003712 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003713 if (error)
3714 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 first = FALSE;
3716 }
3717
3718 /*
3719 * Get the second variable.
3720 */
3721 *arg = skipwhite(*arg + 2);
3722 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3723 return FAIL;
3724
3725 /*
3726 * Compute the result.
3727 */
3728 if (evaluate && !result)
3729 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003730 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003732 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003733 if (error)
3734 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 }
3736 if (evaluate)
3737 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003738 rettv->v_type = VAR_NUMBER;
3739 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 }
3741 }
3742
3743 return OK;
3744}
3745
3746/*
3747 * Handle second level expression:
3748 * expr3 && expr3 && expr3 logical AND
3749 *
3750 * "arg" must point to the first non-white of the expression.
3751 * "arg" is advanced to the next non-white after the recognized expression.
3752 *
3753 * Return OK or FAIL.
3754 */
3755 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003756eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757{
Bram Moolenaar33570922005-01-25 22:26:29 +00003758 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 long result;
3760 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003761 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762
3763 /*
3764 * Get the first variable.
3765 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003766 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 return FAIL;
3768
3769 /*
3770 * Repeat until there is no following "&&".
3771 */
3772 first = TRUE;
3773 result = TRUE;
3774 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3775 {
3776 if (evaluate && first)
3777 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003778 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003780 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003781 if (error)
3782 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 first = FALSE;
3784 }
3785
3786 /*
3787 * Get the second variable.
3788 */
3789 *arg = skipwhite(*arg + 2);
3790 if (eval4(arg, &var2, evaluate && result) == FAIL)
3791 return FAIL;
3792
3793 /*
3794 * Compute the result.
3795 */
3796 if (evaluate && result)
3797 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003798 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003800 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003801 if (error)
3802 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 }
3804 if (evaluate)
3805 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003806 rettv->v_type = VAR_NUMBER;
3807 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 }
3809 }
3810
3811 return OK;
3812}
3813
3814/*
3815 * Handle third level expression:
3816 * var1 == var2
3817 * var1 =~ var2
3818 * var1 != var2
3819 * var1 !~ var2
3820 * var1 > var2
3821 * var1 >= var2
3822 * var1 < var2
3823 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003824 * var1 is var2
3825 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 *
3827 * "arg" must point to the first non-white of the expression.
3828 * "arg" is advanced to the next non-white after the recognized expression.
3829 *
3830 * Return OK or FAIL.
3831 */
3832 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003833eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834{
Bram Moolenaar33570922005-01-25 22:26:29 +00003835 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 char_u *p;
3837 int i;
3838 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003839 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842
3843 /*
3844 * Get the first variable.
3845 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003846 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 return FAIL;
3848
3849 p = *arg;
3850 switch (p[0])
3851 {
3852 case '=': if (p[1] == '=')
3853 type = TYPE_EQUAL;
3854 else if (p[1] == '~')
3855 type = TYPE_MATCH;
3856 break;
3857 case '!': if (p[1] == '=')
3858 type = TYPE_NEQUAL;
3859 else if (p[1] == '~')
3860 type = TYPE_NOMATCH;
3861 break;
3862 case '>': if (p[1] != '=')
3863 {
3864 type = TYPE_GREATER;
3865 len = 1;
3866 }
3867 else
3868 type = TYPE_GEQUAL;
3869 break;
3870 case '<': if (p[1] != '=')
3871 {
3872 type = TYPE_SMALLER;
3873 len = 1;
3874 }
3875 else
3876 type = TYPE_SEQUAL;
3877 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003878 case 'i': if (p[1] == 's')
3879 {
3880 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3881 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003882 i = p[len];
3883 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003884 {
3885 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3886 type_is = TRUE;
3887 }
3888 }
3889 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 }
3891
3892 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003893 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 */
3895 if (type != TYPE_UNKNOWN)
3896 {
3897 /* extra question mark appended: ignore case */
3898 if (p[len] == '?')
3899 {
3900 ic = TRUE;
3901 ++len;
3902 }
3903 /* extra '#' appended: match case */
3904 else if (p[len] == '#')
3905 {
3906 ic = FALSE;
3907 ++len;
3908 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003909 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 else
3911 ic = p_ic;
3912
3913 /*
3914 * Get the second variable.
3915 */
3916 *arg = skipwhite(p + len);
3917 if (eval5(arg, &var2, evaluate) == FAIL)
3918 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003919 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 return FAIL;
3921 }
Bram Moolenaar31988702018-02-13 12:57:42 +01003922 if (evaluate)
3923 {
3924 int ret = typval_compare(rettv, &var2, type, type_is, ic);
3925
3926 clear_tv(&var2);
3927 return ret;
3928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 }
3930
3931 return OK;
3932}
3933
3934/*
3935 * Handle fourth level expression:
3936 * + number addition
3937 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003938 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003939 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 *
3941 * "arg" must point to the first non-white of the expression.
3942 * "arg" is advanced to the next non-white after the recognized expression.
3943 *
3944 * Return OK or FAIL.
3945 */
3946 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003947eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948{
Bram Moolenaar33570922005-01-25 22:26:29 +00003949 typval_T var2;
3950 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003952 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003953#ifdef FEAT_FLOAT
3954 float_T f1 = 0, f2 = 0;
3955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 char_u *s1, *s2;
3957 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3958 char_u *p;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003959 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960
3961 /*
3962 * Get the first variable.
3963 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003964 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 return FAIL;
3966
3967 /*
3968 * Repeat computing, until no '+', '-' or '.' is following.
3969 */
3970 for (;;)
3971 {
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003972 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 op = **arg;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003974 concat = op == '.'
3975 && (*(*arg + 1) == '.' || current_sctx.sc_version < 2);
3976 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 break;
3978
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003979 if ((op != '+' || (rettv->v_type != VAR_LIST
3980 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003981#ifdef FEAT_FLOAT
3982 && (op == '.' || rettv->v_type != VAR_FLOAT)
3983#endif
3984 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003985 {
3986 /* For "list + ...", an illegal use of the first operand as
3987 * a number cannot be determined before evaluating the 2nd
3988 * operand: if this is also a list, all is ok.
3989 * For "something . ...", "something - ..." or "non-list + ...",
3990 * we know that the first operand needs to be a string or number
3991 * without evaluating the 2nd operand. So check before to avoid
3992 * side effects after an error. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003993 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003994 {
3995 clear_tv(rettv);
3996 return FAIL;
3997 }
3998 }
3999
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 /*
4001 * Get the second variable.
4002 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02004003 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
4004 ++*arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004006 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004008 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 return FAIL;
4010 }
4011
4012 if (evaluate)
4013 {
4014 /*
4015 * Compute the result.
4016 */
4017 if (op == '.')
4018 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004019 s1 = tv_get_string_buf(rettv, buf1); /* already checked */
4020 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004021 if (s2 == NULL) /* type error ? */
4022 {
4023 clear_tv(rettv);
4024 clear_tv(&var2);
4025 return FAIL;
4026 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004027 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004028 clear_tv(rettv);
4029 rettv->v_type = VAR_STRING;
4030 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004032 else if (op == '+' && rettv->v_type == VAR_BLOB
4033 && var2.v_type == VAR_BLOB)
4034 {
4035 blob_T *b1 = rettv->vval.v_blob;
4036 blob_T *b2 = var2.vval.v_blob;
4037 blob_T *b = blob_alloc();
4038 int i;
4039
4040 if (b != NULL)
4041 {
4042 for (i = 0; i < blob_len(b1); i++)
4043 ga_append(&b->bv_ga, blob_get(b1, i));
4044 for (i = 0; i < blob_len(b2); i++)
4045 ga_append(&b->bv_ga, blob_get(b2, i));
4046
4047 clear_tv(rettv);
4048 rettv_blob_set(rettv, b);
4049 }
4050 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004051 else if (op == '+' && rettv->v_type == VAR_LIST
4052 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004053 {
4054 /* concatenate Lists */
4055 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4056 &var3) == FAIL)
4057 {
4058 clear_tv(rettv);
4059 clear_tv(&var2);
4060 return FAIL;
4061 }
4062 clear_tv(rettv);
4063 *rettv = var3;
4064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 else
4066 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004067 int error = FALSE;
4068
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004069#ifdef FEAT_FLOAT
4070 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004071 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004072 f1 = rettv->vval.v_float;
4073 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004074 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004075 else
4076#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004077 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004078 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004079 if (error)
4080 {
4081 /* This can only happen for "list + non-list". For
4082 * "non-list + ..." or "something - ...", we returned
4083 * before evaluating the 2nd operand. */
4084 clear_tv(rettv);
4085 return FAIL;
4086 }
4087#ifdef FEAT_FLOAT
4088 if (var2.v_type == VAR_FLOAT)
4089 f1 = n1;
4090#endif
4091 }
4092#ifdef FEAT_FLOAT
4093 if (var2.v_type == VAR_FLOAT)
4094 {
4095 f2 = var2.vval.v_float;
4096 n2 = 0;
4097 }
4098 else
4099#endif
4100 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004101 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004102 if (error)
4103 {
4104 clear_tv(rettv);
4105 clear_tv(&var2);
4106 return FAIL;
4107 }
4108#ifdef FEAT_FLOAT
4109 if (rettv->v_type == VAR_FLOAT)
4110 f2 = n2;
4111#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004112 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004113 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004114
4115#ifdef FEAT_FLOAT
4116 /* If there is a float on either side the result is a float. */
4117 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4118 {
4119 if (op == '+')
4120 f1 = f1 + f2;
4121 else
4122 f1 = f1 - f2;
4123 rettv->v_type = VAR_FLOAT;
4124 rettv->vval.v_float = f1;
4125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004127#endif
4128 {
4129 if (op == '+')
4130 n1 = n1 + n2;
4131 else
4132 n1 = n1 - n2;
4133 rettv->v_type = VAR_NUMBER;
4134 rettv->vval.v_number = n1;
4135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004137 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 }
4139 }
4140 return OK;
4141}
4142
4143/*
4144 * Handle fifth level expression:
4145 * * number multiplication
4146 * / number division
4147 * % number modulo
4148 *
4149 * "arg" must point to the first non-white of the expression.
4150 * "arg" is advanced to the next non-white after the recognized expression.
4151 *
4152 * Return OK or FAIL.
4153 */
4154 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004155eval6(
4156 char_u **arg,
4157 typval_T *rettv,
4158 int evaluate,
4159 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160{
Bram Moolenaar33570922005-01-25 22:26:29 +00004161 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004163 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004164#ifdef FEAT_FLOAT
4165 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02004166 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004167#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004168 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169
4170 /*
4171 * Get the first variable.
4172 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004173 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 return FAIL;
4175
4176 /*
4177 * Repeat computing, until no '*', '/' or '%' is following.
4178 */
4179 for (;;)
4180 {
4181 op = **arg;
4182 if (op != '*' && op != '/' && op != '%')
4183 break;
4184
4185 if (evaluate)
4186 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004187#ifdef FEAT_FLOAT
4188 if (rettv->v_type == VAR_FLOAT)
4189 {
4190 f1 = rettv->vval.v_float;
4191 use_float = TRUE;
4192 n1 = 0;
4193 }
4194 else
4195#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004196 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004197 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004198 if (error)
4199 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 }
4201 else
4202 n1 = 0;
4203
4204 /*
4205 * Get the second variable.
4206 */
4207 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004208 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 return FAIL;
4210
4211 if (evaluate)
4212 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004213#ifdef FEAT_FLOAT
4214 if (var2.v_type == VAR_FLOAT)
4215 {
4216 if (!use_float)
4217 {
4218 f1 = n1;
4219 use_float = TRUE;
4220 }
4221 f2 = var2.vval.v_float;
4222 n2 = 0;
4223 }
4224 else
4225#endif
4226 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004227 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004228 clear_tv(&var2);
4229 if (error)
4230 return FAIL;
4231#ifdef FEAT_FLOAT
4232 if (use_float)
4233 f2 = n2;
4234#endif
4235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236
4237 /*
4238 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004239 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004241#ifdef FEAT_FLOAT
4242 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004244 if (op == '*')
4245 f1 = f1 * f2;
4246 else if (op == '/')
4247 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004248# ifdef VMS
4249 /* VMS crashes on divide by zero, work around it */
4250 if (f2 == 0.0)
4251 {
4252 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004253 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004254 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004255 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004256 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004257 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004258 }
4259 else
4260 f1 = f1 / f2;
4261# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004262 /* We rely on the floating point library to handle divide
4263 * by zero to result in "inf" and not a crash. */
4264 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004265# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004268 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004269 emsg(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004270 return FAIL;
4271 }
4272 rettv->v_type = VAR_FLOAT;
4273 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 }
4275 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004278 if (op == '*')
4279 n1 = n1 * n2;
4280 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01004281 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01004283 n1 = num_modulus(n1, n2);
4284
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004285 rettv->v_type = VAR_NUMBER;
4286 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 }
4289 }
4290
4291 return OK;
4292}
4293
4294/*
4295 * Handle sixth level expression:
4296 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004297 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004298 * "string" string constant
4299 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 * &option-name option value
4301 * @r register contents
4302 * identifier variable value
4303 * function() function call
4304 * $VAR environment variable
4305 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004306 * [expr, expr] List
4307 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 *
4309 * Also handle:
4310 * ! in front logical NOT
4311 * - in front unary minus
4312 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004313 * trailing [] subscript in String or List
4314 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 *
4316 * "arg" must point to the first non-white of the expression.
4317 * "arg" is advanced to the next non-white after the recognized expression.
4318 *
4319 * Return OK or FAIL.
4320 */
4321 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004322eval7(
4323 char_u **arg,
4324 typval_T *rettv,
4325 int evaluate,
4326 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004328 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 int len;
4330 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 char_u *start_leader, *end_leader;
4332 int ret = OK;
4333 char_u *alias;
4334
4335 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004336 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004337 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004339 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340
4341 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004342 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 */
4344 start_leader = *arg;
4345 while (**arg == '!' || **arg == '-' || **arg == '+')
4346 *arg = skipwhite(*arg + 1);
4347 end_leader = *arg;
4348
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004349 if (**arg == '.' && (!isdigit(*(*arg + 1))
4350#ifdef FEAT_FLOAT
4351 || current_sctx.sc_version < 2
4352#endif
4353 ))
4354 {
4355 semsg(_(e_invexpr2), *arg);
4356 ++*arg;
4357 return FAIL;
4358 }
4359
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 switch (**arg)
4361 {
4362 /*
4363 * Number constant.
4364 */
4365 case '0':
4366 case '1':
4367 case '2':
4368 case '3':
4369 case '4':
4370 case '5':
4371 case '6':
4372 case '7':
4373 case '8':
4374 case '9':
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004375 case '.':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004376 {
4377#ifdef FEAT_FLOAT
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004378 char_u *p;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004379 int get_float = FALSE;
4380
4381 /* We accept a float when the format matches
4382 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004383 * strict to avoid backwards compatibility problems.
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004384 * With script version 2 and later the leading digit can be
4385 * omitted.
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004386 * Don't look for a float after the "." operator, so that
4387 * ":let vers = 1.2.3" doesn't fail. */
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004388 if (**arg == '.')
4389 p = *arg;
4390 else
4391 p = skipdigits(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004392 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004394 get_float = TRUE;
4395 p = skipdigits(p + 2);
4396 if (*p == 'e' || *p == 'E')
4397 {
4398 ++p;
4399 if (*p == '-' || *p == '+')
4400 ++p;
4401 if (!vim_isdigit(*p))
4402 get_float = FALSE;
4403 else
4404 p = skipdigits(p + 1);
4405 }
4406 if (ASCII_ISALPHA(*p) || *p == '.')
4407 get_float = FALSE;
4408 }
4409 if (get_float)
4410 {
4411 float_T f;
4412
4413 *arg += string2float(*arg, &f);
4414 if (evaluate)
4415 {
4416 rettv->v_type = VAR_FLOAT;
4417 rettv->vval.v_float = f;
4418 }
4419 }
4420 else
4421#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004422 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004423 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004424 char_u *bp;
Bram Moolenaare519dfd2019-01-13 15:42:02 +01004425 blob_T *blob = NULL; // init for gcc
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004426
4427 // Blob constant: 0z0123456789abcdef
4428 if (evaluate)
4429 blob = blob_alloc();
4430 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
4431 {
4432 if (!vim_isxdigit(bp[1]))
4433 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004434 if (blob != NULL)
4435 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004436 emsg(_("E973: Blob literal should have an even number of hex characters"));
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004437 ga_clear(&blob->bv_ga);
4438 VIM_CLEAR(blob);
4439 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004440 ret = FAIL;
4441 break;
4442 }
4443 if (blob != NULL)
4444 ga_append(&blob->bv_ga,
4445 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
Bram Moolenaar4131fd52019-01-17 16:32:53 +01004446 if (bp[2] == '.' && vim_isxdigit(bp[3]))
4447 ++bp;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004448 }
4449 if (blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004450 rettv_blob_set(rettv, blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004451 *arg = bp;
4452 }
4453 else
4454 {
4455 // decimal, hex or octal number
Bram Moolenaar16e9b852019-05-19 19:59:35 +02004456 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0, TRUE);
4457 if (len == 0)
4458 {
4459 semsg(_(e_invexpr2), *arg);
4460 ret = FAIL;
4461 break;
4462 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004463 *arg += len;
4464 if (evaluate)
4465 {
4466 rettv->v_type = VAR_NUMBER;
4467 rettv->vval.v_number = n;
4468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 }
4470 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472
4473 /*
4474 * String constant: "string".
4475 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004476 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 break;
4478
4479 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004480 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004482 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004483 break;
4484
4485 /*
4486 * List: [expr, expr]
4487 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004488 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 break;
4490
4491 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004492 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004493 * Dictionary: {key: val, key: val}
4494 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004495 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4496 if (ret == NOTDONE)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004497 ret = dict_get_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004498 break;
4499
4500 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004501 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004503 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 break;
4505
4506 /*
4507 * Environment variable: $VAR.
4508 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004509 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 break;
4511
4512 /*
4513 * Register contents: @r.
4514 */
4515 case '@': ++*arg;
4516 if (evaluate)
4517 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004518 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004519 rettv->vval.v_string = get_reg_contents(**arg,
4520 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 }
4522 if (**arg != NUL)
4523 ++*arg;
4524 break;
4525
4526 /*
4527 * nested expression: (expression).
4528 */
4529 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004530 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 if (**arg == ')')
4532 ++*arg;
4533 else if (ret == OK)
4534 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004535 emsg(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004536 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 ret = FAIL;
4538 }
4539 break;
4540
Bram Moolenaar8c711452005-01-14 21:53:12 +00004541 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 break;
4543 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004544
4545 if (ret == NOTDONE)
4546 {
4547 /*
4548 * Must be a variable or function name.
4549 * Can also be a curly-braces kind of name: {expr}.
4550 */
4551 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004552 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004553 if (alias != NULL)
4554 s = alias;
4555
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004556 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004557 ret = FAIL;
4558 else
4559 {
4560 if (**arg == '(') /* recursive! */
4561 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004562 partial_T *partial;
4563
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004564 if (!evaluate)
4565 check_vars(s, len);
4566
Bram Moolenaar8c711452005-01-14 21:53:12 +00004567 /* If "s" is the name of a variable of type VAR_FUNC
4568 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004569 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004570
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004571 /* Need to make a copy, in case evaluating the arguments makes
4572 * the name invalid. */
4573 s = vim_strsave(s);
4574 if (s == NULL)
4575 ret = FAIL;
4576 else
4577 /* Invoke the function. */
4578 ret = get_func_tv(s, len, rettv, arg,
4579 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4580 &len, evaluate, partial, NULL);
4581 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004582
4583 /* If evaluate is FALSE rettv->v_type was not set in
4584 * get_func_tv, but it's needed in handle_subscript() to parse
4585 * what follows. So set it here. */
4586 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4587 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004588 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004589 rettv->v_type = VAR_FUNC;
4590 }
4591
Bram Moolenaar8c711452005-01-14 21:53:12 +00004592 /* Stop the expression evaluation when immediately
4593 * aborting on error, or when an interrupt occurred or
4594 * an exception was thrown but not caught. */
4595 if (aborting())
4596 {
4597 if (ret == OK)
4598 clear_tv(rettv);
4599 ret = FAIL;
4600 }
4601 }
4602 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004603 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004604 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004605 {
4606 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004607 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004608 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004609 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004610 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004611 }
4612
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 *arg = skipwhite(*arg);
4614
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004615 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4616 * expr(expr). */
4617 if (ret == OK)
4618 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619
4620 /*
4621 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4622 */
4623 if (ret == OK && evaluate && end_leader > start_leader)
4624 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004625 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004626 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004627#ifdef FEAT_FLOAT
4628 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004629
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004630 if (rettv->v_type == VAR_FLOAT)
4631 f = rettv->vval.v_float;
4632 else
4633#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004634 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004635 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004637 clear_tv(rettv);
4638 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004640 else
4641 {
4642 while (end_leader > start_leader)
4643 {
4644 --end_leader;
4645 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004646 {
4647#ifdef FEAT_FLOAT
4648 if (rettv->v_type == VAR_FLOAT)
4649 f = !f;
4650 else
4651#endif
4652 val = !val;
4653 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004654 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004655 {
4656#ifdef FEAT_FLOAT
4657 if (rettv->v_type == VAR_FLOAT)
4658 f = -f;
4659 else
4660#endif
4661 val = -val;
4662 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004663 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004664#ifdef FEAT_FLOAT
4665 if (rettv->v_type == VAR_FLOAT)
4666 {
4667 clear_tv(rettv);
4668 rettv->vval.v_float = f;
4669 }
4670 else
4671#endif
4672 {
4673 clear_tv(rettv);
4674 rettv->v_type = VAR_NUMBER;
4675 rettv->vval.v_number = val;
4676 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 }
4679
4680 return ret;
4681}
4682
4683/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004684 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4685 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004686 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4687 */
4688 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004689eval_index(
4690 char_u **arg,
4691 typval_T *rettv,
4692 int evaluate,
4693 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004694{
4695 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004696 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004697 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004698 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004699 long len = -1;
4700 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004701 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004702 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004703
Bram Moolenaara03f2332016-02-06 18:09:59 +01004704 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004705 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004706 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004707 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004708 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004709 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004710 return FAIL;
4711 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004712#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004713 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004714 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004715 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004716#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004717 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004718 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004719 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004720 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004721 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004722 return FAIL;
4723 case VAR_UNKNOWN:
4724 if (evaluate)
4725 return FAIL;
4726 /* FALLTHROUGH */
4727
4728 case VAR_STRING:
4729 case VAR_NUMBER:
4730 case VAR_LIST:
4731 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004732 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004733 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004734 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004735
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004736 init_tv(&var1);
4737 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004738 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004739 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004740 /*
4741 * dict.name
4742 */
4743 key = *arg + 1;
4744 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4745 ;
4746 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004747 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004748 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004749 }
4750 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004751 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004752 /*
4753 * something[idx]
4754 *
4755 * Get the (first) variable from inside the [].
4756 */
4757 *arg = skipwhite(*arg + 1);
4758 if (**arg == ':')
4759 empty1 = TRUE;
4760 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4761 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004762 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004763 {
4764 /* not a number or string */
4765 clear_tv(&var1);
4766 return FAIL;
4767 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004768
4769 /*
4770 * Get the second variable from inside the [:].
4771 */
4772 if (**arg == ':')
4773 {
4774 range = TRUE;
4775 *arg = skipwhite(*arg + 1);
4776 if (**arg == ']')
4777 empty2 = TRUE;
4778 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4779 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004780 if (!empty1)
4781 clear_tv(&var1);
4782 return FAIL;
4783 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004784 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004785 {
4786 /* not a number or string */
4787 if (!empty1)
4788 clear_tv(&var1);
4789 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004790 return FAIL;
4791 }
4792 }
4793
4794 /* Check for the ']'. */
4795 if (**arg != ']')
4796 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004797 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004798 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004799 clear_tv(&var1);
4800 if (range)
4801 clear_tv(&var2);
4802 return FAIL;
4803 }
4804 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004805 }
4806
4807 if (evaluate)
4808 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004809 n1 = 0;
4810 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004811 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004812 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004813 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004814 }
4815 if (range)
4816 {
4817 if (empty2)
4818 n2 = -1;
4819 else
4820 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004821 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004822 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004823 }
4824 }
4825
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004826 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004827 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004828 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004829 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004830 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004831 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004832 case VAR_SPECIAL:
4833 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004834 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004835 break; /* not evaluating, skipping over subscript */
4836
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004837 case VAR_NUMBER:
4838 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004839 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004840 len = (long)STRLEN(s);
4841 if (range)
4842 {
4843 /* The resulting variable is a substring. If the indexes
4844 * are out of range the result is empty. */
4845 if (n1 < 0)
4846 {
4847 n1 = len + n1;
4848 if (n1 < 0)
4849 n1 = 0;
4850 }
4851 if (n2 < 0)
4852 n2 = len + n2;
4853 else if (n2 >= len)
4854 n2 = len;
4855 if (n1 >= len || n2 < 0 || n1 > n2)
4856 s = NULL;
4857 else
4858 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4859 }
4860 else
4861 {
4862 /* The resulting variable is a string of a single
4863 * character. If the index is too big or negative the
4864 * result is empty. */
4865 if (n1 >= len || n1 < 0)
4866 s = NULL;
4867 else
4868 s = vim_strnsave(s + n1, 1);
4869 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004870 clear_tv(rettv);
4871 rettv->v_type = VAR_STRING;
4872 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004873 break;
4874
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004875 case VAR_BLOB:
4876 len = blob_len(rettv->vval.v_blob);
4877 if (range)
4878 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01004879 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004880 // are out of range the result is empty.
4881 if (n1 < 0)
4882 {
4883 n1 = len + n1;
4884 if (n1 < 0)
4885 n1 = 0;
4886 }
4887 if (n2 < 0)
4888 n2 = len + n2;
4889 else if (n2 >= len)
4890 n2 = len - 1;
4891 if (n1 >= len || n2 < 0 || n1 > n2)
4892 {
4893 clear_tv(rettv);
4894 rettv->v_type = VAR_BLOB;
4895 rettv->vval.v_blob = NULL;
4896 }
4897 else
4898 {
4899 blob_T *blob = blob_alloc();
4900
4901 if (blob != NULL)
4902 {
4903 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
4904 {
4905 blob_free(blob);
4906 return FAIL;
4907 }
4908 blob->bv_ga.ga_len = n2 - n1 + 1;
4909 for (i = n1; i <= n2; i++)
4910 blob_set(blob, i - n1,
4911 blob_get(rettv->vval.v_blob, i));
4912
4913 clear_tv(rettv);
4914 rettv_blob_set(rettv, blob);
4915 }
4916 }
4917 }
4918 else
4919 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01004920 // The resulting variable is a byte value.
4921 // If the index is too big or negative that is an error.
4922 if (n1 < 0)
4923 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004924 if (n1 < len && n1 >= 0)
4925 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01004926 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004927
4928 clear_tv(rettv);
4929 rettv->v_type = VAR_NUMBER;
4930 rettv->vval.v_number = v;
4931 }
4932 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004933 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004934 }
4935 break;
4936
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004937 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004938 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004939 if (n1 < 0)
4940 n1 = len + n1;
4941 if (!empty1 && (n1 < 0 || n1 >= len))
4942 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004943 /* For a range we allow invalid values and return an empty
4944 * list. A list index out of range is an error. */
4945 if (!range)
4946 {
4947 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004948 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004949 return FAIL;
4950 }
4951 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004952 }
4953 if (range)
4954 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004955 list_T *l;
4956 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004957
4958 if (n2 < 0)
4959 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004960 else if (n2 >= len)
4961 n2 = len - 1;
4962 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004963 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004964 l = list_alloc();
4965 if (l == NULL)
4966 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004967 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004968 n1 <= n2; ++n1)
4969 {
4970 if (list_append_tv(l, &item->li_tv) == FAIL)
4971 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004972 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004973 return FAIL;
4974 }
4975 item = item->li_next;
4976 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004977 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004978 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004979 }
4980 else
4981 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004982 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004983 clear_tv(rettv);
4984 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004985 }
4986 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004987
4988 case VAR_DICT:
4989 if (range)
4990 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004991 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004992 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004993 if (len == -1)
4994 clear_tv(&var1);
4995 return FAIL;
4996 }
4997 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004998 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004999
5000 if (len == -1)
5001 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005002 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005003 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005004 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005005 clear_tv(&var1);
5006 return FAIL;
5007 }
5008 }
5009
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005010 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005011
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005012 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005013 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005014 if (len == -1)
5015 clear_tv(&var1);
5016 if (item == NULL)
5017 return FAIL;
5018
5019 copy_tv(&item->di_tv, &var1);
5020 clear_tv(rettv);
5021 *rettv = var1;
5022 }
5023 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005024 }
5025 }
5026
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005027 return OK;
5028}
5029
5030/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 * Get an option value.
5032 * "arg" points to the '&' or '+' before the option name.
5033 * "arg" is advanced to character after the option name.
5034 * Return OK or FAIL.
5035 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005036 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005037get_option_tv(
5038 char_u **arg,
5039 typval_T *rettv, /* when NULL, only check if option exists */
5040 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041{
5042 char_u *option_end;
5043 long numval;
5044 char_u *stringval;
5045 int opt_type;
5046 int c;
5047 int working = (**arg == '+'); /* has("+option") */
5048 int ret = OK;
5049 int opt_flags;
5050
5051 /*
5052 * Isolate the option name and find its value.
5053 */
5054 option_end = find_option_end(arg, &opt_flags);
5055 if (option_end == NULL)
5056 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005057 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005058 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 return FAIL;
5060 }
5061
5062 if (!evaluate)
5063 {
5064 *arg = option_end;
5065 return OK;
5066 }
5067
5068 c = *option_end;
5069 *option_end = NUL;
5070 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005071 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072
5073 if (opt_type == -3) /* invalid name */
5074 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005075 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005076 semsg(_("E113: Unknown option: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 ret = FAIL;
5078 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005079 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 {
5081 if (opt_type == -2) /* hidden string option */
5082 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005083 rettv->v_type = VAR_STRING;
5084 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 }
5086 else if (opt_type == -1) /* hidden number option */
5087 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005088 rettv->v_type = VAR_NUMBER;
5089 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 }
5091 else if (opt_type == 1) /* number option */
5092 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005093 rettv->v_type = VAR_NUMBER;
5094 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 }
5096 else /* string option */
5097 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005098 rettv->v_type = VAR_STRING;
5099 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 }
5101 }
5102 else if (working && (opt_type == -2 || opt_type == -1))
5103 ret = FAIL;
5104
5105 *option_end = c; /* put back for error messages */
5106 *arg = option_end;
5107
5108 return ret;
5109}
5110
5111/*
5112 * Allocate a variable for a string constant.
5113 * Return OK or FAIL.
5114 */
5115 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005116get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117{
5118 char_u *p;
5119 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 int extra = 0;
5121
5122 /*
5123 * Find the end of the string, skipping backslashed characters.
5124 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005125 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 {
5127 if (*p == '\\' && p[1] != NUL)
5128 {
5129 ++p;
5130 /* A "\<x>" form occupies at least 4 characters, and produces up
5131 * to 6 characters: reserve space for 2 extra */
5132 if (*p == '<')
5133 extra += 2;
5134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 }
5136
5137 if (*p != '"')
5138 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005139 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 return FAIL;
5141 }
5142
5143 /* If only parsing, set *arg and return here */
5144 if (!evaluate)
5145 {
5146 *arg = p + 1;
5147 return OK;
5148 }
5149
5150 /*
5151 * Copy the string into allocated memory, handling backslashed
5152 * characters.
5153 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005154 name = alloc(p - *arg + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 if (name == NULL)
5156 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005157 rettv->v_type = VAR_STRING;
5158 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159
Bram Moolenaar8c711452005-01-14 21:53:12 +00005160 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 {
5162 if (*p == '\\')
5163 {
5164 switch (*++p)
5165 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005166 case 'b': *name++ = BS; ++p; break;
5167 case 'e': *name++ = ESC; ++p; break;
5168 case 'f': *name++ = FF; ++p; break;
5169 case 'n': *name++ = NL; ++p; break;
5170 case 'r': *name++ = CAR; ++p; break;
5171 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172
5173 case 'X': /* hex: "\x1", "\x12" */
5174 case 'x':
5175 case 'u': /* Unicode: "\u0023" */
5176 case 'U':
5177 if (vim_isxdigit(p[1]))
5178 {
5179 int n, nr;
5180 int c = toupper(*p);
5181
5182 if (c == 'X')
5183 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005184 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005186 else
5187 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 nr = 0;
5189 while (--n >= 0 && vim_isxdigit(p[1]))
5190 {
5191 ++p;
5192 nr = (nr << 4) + hex2nr(*p);
5193 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005194 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 /* For "\u" store the number according to
5196 * 'encoding'. */
5197 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005198 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005200 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 break;
5203
5204 /* octal: "\1", "\12", "\123" */
5205 case '0':
5206 case '1':
5207 case '2':
5208 case '3':
5209 case '4':
5210 case '5':
5211 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005212 case '7': *name = *p++ - '0';
5213 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005215 *name = (*name << 3) + *p++ - '0';
5216 if (*p >= '0' && *p <= '7')
5217 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005219 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 break;
5221
5222 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005223 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 if (extra != 0)
5225 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005226 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 break;
5228 }
5229 /* FALLTHROUGH */
5230
Bram Moolenaar8c711452005-01-14 21:53:12 +00005231 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 break;
5233 }
5234 }
5235 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005236 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005239 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02005240 if (*p != NUL) /* just in case */
5241 ++p;
5242 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 return OK;
5245}
5246
5247/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005248 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 * Return OK or FAIL.
5250 */
5251 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005252get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253{
5254 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005255 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005256 int reduce = 0;
5257
5258 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005259 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005260 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005261 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005262 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005263 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005264 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005265 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005266 break;
5267 ++reduce;
5268 ++p;
5269 }
5270 }
5271
Bram Moolenaar8c711452005-01-14 21:53:12 +00005272 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005273 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005274 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005275 return FAIL;
5276 }
5277
Bram Moolenaar8c711452005-01-14 21:53:12 +00005278 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005279 if (!evaluate)
5280 {
5281 *arg = p + 1;
5282 return OK;
5283 }
5284
5285 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005286 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005287 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005288 str = alloc((p - *arg) - reduce);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005289 if (str == NULL)
5290 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005291 rettv->v_type = VAR_STRING;
5292 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005293
Bram Moolenaar8c711452005-01-14 21:53:12 +00005294 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005295 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005296 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005297 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005298 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005299 break;
5300 ++p;
5301 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005302 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005303 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005304 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005305 *arg = p + 1;
5306
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005307 return OK;
5308}
5309
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005310/*
5311 * Return the function name of the partial.
5312 */
5313 char_u *
5314partial_name(partial_T *pt)
5315{
5316 if (pt->pt_name != NULL)
5317 return pt->pt_name;
5318 return pt->pt_func->uf_name;
5319}
5320
Bram Moolenaarddecc252016-04-06 22:59:37 +02005321 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005322partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005323{
5324 int i;
5325
5326 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005327 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005328 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005329 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005330 if (pt->pt_name != NULL)
5331 {
5332 func_unref(pt->pt_name);
5333 vim_free(pt->pt_name);
5334 }
5335 else
5336 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005337 vim_free(pt);
5338}
5339
5340/*
5341 * Unreference a closure: decrement the reference count and free it when it
5342 * becomes zero.
5343 */
5344 void
5345partial_unref(partial_T *pt)
5346{
5347 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005348 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005349}
5350
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005351static int tv_equal_recurse_limit;
5352
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005353 static int
5354func_equal(
5355 typval_T *tv1,
5356 typval_T *tv2,
5357 int ic) /* ignore case */
5358{
5359 char_u *s1, *s2;
5360 dict_T *d1, *d2;
5361 int a1, a2;
5362 int i;
5363
5364 /* empty and NULL function name considered the same */
5365 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005366 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005367 if (s1 != NULL && *s1 == NUL)
5368 s1 = NULL;
5369 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005370 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005371 if (s2 != NULL && *s2 == NUL)
5372 s2 = NULL;
5373 if (s1 == NULL || s2 == NULL)
5374 {
5375 if (s1 != s2)
5376 return FALSE;
5377 }
5378 else if (STRCMP(s1, s2) != 0)
5379 return FALSE;
5380
5381 /* empty dict and NULL dict is different */
5382 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5383 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5384 if (d1 == NULL || d2 == NULL)
5385 {
5386 if (d1 != d2)
5387 return FALSE;
5388 }
5389 else if (!dict_equal(d1, d2, ic, TRUE))
5390 return FALSE;
5391
5392 /* empty list and no list considered the same */
5393 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5394 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5395 if (a1 != a2)
5396 return FALSE;
5397 for (i = 0; i < a1; ++i)
5398 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5399 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5400 return FALSE;
5401
5402 return TRUE;
5403}
5404
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005405/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005406 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005407 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005408 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005409 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005410 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005411tv_equal(
5412 typval_T *tv1,
5413 typval_T *tv2,
5414 int ic, /* ignore case */
5415 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005416{
5417 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005418 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005419 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005420 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005421
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005422 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005423 * recursiveness to a limit. We guess they are equal then.
5424 * A fixed limit has the problem of still taking an awful long time.
5425 * Reduce the limit every time running into it. That should work fine for
5426 * deeply linked structures that are not recursively linked and catch
5427 * recursiveness quickly. */
5428 if (!recursive)
5429 tv_equal_recurse_limit = 1000;
5430 if (recursive_cnt >= tv_equal_recurse_limit)
5431 {
5432 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005433 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005434 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005435
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005436 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5437 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005438 if ((tv1->v_type == VAR_FUNC
5439 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5440 && (tv2->v_type == VAR_FUNC
5441 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5442 {
5443 ++recursive_cnt;
5444 r = func_equal(tv1, tv2, ic);
5445 --recursive_cnt;
5446 return r;
5447 }
5448
5449 if (tv1->v_type != tv2->v_type)
5450 return FALSE;
5451
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005452 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005453 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005454 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005455 ++recursive_cnt;
5456 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5457 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005458 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005459
5460 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005461 ++recursive_cnt;
5462 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5463 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005464 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005465
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005466 case VAR_BLOB:
5467 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
5468
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005469 case VAR_NUMBER:
5470 return tv1->vval.v_number == tv2->vval.v_number;
5471
5472 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005473 s1 = tv_get_string_buf(tv1, buf1);
5474 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005475 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005476
5477 case VAR_SPECIAL:
5478 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005479
5480 case VAR_FLOAT:
5481#ifdef FEAT_FLOAT
5482 return tv1->vval.v_float == tv2->vval.v_float;
5483#endif
5484 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005485#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005486 return tv1->vval.v_job == tv2->vval.v_job;
5487#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005488 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005489#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005490 return tv1->vval.v_channel == tv2->vval.v_channel;
5491#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005492 case VAR_FUNC:
5493 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005494 case VAR_UNKNOWN:
5495 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005496 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005497
Bram Moolenaara03f2332016-02-06 18:09:59 +01005498 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5499 * does not equal anything, not even itself. */
5500 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005501}
5502
5503/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005504 * Return the next (unique) copy ID.
5505 * Used for serializing nested structures.
5506 */
5507 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005508get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005509{
5510 current_copyID += COPYID_INC;
5511 return current_copyID;
5512}
5513
5514/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005515 * Garbage collection for lists and dictionaries.
5516 *
5517 * We use reference counts to be able to free most items right away when they
5518 * are no longer used. But for composite items it's possible that it becomes
5519 * unused while the reference count is > 0: When there is a recursive
5520 * reference. Example:
5521 * :let l = [1, 2, 3]
5522 * :let d = {9: l}
5523 * :let l[1] = d
5524 *
5525 * Since this is quite unusual we handle this with garbage collection: every
5526 * once in a while find out which lists and dicts are not referenced from any
5527 * variable.
5528 *
5529 * Here is a good reference text about garbage collection (refers to Python
5530 * but it applies to all reference-counting mechanisms):
5531 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005532 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005533
5534/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005535 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005536 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005537 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005538 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005539 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005540garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005541{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005542 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005543 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005544 buf_T *buf;
5545 win_T *wp;
5546 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005547 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005548 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005549
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005550 if (!testing)
5551 {
5552 /* Only do this once. */
5553 want_garbage_collect = FALSE;
5554 may_garbage_collect = FALSE;
5555 garbage_collect_at_exit = FALSE;
5556 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005557
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005558 /* We advance by two because we add one for items referenced through
5559 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005560 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005561
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005562 /*
5563 * 1. Go through all accessible variables and mark all lists and dicts
5564 * with copyID.
5565 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005566
5567 /* Don't free variables in the previous_funccal list unless they are only
5568 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005569 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005570 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005571
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005572 /* script-local variables */
5573 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005574 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005575
5576 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005577 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005578 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5579 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005580
5581 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005582 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005583 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5584 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005585 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005586 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5587 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005588#ifdef FEAT_TEXT_PROP
5589 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
5590 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5591 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005592 FOR_ALL_TABPAGES(tp)
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02005593 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005594 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5595 NULL, NULL);
5596#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005597
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005598 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005599 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005600 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5601 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005602 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005603 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005604
5605 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005606 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005607
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005608 /* named functions (matters for closures) */
5609 abort = abort || set_ref_in_functions(copyID);
5610
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005611 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005612 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005613
Bram Moolenaard812df62008-11-09 12:46:09 +00005614 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005615 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005616
Bram Moolenaar1dced572012-04-05 16:54:08 +02005617#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005618 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005619#endif
5620
Bram Moolenaardb913952012-06-29 12:54:53 +02005621#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005622 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005623#endif
5624
5625#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005626 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005627#endif
5628
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005629#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005630 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005631 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005632#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005633#ifdef FEAT_NETBEANS_INTG
5634 abort = abort || set_ref_in_nb_channel(copyID);
5635#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005636
Bram Moolenaare3188e22016-05-31 21:13:04 +02005637#ifdef FEAT_TIMERS
5638 abort = abort || set_ref_in_timer(copyID);
5639#endif
5640
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005641#ifdef FEAT_QUICKFIX
5642 abort = abort || set_ref_in_quickfix(copyID);
5643#endif
5644
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005645#ifdef FEAT_TERMINAL
5646 abort = abort || set_ref_in_term(copyID);
5647#endif
5648
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005649 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005650 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005651 /*
5652 * 2. Free lists and dictionaries that are not referenced.
5653 */
5654 did_free = free_unref_items(copyID);
5655
5656 /*
5657 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005658 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005659 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005660 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005661 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005662 else if (p_verbose > 0)
5663 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005664 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005665 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005666
5667 return did_free;
5668}
5669
5670/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005671 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005672 */
5673 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005674free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005675{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005676 int did_free = FALSE;
5677
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005678 /* Let all "free" functions know that we are here. This means no
5679 * dictionaries, lists, channels or jobs are to be freed, because we will
5680 * do that here. */
5681 in_free_unref_items = TRUE;
5682
5683 /*
5684 * PASS 1: free the contents of the items. We don't free the items
5685 * themselves yet, so that it is possible to decrement refcount counters
5686 */
5687
Bram Moolenaarcd524592016-07-17 14:57:05 +02005688 /* Go through the list of dicts and free items without the copyID. */
5689 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005690
Bram Moolenaarda861d62016-07-17 15:46:27 +02005691 /* Go through the list of lists and free items without the copyID. */
5692 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005693
5694#ifdef FEAT_JOB_CHANNEL
5695 /* Go through the list of jobs and free items without the copyID. This
5696 * must happen before doing channels, because jobs refer to channels, but
5697 * the reference from the channel to the job isn't tracked. */
5698 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5699
5700 /* Go through the list of channels and free items without the copyID. */
5701 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5702#endif
5703
5704 /*
5705 * PASS 2: free the items themselves.
5706 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005707 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005708 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005709
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005710#ifdef FEAT_JOB_CHANNEL
5711 /* Go through the list of jobs and free items without the copyID. This
5712 * must happen before doing channels, because jobs refer to channels, but
5713 * the reference from the channel to the job isn't tracked. */
5714 free_unused_jobs(copyID, COPYID_MASK);
5715
5716 /* Go through the list of channels and free items without the copyID. */
5717 free_unused_channels(copyID, COPYID_MASK);
5718#endif
5719
5720 in_free_unref_items = FALSE;
5721
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005722 return did_free;
5723}
5724
5725/*
5726 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005727 * "list_stack" is used to add lists to be marked. Can be NULL.
5728 *
5729 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005730 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005731 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005732set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005733{
5734 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005735 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005736 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005737 hashtab_T *cur_ht;
5738 ht_stack_T *ht_stack = NULL;
5739 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005740
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005741 cur_ht = ht;
5742 for (;;)
5743 {
5744 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005745 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005746 /* Mark each item in the hashtab. If the item contains a hashtab
5747 * it is added to ht_stack, if it contains a list it is added to
5748 * list_stack. */
5749 todo = (int)cur_ht->ht_used;
5750 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5751 if (!HASHITEM_EMPTY(hi))
5752 {
5753 --todo;
5754 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5755 &ht_stack, list_stack);
5756 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005757 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005758
5759 if (ht_stack == NULL)
5760 break;
5761
5762 /* take an item from the stack */
5763 cur_ht = ht_stack->ht;
5764 tempitem = ht_stack;
5765 ht_stack = ht_stack->prev;
5766 free(tempitem);
5767 }
5768
5769 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005770}
5771
5772/*
5773 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005774 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5775 *
5776 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005777 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005778 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005779set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005780{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005781 listitem_T *li;
5782 int abort = FALSE;
5783 list_T *cur_l;
5784 list_stack_T *list_stack = NULL;
5785 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005786
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005787 cur_l = l;
5788 for (;;)
5789 {
5790 if (!abort)
5791 /* Mark each item in the list. If the item contains a hashtab
5792 * it is added to ht_stack, if it contains a list it is added to
5793 * list_stack. */
5794 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5795 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5796 ht_stack, &list_stack);
5797 if (list_stack == NULL)
5798 break;
5799
5800 /* take an item from the stack */
5801 cur_l = list_stack->list;
5802 tempitem = list_stack;
5803 list_stack = list_stack->prev;
5804 free(tempitem);
5805 }
5806
5807 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005808}
5809
5810/*
5811 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005812 * "list_stack" is used to add lists to be marked. Can be NULL.
5813 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5814 *
5815 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005816 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005817 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005818set_ref_in_item(
5819 typval_T *tv,
5820 int copyID,
5821 ht_stack_T **ht_stack,
5822 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005823{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005824 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005825
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005826 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005827 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005828 dict_T *dd = tv->vval.v_dict;
5829
Bram Moolenaara03f2332016-02-06 18:09:59 +01005830 if (dd != NULL && dd->dv_copyID != copyID)
5831 {
5832 /* Didn't see this dict yet. */
5833 dd->dv_copyID = copyID;
5834 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005835 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005836 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5837 }
5838 else
5839 {
5840 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5841 if (newitem == NULL)
5842 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005843 else
5844 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005845 newitem->ht = &dd->dv_hashtab;
5846 newitem->prev = *ht_stack;
5847 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005848 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005849 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005850 }
5851 }
5852 else if (tv->v_type == VAR_LIST)
5853 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005854 list_T *ll = tv->vval.v_list;
5855
Bram Moolenaara03f2332016-02-06 18:09:59 +01005856 if (ll != NULL && ll->lv_copyID != copyID)
5857 {
5858 /* Didn't see this list yet. */
5859 ll->lv_copyID = copyID;
5860 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005861 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005862 abort = set_ref_in_list(ll, copyID, ht_stack);
5863 }
5864 else
5865 {
5866 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005867 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005868 if (newitem == NULL)
5869 abort = TRUE;
5870 else
5871 {
5872 newitem->list = ll;
5873 newitem->prev = *list_stack;
5874 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005875 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005876 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005877 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005878 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005879 else if (tv->v_type == VAR_FUNC)
5880 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005881 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005882 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005883 else if (tv->v_type == VAR_PARTIAL)
5884 {
5885 partial_T *pt = tv->vval.v_partial;
5886 int i;
5887
5888 /* A partial does not have a copyID, because it cannot contain itself.
5889 */
5890 if (pt != NULL)
5891 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005892 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005893
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005894 if (pt->pt_dict != NULL)
5895 {
5896 typval_T dtv;
5897
5898 dtv.v_type = VAR_DICT;
5899 dtv.vval.v_dict = pt->pt_dict;
5900 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5901 }
5902
5903 for (i = 0; i < pt->pt_argc; ++i)
5904 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5905 ht_stack, list_stack);
5906 }
5907 }
5908#ifdef FEAT_JOB_CHANNEL
5909 else if (tv->v_type == VAR_JOB)
5910 {
5911 job_T *job = tv->vval.v_job;
5912 typval_T dtv;
5913
5914 if (job != NULL && job->jv_copyID != copyID)
5915 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005916 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005917 if (job->jv_channel != NULL)
5918 {
5919 dtv.v_type = VAR_CHANNEL;
5920 dtv.vval.v_channel = job->jv_channel;
5921 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5922 }
5923 if (job->jv_exit_partial != NULL)
5924 {
5925 dtv.v_type = VAR_PARTIAL;
5926 dtv.vval.v_partial = job->jv_exit_partial;
5927 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5928 }
5929 }
5930 }
5931 else if (tv->v_type == VAR_CHANNEL)
5932 {
5933 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005934 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005935 typval_T dtv;
5936 jsonq_T *jq;
5937 cbq_T *cq;
5938
5939 if (ch != NULL && ch->ch_copyID != copyID)
5940 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005941 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005942 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005943 {
5944 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5945 jq = jq->jq_next)
5946 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5947 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5948 cq = cq->cq_next)
5949 if (cq->cq_partial != NULL)
5950 {
5951 dtv.v_type = VAR_PARTIAL;
5952 dtv.vval.v_partial = cq->cq_partial;
5953 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5954 }
5955 if (ch->ch_part[part].ch_partial != NULL)
5956 {
5957 dtv.v_type = VAR_PARTIAL;
5958 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5959 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5960 }
5961 }
5962 if (ch->ch_partial != NULL)
5963 {
5964 dtv.v_type = VAR_PARTIAL;
5965 dtv.vval.v_partial = ch->ch_partial;
5966 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5967 }
5968 if (ch->ch_close_partial != NULL)
5969 {
5970 dtv.v_type = VAR_PARTIAL;
5971 dtv.vval.v_partial = ch->ch_close_partial;
5972 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5973 }
5974 }
5975 }
5976#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005977 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005978}
5979
Bram Moolenaar17a13432016-01-24 14:22:10 +01005980 static char *
5981get_var_special_name(int nr)
5982{
5983 switch (nr)
5984 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005985 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005986 case VVAL_TRUE: return "v:true";
5987 case VVAL_NONE: return "v:none";
5988 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005989 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01005990 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01005991 return "42";
5992}
5993
Bram Moolenaar8c711452005-01-14 21:53:12 +00005994/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005995 * Return a string with the string representation of a variable.
5996 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005997 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005998 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005999 * When both "echo_style" and "composite_val" are FALSE, put quotes around
6000 * stings as "string()", otherwise does not put quotes around strings, as
6001 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006002 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6003 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006004 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006005 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006006 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006007echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006008 typval_T *tv,
6009 char_u **tofree,
6010 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006011 int copyID,
6012 int echo_style,
6013 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006014 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006015{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006016 static int recurse = 0;
6017 char_u *r = NULL;
6018
Bram Moolenaar33570922005-01-25 22:26:29 +00006019 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006020 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006021 if (!did_echo_string_emsg)
6022 {
6023 /* Only give this message once for a recursive call to avoid
6024 * flooding the user with errors. And stop iterating over lists
6025 * and dicts. */
6026 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006027 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006028 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006029 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006030 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006031 }
6032 ++recurse;
6033
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006034 switch (tv->v_type)
6035 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006036 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006037 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006038 {
6039 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02006040 r = tv->vval.v_string;
6041 if (r == NULL)
6042 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006043 }
6044 else
6045 {
6046 *tofree = string_quote(tv->vval.v_string, FALSE);
6047 r = *tofree;
6048 }
6049 break;
6050
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006051 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006052 if (echo_style)
6053 {
6054 *tofree = NULL;
6055 r = tv->vval.v_string;
6056 }
6057 else
6058 {
6059 *tofree = string_quote(tv->vval.v_string, TRUE);
6060 r = *tofree;
6061 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006062 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006063
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006064 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006065 {
6066 partial_T *pt = tv->vval.v_partial;
6067 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006068 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006069 garray_T ga;
6070 int i;
6071 char_u *tf;
6072
6073 ga_init2(&ga, 1, 100);
6074 ga_concat(&ga, (char_u *)"function(");
6075 if (fname != NULL)
6076 {
6077 ga_concat(&ga, fname);
6078 vim_free(fname);
6079 }
6080 if (pt != NULL && pt->pt_argc > 0)
6081 {
6082 ga_concat(&ga, (char_u *)", [");
6083 for (i = 0; i < pt->pt_argc; ++i)
6084 {
6085 if (i > 0)
6086 ga_concat(&ga, (char_u *)", ");
6087 ga_concat(&ga,
6088 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6089 vim_free(tf);
6090 }
6091 ga_concat(&ga, (char_u *)"]");
6092 }
6093 if (pt != NULL && pt->pt_dict != NULL)
6094 {
6095 typval_T dtv;
6096
6097 ga_concat(&ga, (char_u *)", ");
6098 dtv.v_type = VAR_DICT;
6099 dtv.vval.v_dict = pt->pt_dict;
6100 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6101 vim_free(tf);
6102 }
6103 ga_concat(&ga, (char_u *)")");
6104
6105 *tofree = ga.ga_data;
6106 r = *tofree;
6107 break;
6108 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006109
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006110 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006111 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006112 break;
6113
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006114 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006115 if (tv->vval.v_list == NULL)
6116 {
6117 *tofree = NULL;
6118 r = NULL;
6119 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006120 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6121 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006122 {
6123 *tofree = NULL;
6124 r = (char_u *)"[...]";
6125 }
6126 else
6127 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006128 int old_copyID = tv->vval.v_list->lv_copyID;
6129
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006130 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006131 *tofree = list2string(tv, copyID, restore_copyID);
6132 if (restore_copyID)
6133 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006134 r = *tofree;
6135 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006136 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006137
Bram Moolenaar8c711452005-01-14 21:53:12 +00006138 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006139 if (tv->vval.v_dict == NULL)
6140 {
6141 *tofree = NULL;
6142 r = NULL;
6143 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006144 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6145 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006146 {
6147 *tofree = NULL;
6148 r = (char_u *)"{...}";
6149 }
6150 else
6151 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006152 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006153 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006154 *tofree = dict2string(tv, copyID, restore_copyID);
6155 if (restore_copyID)
6156 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006157 r = *tofree;
6158 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006159 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006160
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006161 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006162 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006163 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006164 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006165 break;
6166
Bram Moolenaar835dc632016-02-07 14:27:38 +01006167 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006168 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006169 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006170 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006171 if (composite_val)
6172 {
6173 *tofree = string_quote(r, FALSE);
6174 r = *tofree;
6175 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006176 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006177
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006178 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006179#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006180 *tofree = NULL;
6181 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6182 r = numbuf;
6183 break;
6184#endif
6185
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006186 case VAR_SPECIAL:
6187 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006188 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006189 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006190 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006191
Bram Moolenaar8502c702014-06-17 12:51:16 +02006192 if (--recurse == 0)
6193 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006194 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006195}
6196
6197/*
6198 * Return a string with the string representation of a variable.
6199 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6200 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006201 * Does not put quotes around strings, as ":echo" displays values.
6202 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6203 * May return NULL.
6204 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006205 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006206echo_string(
6207 typval_T *tv,
6208 char_u **tofree,
6209 char_u *numbuf,
6210 int copyID)
6211{
6212 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6213}
6214
6215/*
6216 * Return a string with the string representation of a variable.
6217 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6218 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006219 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006220 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006221 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02006222 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006223tv2string(
6224 typval_T *tv,
6225 char_u **tofree,
6226 char_u *numbuf,
6227 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006228{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006229 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006230}
6231
6232/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006233 * Return string "str" in ' quotes, doubling ' characters.
6234 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006235 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006236 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006237 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006238string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006239{
Bram Moolenaar33570922005-01-25 22:26:29 +00006240 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006241 char_u *p, *r, *s;
6242
Bram Moolenaar33570922005-01-25 22:26:29 +00006243 len = (function ? 13 : 3);
6244 if (str != NULL)
6245 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006246 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006247 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00006248 if (*p == '\'')
6249 ++len;
6250 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006251 s = r = alloc(len);
6252 if (r != NULL)
6253 {
6254 if (function)
6255 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006256 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006257 r += 10;
6258 }
6259 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006260 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006261 if (str != NULL)
6262 for (p = str; *p != NUL; )
6263 {
6264 if (*p == '\'')
6265 *r++ = '\'';
6266 MB_COPY_CHAR(p, r);
6267 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006268 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006269 if (function)
6270 *r++ = ')';
6271 *r++ = NUL;
6272 }
6273 return s;
6274}
6275
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006276#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006277/*
6278 * Convert the string "text" to a floating point number.
6279 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
6280 * this always uses a decimal point.
6281 * Returns the length of the text that was consumed.
6282 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006283 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006284string2float(
6285 char_u *text,
6286 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006287{
6288 char *s = (char *)text;
6289 float_T f;
6290
Bram Moolenaar62473612017-01-08 19:25:40 +01006291 /* MS-Windows does not deal with "inf" and "nan" properly. */
6292 if (STRNICMP(text, "inf", 3) == 0)
6293 {
6294 *value = INFINITY;
6295 return 3;
6296 }
6297 if (STRNICMP(text, "-inf", 3) == 0)
6298 {
6299 *value = -INFINITY;
6300 return 4;
6301 }
6302 if (STRNICMP(text, "nan", 3) == 0)
6303 {
6304 *value = NAN;
6305 return 3;
6306 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006307 f = strtod(s, &s);
6308 *value = f;
6309 return (int)((char_u *)s - text);
6310}
6311#endif
6312
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006313/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 * Get the value of an environment variable.
6315 * "arg" is pointing to the '$'. It is advanced to after the name.
6316 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006317 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 */
6319 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006320get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321{
6322 char_u *string = NULL;
6323 int len;
6324 int cc;
6325 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006326 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327
6328 ++*arg;
6329 name = *arg;
6330 len = get_env_len(arg);
6331 if (evaluate)
6332 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006333 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006334 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006335
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006336 cc = name[len];
6337 name[len] = NUL;
6338 /* first try vim_getenv(), fast for normal environment vars */
6339 string = vim_getenv(name, &mustfree);
6340 if (string != NULL && *string != NUL)
6341 {
6342 if (!mustfree)
6343 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006345 else
6346 {
6347 if (mustfree)
6348 vim_free(string);
6349
6350 /* next try expanding things like $VIM and ${HOME} */
6351 string = expand_env_save(name - 1);
6352 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01006353 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006354 }
6355 name[len] = cc;
6356
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006357 rettv->v_type = VAR_STRING;
6358 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 }
6360
6361 return OK;
6362}
6363
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006364
6365
6366/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006368 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006370 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006371var2fpos(
6372 typval_T *varp,
6373 int dollar_lnum, /* TRUE when $ is last line */
6374 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006376 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006378 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379
Bram Moolenaara5525202006-03-02 22:52:09 +00006380 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006381 if (varp->v_type == VAR_LIST)
6382 {
6383 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006384 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006385 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006386 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006387
6388 l = varp->vval.v_list;
6389 if (l == NULL)
6390 return NULL;
6391
6392 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006393 pos.lnum = list_find_nr(l, 0L, &error);
6394 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006395 return NULL; /* invalid line number */
6396
6397 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006398 pos.col = list_find_nr(l, 1L, &error);
6399 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006400 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006401 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006402
6403 /* We accept "$" for the column number: last column. */
6404 li = list_find(l, 1L);
6405 if (li != NULL && li->li_tv.v_type == VAR_STRING
6406 && li->li_tv.vval.v_string != NULL
6407 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6408 pos.col = len + 1;
6409
Bram Moolenaara5525202006-03-02 22:52:09 +00006410 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006411 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006412 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006413 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006414
Bram Moolenaara5525202006-03-02 22:52:09 +00006415 /* Get the virtual offset. Defaults to zero. */
6416 pos.coladd = list_find_nr(l, 2L, &error);
6417 if (error)
6418 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006419
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006420 return &pos;
6421 }
6422
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006423 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006424 if (name == NULL)
6425 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006426 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006428 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6429 {
6430 if (VIsual_active)
6431 return &VIsual;
6432 return &curwin->w_cursor;
6433 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006434 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006436 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6438 return NULL;
6439 return pp;
6440 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006441
Bram Moolenaara5525202006-03-02 22:52:09 +00006442 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006443
Bram Moolenaar477933c2007-07-17 14:32:23 +00006444 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006445 {
6446 pos.col = 0;
6447 if (name[1] == '0') /* "w0": first visible line */
6448 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006449 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006450 /* In silent Ex mode topline is zero, but that's not a valid line
6451 * number; use one instead. */
6452 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006453 return &pos;
6454 }
6455 else if (name[1] == '$') /* "w$": last visible line */
6456 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006457 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006458 /* In silent Ex mode botline is zero, return zero then. */
6459 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006460 return &pos;
6461 }
6462 }
6463 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006465 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 {
6467 pos.lnum = curbuf->b_ml.ml_line_count;
6468 pos.col = 0;
6469 }
6470 else
6471 {
6472 pos.lnum = curwin->w_cursor.lnum;
6473 pos.col = (colnr_T)STRLEN(ml_get_curline());
6474 }
6475 return &pos;
6476 }
6477 return NULL;
6478}
6479
6480/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006481 * Convert list in "arg" into a position and optional file number.
6482 * When "fnump" is NULL there is no file number, only 3 items.
6483 * Note that the column is passed on as-is, the caller may want to decrement
6484 * it to use 1 for the first column.
6485 * Return FAIL when conversion is not possible, doesn't check the position for
6486 * validity.
6487 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006488 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006489list2fpos(
6490 typval_T *arg,
6491 pos_T *posp,
6492 int *fnump,
6493 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006494{
6495 list_T *l = arg->vval.v_list;
6496 long i = 0;
6497 long n;
6498
Bram Moolenaar493c1782014-05-28 14:34:46 +02006499 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6500 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006501 if (arg->v_type != VAR_LIST
6502 || l == NULL
6503 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006504 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006505 return FAIL;
6506
6507 if (fnump != NULL)
6508 {
6509 n = list_find_nr(l, i++, NULL); /* fnum */
6510 if (n < 0)
6511 return FAIL;
6512 if (n == 0)
6513 n = curbuf->b_fnum; /* current buffer */
6514 *fnump = n;
6515 }
6516
6517 n = list_find_nr(l, i++, NULL); /* lnum */
6518 if (n < 0)
6519 return FAIL;
6520 posp->lnum = n;
6521
6522 n = list_find_nr(l, i++, NULL); /* col */
6523 if (n < 0)
6524 return FAIL;
6525 posp->col = n;
6526
Bram Moolenaar493c1782014-05-28 14:34:46 +02006527 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006528 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006529 posp->coladd = 0;
6530 else
6531 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006532
Bram Moolenaar493c1782014-05-28 14:34:46 +02006533 if (curswantp != NULL)
6534 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6535
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006536 return OK;
6537}
6538
6539/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 * Get the length of an environment variable name.
6541 * Advance "arg" to the first character after the name.
6542 * Return 0 for error.
6543 */
6544 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006545get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546{
6547 char_u *p;
6548 int len;
6549
6550 for (p = *arg; vim_isIDc(*p); ++p)
6551 ;
6552 if (p == *arg) /* no name found */
6553 return 0;
6554
6555 len = (int)(p - *arg);
6556 *arg = p;
6557 return len;
6558}
6559
6560/*
6561 * Get the length of the name of a function or internal variable.
6562 * "arg" is advanced to the first non-white character after the name.
6563 * Return 0 if something is wrong.
6564 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006565 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006566get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567{
6568 char_u *p;
6569 int len;
6570
6571 /* Find the end of the name. */
6572 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006573 {
6574 if (*p == ':')
6575 {
6576 /* "s:" is start of "s:var", but "n:" is not and can be used in
6577 * slice "[n:]". Also "xx:" is not a namespace. */
6578 len = (int)(p - *arg);
6579 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6580 || len > 1)
6581 break;
6582 }
6583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 if (p == *arg) /* no name found */
6585 return 0;
6586
6587 len = (int)(p - *arg);
6588 *arg = skipwhite(p);
6589
6590 return len;
6591}
6592
6593/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006594 * Get the length of the name of a variable or function.
6595 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006597 * Return -1 if curly braces expansion failed.
6598 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 * If the name contains 'magic' {}'s, expand them and return the
6600 * expanded name in an allocated string via 'alias' - caller must free.
6601 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006602 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006603get_name_len(
6604 char_u **arg,
6605 char_u **alias,
6606 int evaluate,
6607 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608{
6609 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 char_u *p;
6611 char_u *expr_start;
6612 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613
6614 *alias = NULL; /* default to no alias */
6615
6616 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6617 && (*arg)[2] == (int)KE_SNR)
6618 {
6619 /* hard coded <SNR>, already translated */
6620 *arg += 3;
6621 return get_id_len(arg) + 3;
6622 }
6623 len = eval_fname_script(*arg);
6624 if (len > 0)
6625 {
6626 /* literal "<SID>", "s:" or "<SNR>" */
6627 *arg += len;
6628 }
6629
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006631 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006633 p = find_name_end(*arg, &expr_start, &expr_end,
6634 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 if (expr_start != NULL)
6636 {
6637 char_u *temp_string;
6638
6639 if (!evaluate)
6640 {
6641 len += (int)(p - *arg);
6642 *arg = skipwhite(p);
6643 return len;
6644 }
6645
6646 /*
6647 * Include any <SID> etc in the expanded string:
6648 * Thus the -len here.
6649 */
6650 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6651 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006652 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 *alias = temp_string;
6654 *arg = skipwhite(p);
6655 return (int)STRLEN(temp_string);
6656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657
6658 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006659 // Only give an error when there is something, otherwise it will be
6660 // reported at a higher level.
6661 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006662 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663
6664 return len;
6665}
6666
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006667/*
6668 * Find the end of a variable or function name, taking care of magic braces.
6669 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6670 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006671 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006672 * Return a pointer to just after the name. Equal to "arg" if there is no
6673 * valid name.
6674 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006675 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006676find_name_end(
6677 char_u *arg,
6678 char_u **expr_start,
6679 char_u **expr_end,
6680 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006682 int mb_nest = 0;
6683 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006685 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006687 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006689 *expr_start = NULL;
6690 *expr_end = NULL;
6691 }
6692
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006693 /* Quick check for valid starting character. */
6694 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6695 return arg;
6696
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006697 for (p = arg; *p != NUL
6698 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006699 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006700 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006701 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006702 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006703 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006704 if (*p == '\'')
6705 {
6706 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006707 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006708 ;
6709 if (*p == NUL)
6710 break;
6711 }
6712 else if (*p == '"')
6713 {
6714 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006715 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006716 if (*p == '\\' && p[1] != NUL)
6717 ++p;
6718 if (*p == NUL)
6719 break;
6720 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006721 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6722 {
6723 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006724 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006725 len = (int)(p - arg);
6726 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006727 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006728 break;
6729 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006730
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006731 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006733 if (*p == '[')
6734 ++br_nest;
6735 else if (*p == ']')
6736 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006738
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006739 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006741 if (*p == '{')
6742 {
6743 mb_nest++;
6744 if (expr_start != NULL && *expr_start == NULL)
6745 *expr_start = p;
6746 }
6747 else if (*p == '}')
6748 {
6749 mb_nest--;
6750 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6751 *expr_end = p;
6752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 }
6755
6756 return p;
6757}
6758
6759/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006760 * Expands out the 'magic' {}'s in a variable/function name.
6761 * Note that this can call itself recursively, to deal with
6762 * constructs like foo{bar}{baz}{bam}
6763 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6764 * "in_start" ^
6765 * "expr_start" ^
6766 * "expr_end" ^
6767 * "in_end" ^
6768 *
6769 * Returns a new allocated string, which the caller must free.
6770 * Returns NULL for failure.
6771 */
6772 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006773make_expanded_name(
6774 char_u *in_start,
6775 char_u *expr_start,
6776 char_u *expr_end,
6777 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006778{
6779 char_u c1;
6780 char_u *retval = NULL;
6781 char_u *temp_result;
6782 char_u *nextcmd = NULL;
6783
6784 if (expr_end == NULL || in_end == NULL)
6785 return NULL;
6786 *expr_start = NUL;
6787 *expr_end = NUL;
6788 c1 = *in_end;
6789 *in_end = NUL;
6790
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006791 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006792 if (temp_result != NULL && nextcmd == NULL)
6793 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006794 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6795 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006796 if (retval != NULL)
6797 {
6798 STRCPY(retval, in_start);
6799 STRCAT(retval, temp_result);
6800 STRCAT(retval, expr_end + 1);
6801 }
6802 }
6803 vim_free(temp_result);
6804
6805 *in_end = c1; /* put char back for error messages */
6806 *expr_start = '{';
6807 *expr_end = '}';
6808
6809 if (retval != NULL)
6810 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006811 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006812 if (expr_start != NULL)
6813 {
6814 /* Further expansion! */
6815 temp_result = make_expanded_name(retval, expr_start,
6816 expr_end, temp_result);
6817 vim_free(retval);
6818 retval = temp_result;
6819 }
6820 }
6821
6822 return retval;
6823}
6824
6825/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006827 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006829 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006830eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006832 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6833}
6834
6835/*
6836 * Return TRUE if character "c" can be used as the first character in a
6837 * variable or function name (excluding '{' and '}').
6838 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006839 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006840eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006841{
6842 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843}
6844
6845/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 * Set number v: variable to "val".
6847 */
6848 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006849set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006851 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852}
6853
6854/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006855 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006857 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006858get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006860 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861}
6862
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006863/*
6864 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01006865 * If the String variable has never been set, return an empty string.
6866 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006867 */
6868 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006869get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006870{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006871 return tv_get_string(&vimvars[idx].vv_tv);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006872}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006873
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006875 * Get List v: variable value. Caller must take care of reference count when
6876 * needed.
6877 */
6878 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006879get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006880{
6881 return vimvars[idx].vv_list;
6882}
6883
6884/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006885 * Get Dict v: variable value. Caller must take care of reference count when
6886 * needed.
6887 */
6888 dict_T *
6889get_vim_var_dict(int idx)
6890{
6891 return vimvars[idx].vv_dict;
6892}
6893
6894/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006895 * Set v:char to character "c".
6896 */
6897 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006898set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006899{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006900 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006901
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006902 if (has_mbyte)
6903 buf[(*mb_char2bytes)(c, buf)] = NUL;
6904 else
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006905 {
6906 buf[0] = c;
6907 buf[1] = NUL;
6908 }
6909 set_vim_var_string(VV_CHAR, buf, -1);
6910}
6911
6912/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006913 * Set v:count to "count" and v:count1 to "count1".
6914 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 */
6916 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006917set_vcount(
6918 long count,
6919 long count1,
6920 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006922 if (set_prevcount)
6923 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006924 vimvars[VV_COUNT].vv_nr = count;
6925 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926}
6927
6928/*
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02006929 * Save variables that might be changed as a side effect. Used when executing
6930 * a timer callback.
6931 */
6932 void
6933save_vimvars(vimvars_save_T *vvsave)
6934{
6935 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
6936 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
6937 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
6938}
6939
6940/*
6941 * Restore variables saved by save_vimvars().
6942 */
6943 void
6944restore_vimvars(vimvars_save_T *vvsave)
6945{
6946 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
6947 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
6948 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
6949}
6950
6951/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 * Set string v: variable to a copy of "val".
6953 */
6954 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006955set_vim_var_string(
6956 int idx,
6957 char_u *val,
6958 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959{
Bram Moolenaara542c682016-01-31 16:28:04 +01006960 clear_tv(&vimvars[idx].vv_di.di_tv);
6961 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006963 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006965 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006967 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968}
6969
6970/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006971 * Set List v: variable to "val".
6972 */
6973 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006974set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006975{
Bram Moolenaara542c682016-01-31 16:28:04 +01006976 clear_tv(&vimvars[idx].vv_di.di_tv);
6977 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006978 vimvars[idx].vv_list = val;
6979 if (val != NULL)
6980 ++val->lv_refcount;
6981}
6982
6983/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006984 * Set Dictionary v: variable to "val".
6985 */
6986 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006987set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006988{
Bram Moolenaara542c682016-01-31 16:28:04 +01006989 clear_tv(&vimvars[idx].vv_di.di_tv);
6990 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006991 vimvars[idx].vv_dict = val;
6992 if (val != NULL)
6993 {
6994 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006995 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02006996 }
6997}
6998
6999/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 * Set v:register if needed.
7001 */
7002 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007003set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004{
7005 char_u regname;
7006
7007 if (c == 0 || c == ' ')
7008 regname = '"';
7009 else
7010 regname = c;
7011 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007012 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 set_vim_var_string(VV_REG, &regname, 1);
7014}
7015
7016/*
7017 * Get or set v:exception. If "oldval" == NULL, return the current value.
7018 * Otherwise, restore the value to "oldval" and return NULL.
7019 * Must always be called in pairs to save and restore v:exception! Does not
7020 * take care of memory allocations.
7021 */
7022 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007023v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024{
7025 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007026 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027
Bram Moolenaare9a41262005-01-15 22:18:47 +00007028 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 return NULL;
7030}
7031
7032/*
7033 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
7034 * Otherwise, restore the value to "oldval" and return NULL.
7035 * Must always be called in pairs to save and restore v:throwpoint! Does not
7036 * take care of memory allocations.
7037 */
7038 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007039v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040{
7041 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007042 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043
Bram Moolenaare9a41262005-01-15 22:18:47 +00007044 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 return NULL;
7046}
7047
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048/*
7049 * Set v:cmdarg.
7050 * If "eap" != NULL, use "eap" to generate the value and return the old value.
7051 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
7052 * Must always be called in pairs!
7053 */
7054 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007055set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056{
7057 char_u *oldval;
7058 char_u *newval;
7059 unsigned len;
7060
Bram Moolenaare9a41262005-01-15 22:18:47 +00007061 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007062 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007064 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007065 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007066 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 }
7068
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007069 if (eap->force_bin == FORCE_BIN)
7070 len = 6;
7071 else if (eap->force_bin == FORCE_NOBIN)
7072 len = 8;
7073 else
7074 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007075
7076 if (eap->read_edit)
7077 len += 7;
7078
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007079 if (eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02007080 len += 10; /* " ++ff=unix" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007081 if (eap->force_enc != 0)
7082 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02007083 if (eap->bad_char != 0)
7084 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007085
7086 newval = alloc(len + 1);
7087 if (newval == NULL)
7088 return NULL;
7089
7090 if (eap->force_bin == FORCE_BIN)
7091 sprintf((char *)newval, " ++bin");
7092 else if (eap->force_bin == FORCE_NOBIN)
7093 sprintf((char *)newval, " ++nobin");
7094 else
7095 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007096
7097 if (eap->read_edit)
7098 STRCAT(newval, " ++edit");
7099
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007100 if (eap->force_ff != 0)
7101 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
Bram Moolenaar333b80a2018-04-04 22:57:29 +02007102 eap->force_ff == 'u' ? "unix"
7103 : eap->force_ff == 'd' ? "dos"
7104 : "mac");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007105 if (eap->force_enc != 0)
7106 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
7107 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02007108 if (eap->bad_char == BAD_KEEP)
7109 STRCPY(newval + STRLEN(newval), " ++bad=keep");
7110 else if (eap->bad_char == BAD_DROP)
7111 STRCPY(newval + STRLEN(newval), " ++bad=drop");
7112 else if (eap->bad_char != 0)
7113 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007114 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007115 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117
7118/*
7119 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01007120 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007122 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007123get_var_tv(
7124 char_u *name,
7125 int len, /* length of "name" */
7126 typval_T *rettv, /* NULL when only checking existence */
7127 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
7128 int verbose, /* may give error message */
7129 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130{
7131 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007132 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007133 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135
7136 /* truncate the name, so that we can use strcmp() */
7137 cc = name[len];
7138 name[len] = NUL;
7139
7140 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 * Check for user-defined variables.
7142 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01007143 v = find_var(name, NULL, no_autoload);
7144 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007146 tv = &v->di_tv;
7147 if (dip != NULL)
7148 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 }
7150
Bram Moolenaare9a41262005-01-15 22:18:47 +00007151 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007153 if (rettv != NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007154 semsg(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 ret = FAIL;
7156 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007157 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007158 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159
7160 name[len] = cc;
7161
7162 return ret;
7163}
7164
7165/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007166 * Check if variable "name[len]" is a local variable or an argument.
7167 * If so, "*eval_lavars_used" is set to TRUE.
7168 */
7169 static void
7170check_vars(char_u *name, int len)
7171{
7172 int cc;
7173 char_u *varname;
7174 hashtab_T *ht;
7175
7176 if (eval_lavars_used == NULL)
7177 return;
7178
7179 /* truncate the name, so that we can use strcmp() */
7180 cc = name[len];
7181 name[len] = NUL;
7182
7183 ht = find_var_ht(name, &varname);
7184 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
7185 {
7186 if (find_var(name, NULL, TRUE) != NULL)
7187 *eval_lavars_used = TRUE;
7188 }
7189
7190 name[len] = cc;
7191}
7192
7193/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007194 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
7195 * Also handle function call with Funcref variable: func(expr)
7196 * Can all be combined: dict.func(expr)[idx]['func'](expr)
7197 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007198 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007199handle_subscript(
7200 char_u **arg,
7201 typval_T *rettv,
7202 int evaluate, /* do more than finding the end */
7203 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007204{
7205 int ret = OK;
7206 dict_T *selfdict = NULL;
7207 char_u *s;
7208 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007209 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007210
7211 while (ret == OK
7212 && (**arg == '['
7213 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007214 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7215 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01007216 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007217 {
7218 if (**arg == '(')
7219 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007220 partial_T *pt = NULL;
7221
Bram Moolenaard9fba312005-06-26 22:34:35 +00007222 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007223 if (evaluate)
7224 {
7225 functv = *rettv;
7226 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007227
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007228 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007229 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007230 {
7231 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007232 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007233 }
7234 else
7235 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007236 }
7237 else
7238 s = (char_u *)"";
Bram Moolenaar6ed88192019-05-11 18:37:44 +02007239 ret = get_func_tv(s, -1, rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00007240 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007241 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007242
7243 /* Clear the funcref afterwards, so that deleting it while
7244 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007245 if (evaluate)
7246 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007247
7248 /* Stop the expression evaluation when immediately aborting on
7249 * error, or when an interrupt occurred or an exception was thrown
7250 * but not caught. */
7251 if (aborting())
7252 {
7253 if (ret == OK)
7254 clear_tv(rettv);
7255 ret = FAIL;
7256 }
7257 dict_unref(selfdict);
7258 selfdict = NULL;
7259 }
7260 else /* **arg == '[' || **arg == '.' */
7261 {
7262 dict_unref(selfdict);
7263 if (rettv->v_type == VAR_DICT)
7264 {
7265 selfdict = rettv->vval.v_dict;
7266 if (selfdict != NULL)
7267 ++selfdict->dv_refcount;
7268 }
7269 else
7270 selfdict = NULL;
7271 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
7272 {
7273 clear_tv(rettv);
7274 ret = FAIL;
7275 }
7276 }
7277 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007278
Bram Moolenaar1d429612016-05-24 15:44:17 +02007279 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
7280 * Don't do this when "Func" is already a partial that was bound
7281 * explicitly (pt_auto is FALSE). */
7282 if (selfdict != NULL
7283 && (rettv->v_type == VAR_FUNC
7284 || (rettv->v_type == VAR_PARTIAL
7285 && (rettv->vval.v_partial->pt_auto
7286 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007287 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007288
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007289 dict_unref(selfdict);
7290 return ret;
7291}
7292
7293/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007294 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007295 * value).
7296 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01007297 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007298alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007299{
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02007300 return (typval_T *)alloc_clear(sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007301}
7302
7303/*
7304 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 * The string "s" must have been allocated, it is consumed.
7306 * Return NULL for out of memory, the variable otherwise.
7307 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007308 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007309alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310{
Bram Moolenaar33570922005-01-25 22:26:29 +00007311 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007313 rettv = alloc_tv();
7314 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007316 rettv->v_type = VAR_STRING;
7317 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 }
7319 else
7320 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007321 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322}
7323
7324/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007325 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007327 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007328free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329{
7330 if (varp != NULL)
7331 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007332 switch (varp->v_type)
7333 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007334 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007335 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007336 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007337 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007338 vim_free(varp->vval.v_string);
7339 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007340 case VAR_PARTIAL:
7341 partial_unref(varp->vval.v_partial);
7342 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007343 case VAR_BLOB:
7344 blob_unref(varp->vval.v_blob);
7345 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007346 case VAR_LIST:
7347 list_unref(varp->vval.v_list);
7348 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007349 case VAR_DICT:
7350 dict_unref(varp->vval.v_dict);
7351 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007352 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007353#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007354 job_unref(varp->vval.v_job);
7355 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007356#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007357 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007358#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007359 channel_unref(varp->vval.v_channel);
7360 break;
7361#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007362 case VAR_NUMBER:
7363 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007364 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007365 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007366 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 vim_free(varp);
7369 }
7370}
7371
7372/*
7373 * Free the memory for a variable value and set the value to NULL or 0.
7374 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007375 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007376clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377{
7378 if (varp != NULL)
7379 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007380 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007382 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007383 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007384 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007385 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007386 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007387 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007388 case VAR_PARTIAL:
7389 partial_unref(varp->vval.v_partial);
7390 varp->vval.v_partial = NULL;
7391 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007392 case VAR_BLOB:
7393 blob_unref(varp->vval.v_blob);
7394 varp->vval.v_blob = NULL;
7395 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007396 case VAR_LIST:
7397 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007398 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007399 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007400 case VAR_DICT:
7401 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007402 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007403 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007404 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007405 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007406 varp->vval.v_number = 0;
7407 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007408 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007409#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007410 varp->vval.v_float = 0.0;
7411 break;
7412#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007413 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007414#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007415 job_unref(varp->vval.v_job);
7416 varp->vval.v_job = NULL;
7417#endif
7418 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007419 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007420#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007421 channel_unref(varp->vval.v_channel);
7422 varp->vval.v_channel = NULL;
7423#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007424 case VAR_UNKNOWN:
7425 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007427 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 }
7429}
7430
7431/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007432 * Set the value of a variable to NULL without freeing items.
7433 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007434 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007435init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007436{
7437 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007438 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007439}
7440
7441/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 * Get the number value of a variable.
7443 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007444 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007445 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007446 * caller of incompatible types: it sets *denote to TRUE if "denote"
7447 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007449 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007450tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007452 int error = FALSE;
7453
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007454 return tv_get_number_chk(varp, &error); /* return 0L on error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007455}
7456
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007457 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007458tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007459{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007460 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007462 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007464 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007465 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007466 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007467#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007468 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007469 break;
7470#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007471 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007472 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007473 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007474 break;
7475 case VAR_STRING:
7476 if (varp->vval.v_string != NULL)
7477 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02007478 STR2NR_ALL, &n, NULL, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007479 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007480 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007481 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007482 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007483 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007484 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007485 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007486 case VAR_SPECIAL:
7487 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7488 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007489 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007490#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007491 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007492 break;
7493#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007494 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007495#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007496 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007497 break;
7498#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007499 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007500 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007501 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007502 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007503 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007504 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007506 if (denote == NULL) /* useful for values that must be unsigned */
7507 n = -1;
7508 else
7509 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007510 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511}
7512
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007513#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007514 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007515tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007516{
7517 switch (varp->v_type)
7518 {
7519 case VAR_NUMBER:
7520 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007521 case VAR_FLOAT:
7522 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007523 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007524 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007525 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007526 break;
7527 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007528 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007529 break;
7530 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007531 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007532 break;
7533 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007534 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007535 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007536 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007537 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007538 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007539 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007540# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007541 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007542 break;
7543# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007544 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007545# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007546 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007547 break;
7548# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007549 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007550 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007551 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007552 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007553 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007554 break;
7555 }
7556 return 0;
7557}
7558#endif
7559
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 * Get the string value of a variable.
7562 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007563 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7564 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 * If the String variable has never been set, return an empty string.
7566 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007567 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007568 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007570 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007571tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572{
7573 static char_u mybuf[NUMBUFLEN];
7574
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007575 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576}
7577
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007578 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007579tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007581 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007582
7583 return res != NULL ? res : (char_u *)"";
7584}
7585
Bram Moolenaar7d647822014-04-05 21:28:56 +02007586/*
7587 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7588 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007589 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007590tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007591{
7592 static char_u mybuf[NUMBUFLEN];
7593
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007594 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007595}
7596
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007597 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007598tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007599{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007600 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007602 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007603 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007604 (long_long_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007605 return buf;
7606 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007607 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007608 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007609 break;
7610 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007611 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007612 break;
7613 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007614 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007615 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007616 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007617#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007618 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007619 break;
7620#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007621 case VAR_STRING:
7622 if (varp->vval.v_string != NULL)
7623 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007624 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007625 case VAR_SPECIAL:
7626 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7627 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007628 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007629 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007630 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007631 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007632#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007633 {
7634 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007635 char *status;
7636
7637 if (job == NULL)
7638 return (char_u *)"no process";
7639 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007640 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007641 : "run";
7642# ifdef UNIX
7643 vim_snprintf((char *)buf, NUMBUFLEN,
7644 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01007645# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007646 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007647 "process %ld %s",
7648 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007649 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007650# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007651 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007652 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7653# endif
7654 return buf;
7655 }
7656#endif
7657 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007658 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007659#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007660 {
7661 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007662 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007663
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007664 if (channel == NULL)
7665 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7666 else
7667 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007668 "channel %d %s", channel->ch_id, status);
7669 return buf;
7670 }
7671#endif
7672 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007673 case VAR_UNKNOWN:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007674 emsg(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007675 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007677 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678}
7679
7680/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007681 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
7682 * string() on Dict, List, etc.
7683 */
7684 char_u *
7685tv_stringify(typval_T *varp, char_u *buf)
7686{
7687 if (varp->v_type == VAR_LIST
7688 || varp->v_type == VAR_DICT
7689 || varp->v_type == VAR_FUNC
7690 || varp->v_type == VAR_PARTIAL
7691 || varp->v_type == VAR_FLOAT)
7692 {
7693 typval_T tmp;
7694
7695 f_string(varp, &tmp);
7696 tv_get_string_buf(&tmp, buf);
7697 clear_tv(varp);
7698 *varp = tmp;
7699 return tmp.vval.v_string;
7700 }
7701 return tv_get_string_buf(varp, buf);
7702}
7703
7704/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 * Find variable "name" in the list of variables.
7706 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007707 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007708 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007709 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007711 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007712find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007715 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007716 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717
Bram Moolenaara7043832005-01-21 11:56:39 +00007718 ht = find_var_ht(name, &varname);
7719 if (htp != NULL)
7720 *htp = ht;
7721 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007723 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7724 if (ret != NULL)
7725 return ret;
7726
7727 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007728 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729}
7730
7731/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007732 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007733 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007735 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007736find_var_in_ht(
7737 hashtab_T *ht,
7738 int htname,
7739 char_u *varname,
7740 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007741{
Bram Moolenaar33570922005-01-25 22:26:29 +00007742 hashitem_T *hi;
7743
7744 if (*varname == NUL)
7745 {
7746 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007747 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007748 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007749 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007750 case 'g': return &globvars_var;
7751 case 'v': return &vimvars_var;
7752 case 'b': return &curbuf->b_bufvar;
7753 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007754 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007755 case 'l': return get_funccal_local_var();
7756 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007757 }
7758 return NULL;
7759 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007760
7761 hi = hash_find(ht, varname);
7762 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007763 {
7764 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007765 * worked find the variable again. Don't auto-load a script if it was
7766 * loaded already, otherwise it would be loaded every time when
7767 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007768 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007769 {
7770 /* Note: script_autoload() may make "hi" invalid. It must either
7771 * be obtained again or not used. */
7772 if (!script_autoload(varname, FALSE) || aborting())
7773 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007774 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007775 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007776 if (HASHITEM_EMPTY(hi))
7777 return NULL;
7778 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007779 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007780}
7781
7782/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007783 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007784 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007785 * Set "varname" to the start of name without ':'.
7786 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007787 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007788find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007790 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007791 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007792
Bram Moolenaar73627d02015-08-11 15:46:09 +02007793 if (name[0] == NUL)
7794 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 if (name[1] != ':')
7796 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007797 /* The name must not start with a colon or #. */
7798 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 return NULL;
7800 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007801
Bram Moolenaard2e716e2019-04-20 14:39:52 +02007802 // "version" is "v:version" in all scopes if scriptversion < 3.
7803 // Same for a few other variables marked with VV_COMPAT.
7804 if (current_sctx.sc_version < 3)
7805 {
7806 hi = hash_find(&compat_hashtab, name);
7807 if (!HASHITEM_EMPTY(hi))
7808 return &compat_hashtab;
7809 }
Bram Moolenaar532c7802005-01-27 14:44:31 +00007810
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007811 ht = get_funccal_local_ht();
7812 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007813 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007814 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 }
7816 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007817 if (*name == 'g') /* global variable */
7818 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007819 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7820 */
7821 if (vim_strchr(name + 2, ':') != NULL
7822 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007823 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007825 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007827 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007828 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007829 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007830 if (*name == 'v') /* v: variable */
7831 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007832 if (*name == 'a') /* a: function argument */
7833 return get_funccal_args_ht();
7834 if (*name == 'l') /* l: local function variable */
7835 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 if (*name == 's' /* script variable */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007837 && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
7838 return &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 return NULL;
7840}
7841
7842/*
7843 * Get the string value of a (global/local) variable.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007844 * Note: see tv_get_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 * Returns NULL when it doesn't exist.
7846 */
7847 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007848get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849{
Bram Moolenaar33570922005-01-25 22:26:29 +00007850 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007852 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 if (v == NULL)
7854 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007855 return tv_get_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856}
7857
7858/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007859 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 * sourcing this script and when executing functions defined in the script.
7861 */
7862 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007863new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864{
Bram Moolenaara7043832005-01-21 11:56:39 +00007865 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007866 hashtab_T *ht;
7867 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007868
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7870 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007871 /* Re-allocating ga_data means that an ht_array pointing to
7872 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007873 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007874 for (i = 1; i <= ga_scripts.ga_len; ++i)
7875 {
7876 ht = &SCRIPT_VARS(i);
7877 if (ht->ht_mask == HT_INIT_SIZE - 1)
7878 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007879 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007880 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007881 }
7882
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 while (ga_scripts.ga_len < id)
7884 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007885 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007886 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007887 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 }
7890 }
7891}
7892
7893/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007894 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7895 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 */
7897 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007898init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899{
Bram Moolenaar33570922005-01-25 22:26:29 +00007900 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007901 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007902 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007903 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007904 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007905 dict_var->di_tv.vval.v_dict = dict;
7906 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007907 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007908 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7909 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910}
7911
7912/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007913 * Unreference a dictionary initialized by init_var_dict().
7914 */
7915 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007916unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007917{
7918 /* Now the dict needs to be freed if no one else is using it, go back to
7919 * normal reference counting. */
7920 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7921 dict_unref(dict);
7922}
7923
7924/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007926 * Frees all allocated variables and the value they contain.
7927 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 */
7929 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007930vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007931{
7932 vars_clear_ext(ht, TRUE);
7933}
7934
7935/*
7936 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7937 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007938 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007939vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940{
Bram Moolenaara7043832005-01-21 11:56:39 +00007941 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007942 hashitem_T *hi;
7943 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944
Bram Moolenaar33570922005-01-25 22:26:29 +00007945 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007946 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007947 for (hi = ht->ht_array; todo > 0; ++hi)
7948 {
7949 if (!HASHITEM_EMPTY(hi))
7950 {
7951 --todo;
7952
Bram Moolenaar33570922005-01-25 22:26:29 +00007953 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007954 * ht_array might change then. hash_clear() takes care of it
7955 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007956 v = HI2DI(hi);
7957 if (free_val)
7958 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007959 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007960 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007961 }
7962 }
7963 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007964 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965}
7966
Bram Moolenaara7043832005-01-21 11:56:39 +00007967/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007968 * Delete a variable from hashtab "ht" at item "hi".
7969 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007970 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007972delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973{
Bram Moolenaar33570922005-01-25 22:26:29 +00007974 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007975
7976 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007977 clear_tv(&di->di_tv);
7978 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979}
7980
7981/*
7982 * List the value of one internal variable.
7983 */
7984 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01007985list_one_var(dictitem_T *v, char *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007987 char_u *tofree;
7988 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007989 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007990
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007991 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007992 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007993 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007994 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995}
7996
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007998list_one_var_a(
Bram Moolenaar32526b32019-01-19 17:43:09 +01007999 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01008000 char_u *name,
8001 int type,
8002 char_u *string,
8003 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004{
Bram Moolenaar31859182007-08-14 20:41:13 +00008005 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
8006 msg_start();
8007 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 if (name != NULL) /* "a:" vars don't have a name stored */
Bram Moolenaar32526b32019-01-19 17:43:09 +01008009 msg_puts((char *)name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 msg_putchar(' ');
8011 msg_advance(22);
8012 if (type == VAR_NUMBER)
8013 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008014 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008015 msg_putchar('*');
8016 else if (type == VAR_LIST)
8017 {
8018 msg_putchar('[');
8019 if (*string == '[')
8020 ++string;
8021 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008022 else if (type == VAR_DICT)
8023 {
8024 msg_putchar('{');
8025 if (*string == '{')
8026 ++string;
8027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 else
8029 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008030
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008032
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008033 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008034 msg_puts("()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00008035 if (*first)
8036 {
8037 msg_clr_eos();
8038 *first = FALSE;
8039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040}
8041
8042/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008043 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 * If the variable already exists, the value is updated.
8045 * Otherwise the variable is created.
8046 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008047 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008048set_var(
8049 char_u *name,
8050 typval_T *tv,
8051 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052{
Bram Moolenaar33570922005-01-25 22:26:29 +00008053 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008055 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008057 ht = find_var_ht(name, &varname);
8058 if (ht == NULL || *varname == NUL)
8059 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008060 semsg(_(e_illvar), name);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008061 return;
8062 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02008063 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008064
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008065 /* Search in parent scope which is possible to reference from lambda */
8066 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02008067 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008068
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008069 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
8070 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008071 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008072
Bram Moolenaar33570922005-01-25 22:26:29 +00008073 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008075 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02008076 if (var_check_ro(v->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008077 || var_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00008078 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008079
8080 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008081 * Handle setting internal v: variables separately where needed to
8082 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00008083 */
8084 if (ht == &vimvarht)
8085 {
8086 if (v->di_tv.v_type == VAR_STRING)
8087 {
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01008088 VIM_CLEAR(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008089 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01008090 {
8091 char_u *val = tv_get_string(tv);
8092
8093 // Careful: when assigning to v:errmsg and tv_get_string()
8094 // causes an error message the variable will alrady be set.
8095 if (v->di_tv.vval.v_string == NULL)
8096 v->di_tv.vval.v_string = vim_strsave(val);
8097 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008098 else
8099 {
8100 /* Take over the string to avoid an extra alloc/free. */
8101 v->di_tv.vval.v_string = tv->vval.v_string;
8102 tv->vval.v_string = NULL;
8103 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008104 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008105 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008106 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008107 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008108 v->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008109 if (STRCMP(varname, "searchforward") == 0)
8110 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01008111#ifdef FEAT_SEARCH_EXTRA
8112 else if (STRCMP(varname, "hlsearch") == 0)
8113 {
8114 no_hlsearch = !v->di_tv.vval.v_number;
8115 redraw_all_later(SOME_VALID);
8116 }
8117#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008118 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008119 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008120 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01008121 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008122 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01008123 return;
8124 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008125 }
8126
8127 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 }
8129 else /* add a new variable */
8130 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01008131 // Can't add "v:" or "a:" variable.
8132 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00008133 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008134 semsg(_(e_illvar), name);
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00008135 return;
8136 }
8137
Bram Moolenaar31b81602019-02-10 22:14:27 +01008138 // Make sure the variable name is valid.
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008139 if (!valid_varname(varname))
8140 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00008141
Bram Moolenaar964b3742019-05-24 18:54:09 +02008142 v = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(varname));
Bram Moolenaara7043832005-01-21 11:56:39 +00008143 if (v == NULL)
8144 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008145 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00008146 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008148 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 return;
8150 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02008151 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 }
Bram Moolenaara7043832005-01-21 11:56:39 +00008153
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008154 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00008155 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00008156 else
8157 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008158 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008159 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008160 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00008161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162}
8163
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008164/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008165 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00008166 * Also give an error message.
8167 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008168 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008169var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00008170{
8171 if (flags & DI_FLAGS_RO)
8172 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008173 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008174 return TRUE;
8175 }
8176 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
8177 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008178 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008179 return TRUE;
8180 }
8181 return FALSE;
8182}
8183
8184/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008185 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
8186 * Also give an error message.
8187 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008188 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008189var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008190{
8191 if (flags & DI_FLAGS_FIX)
8192 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008193 semsg(_("E795: Cannot delete variable %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008194 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008195 return TRUE;
8196 }
8197 return FALSE;
8198}
8199
8200/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008201 * Check if a funcref is assigned to a valid variable name.
8202 * Return TRUE and give an error if not.
8203 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008204 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008205var_check_func_name(
8206 char_u *name, /* points to start of variable name */
8207 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008208{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02008209 /* Allow for w: b: s: and t:. */
8210 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008211 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
8212 ? name[2] : name[0]))
8213 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008214 semsg(_("E704: Funcref variable name must start with a capital: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008215 name);
8216 return TRUE;
8217 }
8218 /* Don't allow hiding a function. When "v" is not NULL we might be
8219 * assigning another function to the same var, the type is checked
8220 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02008221 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008222 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008223 semsg(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008224 name);
8225 return TRUE;
8226 }
8227 return FALSE;
8228}
8229
8230/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008231 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02008232 * Also give an error message, using "name" or _("name") when use_gettext is
8233 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008234 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008235 int
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008236var_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008237{
8238 if (lock & VAR_LOCKED)
8239 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008240 semsg(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008241 name == NULL ? (char_u *)_("Unknown")
8242 : use_gettext ? (char_u *)_(name)
8243 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008244 return TRUE;
8245 }
8246 if (lock & VAR_FIXED)
8247 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008248 semsg(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008249 name == NULL ? (char_u *)_("Unknown")
8250 : use_gettext ? (char_u *)_(name)
8251 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008252 return TRUE;
8253 }
8254 return FALSE;
8255}
8256
8257/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008258 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
8259 * Also give an error message, using "name" or _("name") when use_gettext is
8260 * TRUE.
8261 */
8262 static int
8263tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
8264{
8265 int lock = 0;
8266
8267 switch (tv->v_type)
8268 {
8269 case VAR_BLOB:
8270 if (tv->vval.v_blob != NULL)
8271 lock = tv->vval.v_blob->bv_lock;
8272 break;
8273 case VAR_LIST:
8274 if (tv->vval.v_list != NULL)
8275 lock = tv->vval.v_list->lv_lock;
8276 break;
8277 case VAR_DICT:
8278 if (tv->vval.v_dict != NULL)
8279 lock = tv->vval.v_dict->dv_lock;
8280 break;
8281 default:
8282 break;
8283 }
8284 return var_check_lock(tv->v_lock, name, use_gettext)
8285 || (lock != 0 && var_check_lock(lock, name, use_gettext));
8286}
8287
8288/*
8289 * Check if a variable name is valid.
8290 * Return FALSE and give an error if not.
8291 */
8292 int
8293valid_varname(char_u *varname)
8294{
8295 char_u *p;
8296
8297 for (p = varname; *p != NUL; ++p)
8298 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
8299 && *p != AUTOLOAD_CHAR)
8300 {
8301 semsg(_(e_illvar), varname);
8302 return FALSE;
8303 }
8304 return TRUE;
8305}
8306
8307/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008308 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008309 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008310 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008311 * It is OK for "from" and "to" to point to the same item. This is used to
8312 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008313 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008314 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008315copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008317 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008318 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008319 switch (from->v_type)
8320 {
8321 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008322 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008323 to->vval.v_number = from->vval.v_number;
8324 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008325 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008326#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008327 to->vval.v_float = from->vval.v_float;
8328 break;
8329#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008330 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008331#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01008332 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01008333 if (to->vval.v_job != NULL)
8334 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01008335 break;
8336#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01008337 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008338#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01008339 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01008340 if (to->vval.v_channel != NULL)
8341 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01008342 break;
8343#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008344 case VAR_STRING:
8345 case VAR_FUNC:
8346 if (from->vval.v_string == NULL)
8347 to->vval.v_string = NULL;
8348 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008349 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008350 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008351 if (from->v_type == VAR_FUNC)
8352 func_ref(to->vval.v_string);
8353 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008354 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008355 case VAR_PARTIAL:
8356 if (from->vval.v_partial == NULL)
8357 to->vval.v_partial = NULL;
8358 else
8359 {
8360 to->vval.v_partial = from->vval.v_partial;
8361 ++to->vval.v_partial->pt_refcount;
8362 }
8363 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008364 case VAR_BLOB:
8365 if (from->vval.v_blob == NULL)
8366 to->vval.v_blob = NULL;
8367 else
8368 {
8369 to->vval.v_blob = from->vval.v_blob;
8370 ++to->vval.v_blob->bv_refcount;
8371 }
8372 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008373 case VAR_LIST:
8374 if (from->vval.v_list == NULL)
8375 to->vval.v_list = NULL;
8376 else
8377 {
8378 to->vval.v_list = from->vval.v_list;
8379 ++to->vval.v_list->lv_refcount;
8380 }
8381 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008382 case VAR_DICT:
8383 if (from->vval.v_dict == NULL)
8384 to->vval.v_dict = NULL;
8385 else
8386 {
8387 to->vval.v_dict = from->vval.v_dict;
8388 ++to->vval.v_dict->dv_refcount;
8389 }
8390 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008391 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008392 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008393 break;
8394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395}
8396
8397/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008398 * Make a copy of an item.
8399 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008400 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
8401 * reference to an already copied list/dict can be used.
8402 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008403 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008404 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008405item_copy(
8406 typval_T *from,
8407 typval_T *to,
8408 int deep,
8409 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008410{
8411 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008412 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008413
Bram Moolenaar33570922005-01-25 22:26:29 +00008414 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008415 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008416 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008417 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008418 }
8419 ++recurse;
8420
8421 switch (from->v_type)
8422 {
8423 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008424 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008425 case VAR_STRING:
8426 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008427 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008428 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008429 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008430 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008431 copy_tv(from, to);
8432 break;
8433 case VAR_LIST:
8434 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008435 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008436 if (from->vval.v_list == NULL)
8437 to->vval.v_list = NULL;
8438 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8439 {
8440 /* use the copy made earlier */
8441 to->vval.v_list = from->vval.v_list->lv_copylist;
8442 ++to->vval.v_list->lv_refcount;
8443 }
8444 else
8445 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8446 if (to->vval.v_list == NULL)
8447 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008448 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008449 case VAR_BLOB:
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008450 ret = blob_copy(from, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008451 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008452 case VAR_DICT:
8453 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008454 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008455 if (from->vval.v_dict == NULL)
8456 to->vval.v_dict = NULL;
8457 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8458 {
8459 /* use the copy made earlier */
8460 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8461 ++to->vval.v_dict->dv_refcount;
8462 }
8463 else
8464 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8465 if (to->vval.v_dict == NULL)
8466 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008467 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008468 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008469 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008470 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008471 }
8472 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008473 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008474}
8475
8476/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008477 * This function is used by f_input() and f_inputdialog() functions. The third
8478 * argument to f_input() specifies the type of completion to use at the
8479 * prompt. The third argument to f_inputdialog() specifies the value to return
8480 * when the user cancels the prompt.
8481 */
8482 void
8483get_user_input(
8484 typval_T *argvars,
8485 typval_T *rettv,
8486 int inputdialog,
8487 int secret)
8488{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008489 char_u *prompt = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008490 char_u *p = NULL;
8491 int c;
8492 char_u buf[NUMBUFLEN];
8493 int cmd_silent_save = cmd_silent;
8494 char_u *defstr = (char_u *)"";
8495 int xp_type = EXPAND_NOTHING;
8496 char_u *xp_arg = NULL;
8497
8498 rettv->v_type = VAR_STRING;
8499 rettv->vval.v_string = NULL;
8500
8501#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008502 /* While starting up, there is no place to enter text. When running tests
8503 * with --not-a-term we assume feedkeys() will be used. */
8504 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008505 return;
8506#endif
8507
8508 cmd_silent = FALSE; /* Want to see the prompt. */
8509 if (prompt != NULL)
8510 {
8511 /* Only the part of the message after the last NL is considered as
8512 * prompt for the command line */
8513 p = vim_strrchr(prompt, '\n');
8514 if (p == NULL)
8515 p = prompt;
8516 else
8517 {
8518 ++p;
8519 c = *p;
8520 *p = NUL;
8521 msg_start();
8522 msg_clr_eos();
Bram Moolenaar32526b32019-01-19 17:43:09 +01008523 msg_puts_attr((char *)prompt, echo_attr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008524 msg_didout = FALSE;
8525 msg_starthere();
8526 *p = c;
8527 }
8528 cmdline_row = msg_row;
8529
8530 if (argvars[1].v_type != VAR_UNKNOWN)
8531 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008532 defstr = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008533 if (defstr != NULL)
8534 stuffReadbuffSpec(defstr);
8535
8536 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8537 {
8538 char_u *xp_name;
8539 int xp_namelen;
8540 long argt;
8541
8542 /* input() with a third argument: completion */
8543 rettv->vval.v_string = NULL;
8544
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008545 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008546 if (xp_name == NULL)
8547 return;
8548
8549 xp_namelen = (int)STRLEN(xp_name);
8550
8551 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8552 &xp_arg) == FAIL)
8553 return;
8554 }
8555 }
8556
8557 if (defstr != NULL)
8558 {
8559 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02008560
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008561 ex_normal_busy = 0;
8562 rettv->vval.v_string =
8563 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8564 xp_type, xp_arg);
8565 ex_normal_busy = save_ex_normal_busy;
8566 }
8567 if (inputdialog && rettv->vval.v_string == NULL
8568 && argvars[1].v_type != VAR_UNKNOWN
8569 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008570 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008571 &argvars[2], buf));
8572
8573 vim_free(xp_arg);
8574
8575 /* since the user typed this, no need to wait for return */
8576 need_wait_return = FALSE;
8577 msg_didout = FALSE;
8578 }
8579 cmd_silent = cmd_silent_save;
8580}
8581
8582/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 * ":echo expr1 ..." print each argument separated with a space, add a
8584 * newline at the end.
8585 * ":echon expr1 ..." print each argument plain.
8586 */
8587 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008588ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589{
8590 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008591 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008592 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593 char_u *p;
8594 int needclr = TRUE;
8595 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008596 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01008597 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008598 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599
8600 if (eap->skip)
8601 ++emsg_skip;
8602 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8603 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008604 /* If eval1() causes an error message the text from the command may
8605 * still need to be cleared. E.g., "echo 22,44". */
8606 need_clr_eos = needclr;
8607
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008609 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 {
8611 /*
8612 * Report the invalid expression unless the expression evaluation
8613 * has been cancelled due to an aborting error, an interrupt, or an
8614 * exception.
8615 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008616 if (!aborting() && did_emsg == did_emsg_before
8617 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008618 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008619 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 break;
8621 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008622 need_clr_eos = FALSE;
8623
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 if (!eap->skip)
8625 {
8626 if (atstart)
8627 {
8628 atstart = FALSE;
8629 /* Call msg_start() after eval1(), evaluating the expression
8630 * may cause a message to appear. */
8631 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008632 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008633 /* Mark the saved text as finishing the line, so that what
8634 * follows is displayed on a new line when scrolling back
8635 * at the more prompt. */
8636 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 }
8640 else if (eap->cmdidx == CMD_echo)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008641 msg_puts_attr(" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008642 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008643 if (p != NULL)
8644 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008646 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008648 if (*p != TAB && needclr)
8649 {
8650 /* remove any text still there from the command */
8651 msg_clr_eos();
8652 needclr = FALSE;
8653 }
8654 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 }
8656 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008657 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008658 if (has_mbyte)
8659 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008660 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008661
8662 (void)msg_outtrans_len_attr(p, i, echo_attr);
8663 p += i - 1;
8664 }
8665 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008666 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008669 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008671 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 arg = skipwhite(arg);
8673 }
8674 eap->nextcmd = check_nextcmd(arg);
8675
8676 if (eap->skip)
8677 --emsg_skip;
8678 else
8679 {
8680 /* remove text that may still be there from the command */
8681 if (needclr)
8682 msg_clr_eos();
8683 if (eap->cmdidx == CMD_echo)
8684 msg_end();
8685 }
8686}
8687
8688/*
8689 * ":echohl {name}".
8690 */
8691 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008692ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008694 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695}
8696
8697/*
8698 * ":execute expr1 ..." execute the result of an expression.
8699 * ":echomsg expr1 ..." Print a message
8700 * ":echoerr expr1 ..." Print an error
8701 * Each gets spaces around each argument and a newline at the end for
8702 * echo commands
8703 */
8704 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008705ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706{
8707 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008708 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 int ret = OK;
8710 char_u *p;
8711 garray_T ga;
8712 int len;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01008713 int save_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714
8715 ga_init2(&ga, 1, 80);
8716
8717 if (eap->skip)
8718 ++emsg_skip;
8719 while (*arg != NUL && *arg != '|' && *arg != '\n')
8720 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01008721 ret = eval1_emsg(&arg, &rettv, !eap->skip);
8722 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724
8725 if (!eap->skip)
8726 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008727 char_u buf[NUMBUFLEN];
8728
8729 if (eap->cmdidx == CMD_execute)
8730 p = tv_get_string_buf(&rettv, buf);
8731 else
8732 p = tv_stringify(&rettv, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 len = (int)STRLEN(p);
8734 if (ga_grow(&ga, len + 2) == FAIL)
8735 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008736 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 ret = FAIL;
8738 break;
8739 }
8740 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 ga.ga_len += len;
8744 }
8745
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008746 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 arg = skipwhite(arg);
8748 }
8749
8750 if (ret != FAIL && ga.ga_data != NULL)
8751 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008752 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8753 {
8754 /* Mark the already saved text as finishing the line, so that what
8755 * follows is displayed on a new line when scrolling back at the
8756 * more prompt. */
8757 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008758 }
8759
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008761 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01008762 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008763 out_flush();
8764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 else if (eap->cmdidx == CMD_echoerr)
8766 {
8767 /* We don't want to abort following commands, restore did_emsg. */
8768 save_did_emsg = did_emsg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008769 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 if (!force_abort)
8771 did_emsg = save_did_emsg;
8772 }
8773 else if (eap->cmdidx == CMD_execute)
8774 do_cmdline((char_u *)ga.ga_data,
8775 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8776 }
8777
8778 ga_clear(&ga);
8779
8780 if (eap->skip)
8781 --emsg_skip;
8782
8783 eap->nextcmd = check_nextcmd(arg);
8784}
8785
8786/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008787 * Find window specified by "vp" in tabpage "tp".
8788 */
8789 win_T *
8790find_win_by_nr(
8791 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01008792 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008793{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008794 win_T *wp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008795 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008796
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008797 if (nr < 0)
8798 return NULL;
8799 if (nr == 0)
8800 return curwin;
8801
Bram Moolenaar29323592016-07-24 22:04:11 +02008802 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8803 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008804 if (nr >= LOWEST_WIN_ID)
8805 {
8806 if (wp->w_id == nr)
8807 return wp;
8808 }
8809 else if (--nr <= 0)
8810 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008811 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008812 if (nr >= LOWEST_WIN_ID)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008813 {
8814#ifdef FEAT_TEXT_PROP
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02008815 // check tab-local popup windows
8816 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008817 if (wp->w_id == nr)
8818 return wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02008819 // check global popup windows
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008820 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
8821 if (wp->w_id == nr)
8822 return wp;
8823#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008824 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008825 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008826 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008827}
8828
8829/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02008830 * Find a window: When using a Window ID in any tab page, when using a number
8831 * in the current tab page.
8832 */
8833 win_T *
8834find_win_by_nr_or_id(typval_T *vp)
8835{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008836 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02008837
8838 if (nr >= LOWEST_WIN_ID)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01008839 return win_id2wp(tv_get_number(vp));
Bram Moolenaare6e39892018-10-25 12:32:11 +02008840 return find_win_by_nr(vp, NULL);
8841}
8842
8843/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008844 * Find window specified by "wvp" in tabpage "tvp".
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008845 * Returns the tab page in 'ptp'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008846 */
8847 win_T *
8848find_tabwin(
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008849 typval_T *wvp, // VAR_UNKNOWN for current window
8850 typval_T *tvp, // VAR_UNKNOWN for current tab page
8851 tabpage_T **ptp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008852{
8853 win_T *wp = NULL;
8854 tabpage_T *tp = NULL;
8855 long n;
8856
8857 if (wvp->v_type != VAR_UNKNOWN)
8858 {
8859 if (tvp->v_type != VAR_UNKNOWN)
8860 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008861 n = (long)tv_get_number(tvp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008862 if (n >= 0)
8863 tp = find_tabpage(n);
8864 }
8865 else
8866 tp = curtab;
8867
8868 if (tp != NULL)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008869 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008870 wp = find_win_by_nr(wvp, tp);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008871 if (wp == NULL && wvp->v_type == VAR_NUMBER
8872 && wvp->vval.v_number != -1)
8873 // A window with the specified number is not found
8874 tp = NULL;
8875 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008876 }
8877 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008878 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008879 wp = curwin;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008880 tp = curtab;
8881 }
8882
8883 if (ptp != NULL)
8884 *ptp = tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008885
8886 return wp;
8887}
8888
8889/*
8890 * getwinvar() and gettabwinvar()
8891 */
8892 void
8893getwinvar(
8894 typval_T *argvars,
8895 typval_T *rettv,
8896 int off) /* 1 for gettabwinvar() */
8897{
8898 win_T *win;
8899 char_u *varname;
8900 dictitem_T *v;
8901 tabpage_T *tp = NULL;
8902 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008903 win_T *oldcurwin;
8904 tabpage_T *oldtabpage;
8905 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008906
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008907 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008908 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008909 else
8910 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008911 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008912 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008913 ++emsg_off;
8914
8915 rettv->v_type = VAR_STRING;
8916 rettv->vval.v_string = NULL;
8917
8918 if (win != NULL && varname != NULL)
8919 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008920 /* Set curwin to be our win, temporarily. Also set the tabpage,
8921 * otherwise the window is not valid. Only do this when needed,
8922 * autocommands get blocked. */
8923 need_switch_win = !(tp == curtab && win == curwin);
8924 if (!need_switch_win
8925 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008926 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008927 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008928 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008929 if (varname[1] == NUL)
8930 {
8931 /* get all window-local options in a dict */
8932 dict_T *opts = get_winbuf_options(FALSE);
8933
8934 if (opts != NULL)
8935 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008936 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02008937 done = TRUE;
8938 }
8939 }
8940 else if (get_option_tv(&varname, rettv, 1) == OK)
8941 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008942 done = TRUE;
8943 }
8944 else
8945 {
8946 /* Look up the variable. */
8947 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8948 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8949 varname, FALSE);
8950 if (v != NULL)
8951 {
8952 copy_tv(&v->di_tv, rettv);
8953 done = TRUE;
8954 }
8955 }
8956 }
8957
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008958 if (need_switch_win)
8959 /* restore previous notion of curwin */
8960 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008961 }
8962
8963 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8964 /* use the default return value */
8965 copy_tv(&argvars[off + 2], rettv);
8966
8967 --emsg_off;
8968}
8969
8970/*
8971 * "setwinvar()" and "settabwinvar()" functions
8972 */
8973 void
8974setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8975{
8976 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008977 win_T *save_curwin;
8978 tabpage_T *save_curtab;
8979 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008980 char_u *varname, *winvarname;
8981 typval_T *varp;
8982 char_u nbuf[NUMBUFLEN];
8983 tabpage_T *tp = NULL;
8984
Bram Moolenaare0fb7d12019-02-12 20:48:10 +01008985 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008986 return;
8987
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008988 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008989 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008990 else
8991 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008992 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008993 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008994 varp = &argvars[off + 2];
8995
8996 if (win != NULL && varname != NULL && varp != NULL)
8997 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008998 need_switch_win = !(tp == curtab && win == curwin);
8999 if (!need_switch_win
9000 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009001 {
9002 if (*varname == '&')
9003 {
9004 long numval;
9005 char_u *strval;
9006 int error = FALSE;
9007
9008 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009009 numval = (long)tv_get_number_chk(varp, &error);
9010 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009011 if (!error && strval != NULL)
9012 set_option_value(varname, numval, strval, OPT_LOCAL);
9013 }
9014 else
9015 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02009016 winvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009017 if (winvarname != NULL)
9018 {
9019 STRCPY(winvarname, "w:");
9020 STRCPY(winvarname + 2, varname);
9021 set_var(winvarname, varp, TRUE);
9022 vim_free(winvarname);
9023 }
9024 }
9025 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009026 if (need_switch_win)
9027 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009028 }
9029}
9030
9031/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
9033 * "arg" points to the "&" or '+' when called, to "option" when returning.
9034 * Returns NULL when no option name found. Otherwise pointer to the char
9035 * after the option name.
9036 */
9037 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009038find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039{
9040 char_u *p = *arg;
9041
9042 ++p;
9043 if (*p == 'g' && p[1] == ':')
9044 {
9045 *opt_flags = OPT_GLOBAL;
9046 p += 2;
9047 }
9048 else if (*p == 'l' && p[1] == ':')
9049 {
9050 *opt_flags = OPT_LOCAL;
9051 p += 2;
9052 }
9053 else
9054 *opt_flags = 0;
9055
9056 if (!ASCII_ISALPHA(*p))
9057 return NULL;
9058 *arg = p;
9059
9060 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
9061 p += 4; /* termcap option */
9062 else
9063 while (ASCII_ISALPHA(*p))
9064 ++p;
9065 return p;
9066}
9067
9068/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009069 * Return the autoload script name for a function or variable name.
9070 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02009072 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009073autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02009074{
Bram Moolenaara1544c02013-05-30 12:35:52 +02009075 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009076 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02009077
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009078 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaar964b3742019-05-24 18:54:09 +02009079 scriptname = alloc(STRLEN(name) + 14);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009080 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02009081 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009082 STRCPY(scriptname, "autoload/");
9083 STRCAT(scriptname, name);
9084 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
9085 STRCAT(scriptname, ".vim");
9086 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
9087 *p = '/';
9088 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009089}
9090
9091/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009092 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009093 * Return TRUE if a package was loaded.
9094 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009095 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009096script_autoload(
9097 char_u *name,
9098 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009099{
9100 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009101 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009102 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009103 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009104
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009105 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00009106 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009107 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009108 return FALSE;
9109
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009110 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009111
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009112 /* Find the name in the list of previously loaded package names. Skip
9113 * "autoload/", it's always the same. */
9114 for (i = 0; i < ga_loaded.ga_len; ++i)
9115 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
9116 break;
9117 if (!reload && i < ga_loaded.ga_len)
9118 ret = FALSE; /* was loaded already */
9119 else
9120 {
9121 /* Remember the name if it wasn't loaded already. */
9122 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
9123 {
9124 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
9125 tofree = NULL;
9126 }
9127
9128 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009129 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009130 ret = TRUE;
9131 }
9132
9133 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009134 return ret;
9135}
9136
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
9138typedef enum
9139{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009140 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
9141 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
9142 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143} var_flavour_T;
9144
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01009146var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147{
9148 char_u *p = varname;
9149
9150 if (ASCII_ISUPPER(*p))
9151 {
9152 while (*(++p))
9153 if (ASCII_ISLOWER(*p))
9154 return VAR_FLAVOUR_SESSION;
9155 return VAR_FLAVOUR_VIMINFO;
9156 }
9157 else
9158 return VAR_FLAVOUR_DEFAULT;
9159}
9160#endif
9161
9162#if defined(FEAT_VIMINFO) || defined(PROTO)
9163/*
9164 * Restore global vars that start with a capital from the viminfo file
9165 */
9166 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009167read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168{
9169 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009170 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00009171 typval_T tv;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009172 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173
9174 if (!writing && (find_viminfo_parameter('!') != NULL))
9175 {
9176 tab = vim_strchr(virp->vir_line + 1, '\t');
9177 if (tab != NULL)
9178 {
9179 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009180 switch (*tab)
9181 {
9182 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009183#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009184 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009185#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009186 case 'D': type = VAR_DICT; break;
9187 case 'L': type = VAR_LIST; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009188 case 'B': type = VAR_BLOB; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009189 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191
9192 tab = vim_strchr(tab, '\t');
9193 if (tab != NULL)
9194 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009195 tv.v_type = type;
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009196 if (type == VAR_STRING || type == VAR_DICT
9197 || type == VAR_LIST || type == VAR_BLOB)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009198 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009200#ifdef FEAT_FLOAT
9201 else if (type == VAR_FLOAT)
9202 (void)string2float(tab + 1, &tv.vval.v_float);
9203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009205 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009206 if (type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009207 {
9208 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
9209
9210 if (etv == NULL)
9211 /* Failed to parse back the dict or list, use it as a
9212 * string. */
9213 tv.v_type = VAR_STRING;
9214 else
9215 {
9216 vim_free(tv.vval.v_string);
9217 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01009218 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009219 }
9220 }
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009221 else if (type == VAR_BLOB)
9222 {
9223 blob_T *blob = string2blob(tv.vval.v_string);
9224
9225 if (blob == NULL)
9226 // Failed to parse back the blob, use it as a string.
9227 tv.v_type = VAR_STRING;
9228 else
9229 {
9230 vim_free(tv.vval.v_string);
9231 tv.v_type = VAR_BLOB;
9232 tv.vval.v_blob = blob;
9233 }
9234 }
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009235
Bram Moolenaarb20e3342016-01-18 23:29:01 +01009236 /* when in a function use global variables */
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009237 save_funccal(&funccal_entry);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009238 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009239 restore_funccal();
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009240
9241 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009242 vim_free(tv.vval.v_string);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009243 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST ||
9244 tv.v_type == VAR_BLOB)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009245 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 }
9247 }
9248 }
9249
9250 return viminfo_readline(virp);
9251}
9252
9253/*
9254 * Write global vars that start with a capital to the viminfo file
9255 */
9256 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009257write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258{
Bram Moolenaar33570922005-01-25 22:26:29 +00009259 hashitem_T *hi;
9260 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009261 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01009262 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009263 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009264 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009265 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266
9267 if (find_viminfo_parameter('!') == NULL)
9268 return;
9269
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02009270 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00009271
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009272 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009273 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009275 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009277 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009278 this_var = HI2DI(hi);
9279 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009280 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009281 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00009282 {
9283 case VAR_STRING: s = "STR"; break;
9284 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009285 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009286 case VAR_DICT: s = "DIC"; break;
9287 case VAR_LIST: s = "LIS"; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009288 case VAR_BLOB: s = "BLO"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009289 case VAR_SPECIAL: s = "XPL"; break;
9290
9291 case VAR_UNKNOWN:
9292 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009293 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01009294 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01009295 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01009296 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00009297 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009298 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009299 if (this_var->di_tv.v_type == VAR_SPECIAL)
9300 {
9301 sprintf((char *)numbuf, "%ld",
9302 (long)this_var->di_tv.vval.v_number);
9303 p = numbuf;
9304 tofree = NULL;
9305 }
9306 else
9307 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009308 if (p != NULL)
9309 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00009310 vim_free(tofree);
9311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312 }
9313 }
9314}
9315#endif
9316
9317#if defined(FEAT_SESSION) || defined(PROTO)
9318 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009319store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320{
Bram Moolenaar33570922005-01-25 22:26:29 +00009321 hashitem_T *hi;
9322 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009323 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324 char_u *p, *t;
9325
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009326 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009327 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009329 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009331 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009332 this_var = HI2DI(hi);
9333 if ((this_var->di_tv.v_type == VAR_NUMBER
9334 || this_var->di_tv.v_type == VAR_STRING)
9335 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009336 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009337 /* Escape special characters with a backslash. Turn a LF and
9338 * CR into \n and \r. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009339 p = vim_strsave_escaped(tv_get_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00009340 (char_u *)"\\\"\n\r");
9341 if (p == NULL) /* out of memory */
9342 break;
9343 for (t = p; *t != NUL; ++t)
9344 if (*t == '\n')
9345 *t = 'n';
9346 else if (*t == '\r')
9347 *t = 'r';
9348 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00009349 this_var->di_key,
9350 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9351 : ' ',
9352 p,
9353 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9354 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00009355 || put_eol(fd) == FAIL)
9356 {
9357 vim_free(p);
9358 return FAIL;
9359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360 vim_free(p);
9361 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009362#ifdef FEAT_FLOAT
9363 else if (this_var->di_tv.v_type == VAR_FLOAT
9364 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
9365 {
9366 float_T f = this_var->di_tv.vval.v_float;
9367 int sign = ' ';
9368
9369 if (f < 0)
9370 {
9371 f = -f;
9372 sign = '-';
9373 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01009374 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009375 this_var->di_key, sign, f) < 0)
9376 || put_eol(fd) == FAIL)
9377 return FAIL;
9378 }
9379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380 }
9381 }
9382 return OK;
9383}
9384#endif
9385
Bram Moolenaar661b1822005-07-28 22:36:45 +00009386/*
9387 * Display script name where an item was last set.
9388 * Should only be invoked when 'verbose' is non-zero.
9389 */
9390 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009391last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009392{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009393 char_u *p;
9394
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009395 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009396 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009397 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009398 if (p != NULL)
9399 {
9400 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01009401 msg_puts(_("\n\tLast set from "));
9402 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009403 if (script_ctx.sc_lnum > 0)
9404 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01009405 msg_puts(_(" line "));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009406 msg_outnum((long)script_ctx.sc_lnum);
9407 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009408 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009409 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009410 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00009411 }
9412}
9413
Bram Moolenaar61be3762019-03-19 23:04:17 +01009414/*
9415 * Reset v:option_new, v:option_old and v:option_type.
9416 */
Bram Moolenaar53744302015-07-17 17:38:22 +02009417 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009418reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02009419{
9420 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
9421 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
9422 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
9423}
9424
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009425/*
9426 * Prepare "gap" for an assert error and add the sourcing position.
9427 */
9428 void
9429prepare_assert_error(garray_T *gap)
9430{
9431 char buf[NUMBUFLEN];
9432
9433 ga_init2(gap, 1, 100);
9434 if (sourcing_name != NULL)
9435 {
9436 ga_concat(gap, sourcing_name);
9437 if (sourcing_lnum > 0)
9438 ga_concat(gap, (char_u *)" ");
9439 }
9440 if (sourcing_lnum > 0)
9441 {
9442 sprintf(buf, "line %ld", (long)sourcing_lnum);
9443 ga_concat(gap, (char_u *)buf);
9444 }
9445 if (sourcing_name != NULL || sourcing_lnum > 0)
9446 ga_concat(gap, (char_u *)": ");
9447}
9448
9449/*
9450 * Add an assert error to v:errors.
9451 */
9452 void
9453assert_error(garray_T *gap)
9454{
9455 struct vimvar *vp = &vimvars[VV_ERRORS];
9456
9457 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9458 /* Make sure v:errors is a list. */
9459 set_vim_var_list(VV_ERRORS, list_alloc());
9460 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9461}
9462
Bram Moolenaar65a54642018-04-28 16:56:53 +02009463 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009464assert_equal_common(typval_T *argvars, assert_type_T atype)
9465{
9466 garray_T ga;
9467
9468 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9469 != (atype == ASSERT_EQUAL))
9470 {
9471 prepare_assert_error(&ga);
9472 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9473 atype);
9474 assert_error(&ga);
9475 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009476 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009477 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009478 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009479}
9480
Bram Moolenaar65a54642018-04-28 16:56:53 +02009481 int
Bram Moolenaard96ff162018-02-18 22:13:29 +01009482assert_equalfile(typval_T *argvars)
9483{
9484 char_u buf1[NUMBUFLEN];
9485 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009486 char_u *fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
9487 char_u *fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009488 garray_T ga;
9489 FILE *fd1;
9490 FILE *fd2;
9491
9492 if (fname1 == NULL || fname2 == NULL)
Bram Moolenaar65a54642018-04-28 16:56:53 +02009493 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009494
9495 IObuff[0] = NUL;
9496 fd1 = mch_fopen((char *)fname1, READBIN);
9497 if (fd1 == NULL)
9498 {
9499 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname1);
9500 }
9501 else
9502 {
9503 fd2 = mch_fopen((char *)fname2, READBIN);
9504 if (fd2 == NULL)
9505 {
9506 fclose(fd1);
9507 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname2);
9508 }
9509 else
9510 {
9511 int c1, c2;
9512 long count = 0;
9513
9514 for (;;)
9515 {
9516 c1 = fgetc(fd1);
9517 c2 = fgetc(fd2);
9518 if (c1 == EOF)
9519 {
9520 if (c2 != EOF)
9521 STRCPY(IObuff, "first file is shorter");
9522 break;
9523 }
9524 else if (c2 == EOF)
9525 {
9526 STRCPY(IObuff, "second file is shorter");
9527 break;
9528 }
9529 else if (c1 != c2)
9530 {
9531 vim_snprintf((char *)IObuff, IOSIZE,
9532 "difference at byte %ld", count);
9533 break;
9534 }
9535 ++count;
9536 }
Bram Moolenaar30494182018-02-20 21:46:05 +01009537 fclose(fd1);
9538 fclose(fd2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009539 }
9540 }
9541 if (IObuff[0] != NUL)
9542 {
9543 prepare_assert_error(&ga);
9544 ga_concat(&ga, IObuff);
9545 assert_error(&ga);
9546 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009547 return 1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009548 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009549 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009550}
9551
Bram Moolenaar65a54642018-04-28 16:56:53 +02009552 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009553assert_match_common(typval_T *argvars, assert_type_T atype)
9554{
9555 garray_T ga;
9556 char_u buf1[NUMBUFLEN];
9557 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009558 char_u *pat = tv_get_string_buf_chk(&argvars[0], buf1);
9559 char_u *text = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009560
9561 if (pat == NULL || text == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009562 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009563 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
9564 {
9565 prepare_assert_error(&ga);
9566 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9567 atype);
9568 assert_error(&ga);
9569 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009570 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009571 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009572 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009573}
9574
Bram Moolenaar65a54642018-04-28 16:56:53 +02009575 int
Bram Moolenaar61c04492016-07-23 15:35:35 +02009576assert_inrange(typval_T *argvars)
9577{
9578 garray_T ga;
9579 int error = FALSE;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009580 char_u *tofree;
9581 char msg[200];
9582 char_u numbuf[NUMBUFLEN];
9583
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009584#ifdef FEAT_FLOAT
9585 if (argvars[0].v_type == VAR_FLOAT
9586 || argvars[1].v_type == VAR_FLOAT
9587 || argvars[2].v_type == VAR_FLOAT)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009588 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009589 float_T flower = tv_get_float(&argvars[0]);
9590 float_T fupper = tv_get_float(&argvars[1]);
9591 float_T factual = tv_get_float(&argvars[2]);
9592
9593 if (factual < flower || factual > fupper)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009594 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009595 prepare_assert_error(&ga);
9596 if (argvars[3].v_type != VAR_UNKNOWN)
9597 {
9598 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9599 vim_free(tofree);
9600 }
9601 else
9602 {
9603 vim_snprintf(msg, 200, "Expected range %g - %g, but got %g",
9604 flower, fupper, factual);
9605 ga_concat(&ga, (char_u *)msg);
9606 }
9607 assert_error(&ga);
9608 ga_clear(&ga);
9609 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009610 }
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009611 }
9612 else
9613#endif
9614 {
9615 varnumber_T lower = tv_get_number_chk(&argvars[0], &error);
9616 varnumber_T upper = tv_get_number_chk(&argvars[1], &error);
9617 varnumber_T actual = tv_get_number_chk(&argvars[2], &error);
9618
9619 if (error)
9620 return 0;
9621 if (actual < lower || actual > upper)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009622 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009623 prepare_assert_error(&ga);
9624 if (argvars[3].v_type != VAR_UNKNOWN)
9625 {
9626 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9627 vim_free(tofree);
9628 }
9629 else
9630 {
9631 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
Bram Moolenaar61c04492016-07-23 15:35:35 +02009632 (long)lower, (long)upper, (long)actual);
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009633 ga_concat(&ga, (char_u *)msg);
9634 }
9635 assert_error(&ga);
9636 ga_clear(&ga);
9637 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009638 }
Bram Moolenaar61c04492016-07-23 15:35:35 +02009639 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009640 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009641}
9642
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009643/*
9644 * Common for assert_true() and assert_false().
Bram Moolenaar65a54642018-04-28 16:56:53 +02009645 * Return non-zero for failure.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009646 */
Bram Moolenaar65a54642018-04-28 16:56:53 +02009647 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009648assert_bool(typval_T *argvars, int isTrue)
9649{
9650 int error = FALSE;
9651 garray_T ga;
9652
9653 if (argvars[0].v_type == VAR_SPECIAL
9654 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar65a54642018-04-28 16:56:53 +02009655 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009656 if (argvars[0].v_type != VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009657 || (tv_get_number_chk(&argvars[0], &error) == 0) == isTrue
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009658 || error)
9659 {
9660 prepare_assert_error(&ga);
9661 fill_assert_error(&ga, &argvars[1],
9662 (char_u *)(isTrue ? "True" : "False"),
9663 NULL, &argvars[0], ASSERT_OTHER);
9664 assert_error(&ga);
9665 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009666 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009667 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009668 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009669}
9670
Bram Moolenaar65a54642018-04-28 16:56:53 +02009671 int
Bram Moolenaar42205552017-03-18 19:42:22 +01009672assert_report(typval_T *argvars)
9673{
9674 garray_T ga;
9675
9676 prepare_assert_error(&ga);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009677 ga_concat(&ga, tv_get_string(&argvars[0]));
Bram Moolenaar42205552017-03-18 19:42:22 +01009678 assert_error(&ga);
9679 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009680 return 1;
Bram Moolenaar42205552017-03-18 19:42:22 +01009681}
9682
Bram Moolenaar65a54642018-04-28 16:56:53 +02009683 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009684assert_exception(typval_T *argvars)
9685{
9686 garray_T ga;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009687 char_u *error = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009688
9689 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9690 {
9691 prepare_assert_error(&ga);
9692 ga_concat(&ga, (char_u *)"v:exception is not set");
9693 assert_error(&ga);
9694 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009695 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009696 }
9697 else if (error != NULL
9698 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9699 {
9700 prepare_assert_error(&ga);
9701 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9702 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9703 assert_error(&ga);
9704 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009705 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009706 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009707 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009708}
9709
Bram Moolenaar65a54642018-04-28 16:56:53 +02009710 int
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009711assert_beeps(typval_T *argvars)
9712{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009713 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009714 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009715 int ret = 0;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009716
9717 called_vim_beep = FALSE;
9718 suppress_errthrow = TRUE;
9719 emsg_silent = FALSE;
9720 do_cmdline_cmd(cmd);
9721 if (!called_vim_beep)
9722 {
9723 prepare_assert_error(&ga);
9724 ga_concat(&ga, (char_u *)"command did not beep: ");
9725 ga_concat(&ga, cmd);
9726 assert_error(&ga);
9727 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009728 ret = 1;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009729 }
9730
9731 suppress_errthrow = FALSE;
9732 emsg_on_display = FALSE;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009733 return ret;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009734}
9735
Bram Moolenaar25190db2019-05-04 15:05:28 +02009736 static void
9737assert_append_cmd_or_arg(garray_T *gap, typval_T *argvars, char_u *cmd)
9738{
9739 char_u *tofree;
9740 char_u numbuf[NUMBUFLEN];
9741
9742 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9743 {
9744 ga_concat(gap, echo_string(&argvars[2], &tofree, numbuf, 0));
9745 vim_free(tofree);
9746 }
9747 else
9748 ga_concat(gap, cmd);
9749}
9750
Bram Moolenaar65a54642018-04-28 16:56:53 +02009751 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009752assert_fails(typval_T *argvars)
9753{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009754 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009755 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009756 int ret = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009757
9758 called_emsg = FALSE;
9759 suppress_errthrow = TRUE;
9760 emsg_silent = TRUE;
9761 do_cmdline_cmd(cmd);
9762 if (!called_emsg)
9763 {
9764 prepare_assert_error(&ga);
9765 ga_concat(&ga, (char_u *)"command did not fail: ");
Bram Moolenaar25190db2019-05-04 15:05:28 +02009766 assert_append_cmd_or_arg(&ga, argvars, cmd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009767 assert_error(&ga);
9768 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009769 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009770 }
9771 else if (argvars[1].v_type != VAR_UNKNOWN)
9772 {
9773 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009774 char *error = (char *)tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009775
9776 if (error == NULL
9777 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9778 {
9779 prepare_assert_error(&ga);
9780 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9781 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaar25190db2019-05-04 15:05:28 +02009782 ga_concat(&ga, (char_u *)": ");
9783 assert_append_cmd_or_arg(&ga, argvars, cmd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009784 assert_error(&ga);
9785 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009786 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009787 }
9788 }
9789
9790 called_emsg = FALSE;
9791 suppress_errthrow = FALSE;
9792 emsg_silent = FALSE;
9793 emsg_on_display = FALSE;
9794 set_vim_var_string(VV_ERRMSG, NULL, 0);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009795 return ret;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009796}
9797
9798/*
Bram Moolenaar86576712019-01-25 20:48:33 +01009799 * Append "p[clen]" to "gap", escaping unprintable characters.
9800 * Changes NL to \n, CR to \r, etc.
9801 */
9802 static void
9803ga_concat_esc(garray_T *gap, char_u *p, int clen)
9804{
9805 char_u buf[NUMBUFLEN];
9806
9807 if (clen > 1)
9808 {
9809 mch_memmove(buf, p, clen);
9810 buf[clen] = NUL;
9811 ga_concat(gap, buf);
9812 }
9813 else switch (*p)
9814 {
9815 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9816 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9817 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9818 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9819 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9820 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9821 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9822 default:
9823 if (*p < ' ')
9824 {
9825 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9826 ga_concat(gap, buf);
9827 }
9828 else
9829 ga_append(gap, *p);
9830 break;
9831 }
9832}
9833
9834/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009835 * Append "str" to "gap", escaping unprintable characters.
9836 * Changes NL to \n, CR to \r, etc.
9837 */
9838 static void
Bram Moolenaar86576712019-01-25 20:48:33 +01009839ga_concat_shorten_esc(garray_T *gap, char_u *str)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009840{
9841 char_u *p;
Bram Moolenaar86576712019-01-25 20:48:33 +01009842 char_u *s;
9843 int c;
9844 int clen;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009845 char_u buf[NUMBUFLEN];
Bram Moolenaar86576712019-01-25 20:48:33 +01009846 int same_len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009847
9848 if (str == NULL)
9849 {
9850 ga_concat(gap, (char_u *)"NULL");
9851 return;
9852 }
9853
9854 for (p = str; *p != NUL; ++p)
Bram Moolenaar86576712019-01-25 20:48:33 +01009855 {
9856 same_len = 1;
9857 s = p;
9858 c = mb_ptr2char_adv(&s);
9859 clen = s - p;
9860 while (*s != NUL && c == mb_ptr2char(s))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009861 {
Bram Moolenaar86576712019-01-25 20:48:33 +01009862 ++same_len;
9863 s += clen;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009864 }
Bram Moolenaar86576712019-01-25 20:48:33 +01009865 if (same_len > 20)
9866 {
9867 ga_concat(gap, (char_u *)"\\[");
9868 ga_concat_esc(gap, p, clen);
9869 ga_concat(gap, (char_u *)" occurs ");
9870 vim_snprintf((char *)buf, NUMBUFLEN, "%d", same_len);
9871 ga_concat(gap, buf);
9872 ga_concat(gap, (char_u *)" times]");
9873 p = s - 1;
9874 }
9875 else
9876 ga_concat_esc(gap, p, clen);
9877 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009878}
9879
9880/*
9881 * Fill "gap" with information about an assert error.
9882 */
9883 void
9884fill_assert_error(
9885 garray_T *gap,
9886 typval_T *opt_msg_tv,
9887 char_u *exp_str,
9888 typval_T *exp_tv,
9889 typval_T *got_tv,
9890 assert_type_T atype)
9891{
9892 char_u numbuf[NUMBUFLEN];
9893 char_u *tofree;
9894
9895 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9896 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009897 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9898 vim_free(tofree);
9899 ga_concat(gap, (char_u *)": ");
9900 }
9901
9902 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9903 ga_concat(gap, (char_u *)"Pattern ");
9904 else if (atype == ASSERT_NOTEQUAL)
9905 ga_concat(gap, (char_u *)"Expected not equal to ");
9906 else
9907 ga_concat(gap, (char_u *)"Expected ");
9908 if (exp_str == NULL)
9909 {
Bram Moolenaar86576712019-01-25 20:48:33 +01009910 ga_concat_shorten_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009911 vim_free(tofree);
9912 }
9913 else
Bram Moolenaar86576712019-01-25 20:48:33 +01009914 ga_concat_shorten_esc(gap, exp_str);
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009915 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009916 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009917 if (atype == ASSERT_MATCH)
9918 ga_concat(gap, (char_u *)" does not match ");
9919 else if (atype == ASSERT_NOTMATCH)
9920 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009921 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009922 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar86576712019-01-25 20:48:33 +01009923 ga_concat_shorten_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009924 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009925 }
9926}
9927
Bram Moolenaar31988702018-02-13 12:57:42 +01009928/*
9929 * Compare "typ1" and "typ2". Put the result in "typ1".
9930 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009931 int
9932typval_compare(
9933 typval_T *typ1, /* first operand */
9934 typval_T *typ2, /* second operand */
9935 exptype_T type, /* operator */
9936 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +01009937 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009938{
9939 int i;
9940 varnumber_T n1, n2;
9941 char_u *s1, *s2;
9942 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
9943
Bram Moolenaar31988702018-02-13 12:57:42 +01009944 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009945 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009946 /* For "is" a different type always means FALSE, for "notis"
9947 * it means TRUE. */
9948 n1 = (type == TYPE_NEQUAL);
9949 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009950 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
9951 {
9952 if (type_is)
9953 {
9954 n1 = (typ1->v_type == typ2->v_type
9955 && typ1->vval.v_blob == typ2->vval.v_blob);
9956 if (type == TYPE_NEQUAL)
9957 n1 = !n1;
9958 }
9959 else if (typ1->v_type != typ2->v_type
9960 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9961 {
9962 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009963 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009964 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009965 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009966 clear_tv(typ1);
9967 return FAIL;
9968 }
9969 else
9970 {
9971 // Compare two Blobs for being equal or unequal.
9972 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
9973 if (type == TYPE_NEQUAL)
9974 n1 = !n1;
9975 }
9976 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009977 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
9978 {
9979 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009980 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009981 n1 = (typ1->v_type == typ2->v_type
9982 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009983 if (type == TYPE_NEQUAL)
9984 n1 = !n1;
9985 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009986 else if (typ1->v_type != typ2->v_type
9987 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009988 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009989 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009990 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009991 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009992 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009993 clear_tv(typ1);
9994 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009995 }
9996 else
9997 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009998 /* Compare two Lists for being equal or unequal. */
9999 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
10000 ic, FALSE);
10001 if (type == TYPE_NEQUAL)
10002 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010003 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010004 }
Bram Moolenaar31988702018-02-13 12:57:42 +010010005
10006 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
10007 {
10008 if (type_is)
10009 {
10010 n1 = (typ1->v_type == typ2->v_type
10011 && typ1->vval.v_dict == typ2->vval.v_dict);
10012 if (type == TYPE_NEQUAL)
10013 n1 = !n1;
10014 }
10015 else if (typ1->v_type != typ2->v_type
10016 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
10017 {
10018 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010019 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +010010020 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010021 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +010010022 clear_tv(typ1);
10023 return FAIL;
10024 }
10025 else
10026 {
10027 /* Compare two Dictionaries for being equal or unequal. */
10028 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
10029 ic, FALSE);
10030 if (type == TYPE_NEQUAL)
10031 n1 = !n1;
10032 }
10033 }
10034
10035 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
10036 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
10037 {
10038 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
10039 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010040 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +010010041 clear_tv(typ1);
10042 return FAIL;
10043 }
10044 if ((typ1->v_type == VAR_PARTIAL
10045 && typ1->vval.v_partial == NULL)
10046 || (typ2->v_type == VAR_PARTIAL
10047 && typ2->vval.v_partial == NULL))
10048 /* when a partial is NULL assume not equal */
10049 n1 = FALSE;
10050 else if (type_is)
10051 {
10052 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
10053 /* strings are considered the same if their value is
10054 * the same */
10055 n1 = tv_equal(typ1, typ2, ic, FALSE);
10056 else if (typ1->v_type == VAR_PARTIAL
10057 && typ2->v_type == VAR_PARTIAL)
10058 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
10059 else
10060 n1 = FALSE;
10061 }
10062 else
10063 n1 = tv_equal(typ1, typ2, ic, FALSE);
10064 if (type == TYPE_NEQUAL)
10065 n1 = !n1;
10066 }
10067
10068#ifdef FEAT_FLOAT
10069 /*
10070 * If one of the two variables is a float, compare as a float.
10071 * When using "=~" or "!~", always compare as string.
10072 */
10073 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
10074 && type != TYPE_MATCH && type != TYPE_NOMATCH)
10075 {
10076 float_T f1, f2;
10077
Bram Moolenaar38f08e72019-02-20 22:04:32 +010010078 f1 = tv_get_float(typ1);
10079 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +010010080 n1 = FALSE;
10081 switch (type)
10082 {
10083 case TYPE_EQUAL: n1 = (f1 == f2); break;
10084 case TYPE_NEQUAL: n1 = (f1 != f2); break;
10085 case TYPE_GREATER: n1 = (f1 > f2); break;
10086 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
10087 case TYPE_SMALLER: n1 = (f1 < f2); break;
10088 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
10089 case TYPE_UNKNOWN:
10090 case TYPE_MATCH:
10091 case TYPE_NOMATCH: break; /* avoid gcc warning */
10092 }
10093 }
10094#endif
10095
10096 /*
10097 * If one of the two variables is a number, compare as a number.
10098 * When using "=~" or "!~", always compare as string.
10099 */
10100 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
10101 && type != TYPE_MATCH && type != TYPE_NOMATCH)
10102 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010103 n1 = tv_get_number(typ1);
10104 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +010010105 switch (type)
10106 {
10107 case TYPE_EQUAL: n1 = (n1 == n2); break;
10108 case TYPE_NEQUAL: n1 = (n1 != n2); break;
10109 case TYPE_GREATER: n1 = (n1 > n2); break;
10110 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
10111 case TYPE_SMALLER: n1 = (n1 < n2); break;
10112 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
10113 case TYPE_UNKNOWN:
10114 case TYPE_MATCH:
10115 case TYPE_NOMATCH: break; /* avoid gcc warning */
10116 }
10117 }
10118 else
10119 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010120 s1 = tv_get_string_buf(typ1, buf1);
10121 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar31988702018-02-13 12:57:42 +010010122 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
10123 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
10124 else
10125 i = 0;
10126 n1 = FALSE;
10127 switch (type)
10128 {
10129 case TYPE_EQUAL: n1 = (i == 0); break;
10130 case TYPE_NEQUAL: n1 = (i != 0); break;
10131 case TYPE_GREATER: n1 = (i > 0); break;
10132 case TYPE_GEQUAL: n1 = (i >= 0); break;
10133 case TYPE_SMALLER: n1 = (i < 0); break;
10134 case TYPE_SEQUAL: n1 = (i <= 0); break;
10135
10136 case TYPE_MATCH:
10137 case TYPE_NOMATCH:
10138 n1 = pattern_match(s2, s1, ic);
10139 if (type == TYPE_NOMATCH)
10140 n1 = !n1;
10141 break;
10142
10143 case TYPE_UNKNOWN: break; /* avoid gcc warning */
10144 }
10145 }
10146 clear_tv(typ1);
10147 typ1->v_type = VAR_NUMBER;
10148 typ1->vval.v_number = n1;
10149
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010150 return OK;
10151}
10152
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010153 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010154typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010155{
10156 char_u *tofree;
10157 char_u numbuf[NUMBUFLEN];
10158 char_u *ret = NULL;
10159
10160 if (arg == NULL)
10161 return vim_strsave((char_u *)"(does not exist)");
10162 ret = tv2string(arg, &tofree, numbuf, 0);
10163 /* Make a copy if we have a value but it's not in allocated memory. */
10164 if (ret != NULL && tofree == NULL)
10165 ret = vim_strsave(ret);
10166 return ret;
10167}
10168
10169 int
10170var_exists(char_u *var)
10171{
10172 char_u *name;
10173 char_u *tofree;
10174 typval_T tv;
10175 int len = 0;
10176 int n = FALSE;
10177
10178 /* get_name_len() takes care of expanding curly braces */
10179 name = var;
10180 len = get_name_len(&var, &tofree, TRUE, FALSE);
10181 if (len > 0)
10182 {
10183 if (tofree != NULL)
10184 name = tofree;
10185 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
10186 if (n)
10187 {
10188 /* handle d.key, l[idx], f(expr) */
10189 n = (handle_subscript(&var, &tv, TRUE, FALSE) == OK);
10190 if (n)
10191 clear_tv(&tv);
10192 }
10193 }
10194 if (*var != NUL)
10195 n = FALSE;
10196
10197 vim_free(tofree);
10198 return n;
10199}
10200
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201#endif /* FEAT_EVAL */
10202
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010204#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205
Bram Moolenaar4f974752019-02-17 17:44:42 +010010206#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207/*
10208 * Functions for ":8" filename modifier: get 8.3 version of a filename.
10209 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210
10211/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010212 * Get the short path (8.3) for the filename in "fnamep".
10213 * Only works for a valid file name.
10214 * When the path gets longer "fnamep" is changed and the allocated buffer
10215 * is put in "bufp".
10216 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
10217 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218 */
10219 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010220get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010222 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223 char_u *newbuf;
10224
10225 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010226 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227 if (l > len - 1)
10228 {
10229 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010230 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231 newbuf = vim_strnsave(*fnamep, l);
10232 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010233 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234
10235 vim_free(*bufp);
10236 *fnamep = *bufp = newbuf;
10237
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010238 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010239 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240 }
10241
10242 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010243 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244}
10245
10246/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010247 * Get the short path (8.3) for the filename in "fname". The converted
10248 * path is returned in "bufp".
10249 *
10250 * Some of the directories specified in "fname" may not exist. This function
10251 * will shorten the existing directories at the beginning of the path and then
10252 * append the remaining non-existing path.
10253 *
10254 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020010255 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010256 * bufp - Pointer to an allocated buffer for the filename.
10257 * fnamelen - Length of the filename pointed to by fname
10258 *
10259 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260 */
10261 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010262shortpath_for_invalid_fname(
10263 char_u **fname,
10264 char_u **bufp,
10265 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010267 char_u *short_fname, *save_fname, *pbuf_unused;
10268 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010270 int old_len, len;
10271 int new_len, sfx_len;
10272 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273
10274 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010275 old_len = *fnamelen;
10276 save_fname = vim_strnsave(*fname, old_len);
10277 pbuf_unused = NULL;
10278 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010279
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010280 endp = save_fname + old_len - 1; /* Find the end of the copy */
10281 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010283 /*
10284 * Try shortening the supplied path till it succeeds by removing one
10285 * directory at a time from the tail of the path.
10286 */
10287 len = 0;
10288 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010290 /* go back one path-separator */
10291 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
10292 --endp;
10293 if (endp <= save_fname)
10294 break; /* processed the complete path */
10295
10296 /*
10297 * Replace the path separator with a NUL and try to shorten the
10298 * resulting path.
10299 */
10300 ch = *endp;
10301 *endp = 0;
10302 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010303 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010304 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
10305 {
10306 retval = FAIL;
10307 goto theend;
10308 }
10309 *endp = ch; /* preserve the string */
10310
10311 if (len > 0)
10312 break; /* successfully shortened the path */
10313
10314 /* failed to shorten the path. Skip the path separator */
10315 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316 }
10317
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010318 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010320 /*
10321 * Succeeded in shortening the path. Now concatenate the shortened
10322 * path with the remaining path at the tail.
10323 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010324
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010325 /* Compute the length of the new path. */
10326 sfx_len = (int)(save_endp - endp) + 1;
10327 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010329 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010331 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010333 /* There is not enough space in the currently allocated string,
10334 * copy it to a buffer big enough. */
10335 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010337 {
10338 retval = FAIL;
10339 goto theend;
10340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341 }
10342 else
10343 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010344 /* Transfer short_fname to the main buffer (it's big enough),
10345 * unless get_short_pathname() did its work in-place. */
10346 *fname = *bufp = save_fname;
10347 if (short_fname != save_fname)
10348 vim_strncpy(save_fname, short_fname, len);
10349 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010351
10352 /* concat the not-shortened part of the path */
10353 vim_strncpy(*fname + len, endp, sfx_len);
10354 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010355 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010356
10357theend:
10358 vim_free(pbuf_unused);
10359 vim_free(save_fname);
10360
10361 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362}
10363
10364/*
10365 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010366 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010367 */
10368 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010369shortpath_for_partial(
10370 char_u **fnamep,
10371 char_u **bufp,
10372 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373{
10374 int sepcount, len, tflen;
10375 char_u *p;
10376 char_u *pbuf, *tfname;
10377 int hasTilde;
10378
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010379 /* Count up the path separators from the RHS.. so we know which part
10380 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010382 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383 if (vim_ispathsep(*p))
10384 ++sepcount;
10385
10386 /* Need full path first (use expand_env() to remove a "~/") */
10387 hasTilde = (**fnamep == '~');
10388 if (hasTilde)
10389 pbuf = tfname = expand_env_save(*fnamep);
10390 else
10391 pbuf = tfname = FullName_save(*fnamep, FALSE);
10392
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010393 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010395 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
10396 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010397
10398 if (len == 0)
10399 {
10400 /* Don't have a valid filename, so shorten the rest of the
10401 * path if we can. This CAN give us invalid 8.3 filenames, but
10402 * there's not a lot of point in guessing what it might be.
10403 */
10404 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010405 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
10406 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407 }
10408
10409 /* Count the paths backward to find the beginning of the desired string. */
10410 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010411 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010412 if (has_mbyte)
10413 p -= mb_head_off(tfname, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010414 if (vim_ispathsep(*p))
10415 {
10416 if (sepcount == 0 || (hasTilde && sepcount == 1))
10417 break;
10418 else
10419 sepcount --;
10420 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422 if (hasTilde)
10423 {
10424 --p;
10425 if (p >= tfname)
10426 *p = '~';
10427 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010428 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429 }
10430 else
10431 ++p;
10432
10433 /* Copy in the string - p indexes into tfname - allocated at pbuf */
10434 vim_free(*bufp);
10435 *fnamelen = (int)STRLEN(p);
10436 *bufp = pbuf;
10437 *fnamep = p;
10438
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010439 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440}
Bram Moolenaar4f974752019-02-17 17:44:42 +010010441#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442
10443/*
10444 * Adjust a filename, according to a string of modifiers.
10445 * *fnamep must be NUL terminated when called. When returning, the length is
10446 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010447 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448 * When there is an error, *fnamep is set to NULL.
10449 */
10450 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010451modify_fname(
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010452 char_u *src, // string with modifiers
10453 int tilde_file, // "~" is a file name, not $HOME
10454 int *usedlen, // characters after src that are used
10455 char_u **fnamep, // file name so far
10456 char_u **bufp, // buffer for allocated file name or NULL
10457 int *fnamelen) // length of fnamep
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458{
10459 int valid = 0;
10460 char_u *tail;
10461 char_u *s, *p, *pbuf;
10462 char_u dirname[MAXPATHL];
10463 int c;
10464 int has_fullname = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010465#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010466 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467 int has_shortname = 0;
10468#endif
10469
10470repeat:
10471 /* ":p" - full path/file_name */
10472 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
10473 {
10474 has_fullname = 1;
10475
10476 valid |= VALID_PATH;
10477 *usedlen += 2;
10478
10479 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
10480 if ((*fnamep)[0] == '~'
10481#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
10482 && ((*fnamep)[1] == '/'
10483# ifdef BACKSLASH_IN_FILENAME
10484 || (*fnamep)[1] == '\\'
10485# endif
10486 || (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010488 && !(tilde_file && (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489 )
10490 {
10491 *fnamep = expand_env_save(*fnamep);
10492 vim_free(*bufp); /* free any allocated file name */
10493 *bufp = *fnamep;
10494 if (*fnamep == NULL)
10495 return -1;
10496 }
10497
10498 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010499 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500 {
10501 if (vim_ispathsep(*p)
10502 && p[1] == '.'
10503 && (p[2] == NUL
10504 || vim_ispathsep(p[2])
10505 || (p[2] == '.'
10506 && (p[3] == NUL || vim_ispathsep(p[3])))))
10507 break;
10508 }
10509
10510 /* FullName_save() is slow, don't use it when not needed. */
10511 if (*p != NUL || !vim_isAbsName(*fnamep))
10512 {
10513 *fnamep = FullName_save(*fnamep, *p != NUL);
10514 vim_free(*bufp); /* free any allocated file name */
10515 *bufp = *fnamep;
10516 if (*fnamep == NULL)
10517 return -1;
10518 }
10519
Bram Moolenaar4f974752019-02-17 17:44:42 +010010520#ifdef MSWIN
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010521# if _WIN32_WINNT >= 0x0500
10522 if (vim_strchr(*fnamep, '~') != NULL)
10523 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010524 // Expand 8.3 filename to full path. Needed to make sure the same
10525 // file does not have two different names.
10526 // Note: problem does not occur if _WIN32_WINNT < 0x0500.
10527 WCHAR *wfname = enc_to_utf16(*fnamep, NULL);
10528 WCHAR buf[_MAX_PATH];
10529
10530 if (wfname != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010531 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010532 if (GetLongPathNameW(wfname, buf, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010533 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010534 char_u *p = utf16_to_enc(buf, NULL);
10535
10536 if (p != NULL)
10537 {
10538 vim_free(*bufp); // free any allocated file name
10539 *bufp = *fnamep = p;
10540 }
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010541 }
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010542 vim_free(wfname);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010543 }
10544 }
10545# endif
10546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547 /* Append a path separator to a directory. */
10548 if (mch_isdir(*fnamep))
10549 {
10550 /* Make room for one or two extra characters. */
10551 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
10552 vim_free(*bufp); /* free any allocated file name */
10553 *bufp = *fnamep;
10554 if (*fnamep == NULL)
10555 return -1;
10556 add_pathsep(*fnamep);
10557 }
10558 }
10559
10560 /* ":." - path relative to the current directory */
10561 /* ":~" - path relative to the home directory */
10562 /* ":8" - shortname path - postponed till after */
10563 while (src[*usedlen] == ':'
10564 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
10565 {
10566 *usedlen += 2;
10567 if (c == '8')
10568 {
Bram Moolenaar4f974752019-02-17 17:44:42 +010010569#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570 has_shortname = 1; /* Postpone this. */
10571#endif
10572 continue;
10573 }
10574 pbuf = NULL;
10575 /* Need full path first (use expand_env() to remove a "~/") */
10576 if (!has_fullname)
10577 {
10578 if (c == '.' && **fnamep == '~')
10579 p = pbuf = expand_env_save(*fnamep);
10580 else
10581 p = pbuf = FullName_save(*fnamep, FALSE);
10582 }
10583 else
10584 p = *fnamep;
10585
10586 has_fullname = 0;
10587
10588 if (p != NULL)
10589 {
10590 if (c == '.')
10591 {
10592 mch_dirname(dirname, MAXPATHL);
10593 s = shorten_fname(p, dirname);
10594 if (s != NULL)
10595 {
10596 *fnamep = s;
10597 if (pbuf != NULL)
10598 {
10599 vim_free(*bufp); /* free any allocated file name */
10600 *bufp = pbuf;
10601 pbuf = NULL;
10602 }
10603 }
10604 }
10605 else
10606 {
10607 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
10608 /* Only replace it when it starts with '~' */
10609 if (*dirname == '~')
10610 {
10611 s = vim_strsave(dirname);
10612 if (s != NULL)
10613 {
10614 *fnamep = s;
10615 vim_free(*bufp);
10616 *bufp = s;
10617 }
10618 }
10619 }
10620 vim_free(pbuf);
10621 }
10622 }
10623
10624 tail = gettail(*fnamep);
10625 *fnamelen = (int)STRLEN(*fnamep);
10626
10627 /* ":h" - head, remove "/file_name", can be repeated */
10628 /* Don't remove the first "/" or "c:\" */
10629 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
10630 {
10631 valid |= VALID_HEAD;
10632 *usedlen += 2;
10633 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010634 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010635 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636 *fnamelen = (int)(tail - *fnamep);
10637#ifdef VMS
10638 if (*fnamelen > 0)
10639 *fnamelen += 1; /* the path separator is part of the path */
10640#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010641 if (*fnamelen == 0)
10642 {
10643 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
10644 p = vim_strsave((char_u *)".");
10645 if (p == NULL)
10646 return -1;
10647 vim_free(*bufp);
10648 *bufp = *fnamep = tail = p;
10649 *fnamelen = 1;
10650 }
10651 else
10652 {
10653 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010654 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010656 }
10657
10658 /* ":8" - shortname */
10659 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
10660 {
10661 *usedlen += 2;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010662#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663 has_shortname = 1;
10664#endif
10665 }
10666
Bram Moolenaar4f974752019-02-17 17:44:42 +010010667#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010668 /*
10669 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010670 */
10671 if (has_shortname)
10672 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010673 /* Copy the string if it is shortened by :h and when it wasn't copied
10674 * yet, because we are going to change it in place. Avoids changing
10675 * the buffer name for "%:8". */
10676 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677 {
10678 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020010679 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680 return -1;
10681 vim_free(*bufp);
10682 *bufp = *fnamep = p;
10683 }
10684
10685 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020010686 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687 if (!has_fullname && !vim_isAbsName(*fnamep))
10688 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010689 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690 return -1;
10691 }
10692 else
10693 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010694 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695
Bram Moolenaardc935552011-08-17 15:23:23 +020010696 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010698 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010699 return -1;
10700
10701 if (l == 0)
10702 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010703 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010705 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706 return -1;
10707 }
10708 *fnamelen = l;
10709 }
10710 }
Bram Moolenaar4f974752019-02-17 17:44:42 +010010711#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010712
10713 /* ":t" - tail, just the basename */
10714 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
10715 {
10716 *usedlen += 2;
10717 *fnamelen -= (int)(tail - *fnamep);
10718 *fnamep = tail;
10719 }
10720
10721 /* ":e" - extension, can be repeated */
10722 /* ":r" - root, without extension, can be repeated */
10723 while (src[*usedlen] == ':'
10724 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
10725 {
10726 /* find a '.' in the tail:
10727 * - for second :e: before the current fname
10728 * - otherwise: The last '.'
10729 */
10730 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
10731 s = *fnamep - 2;
10732 else
10733 s = *fnamep + *fnamelen - 1;
10734 for ( ; s > tail; --s)
10735 if (s[0] == '.')
10736 break;
10737 if (src[*usedlen + 1] == 'e') /* :e */
10738 {
10739 if (s > tail)
10740 {
10741 *fnamelen += (int)(*fnamep - (s + 1));
10742 *fnamep = s + 1;
10743#ifdef VMS
10744 /* cut version from the extension */
10745 s = *fnamep + *fnamelen - 1;
10746 for ( ; s > *fnamep; --s)
10747 if (s[0] == ';')
10748 break;
10749 if (s > *fnamep)
10750 *fnamelen = s - *fnamep;
10751#endif
10752 }
10753 else if (*fnamep <= tail)
10754 *fnamelen = 0;
10755 }
10756 else /* :r */
10757 {
10758 if (s > tail) /* remove one extension */
10759 *fnamelen = (int)(s - *fnamep);
10760 }
10761 *usedlen += 2;
10762 }
10763
10764 /* ":s?pat?foo?" - substitute */
10765 /* ":gs?pat?foo?" - global substitute */
10766 if (src[*usedlen] == ':'
10767 && (src[*usedlen + 1] == 's'
10768 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
10769 {
10770 char_u *str;
10771 char_u *pat;
10772 char_u *sub;
10773 int sep;
10774 char_u *flags;
10775 int didit = FALSE;
10776
10777 flags = (char_u *)"";
10778 s = src + *usedlen + 2;
10779 if (src[*usedlen + 1] == 'g')
10780 {
10781 flags = (char_u *)"g";
10782 ++s;
10783 }
10784
10785 sep = *s++;
10786 if (sep)
10787 {
10788 /* find end of pattern */
10789 p = vim_strchr(s, sep);
10790 if (p != NULL)
10791 {
10792 pat = vim_strnsave(s, (int)(p - s));
10793 if (pat != NULL)
10794 {
10795 s = p + 1;
10796 /* find end of substitution */
10797 p = vim_strchr(s, sep);
10798 if (p != NULL)
10799 {
10800 sub = vim_strnsave(s, (int)(p - s));
10801 str = vim_strnsave(*fnamep, *fnamelen);
10802 if (sub != NULL && str != NULL)
10803 {
10804 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010805 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806 if (s != NULL)
10807 {
10808 *fnamep = s;
10809 *fnamelen = (int)STRLEN(s);
10810 vim_free(*bufp);
10811 *bufp = s;
10812 didit = TRUE;
10813 }
10814 }
10815 vim_free(sub);
10816 vim_free(str);
10817 }
10818 vim_free(pat);
10819 }
10820 }
10821 /* after using ":s", repeat all the modifiers */
10822 if (didit)
10823 goto repeat;
10824 }
10825 }
10826
Bram Moolenaar26df0922014-02-23 23:39:13 +010010827 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10828 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010829 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010830 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010831 if (c != NUL)
10832 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010833 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010834 if (c != NUL)
10835 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010836 if (p == NULL)
10837 return -1;
10838 vim_free(*bufp);
10839 *bufp = *fnamep = p;
10840 *fnamelen = (int)STRLEN(p);
10841 *usedlen += 2;
10842 }
10843
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844 return valid;
10845}
10846
10847/*
10848 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010849 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850 * "flags" can be "g" to do a global substitute.
10851 * Returns an allocated string, NULL for error.
10852 */
10853 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010854do_string_sub(
10855 char_u *str,
10856 char_u *pat,
10857 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010858 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010859 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010860{
10861 int sublen;
10862 regmatch_T regmatch;
10863 int i;
10864 int do_all;
10865 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010866 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867 garray_T ga;
10868 char_u *ret;
10869 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010870 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871
10872 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
10873 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010874 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875
10876 ga_init2(&ga, 1, 200);
10877
10878 do_all = (flags[0] == 'g');
10879
10880 regmatch.rm_ic = p_ic;
10881 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10882 if (regmatch.regprog != NULL)
10883 {
10884 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010885 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
10887 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010010888 /* Skip empty match except for first match. */
10889 if (regmatch.startp[0] == regmatch.endp[0])
10890 {
10891 if (zero_width == regmatch.startp[0])
10892 {
10893 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020010894 i = MB_PTR2LEN(tail);
10895 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
10896 (size_t)i);
10897 ga.ga_len += i;
10898 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010899 continue;
10900 }
10901 zero_width = regmatch.startp[0];
10902 }
10903
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904 /*
10905 * Get some space for a temporary buffer to do the substitution
10906 * into. It will contain:
10907 * - The text up to where the match is.
10908 * - The substituted text.
10909 * - The text after the match.
10910 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010911 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010010912 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
10914 {
10915 ga_clear(&ga);
10916 break;
10917 }
10918
10919 /* copy the text up to where the match is */
10920 i = (int)(regmatch.startp[0] - tail);
10921 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10922 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010923 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924 + ga.ga_len + i, TRUE, TRUE, FALSE);
10925 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010926 tail = regmatch.endp[0];
10927 if (*tail == NUL)
10928 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929 if (!do_all)
10930 break;
10931 }
10932
10933 if (ga.ga_data != NULL)
10934 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10935
Bram Moolenaar473de612013-06-08 18:19:48 +020010936 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937 }
10938
10939 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10940 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010941 if (p_cpo == empty_option)
10942 p_cpo = save_cpo;
10943 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010944 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010945 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946
10947 return ret;
10948}
10949
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010950 static int
10951filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10952{
10953 typval_T rettv;
10954 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010955 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010956
10957 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10958 argv[0] = vimvars[VV_KEY].vv_tv;
10959 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010960 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10961 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010962 if (map)
10963 {
10964 /* map(): replace the list item value */
10965 clear_tv(tv);
10966 rettv.v_lock = 0;
10967 *tv = rettv;
10968 }
10969 else
10970 {
10971 int error = FALSE;
10972
10973 /* filter(): when expr is zero remove the item */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010974 *remp = (tv_get_number_chk(&rettv, &error) == 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010975 clear_tv(&rettv);
10976 /* On type error, nothing has been removed; return FAIL to stop the
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010977 * loop. The error message was given by tv_get_number_chk(). */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010978 if (error)
10979 goto theend;
10980 }
10981 retval = OK;
10982theend:
10983 clear_tv(&vimvars[VV_VAL].vv_tv);
10984 return retval;
10985}
10986
10987
10988/*
10989 * Implementation of map() and filter().
10990 */
10991 void
10992filter_map(typval_T *argvars, typval_T *rettv, int map)
10993{
10994 typval_T *expr;
10995 listitem_T *li, *nli;
10996 list_T *l = NULL;
10997 dictitem_T *di;
10998 hashtab_T *ht;
10999 hashitem_T *hi;
11000 dict_T *d = NULL;
11001 typval_T save_val;
11002 typval_T save_key;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010011003 blob_T *b = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011004 int rem;
11005 int todo;
11006 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
11007 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
11008 : N_("filter() argument"));
11009 int save_did_emsg;
11010 int idx = 0;
11011
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010011012 if (argvars[0].v_type == VAR_BLOB)
11013 {
11014 if ((b = argvars[0].vval.v_blob) == NULL)
11015 return;
11016 }
11017 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011018 {
11019 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011020 || (!map && var_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011021 return;
11022 }
11023 else if (argvars[0].v_type == VAR_DICT)
11024 {
11025 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011026 || (!map && var_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011027 return;
11028 }
11029 else
11030 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011031 semsg(_(e_listdictarg), ermsg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011032 return;
11033 }
11034
11035 expr = &argvars[1];
11036 /* On type errors, the preceding call has already displayed an error
11037 * message. Avoid a misleading error message for an empty string that
11038 * was not passed as argument. */
11039 if (expr->v_type != VAR_UNKNOWN)
11040 {
11041 prepare_vimvar(VV_VAL, &save_val);
11042
11043 /* We reset "did_emsg" to be able to detect whether an error
11044 * occurred during evaluation of the expression. */
11045 save_did_emsg = did_emsg;
11046 did_emsg = FALSE;
11047
11048 prepare_vimvar(VV_KEY, &save_key);
11049 if (argvars[0].v_type == VAR_DICT)
11050 {
11051 vimvars[VV_KEY].vv_type = VAR_STRING;
11052
11053 ht = &d->dv_hashtab;
11054 hash_lock(ht);
11055 todo = (int)ht->ht_used;
11056 for (hi = ht->ht_array; todo > 0; ++hi)
11057 {
11058 if (!HASHITEM_EMPTY(hi))
11059 {
11060 int r;
11061
11062 --todo;
11063 di = HI2DI(hi);
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011064 if (map && (var_check_lock(di->di_tv.v_lock,
11065 arg_errmsg, TRUE)
11066 || var_check_ro(di->di_flags,
11067 arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011068 break;
11069 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
11070 r = filter_map_one(&di->di_tv, expr, map, &rem);
11071 clear_tv(&vimvars[VV_KEY].vv_tv);
11072 if (r == FAIL || did_emsg)
11073 break;
11074 if (!map && rem)
11075 {
11076 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11077 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
11078 break;
11079 dictitem_remove(d, di);
11080 }
11081 }
11082 }
11083 hash_unlock(ht);
11084 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010011085 else if (argvars[0].v_type == VAR_BLOB)
11086 {
11087 int i;
11088 typval_T tv;
11089
11090 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11091 for (i = 0; i < b->bv_ga.ga_len; i++)
11092 {
11093 tv.v_type = VAR_NUMBER;
11094 tv.vval.v_number = blob_get(b, i);
11095 vimvars[VV_KEY].vv_nr = idx;
11096 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg)
11097 break;
11098 if (tv.v_type != VAR_NUMBER)
11099 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011100 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010011101 return;
11102 }
11103 tv.v_type = VAR_NUMBER;
11104 blob_set(b, i, tv.vval.v_number);
11105 if (!map && rem)
11106 {
11107 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
11108
11109 mch_memmove(p + idx, p + i + 1,
11110 (size_t)b->bv_ga.ga_len - i - 1);
11111 --b->bv_ga.ga_len;
11112 --i;
11113 }
11114 }
11115 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011116 else
11117 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +010011118 // argvars[0].v_type == VAR_LIST
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011119 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11120
11121 for (li = l->lv_first; li != NULL; li = nli)
11122 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011123 if (map && var_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011124 break;
11125 nli = li->li_next;
11126 vimvars[VV_KEY].vv_nr = idx;
11127 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
11128 || did_emsg)
11129 break;
11130 if (!map && rem)
11131 listitem_remove(l, li);
11132 ++idx;
11133 }
11134 }
11135
11136 restore_vimvar(VV_KEY, &save_key);
11137 restore_vimvar(VV_VAL, &save_val);
11138
11139 did_emsg |= save_did_emsg;
11140 }
11141
11142 copy_tv(&argvars[0], rettv);
11143}
11144
Bram Moolenaar071d4272004-06-13 20:20:40 +000011145#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */