blob: cdaf103a2832b7af75306af840925f2b0be75551 [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 Moolenaar9937a052019-06-15 15:45:06 +020031static char *e_cannot_mod = N_("E995: Cannot modify existing variable");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020032#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020033static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020034#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000035
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010036#define NAMESPACE_CHAR (char_u *)"abglstvw"
37
Bram Moolenaar230bb3f2013-04-24 14:07:45 +020038static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +000039#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +000040
41/*
Bram Moolenaar532c7802005-01-27 14:44:31 +000042 * Old Vim variables such as "v:version" are also available without the "v:".
43 * Also in functions. We need a special hashtable for them.
44 */
Bram Moolenaar4debb442005-06-01 21:57:40 +000045static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +000046
47/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000048 * When recursively copying lists and dicts we need to remember which ones we
49 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000050 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000051 */
52static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020053
Bram Moolenaard9fba312005-06-26 22:34:35 +000054/*
Bram Moolenaar33570922005-01-25 22:26:29 +000055 * Array to hold the hashtab with variables local to each sourced script.
56 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +000057 */
Bram Moolenaar33570922005-01-25 22:26:29 +000058typedef struct
59{
60 dictitem_T sv_var;
61 dict_T sv_dict;
62} scriptvar_T;
63
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020064static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
65#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
66#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +000067
68static int echo_attr = 0; /* attributes used for ":echo" */
69
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000070/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000071static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
72
Bram Moolenaar071d4272004-06-13 20:20:40 +000073/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000074 * Info used by a ":for" loop.
75 */
Bram Moolenaar33570922005-01-25 22:26:29 +000076typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000077{
78 int fi_semicolon; /* TRUE if ending in '; var]' */
79 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +000080 listwatch_T fi_lw; /* keep an eye on the item used. */
81 list_T *fi_list; /* list being used */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010082 int fi_bi; /* index of blob */
83 blob_T *fi_blob; /* blob being used */
Bram Moolenaar33570922005-01-25 22:26:29 +000084} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000085
Bram Moolenaara7043832005-01-21 11:56:39 +000086
87/*
Bram Moolenaar33570922005-01-25 22:26:29 +000088 * Array to hold the value of v: variables.
89 * The value is in a dictitem, so that it can also be used in the v: scope.
90 * The reason to use this table anyway is for very quick access to the
91 * variables with the VV_ defines.
92 */
Bram Moolenaar33570922005-01-25 22:26:29 +000093
94/* values for vv_flags: */
95#define VV_COMPAT 1 /* compatible, also used without "v:" */
96#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000097#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +000098
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +010099#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +0000100
101static struct vimvar
102{
103 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100104 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000105 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
106} vimvars[VV_LEN] =
107{
108 /*
109 * The order here must match the VV_ defines in vim.h!
110 * Initializing a union does not work, leave tv.vval empty to get zero's.
111 */
112 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
113 {VV_NAME("count1", VAR_NUMBER), VV_RO},
114 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
115 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
116 {VV_NAME("warningmsg", VAR_STRING), 0},
117 {VV_NAME("statusmsg", VAR_STRING), 0},
118 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
119 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
120 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
121 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
122 {VV_NAME("termresponse", VAR_STRING), VV_RO},
123 {VV_NAME("fname", VAR_STRING), VV_RO},
124 {VV_NAME("lang", VAR_STRING), VV_RO},
125 {VV_NAME("lc_time", VAR_STRING), VV_RO},
126 {VV_NAME("ctype", VAR_STRING), VV_RO},
127 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
128 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
129 {VV_NAME("fname_in", VAR_STRING), VV_RO},
130 {VV_NAME("fname_out", VAR_STRING), VV_RO},
131 {VV_NAME("fname_new", VAR_STRING), VV_RO},
132 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
133 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
134 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
135 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
136 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
137 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
138 {VV_NAME("progname", VAR_STRING), VV_RO},
139 {VV_NAME("servername", VAR_STRING), VV_RO},
140 {VV_NAME("dying", VAR_NUMBER), VV_RO},
141 {VV_NAME("exception", VAR_STRING), VV_RO},
142 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
143 {VV_NAME("register", VAR_STRING), VV_RO},
144 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
145 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000146 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
147 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000148 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000149 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
150 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000151 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
152 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
Bram Moolenaarc9721bd2016-06-04 17:41:03 +0200153 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000154 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
155 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
156 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000157 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000158 {VV_NAME("swapname", VAR_STRING), VV_RO},
159 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000160 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200161 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000162 {VV_NAME("mouse_win", VAR_NUMBER), 0},
Bram Moolenaar511972d2016-06-04 18:09:59 +0200163 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000164 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
165 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000166 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000167 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100168 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000169 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200170 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200171 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200172 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200173 {VV_NAME("option_new", VAR_STRING), VV_RO},
174 {VV_NAME("option_old", VAR_STRING), VV_RO},
175 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100176 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100177 {VV_NAME("false", VAR_SPECIAL), VV_RO},
178 {VV_NAME("true", VAR_SPECIAL), VV_RO},
179 {VV_NAME("null", VAR_SPECIAL), VV_RO},
180 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100181 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200182 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaarf562e722016-07-19 17:25:25 +0200183 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
184 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
185 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
186 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
187 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
188 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
189 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
190 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
191 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
192 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100193 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200194 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
195 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
Bram Moolenaarf3af54e2017-08-30 14:53:06 +0200196 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
Bram Moolenaar37df9a42019-06-14 14:39:51 +0200197 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
198 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
199 {VV_NAME("event", VAR_DICT), VV_RO},
200 {VV_NAME("versionlong", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000201};
202
203/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000204#define vv_type vv_di.di_tv.v_type
205#define vv_nr vv_di.di_tv.vval.v_number
206#define vv_float vv_di.di_tv.vval.v_float
207#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000208#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200209#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100210#define vv_blob vv_di.di_tv.vval.v_blob
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000211#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000212
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200213static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000214#define vimvarht vimvardict.dv_hashtab
215
Bram Moolenaar9937a052019-06-15 15:45:06 +0200216static void ex_let_const(exarg_T *eap, int is_const);
217static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, int is_const, char_u *nextchars);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100218static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
219static char_u *skip_var_one(char_u *arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100220static void list_glob_vars(int *first);
221static void list_buf_vars(int *first);
222static void list_win_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100223static void list_tab_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100224static void list_vim_vars(int *first);
225static void list_script_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100226static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaar9937a052019-06-15 15:45:06 +0200227static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int is_const, char_u *endchars, char_u *op);
228static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, int is_const, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100229static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100230static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
231static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
232static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
233static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000234
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100235static int eval2(char_u **arg, typval_T *rettv, int evaluate);
236static int eval3(char_u **arg, typval_T *rettv, int evaluate);
237static int eval4(char_u **arg, typval_T *rettv, int evaluate);
238static int eval5(char_u **arg, typval_T *rettv, int evaluate);
239static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
240static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000241
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100242static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
243static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100244static int free_unref_items(int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100245static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100246static int get_env_len(char_u **arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100247static 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 +0200248static void check_vars(char_u *name, int len);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100249static typval_T *alloc_string_tv(char_u *string);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100250static void delete_var(hashtab_T *ht, hashitem_T *hi);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100251static void list_one_var(dictitem_T *v, char *prefix, int *first);
252static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
Bram Moolenaar9937a052019-06-15 15:45:06 +0200253static void set_var_const(char_u *name, typval_T *tv, int copy, int is_const);
Bram Moolenaar05c00c02019-02-11 22:00:11 +0100254static int tv_check_lock(typval_T *tv, char_u *name, int use_gettext);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100255static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200256
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200257/* for VIM_VERSION_ defines */
258#include "version.h"
259
Bram Moolenaare21c1582019-03-02 11:57:09 +0100260/*
261 * Return "n1" divided by "n2", taking care of dividing by zero.
262 */
263 static varnumber_T
264num_divide(varnumber_T n1, varnumber_T n2)
265{
266 varnumber_T result;
267
268 if (n2 == 0) // give an error message?
269 {
270 if (n1 == 0)
271 result = VARNUM_MIN; // similar to NaN
272 else if (n1 < 0)
273 result = -VARNUM_MAX;
274 else
275 result = VARNUM_MAX;
276 }
277 else
278 result = n1 / n2;
279
280 return result;
281}
282
283/*
284 * Return "n1" modulus "n2", taking care of dividing by zero.
285 */
286 static varnumber_T
287num_modulus(varnumber_T n1, varnumber_T n2)
288{
289 // Give an error when n2 is 0?
290 return (n2 == 0) ? 0 : (n1 % n2);
291}
292
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100293
294#if defined(EBCDIC) || defined(PROTO)
295/*
296 * Compare struct fst by function name.
297 */
298 static int
299compare_func_name(const void *s1, const void *s2)
300{
301 struct fst *p1 = (struct fst *)s1;
302 struct fst *p2 = (struct fst *)s2;
303
304 return STRCMP(p1->f_name, p2->f_name);
305}
306
307/*
308 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100309 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100310 * On machines using EBCDIC we have to sort it.
311 */
312 static void
313sortFunctions(void)
314{
315 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
316
317 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
318}
319#endif
320
321
Bram Moolenaar33570922005-01-25 22:26:29 +0000322/*
323 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000324 */
325 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100326eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000327{
Bram Moolenaar33570922005-01-25 22:26:29 +0000328 int i;
329 struct vimvar *p;
330
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200331 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
332 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200333 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000334 hash_init(&compat_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200335 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000336
337 for (i = 0; i < VV_LEN; ++i)
338 {
339 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200340 if (STRLEN(p->vv_name) > 16)
341 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100342 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200343 getout(1);
344 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000345 STRCPY(p->vv_di.di_key, p->vv_name);
346 if (p->vv_flags & VV_RO)
347 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
348 else if (p->vv_flags & VV_RO_SBX)
349 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
350 else
351 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000352
353 /* add to v: scope dict, unless the value is not always available */
354 if (p->vv_type != VAR_UNKNOWN)
355 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000356 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000357 /* add to compat scope dict */
358 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000359 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100360 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
Bram Moolenaar37df9a42019-06-14 14:39:51 +0200361 vimvars[VV_VERSIONLONG].vv_nr = VIM_VERSION_100 * 10000 + highest_patch();
Bram Moolenaara542c682016-01-31 16:28:04 +0100362
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000363 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100364 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100365 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100366 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100367 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100368
369 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
370 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
371 set_vim_var_nr(VV_NONE, VVAL_NONE);
372 set_vim_var_nr(VV_NULL, VVAL_NULL);
373
Bram Moolenaarf562e722016-07-19 17:25:25 +0200374 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
375 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
376 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
377 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
378 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
379 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
380 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
381 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
382 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
383 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100384 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
Bram Moolenaarf562e722016-07-19 17:25:25 +0200385
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200386 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200387
388#ifdef EBCDIC
389 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100390 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200391 */
392 sortFunctions();
393#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000394}
395
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000396#if defined(EXITFREE) || defined(PROTO)
397 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100398eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000399{
400 int i;
401 struct vimvar *p;
402
403 for (i = 0; i < VV_LEN; ++i)
404 {
405 p = &vimvars[i];
406 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100407 VIM_CLEAR(p->vv_str);
Bram Moolenaard812df62008-11-09 12:46:09 +0000408 else if (p->vv_di.di_tv.v_type == VAR_LIST)
409 {
410 list_unref(p->vv_list);
411 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000412 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000413 }
414 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000415 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000416 hash_clear(&compat_hashtab);
417
Bram Moolenaard9fba312005-06-26 22:34:35 +0000418 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100419# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200420 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100421# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000422
423 /* global variables */
424 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000425
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000426 /* autoloaded script names */
427 ga_clear_strings(&ga_loaded);
428
Bram Moolenaarcca74132013-09-25 21:00:28 +0200429 /* Script-local variables. First clear all the variables and in a second
430 * loop free the scriptvar_T, because a variable in one script might hold
431 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200432 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200433 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200434 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200435 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200436 ga_clear(&ga_scripts);
437
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200438 // unreferenced lists and dicts
439 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200440
441 // functions not garbage collected
442 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000443}
444#endif
445
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446
447/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 * Set an internal variable to a string value. Creates the variable if it does
449 * not already exist.
450 */
451 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100452set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000454 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000455 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456
457 val = vim_strsave(value);
458 if (val != NULL)
459 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000460 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000461 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000463 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000464 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 }
466 }
467}
468
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000469static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200470#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000471static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000472static char_u *redir_endp = NULL;
473static char_u *redir_varname = NULL;
474
475/*
476 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100477 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000478 * Returns OK if successfully completed the setup. FAIL otherwise.
479 */
480 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100481var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000482{
483 int save_emsg;
484 int err;
485 typval_T tv;
486
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000487 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000488 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000489 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100490 emsg(_(e_invarg));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000491 return FAIL;
492 }
493
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000494 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000495 redir_varname = vim_strsave(name);
496 if (redir_varname == NULL)
497 return FAIL;
498
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200499 redir_lval = ALLOC_CLEAR_ONE(lval_T);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000500 if (redir_lval == NULL)
501 {
502 var_redir_stop();
503 return FAIL;
504 }
505
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000506 /* The output is stored in growarray "redir_ga" until redirection ends. */
507 ga_init2(&redir_ga, (int)sizeof(char), 500);
508
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000509 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100510 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000511 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000512 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
513 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200514 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000515 if (redir_endp != NULL && *redir_endp != NUL)
516 /* Trailing characters are present after the variable name */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100517 emsg(_(e_trailing));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000518 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100519 emsg(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000520 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000521 var_redir_stop();
522 return FAIL;
523 }
524
525 /* check if we can write to the variable: set it to or append an empty
526 * string */
527 save_emsg = did_emsg;
528 did_emsg = FALSE;
529 tv.v_type = VAR_STRING;
530 tv.vval.v_string = (char_u *)"";
531 if (append)
Bram Moolenaar9937a052019-06-15 15:45:06 +0200532 set_var_lval(redir_lval, redir_endp, &tv, TRUE, FALSE, (char_u *)".");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000533 else
Bram Moolenaar9937a052019-06-15 15:45:06 +0200534 set_var_lval(redir_lval, redir_endp, &tv, TRUE, FALSE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200535 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000536 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000537 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000538 if (err)
539 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000540 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000541 var_redir_stop();
542 return FAIL;
543 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000544
545 return OK;
546}
547
548/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000549 * Append "value[value_len]" to the variable set by var_redir_start().
550 * The actual appending is postponed until redirection ends, because the value
551 * appended may in fact be the string we write to, changing it may cause freed
552 * memory to be used:
553 * :redir => foo
554 * :let foo
555 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000556 */
557 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100558var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000559{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000560 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000561
562 if (redir_lval == NULL)
563 return;
564
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000565 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000566 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000567 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000568 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000569
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000570 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000571 {
572 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000573 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000574 }
575 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000576 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000577}
578
579/*
580 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000581 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000582 */
583 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100584var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000585{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000586 typval_T tv;
587
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200588 if (EVALCMD_BUSY)
589 {
590 redir_lval = NULL;
591 return;
592 }
593
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000594 if (redir_lval != NULL)
595 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000596 /* If there was no error: assign the text to the variable. */
597 if (redir_endp != NULL)
598 {
599 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
600 tv.v_type = VAR_STRING;
601 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200602 /* Call get_lval() again, if it's inside a Dict or List it may
603 * have changed. */
604 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100605 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200606 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar9937a052019-06-15 15:45:06 +0200607 set_var_lval(redir_lval, redir_endp, &tv, FALSE, FALSE,
608 (char_u *)".");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200609 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000610 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000611
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000612 /* free the collected output */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100613 VIM_CLEAR(redir_ga.ga_data);
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000614
Bram Moolenaard23a8232018-02-10 18:45:26 +0100615 VIM_CLEAR(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000616 }
Bram Moolenaard23a8232018-02-10 18:45:26 +0100617 VIM_CLEAR(redir_varname);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000618}
619
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100621eval_charconvert(
622 char_u *enc_from,
623 char_u *enc_to,
624 char_u *fname_from,
625 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626{
627 int err = FALSE;
628
629 set_vim_var_string(VV_CC_FROM, enc_from, -1);
630 set_vim_var_string(VV_CC_TO, enc_to, -1);
631 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
632 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
633 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
634 err = TRUE;
635 set_vim_var_string(VV_CC_FROM, NULL, -1);
636 set_vim_var_string(VV_CC_TO, NULL, -1);
637 set_vim_var_string(VV_FNAME_IN, NULL, -1);
638 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
639
640 if (err)
641 return FAIL;
642 return OK;
643}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644
645# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
646 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100647eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648{
649 int err = FALSE;
650
651 set_vim_var_string(VV_FNAME_IN, fname, -1);
652 set_vim_var_string(VV_CMDARG, args, -1);
653 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
654 err = TRUE;
655 set_vim_var_string(VV_FNAME_IN, NULL, -1);
656 set_vim_var_string(VV_CMDARG, NULL, -1);
657
658 if (err)
659 {
660 mch_remove(fname);
661 return FAIL;
662 }
663 return OK;
664}
665# endif
666
667# if defined(FEAT_DIFF) || defined(PROTO)
668 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100669eval_diff(
670 char_u *origfile,
671 char_u *newfile,
672 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673{
674 int err = FALSE;
675
676 set_vim_var_string(VV_FNAME_IN, origfile, -1);
677 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
678 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
679 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
680 set_vim_var_string(VV_FNAME_IN, NULL, -1);
681 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
682 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
683}
684
685 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100686eval_patch(
687 char_u *origfile,
688 char_u *difffile,
689 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690{
691 int err;
692
693 set_vim_var_string(VV_FNAME_IN, origfile, -1);
694 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
695 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
696 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
697 set_vim_var_string(VV_FNAME_IN, NULL, -1);
698 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
699 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
700}
701# endif
702
703/*
704 * Top level evaluation function, returning a boolean.
705 * Sets "error" to TRUE if there was an error.
706 * Return TRUE or FALSE.
707 */
708 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100709eval_to_bool(
710 char_u *arg,
711 int *error,
712 char_u **nextcmd,
713 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714{
Bram Moolenaar33570922005-01-25 22:26:29 +0000715 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200716 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717
718 if (skip)
719 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000720 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 else
723 {
724 *error = FALSE;
725 if (!skip)
726 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100727 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000728 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 }
730 }
731 if (skip)
732 --emsg_skip;
733
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200734 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735}
736
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100737/*
738 * Call eval1() and give an error message if not done at a lower level.
739 */
740 static int
741eval1_emsg(char_u **arg, typval_T *rettv, int evaluate)
742{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100743 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100744 int ret;
745 int did_emsg_before = did_emsg;
746 int called_emsg_before = called_emsg;
747
748 ret = eval1(arg, rettv, evaluate);
749 if (ret == FAIL)
750 {
751 // Report the invalid expression unless the expression evaluation has
752 // been cancelled due to an aborting error, an interrupt, or an
753 // exception, or we already gave a more specific error.
754 // Also check called_emsg for when using assert_fails().
755 if (!aborting() && did_emsg == did_emsg_before
756 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100757 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100758 }
759 return ret;
760}
761
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200762 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100763eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
764{
765 char_u *s;
766 int dummy;
767 char_u buf[NUMBUFLEN];
768
769 if (expr->v_type == VAR_FUNC)
770 {
771 s = expr->vval.v_string;
772 if (s == NULL || *s == NUL)
773 return FAIL;
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200774 if (call_func(s, -1, rettv, argc, argv, NULL,
Bram Moolenaar48570482017-10-30 21:48:41 +0100775 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
776 return FAIL;
777 }
778 else if (expr->v_type == VAR_PARTIAL)
779 {
780 partial_T *partial = expr->vval.v_partial;
781
782 s = partial_name(partial);
783 if (s == NULL || *s == NUL)
784 return FAIL;
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200785 if (call_func(s, -1, rettv, argc, argv, NULL,
Bram Moolenaar48570482017-10-30 21:48:41 +0100786 0L, 0L, &dummy, TRUE, partial, NULL) == FAIL)
787 return FAIL;
788 }
789 else
790 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100791 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100792 if (s == NULL)
793 return FAIL;
794 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100795 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100796 return FAIL;
797 if (*s != NUL) /* check for trailing chars after expr */
798 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200799 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100800 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100801 return FAIL;
802 }
803 }
804 return OK;
805}
806
807/*
808 * Like eval_to_bool() but using a typval_T instead of a string.
809 * Works for string, funcref and partial.
810 */
811 int
812eval_expr_to_bool(typval_T *expr, int *error)
813{
814 typval_T rettv;
815 int res;
816
817 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
818 {
819 *error = TRUE;
820 return FALSE;
821 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100822 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100823 clear_tv(&rettv);
824 return res;
825}
826
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827/*
828 * Top level evaluation function, returning a string. If "skip" is TRUE,
829 * only parsing to "nextcmd" is done, without reporting errors. Return
830 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
831 */
832 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100833eval_to_string_skip(
834 char_u *arg,
835 char_u **nextcmd,
836 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837{
Bram Moolenaar33570922005-01-25 22:26:29 +0000838 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 char_u *retval;
840
841 if (skip)
842 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000843 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 retval = NULL;
845 else
846 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100847 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000848 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 }
850 if (skip)
851 --emsg_skip;
852
853 return retval;
854}
855
856/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000857 * Skip over an expression at "*pp".
858 * Return FAIL for an error, OK otherwise.
859 */
860 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100861skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000862{
Bram Moolenaar33570922005-01-25 22:26:29 +0000863 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000864
865 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000866 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000867}
868
869/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000871 * When "convert" is TRUE convert a List into a sequence of lines and convert
872 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 * Return pointer to allocated memory, or NULL for failure.
874 */
875 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100876eval_to_string(
877 char_u *arg,
878 char_u **nextcmd,
879 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880{
Bram Moolenaar33570922005-01-25 22:26:29 +0000881 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000883 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000884#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000885 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000888 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 retval = NULL;
890 else
891 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000892 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000893 {
894 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000895 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200896 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200897 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200898 if (tv.vval.v_list->lv_len > 0)
899 ga_append(&ga, NL);
900 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000901 ga_append(&ga, NUL);
902 retval = (char_u *)ga.ga_data;
903 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000904#ifdef FEAT_FLOAT
905 else if (convert && tv.v_type == VAR_FLOAT)
906 {
907 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
908 retval = vim_strsave(numbuf);
909 }
910#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000911 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100912 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000913 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 }
915
916 return retval;
917}
918
919/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000920 * Call eval_to_string() without using current local variables and using
921 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 */
923 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100924eval_to_string_safe(
925 char_u *arg,
926 char_u **nextcmd,
927 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928{
929 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200930 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200932 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000933 if (use_sandbox)
934 ++sandbox;
935 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000936 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000937 if (use_sandbox)
938 --sandbox;
939 --textlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200940 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 return retval;
942}
943
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944/*
945 * Top level evaluation function, returning a number.
946 * Evaluates "expr" silently.
947 * Returns -1 for an error.
948 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200949 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100950eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951{
Bram Moolenaar33570922005-01-25 22:26:29 +0000952 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200953 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000954 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955
956 ++emsg_off;
957
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000958 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 retval = -1;
960 else
961 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100962 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000963 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 }
965 --emsg_off;
966
967 return retval;
968}
969
Bram Moolenaara40058a2005-07-11 22:42:07 +0000970/*
971 * Prepare v: variable "idx" to be used.
972 * Save the current typeval in "save_tv".
973 * When not used yet add the variable to the v: hashtable.
974 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200975 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100976prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000977{
978 *save_tv = vimvars[idx].vv_tv;
979 if (vimvars[idx].vv_type == VAR_UNKNOWN)
980 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
981}
982
983/*
984 * Restore v: variable "idx" to typeval "save_tv".
985 * When no longer defined, remove the variable from the v: hashtable.
986 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200987 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100988restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000989{
990 hashitem_T *hi;
991
Bram Moolenaara40058a2005-07-11 22:42:07 +0000992 vimvars[idx].vv_tv = *save_tv;
993 if (vimvars[idx].vv_type == VAR_UNKNOWN)
994 {
995 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
996 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100997 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +0000998 else
999 hash_remove(&vimvarht, hi);
1000 }
1001}
1002
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001003#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001004/*
1005 * Evaluate an expression to a list with suggestions.
1006 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001007 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001008 */
1009 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001010eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001011{
1012 typval_T save_val;
1013 typval_T rettv;
1014 list_T *list = NULL;
1015 char_u *p = skipwhite(expr);
1016
1017 /* Set "v:val" to the bad word. */
1018 prepare_vimvar(VV_VAL, &save_val);
1019 vimvars[VV_VAL].vv_type = VAR_STRING;
1020 vimvars[VV_VAL].vv_str = badword;
1021 if (p_verbose == 0)
1022 ++emsg_off;
1023
1024 if (eval1(&p, &rettv, TRUE) == OK)
1025 {
1026 if (rettv.v_type != VAR_LIST)
1027 clear_tv(&rettv);
1028 else
1029 list = rettv.vval.v_list;
1030 }
1031
1032 if (p_verbose == 0)
1033 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001034 restore_vimvar(VV_VAL, &save_val);
1035
1036 return list;
1037}
1038
1039/*
1040 * "list" is supposed to contain two items: a word and a number. Return the
1041 * word in "pp" and the number as the return value.
1042 * Return -1 if anything isn't right.
1043 * Used to get the good word and score from the eval_spell_expr() result.
1044 */
1045 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001046get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001047{
1048 listitem_T *li;
1049
1050 li = list->lv_first;
1051 if (li == NULL)
1052 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001053 *pp = tv_get_string(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001054
1055 li = li->li_next;
1056 if (li == NULL)
1057 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001058 return (int)tv_get_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001059}
1060#endif
1061
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001062/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001063 * Top level evaluation function.
1064 * Returns an allocated typval_T with the result.
1065 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001066 */
1067 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001068eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001069{
1070 typval_T *tv;
1071
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001072 tv = ALLOC_ONE(typval_T);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001073 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001074 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001075
1076 return tv;
1077}
1078
1079
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001081 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +02001082 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
1083 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001084 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001086 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001087call_vim_function(
1088 char_u *func,
1089 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +02001090 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001091 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 int doesrange;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001094 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001096 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001097 ret = call_func(func, -1, rettv, argc, argv, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001099 &doesrange, TRUE, NULL, NULL);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001100 if (ret == FAIL)
1101 clear_tv(rettv);
1102
1103 return ret;
1104}
1105
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001106/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001107 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001108 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001109 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1110 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001111 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001112 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001113call_func_retnr(
1114 char_u *func,
1115 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001116 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001117{
1118 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001119 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001120
Bram Moolenaarded27a12018-08-01 19:06:03 +02001121 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001122 return -1;
1123
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001124 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001125 clear_tv(&rettv);
1126 return retval;
1127}
1128
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001129#if defined(FEAT_CMDL_COMPL) \
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001130 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1131
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001132# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001133/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001134 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001135 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001136 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1137 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001138 */
1139 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001140call_func_retstr(
1141 char_u *func,
1142 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001143 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001144{
1145 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001146 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001147
Bram Moolenaarded27a12018-08-01 19:06:03 +02001148 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001149 return NULL;
1150
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001151 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001152 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 return retval;
1154}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001155# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001156
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001157/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001158 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001159 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1160 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001161 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001162 */
1163 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001164call_func_retlist(
1165 char_u *func,
1166 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001167 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001168{
1169 typval_T rettv;
1170
Bram Moolenaarded27a12018-08-01 19:06:03 +02001171 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001172 return NULL;
1173
1174 if (rettv.v_type != VAR_LIST)
1175 {
1176 clear_tv(&rettv);
1177 return NULL;
1178 }
1179
1180 return rettv.vval.v_list;
1181}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182#endif
1183
Bram Moolenaar05159a02005-02-26 23:04:13 +00001184
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185#ifdef FEAT_FOLDING
1186/*
1187 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1188 * it in "*cp". Doesn't give error messages.
1189 */
1190 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001191eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192{
Bram Moolenaar33570922005-01-25 22:26:29 +00001193 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001194 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001196 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1197 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198
1199 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001200 if (use_sandbox)
1201 ++sandbox;
1202 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001204 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 retval = 0;
1206 else
1207 {
1208 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001209 if (tv.v_type == VAR_NUMBER)
1210 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001211 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 retval = 0;
1213 else
1214 {
1215 /* If the result is a string, check if there is a non-digit before
1216 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001217 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 if (!VIM_ISDIGIT(*s) && *s != '-')
1219 *cp = *s++;
1220 retval = atol((char *)s);
1221 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001222 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 }
1224 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001225 if (use_sandbox)
1226 --sandbox;
1227 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001229 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230}
1231#endif
1232
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233/*
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001234 * Get a list of lines from a HERE document. The here document is a list of
1235 * lines surrounded by a marker.
1236 * cmd << {marker}
1237 * {line1}
1238 * {line2}
1239 * ....
1240 * {marker}
1241 *
1242 * The {marker} is a string. If the optional 'trim' word is supplied before the
1243 * marker, then the leading indentation before the lines (matching the
1244 * indentation in the 'cmd' line) is stripped.
1245 * Returns a List with {lines} or NULL.
1246 */
1247 static list_T *
1248heredoc_get(exarg_T *eap, char_u *cmd)
1249{
1250 char_u *theline;
1251 char_u *marker;
1252 list_T *l;
1253 char_u *p;
1254 int indent_len = 0;
1255
1256 if (eap->getline == NULL)
1257 {
1258 emsg(_("E991: cannot use =<< here"));
1259 return NULL;
1260 }
1261
1262 // Check for the optional 'trim' word before the marker
1263 cmd = skipwhite(cmd);
1264 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
1265 {
1266 cmd = skipwhite(cmd + 4);
1267
1268 // Trim the indentation from all the lines in the here document
1269 // The amount of indentation trimmed is the same as the indentation of
1270 // the :let command line.
1271 p = *eap->cmdlinep;
1272 while (VIM_ISWHITE(*p))
1273 {
1274 p++;
1275 indent_len++;
1276 }
1277 }
1278
1279 // The marker is the next word. Default marker is "."
1280 if (*cmd != NUL && *cmd != '"')
1281 {
1282 marker = skipwhite(cmd);
1283 p = skiptowhite(marker);
1284 if (*skipwhite(p) != NUL && *skipwhite(p) != '"')
1285 {
1286 emsg(_(e_trailing));
1287 return NULL;
1288 }
1289 *p = NUL;
1290 }
1291 else
1292 marker = (char_u *)".";
1293
1294 l = list_alloc();
1295 if (l == NULL)
1296 return NULL;
1297
1298 for (;;)
1299 {
1300 int i = 0;
1301
1302 theline = eap->getline(NUL, eap->cookie, 0);
1303 if (theline != NULL && indent_len > 0)
1304 {
1305 // trim the indent matching the first line
1306 if (STRNCMP(theline, *eap->cmdlinep, indent_len) == 0)
1307 i = indent_len;
1308 }
1309
1310 if (theline == NULL)
1311 {
1312 semsg(_("E990: Missing end marker '%s'"), marker);
1313 break;
1314 }
1315 if (STRCMP(marker, theline + i) == 0)
1316 {
1317 vim_free(theline);
1318 break;
1319 }
1320
1321 if (list_append_string(l, theline + i, -1) == FAIL)
1322 break;
1323 vim_free(theline);
1324 }
1325
1326 return l;
1327}
1328
1329/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001330 * ":let" list all variable values
1331 * ":let var1 var2" list variable values
1332 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001333 * ":let var += expr" assignment command.
1334 * ":let var -= expr" assignment command.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001335 * ":let var *= expr" assignment command.
1336 * ":let var /= expr" assignment command.
1337 * ":let var %= expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001338 * ":let var .= expr" assignment command.
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001339 * ":let var ..= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001340 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 */
1342 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001343ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344{
Bram Moolenaar9937a052019-06-15 15:45:06 +02001345 ex_let_const(eap, FALSE);
1346}
1347
1348/*
1349 * ":const" list all variable values
1350 * ":const var1 var2" list variable values
1351 * ":const var = expr" assignment command.
1352 * ":const [var1, var2] = expr" unpack list.
1353 */
1354 void
1355ex_const(exarg_T *eap)
1356{
1357 ex_let_const(eap, TRUE);
1358}
1359
1360 static void
1361ex_let_const(exarg_T *eap, int is_const)
1362{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001364 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001365 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001367 int var_count = 0;
1368 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001369 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001370 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001371 int first = TRUE;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001372 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373
Bram Moolenaardb552d602006-03-23 22:59:57 +00001374 argend = skip_var_list(arg, &var_count, &semicolon);
1375 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001376 return;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001377 if (argend > arg && argend[-1] == '.') // for var.='str'
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001378 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001379 expr = skipwhite(argend);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001380 concat = expr[0] == '.'
1381 && ((expr[1] == '=' && current_sctx.sc_version < 2)
1382 || (expr[1] == '.' && expr[2] == '='));
1383 if (*expr != '=' && !((vim_strchr((char_u *)"+-*/%", *expr) != NULL
1384 && expr[1] == '=') || concat))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001386 /*
1387 * ":let" without "=": list variables
1388 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001389 if (*arg == '[')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001390 emsg(_(e_invarg));
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001391 else if (expr[0] == '.')
1392 emsg(_("E985: .= is not supported with script version 2"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001393 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001394 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001395 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001396 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001397 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001398 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001399 list_glob_vars(&first);
1400 list_buf_vars(&first);
1401 list_win_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001402 list_tab_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001403 list_script_vars(&first);
1404 list_func_vars(&first);
1405 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 eap->nextcmd = check_nextcmd(arg);
1408 }
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001409 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
1410 {
1411 list_T *l;
1412
1413 // HERE document
1414 l = heredoc_get(eap, expr + 3);
1415 if (l != NULL)
1416 {
1417 rettv_list_set(&rettv, l);
1418 op[0] = '=';
1419 op[1] = NUL;
1420 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9937a052019-06-15 15:45:06 +02001421 is_const, op);
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001422 clear_tv(&rettv);
1423 }
1424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 else
1426 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001427 op[0] = '=';
1428 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001429 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001430 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001431 if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001432 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001433 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001434 if (expr[0] == '.' && expr[1] == '.') // ..=
1435 ++expr;
1436 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001437 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001438 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001439 else
1440 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001441
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 if (eap->skip)
1443 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001444 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 if (eap->skip)
1446 {
1447 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001448 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 --emsg_skip;
1450 }
1451 else if (i != FAIL)
1452 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001453 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9937a052019-06-15 15:45:06 +02001454 is_const, op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001455 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 }
1457 }
1458}
1459
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001460/*
1461 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1462 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001463 * When "nextchars" is not NULL it points to a string with characters that
1464 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1465 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001466 * Returns OK or FAIL;
1467 */
1468 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001469ex_let_vars(
1470 char_u *arg_start,
1471 typval_T *tv,
Bram Moolenaar9937a052019-06-15 15:45:06 +02001472 int copy, // copy values from "tv", don't move
1473 int semicolon, // from skip_var_list()
1474 int var_count, // from skip_var_list()
1475 int is_const, // lock variables for const
Bram Moolenaar7454a062016-01-30 15:14:10 +01001476 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001477{
1478 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001479 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001480 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001481 listitem_T *item;
1482 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001483
1484 if (*arg != '[')
1485 {
1486 /*
1487 * ":let var = expr" or ":for var in list"
1488 */
Bram Moolenaar9937a052019-06-15 15:45:06 +02001489 if (ex_let_one(arg, tv, copy, is_const, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001490 return FAIL;
1491 return OK;
1492 }
1493
1494 /*
1495 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1496 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001497 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001498 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001499 emsg(_(e_listreq));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001500 return FAIL;
1501 }
1502
1503 i = list_len(l);
1504 if (semicolon == 0 && var_count < i)
1505 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001506 emsg(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001507 return FAIL;
1508 }
1509 if (var_count - semicolon > i)
1510 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001511 emsg(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001512 return FAIL;
1513 }
1514
1515 item = l->lv_first;
1516 while (*arg != ']')
1517 {
1518 arg = skipwhite(arg + 1);
Bram Moolenaar9937a052019-06-15 15:45:06 +02001519 arg = ex_let_one(arg, &item->li_tv, TRUE, is_const,
1520 (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001521 item = item->li_next;
1522 if (arg == NULL)
1523 return FAIL;
1524
1525 arg = skipwhite(arg);
1526 if (*arg == ';')
1527 {
1528 /* Put the rest of the list (may be empty) in the var after ';'.
1529 * Create a new list for this. */
1530 l = list_alloc();
1531 if (l == NULL)
1532 return FAIL;
1533 while (item != NULL)
1534 {
1535 list_append_tv(l, &item->li_tv);
1536 item = item->li_next;
1537 }
1538
1539 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001540 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001541 ltv.vval.v_list = l;
1542 l->lv_refcount = 1;
1543
Bram Moolenaar9937a052019-06-15 15:45:06 +02001544 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, is_const,
1545 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001546 clear_tv(&ltv);
1547 if (arg == NULL)
1548 return FAIL;
1549 break;
1550 }
1551 else if (*arg != ',' && *arg != ']')
1552 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001553 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001554 return FAIL;
1555 }
1556 }
1557
1558 return OK;
1559}
1560
1561/*
1562 * Skip over assignable variable "var" or list of variables "[var, var]".
1563 * Used for ":let varvar = expr" and ":for varvar in expr".
1564 * For "[var, var]" increment "*var_count" for each variable.
1565 * for "[var, var; var]" set "semicolon".
1566 * Return NULL for an error.
1567 */
1568 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001569skip_var_list(
1570 char_u *arg,
1571 int *var_count,
1572 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001573{
1574 char_u *p, *s;
1575
1576 if (*arg == '[')
1577 {
1578 /* "[var, var]": find the matching ']'. */
1579 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001580 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001581 {
1582 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1583 s = skip_var_one(p);
1584 if (s == p)
1585 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001586 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001587 return NULL;
1588 }
1589 ++*var_count;
1590
1591 p = skipwhite(s);
1592 if (*p == ']')
1593 break;
1594 else if (*p == ';')
1595 {
1596 if (*semicolon == 1)
1597 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001598 emsg(_("Double ; in list of variables"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001599 return NULL;
1600 }
1601 *semicolon = 1;
1602 }
1603 else if (*p != ',')
1604 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001605 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001606 return NULL;
1607 }
1608 }
1609 return p + 1;
1610 }
1611 else
1612 return skip_var_one(arg);
1613}
1614
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001615/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001616 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001617 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001618 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001619 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001620skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001621{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001622 if (*arg == '@' && arg[1] != NUL)
1623 return arg + 2;
1624 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1625 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001626}
1627
Bram Moolenaara7043832005-01-21 11:56:39 +00001628/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001629 * List variables for hashtab "ht" with prefix "prefix".
1630 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001631 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001632 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001633list_hashtable_vars(
1634 hashtab_T *ht,
Bram Moolenaar32526b32019-01-19 17:43:09 +01001635 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01001636 int empty,
1637 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001638{
Bram Moolenaar33570922005-01-25 22:26:29 +00001639 hashitem_T *hi;
1640 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001641 int todo;
Bram Moolenaarf86db782018-10-25 13:31:37 +02001642 char_u buf[IOSIZE];
Bram Moolenaara7043832005-01-21 11:56:39 +00001643
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001644 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001645 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1646 {
1647 if (!HASHITEM_EMPTY(hi))
1648 {
1649 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001650 di = HI2DI(hi);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001651
1652 // apply :filter /pat/ to variable name
Bram Moolenaar32526b32019-01-19 17:43:09 +01001653 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1654 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001655 if (message_filtered(buf))
1656 continue;
1657
Bram Moolenaar33570922005-01-25 22:26:29 +00001658 if (empty || di->di_tv.v_type != VAR_STRING
1659 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001660 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001661 }
1662 }
1663}
1664
1665/*
1666 * List global variables.
1667 */
1668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001669list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001670{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001671 list_hashtable_vars(&globvarht, "", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001672}
1673
1674/*
1675 * List buffer variables.
1676 */
1677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001678list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001679{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001680 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001681}
1682
1683/*
1684 * List window variables.
1685 */
1686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001687list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001688{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001689 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001690}
1691
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001692/*
1693 * List tab page variables.
1694 */
1695 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001696list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001697{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001698 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001699}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001700
Bram Moolenaara7043832005-01-21 11:56:39 +00001701/*
1702 * List Vim variables.
1703 */
1704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001705list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001706{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001707 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001708}
1709
1710/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001711 * List script-local variables, if there is a script.
1712 */
1713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001714list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001715{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001716 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
1717 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
Bram Moolenaar32526b32019-01-19 17:43:09 +01001718 "s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001719}
1720
1721/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001722 * List variables in "arg".
1723 */
1724 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001725list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001726{
1727 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001728 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001729 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001730 char_u *name_start;
1731 char_u *arg_subsc;
1732 char_u *tofree;
1733 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001734
1735 while (!ends_excmd(*arg) && !got_int)
1736 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001737 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001738 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001739 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001740 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001741 {
1742 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001743 emsg(_(e_trailing));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001744 break;
1745 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001746 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001747 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001748 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001749 /* get_name_len() takes care of expanding curly braces */
1750 name_start = name = arg;
1751 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1752 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001753 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001754 /* This is mainly to keep test 49 working: when expanding
1755 * curly braces fails overrule the exception error message. */
1756 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001757 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001758 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001759 semsg(_(e_invarg2), arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001760 break;
1761 }
1762 error = TRUE;
1763 }
1764 else
1765 {
1766 if (tofree != NULL)
1767 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001768 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001769 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001770 else
1771 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001772 /* handle d.key, l[idx], f(expr) */
1773 arg_subsc = arg;
1774 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001775 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001776 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001777 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001778 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001779 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001780 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001781 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001782 case 'g': list_glob_vars(first); break;
1783 case 'b': list_buf_vars(first); break;
1784 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001785 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001786 case 'v': list_vim_vars(first); break;
1787 case 's': list_script_vars(first); break;
1788 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001789 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001790 semsg(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001791 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001792 }
1793 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001794 {
1795 char_u numbuf[NUMBUFLEN];
1796 char_u *tf;
1797 int c;
1798 char_u *s;
1799
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001800 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001801 c = *arg;
1802 *arg = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001803 list_one_var_a("",
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001804 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001805 tv.v_type,
1806 s == NULL ? (char_u *)"" : s,
1807 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001808 *arg = c;
1809 vim_free(tf);
1810 }
1811 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001812 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001813 }
1814 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001815
1816 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001817 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001818
1819 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001820 }
1821
1822 return arg;
1823}
1824
1825/*
1826 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1827 * Returns a pointer to the char just after the var name.
1828 * Returns NULL if there is an error.
1829 */
1830 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001831ex_let_one(
Bram Moolenaar9937a052019-06-15 15:45:06 +02001832 char_u *arg, // points to variable name
1833 typval_T *tv, // value to assign to variable
1834 int copy, // copy value from "tv"
1835 int is_const, // lock variable for const
1836 char_u *endchars, // valid chars after variable name or NULL
1837 char_u *op) // "+", "-", "." or NULL
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001838{
1839 int c1;
1840 char_u *name;
1841 char_u *p;
1842 char_u *arg_end = NULL;
1843 int len;
1844 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001845 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001846
1847 /*
1848 * ":let $VAR = expr": Set environment variable.
1849 */
1850 if (*arg == '$')
1851 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02001852 if (is_const)
1853 {
1854 emsg(_("E996: Cannot lock an environment variable"));
1855 return NULL;
1856 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001857 /* Find the end of the name. */
1858 ++arg;
1859 name = arg;
1860 len = get_env_len(&arg);
1861 if (len == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001862 semsg(_(e_invarg2), name - 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001863 else
1864 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001865 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001866 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001867 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001868 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001869 emsg(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001870 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871 {
1872 c1 = name[len];
1873 name[len] = NUL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001874 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001875 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001876 {
1877 int mustfree = FALSE;
1878 char_u *s = vim_getenv(name, &mustfree);
1879
1880 if (s != NULL)
1881 {
1882 p = tofree = concat_str(s, p);
1883 if (mustfree)
1884 vim_free(s);
1885 }
1886 }
1887 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001888 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001889 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001890 if (STRICMP(name, "HOME") == 0)
1891 init_homedir();
1892 else if (didset_vim && STRICMP(name, "VIM") == 0)
1893 didset_vim = FALSE;
1894 else if (didset_vimruntime
1895 && STRICMP(name, "VIMRUNTIME") == 0)
1896 didset_vimruntime = FALSE;
1897 arg_end = arg;
1898 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001899 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001900 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 }
1902 }
1903 }
1904
1905 /*
1906 * ":let &option = expr": Set option value.
1907 * ":let &l:option = expr": Set local option value.
1908 * ":let &g:option = expr": Set global option value.
1909 */
1910 else if (*arg == '&')
1911 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02001912 if (is_const)
1913 {
1914 emsg(_("E996: Cannot lock an option"));
1915 return NULL;
1916 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001917 /* Find the end of the name. */
1918 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919 if (p == NULL || (endchars != NULL
1920 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001921 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001922 else
1923 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001924 long n;
1925 int opt_type;
1926 long numval;
1927 char_u *stringval = NULL;
1928 char_u *s;
1929
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001930 c1 = *p;
1931 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001932
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001933 n = (long)tv_get_number(tv);
1934 s = tv_get_string_chk(tv); /* != NULL if number or string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001935 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001936 {
1937 opt_type = get_option_value(arg, &numval,
1938 &stringval, opt_flags);
1939 if ((opt_type == 1 && *op == '.')
1940 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001941 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001942 semsg(_(e_letwrong), op);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001943 s = NULL; // don't set the value
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001944 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001945 else
1946 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001947 if (opt_type == 1) // number
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001948 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001949 switch (*op)
1950 {
1951 case '+': n = numval + n; break;
1952 case '-': n = numval - n; break;
1953 case '*': n = numval * n; break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001954 case '/': n = (long)num_divide(numval, n); break;
1955 case '%': n = (long)num_modulus(numval, n); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001956 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001957 }
Bram Moolenaarff697e62019-02-12 22:28:33 +01001958 else if (opt_type == 0 && stringval != NULL) // string
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001959 {
1960 s = concat_str(stringval, s);
1961 vim_free(stringval);
1962 stringval = s;
1963 }
1964 }
1965 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001966 if (s != NULL)
1967 {
1968 set_option_value(arg, n, s, opt_flags);
1969 arg_end = p;
1970 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001971 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001972 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001973 }
1974 }
1975
1976 /*
1977 * ":let @r = expr": Set register contents.
1978 */
1979 else if (*arg == '@')
1980 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02001981 if (is_const)
1982 {
1983 emsg(_("E996: Cannot lock a register"));
1984 return NULL;
1985 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001986 ++arg;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001987 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001988 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001989 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001990 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001991 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001992 else
1993 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001994 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001995 char_u *s;
1996
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001997 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001998 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001999 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002000 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002001 if (s != NULL)
2002 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002003 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002004 vim_free(s);
2005 }
2006 }
2007 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002008 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002009 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002010 arg_end = arg + 1;
2011 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002012 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002013 }
2014 }
2015
2016 /*
2017 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002018 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002019 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002020 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002021 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002022 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002023
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002024 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002025 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002026 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002027 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002028 emsg(_(e_letunexp));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002029 else
2030 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02002031 set_var_lval(&lv, p, tv, copy, is_const, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002032 arg_end = p;
2033 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002034 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002035 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002036 }
2037
2038 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002039 semsg(_(e_invarg2), arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002040
2041 return arg_end;
2042}
2043
2044/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002045 * Get an lval: variable, Dict item or List item that can be assigned a value
2046 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2047 * "name.key", "name.key[expr]" etc.
2048 * Indexing only works if "name" is an existing List or Dictionary.
2049 * "name" points to the start of the name.
2050 * If "rettv" is not NULL it points to the value to be assigned.
2051 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2052 * wrong; must end in space or cmd separator.
2053 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002054 * flags:
2055 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01002056 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002057 * GLV_NO_AUTOLOAD: do not use script autoloading
2058 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002059 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002060 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002061 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002062 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002063 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002064get_lval(
2065 char_u *name,
2066 typval_T *rettv,
2067 lval_T *lp,
2068 int unlet,
2069 int skip,
2070 int flags, /* GLV_ values */
2071 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002072{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002073 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002074 char_u *expr_start, *expr_end;
2075 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002076 dictitem_T *v;
2077 typval_T var1;
2078 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002079 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002080 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002081 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002082 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002083 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002084 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002085
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002086 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002087 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002088
2089 if (skip)
2090 {
2091 /* When skipping just find the end of the name. */
2092 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002093 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002094 }
2095
2096 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002097 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002098 if (expr_start != NULL)
2099 {
2100 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002101 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002102 && *p != '[' && *p != '.')
2103 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002104 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002105 return NULL;
2106 }
2107
2108 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2109 if (lp->ll_exp_name == NULL)
2110 {
2111 /* Report an invalid expression in braces, unless the
2112 * expression evaluation has been cancelled due to an
2113 * aborting error, an interrupt, or an exception. */
2114 if (!aborting() && !quiet)
2115 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002116 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002117 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002118 return NULL;
2119 }
2120 }
2121 lp->ll_name = lp->ll_exp_name;
2122 }
2123 else
2124 lp->ll_name = name;
2125
2126 /* Without [idx] or .key we are done. */
2127 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2128 return p;
2129
2130 cc = *p;
2131 *p = NUL;
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002132 /* Only pass &ht when we would write to the variable, it prevents autoload
2133 * as well. */
2134 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
2135 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002136 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002137 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002138 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002139 if (v == NULL)
2140 return NULL;
2141
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002142 /*
2143 * Loop until no more [idx] or .key is following.
2144 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002145 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002146 var1.v_type = VAR_UNKNOWN;
2147 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002148 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002149 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002150 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2151 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002152 && lp->ll_tv->vval.v_dict != NULL)
2153 && !(lp->ll_tv->v_type == VAR_BLOB
2154 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002155 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002156 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002157 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002158 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002159 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002160 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002161 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002162 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002163 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002164 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002165 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002166
Bram Moolenaar8c711452005-01-14 21:53:12 +00002167 len = -1;
2168 if (*p == '.')
2169 {
2170 key = p + 1;
2171 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2172 ;
2173 if (len == 0)
2174 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002175 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002176 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002177 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002178 }
2179 p = key + len;
2180 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002181 else
2182 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002183 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002184 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002185 if (*p == ':')
2186 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002187 else
2188 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002189 empty1 = FALSE;
2190 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002191 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002192 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002193 {
2194 /* not a number or string */
2195 clear_tv(&var1);
2196 return NULL;
2197 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002198 }
2199
2200 /* Optionally get the second index [ :expr]. */
2201 if (*p == ':')
2202 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002203 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002204 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002205 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002206 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002207 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002208 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002209 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002210 if (rettv != NULL
2211 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002212 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002213 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002214 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002215 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002216 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002217 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002218 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002219 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002220 }
2221 p = skipwhite(p + 1);
2222 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002223 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002224 else
2225 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002226 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002227 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2228 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002229 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002230 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002231 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002232 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002233 {
2234 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002235 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002236 clear_tv(&var2);
2237 return NULL;
2238 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002239 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002240 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002241 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002242 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002243 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002244
Bram Moolenaar8c711452005-01-14 21:53:12 +00002245 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002246 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002247 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002248 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002249 clear_tv(&var1);
2250 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002251 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002252 }
2253
2254 /* Skip to past ']'. */
2255 ++p;
2256 }
2257
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002258 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002259 {
2260 if (len == -1)
2261 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002262 /* "[key]": get key from "var1" */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002263 key = tv_get_string_chk(&var1); /* is number or string */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002264 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002265 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002266 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002267 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002268 }
2269 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002270 lp->ll_list = NULL;
2271 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002272 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002273
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002274 /* When assigning to a scope dictionary check that a function and
2275 * variable name is valid (only variable name unless it is l: or
2276 * g: dictionary). Disallow overwriting a builtin function. */
2277 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002278 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002279 int prevval;
2280 int wrong;
2281
2282 if (len != -1)
2283 {
2284 prevval = key[len];
2285 key[len] = NUL;
2286 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002287 else
2288 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002289 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2290 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002291 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002292 || !valid_varname(key);
2293 if (len != -1)
2294 key[len] = prevval;
2295 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002296 return NULL;
2297 }
2298
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002299 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002300 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01002301 // Can't add "v:" or "a:" variable.
2302 if (lp->ll_dict == &vimvardict
2303 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002304 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002305 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01002306 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002307 return NULL;
2308 }
2309
Bram Moolenaar31b81602019-02-10 22:14:27 +01002310 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002311 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002312 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002313 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002314 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002315 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002316 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002317 }
2318 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002319 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002320 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002321 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002322 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002323 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002324 p = NULL;
2325 break;
2326 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002327 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002328 else if ((flags & GLV_READ_ONLY) == 0
2329 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002330 {
2331 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002332 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002333 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002334
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002335 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002336 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002337 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002338 else if (lp->ll_tv->v_type == VAR_BLOB)
2339 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002340 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
2341
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002342 /*
2343 * Get the number and item for the only or first index of the List.
2344 */
2345 if (empty1)
2346 lp->ll_n1 = 0;
2347 else
2348 // is number or string
2349 lp->ll_n1 = (long)tv_get_number(&var1);
2350 clear_tv(&var1);
2351
2352 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002353 || lp->ll_n1 > bloblen
2354 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002355 {
2356 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002357 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002358 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002359 return NULL;
2360 }
2361 if (lp->ll_range && !lp->ll_empty2)
2362 {
2363 lp->ll_n2 = (long)tv_get_number(&var2);
2364 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002365 if (lp->ll_n2 < 0
2366 || lp->ll_n2 >= bloblen
2367 || lp->ll_n2 < lp->ll_n1)
2368 {
2369 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002370 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002371 return NULL;
2372 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002373 }
2374 lp->ll_blob = lp->ll_tv->vval.v_blob;
2375 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01002376 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002377 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002378 else
2379 {
2380 /*
2381 * Get the number and item for the only or first index of the List.
2382 */
2383 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002384 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002385 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002386 /* is number or string */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002387 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002388 clear_tv(&var1);
2389
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002390 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002391 lp->ll_list = lp->ll_tv->vval.v_list;
2392 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2393 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002394 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002395 if (lp->ll_n1 < 0)
2396 {
2397 lp->ll_n1 = 0;
2398 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2399 }
2400 }
2401 if (lp->ll_li == NULL)
2402 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002403 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002404 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002405 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002406 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002407 }
2408
2409 /*
2410 * May need to find the item or absolute index for the second
2411 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002412 * When no index given: "lp->ll_empty2" is TRUE.
2413 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002414 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002415 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002416 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002417 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002418 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002419 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002420 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002421 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002422 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002423 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002424 {
2425 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002426 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002427 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002428 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002429 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002430 }
2431
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002432 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2433 if (lp->ll_n1 < 0)
2434 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2435 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002436 {
2437 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002438 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002439 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002440 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002441 }
2442
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002443 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002444 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002445 }
2446
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002447 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002448 return p;
2449}
2450
2451/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002452 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002453 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002454 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002455clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002456{
2457 vim_free(lp->ll_exp_name);
2458 vim_free(lp->ll_newkey);
2459}
2460
2461/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002462 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002463 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01002464 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
2465 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002466 */
2467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002468set_var_lval(
2469 lval_T *lp,
2470 char_u *endp,
2471 typval_T *rettv,
2472 int copy,
Bram Moolenaar9937a052019-06-15 15:45:06 +02002473 int is_const, // Disallow to modify existing variable for :const
Bram Moolenaar7454a062016-01-30 15:14:10 +01002474 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002475{
2476 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002477 listitem_T *ri;
2478 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002479
2480 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002481 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002482 cc = *endp;
2483 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002484 if (lp->ll_blob != NULL)
2485 {
2486 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002487
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002488 if (op != NULL && *op != '=')
2489 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002490 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002491 return;
2492 }
2493
2494 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2495 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002496 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002497
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002498 if (lp->ll_empty2)
2499 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
2500
2501 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002502 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002503 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002504 return;
2505 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002506 if (lp->ll_empty2)
2507 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002508
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002509 ir = 0;
2510 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
2511 blob_set(lp->ll_blob, il,
2512 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002513 }
2514 else
2515 {
2516 val = (int)tv_get_number_chk(rettv, &error);
2517 if (!error)
2518 {
2519 garray_T *gap = &lp->ll_blob->bv_ga;
2520
2521 // Allow for appending a byte. Setting a byte beyond
2522 // the end is an error otherwise.
2523 if (lp->ll_n1 < gap->ga_len
2524 || (lp->ll_n1 == gap->ga_len
2525 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
2526 {
2527 blob_set(lp->ll_blob, lp->ll_n1, val);
2528 if (lp->ll_n1 == gap->ga_len)
2529 ++gap->ga_len;
2530 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002531 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002532 }
2533 }
2534 }
2535 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002536 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002537 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002538
Bram Moolenaar9937a052019-06-15 15:45:06 +02002539 if (is_const)
2540 {
2541 emsg(_(e_cannot_mod));
2542 *endp = cc;
2543 return;
2544 }
2545
Bram Moolenaarff697e62019-02-12 22:28:33 +01002546 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01002547 di = NULL;
2548 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2549 &tv, &di, TRUE, FALSE) == OK)
2550 {
2551 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002552 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2553 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01002554 && tv_op(&tv, rettv, op) == OK)
2555 set_var(lp->ll_name, &tv, FALSE);
2556 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002557 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002558 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002559 else
Bram Moolenaar9937a052019-06-15 15:45:06 +02002560 set_var_const(lp->ll_name, rettv, copy, is_const);
Bram Moolenaar79518e22017-02-17 16:31:35 +01002561 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002563 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002564 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002565 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002566 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 else if (lp->ll_range)
2568 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002569 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002570 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002571
Bram Moolenaar9937a052019-06-15 15:45:06 +02002572 if (is_const)
2573 {
2574 emsg(_("E996: Cannot lock a range"));
2575 return;
2576 }
2577
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002578 /*
2579 * Check whether any of the list items is locked
2580 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002581 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002582 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002583 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002584 return;
2585 ri = ri->li_next;
2586 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2587 break;
2588 ll_li = ll_li->li_next;
2589 ++ll_n1;
2590 }
2591
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 /*
2593 * Assign the List values to the list items.
2594 */
2595 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002596 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002597 if (op != NULL && *op != '=')
2598 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2599 else
2600 {
2601 clear_tv(&lp->ll_li->li_tv);
2602 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2603 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604 ri = ri->li_next;
2605 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2606 break;
2607 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002608 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002610 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002611 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 ri = NULL;
2613 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002614 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002615 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002616 lp->ll_li = lp->ll_li->li_next;
2617 ++lp->ll_n1;
2618 }
2619 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002620 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002621 else if (lp->ll_empty2
2622 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002624 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 }
2626 else
2627 {
2628 /*
2629 * Assign to a List or Dictionary item.
2630 */
Bram Moolenaar9937a052019-06-15 15:45:06 +02002631 if (is_const)
2632 {
2633 emsg(_("E996: Cannot lock a list or dict"));
2634 return;
2635 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 if (lp->ll_newkey != NULL)
2637 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002638 if (op != NULL && *op != '=')
2639 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002640 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002641 return;
2642 }
2643
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002645 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 if (di == NULL)
2647 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002648 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2649 {
2650 vim_free(di);
2651 return;
2652 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002654 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002655 else if (op != NULL && *op != '=')
2656 {
2657 tv_op(lp->ll_tv, rettv, op);
2658 return;
2659 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002660 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002662
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 /*
2664 * Assign the value to the variable or list item.
2665 */
2666 if (copy)
2667 copy_tv(rettv, lp->ll_tv);
2668 else
2669 {
2670 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002671 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002672 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002673 }
2674 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002675}
2676
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002677/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01002678 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2679 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002680 * Returns OK or FAIL.
2681 */
2682 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002683tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002684{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002685 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002686 char_u numbuf[NUMBUFLEN];
2687 char_u *s;
2688
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002689 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2690 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2691 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002692 {
2693 switch (tv1->v_type)
2694 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002695 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002696 case VAR_DICT:
2697 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002698 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002699 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002700 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002701 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002702 break;
2703
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002704 case VAR_BLOB:
2705 if (*op != '+' || tv2->v_type != VAR_BLOB)
2706 break;
2707 // BLOB += BLOB
2708 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2709 {
2710 blob_T *b1 = tv1->vval.v_blob;
2711 blob_T *b2 = tv2->vval.v_blob;
2712 int i, len = blob_len(b2);
2713 for (i = 0; i < len; i++)
2714 ga_append(&b1->bv_ga, blob_get(b2, i));
2715 }
2716 return OK;
2717
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002718 case VAR_LIST:
2719 if (*op != '+' || tv2->v_type != VAR_LIST)
2720 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002721 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002722 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2723 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2724 return OK;
2725
2726 case VAR_NUMBER:
2727 case VAR_STRING:
2728 if (tv2->v_type == VAR_LIST)
2729 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002730 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002731 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002732 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002733 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002734#ifdef FEAT_FLOAT
2735 if (tv2->v_type == VAR_FLOAT)
2736 {
2737 float_T f = n;
2738
Bram Moolenaarff697e62019-02-12 22:28:33 +01002739 if (*op == '%')
2740 break;
2741 switch (*op)
2742 {
2743 case '+': f += tv2->vval.v_float; break;
2744 case '-': f -= tv2->vval.v_float; break;
2745 case '*': f *= tv2->vval.v_float; break;
2746 case '/': f /= tv2->vval.v_float; break;
2747 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002748 clear_tv(tv1);
2749 tv1->v_type = VAR_FLOAT;
2750 tv1->vval.v_float = f;
2751 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002752 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002753#endif
2754 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002755 switch (*op)
2756 {
2757 case '+': n += tv_get_number(tv2); break;
2758 case '-': n -= tv_get_number(tv2); break;
2759 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01002760 case '/': n = num_divide(n, tv_get_number(tv2)); break;
2761 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002762 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002763 clear_tv(tv1);
2764 tv1->v_type = VAR_NUMBER;
2765 tv1->vval.v_number = n;
2766 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002767 }
2768 else
2769 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002770 if (tv2->v_type == VAR_FLOAT)
2771 break;
2772
Bram Moolenaarff697e62019-02-12 22:28:33 +01002773 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002774 s = tv_get_string(tv1);
2775 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002776 clear_tv(tv1);
2777 tv1->v_type = VAR_STRING;
2778 tv1->vval.v_string = s;
2779 }
2780 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002781
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002782 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002783#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002784 {
2785 float_T f;
2786
Bram Moolenaarff697e62019-02-12 22:28:33 +01002787 if (*op == '%' || *op == '.'
2788 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002789 && tv2->v_type != VAR_NUMBER
2790 && tv2->v_type != VAR_STRING))
2791 break;
2792 if (tv2->v_type == VAR_FLOAT)
2793 f = tv2->vval.v_float;
2794 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002795 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01002796 switch (*op)
2797 {
2798 case '+': tv1->vval.v_float += f; break;
2799 case '-': tv1->vval.v_float -= f; break;
2800 case '*': tv1->vval.v_float *= f; break;
2801 case '/': tv1->vval.v_float /= f; break;
2802 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002803 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002804#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002805 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002806 }
2807 }
2808
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002809 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002810 return FAIL;
2811}
2812
2813/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002814 * Evaluate the expression used in a ":for var in expr" command.
2815 * "arg" points to "var".
2816 * Set "*errp" to TRUE for an error, FALSE otherwise;
2817 * Return a pointer that holds the info. Null when there is an error.
2818 */
2819 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002820eval_for_line(
2821 char_u *arg,
2822 int *errp,
2823 char_u **nextcmdp,
2824 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002825{
Bram Moolenaar33570922005-01-25 22:26:29 +00002826 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002827 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002828 typval_T tv;
2829 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002830
2831 *errp = TRUE; /* default: there is an error */
2832
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002833 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002834 if (fi == NULL)
2835 return NULL;
2836
2837 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2838 if (expr == NULL)
2839 return fi;
2840
2841 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002842 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002843 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002844 emsg(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002845 return fi;
2846 }
2847
2848 if (skip)
2849 ++emsg_skip;
2850 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2851 {
2852 *errp = FALSE;
2853 if (!skip)
2854 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002855 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002856 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002857 l = tv.vval.v_list;
2858 if (l == NULL)
2859 {
2860 // a null list is like an empty list: do nothing
2861 clear_tv(&tv);
2862 }
2863 else
2864 {
2865 // No need to increment the refcount, it's already set for
2866 // the list being used in "tv".
2867 fi->fi_list = l;
2868 list_add_watch(l, &fi->fi_lw);
2869 fi->fi_lw.lw_item = l->lv_first;
2870 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002871 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002872 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002873 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002874 fi->fi_bi = 0;
2875 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002876 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002877 typval_T btv;
2878
2879 // Make a copy, so that the iteration still works when the
2880 // blob is changed.
2881 blob_copy(&tv, &btv);
2882 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002883 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002884 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002885 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002886 else
2887 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002888 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002889 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002890 }
2891 }
2892 }
2893 if (skip)
2894 --emsg_skip;
2895
2896 return fi;
2897}
2898
2899/*
2900 * Use the first item in a ":for" list. Advance to the next.
2901 * Assign the values to the variable (list). "arg" points to the first one.
2902 * Return TRUE when a valid item was found, FALSE when at end of list or
2903 * something wrong.
2904 */
2905 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002906next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002907{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002908 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002909 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002910 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002911
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002912 if (fi->fi_blob != NULL)
2913 {
2914 typval_T tv;
2915
2916 if (fi->fi_bi >= blob_len(fi->fi_blob))
2917 return FALSE;
2918 tv.v_type = VAR_NUMBER;
2919 tv.v_lock = VAR_FIXED;
2920 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2921 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002922 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
2923 fi->fi_varcount, FALSE, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002924 }
2925
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002926 item = fi->fi_lw.lw_item;
2927 if (item == NULL)
2928 result = FALSE;
2929 else
2930 {
2931 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002932 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
2933 fi->fi_varcount, FALSE, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002934 }
2935 return result;
2936}
2937
2938/*
2939 * Free the structure used to store info used by ":for".
2940 */
2941 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002942free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002943{
Bram Moolenaar33570922005-01-25 22:26:29 +00002944 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002945
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002946 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002947 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002948 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002949 list_unref(fi->fi_list);
2950 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002951 if (fi != NULL && fi->fi_blob != NULL)
2952 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002953 vim_free(fi);
2954}
2955
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2957
2958 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002959set_context_for_expression(
2960 expand_T *xp,
2961 char_u *arg,
2962 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963{
2964 int got_eq = FALSE;
2965 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002966 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002968 if (cmdidx == CMD_let)
2969 {
2970 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002971 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002972 {
2973 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002974 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002975 {
2976 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002977 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002978 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002979 break;
2980 }
2981 return;
2982 }
2983 }
2984 else
2985 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2986 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 while ((xp->xp_pattern = vim_strpbrk(arg,
2988 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2989 {
2990 c = *xp->xp_pattern;
2991 if (c == '&')
2992 {
2993 c = xp->xp_pattern[1];
2994 if (c == '&')
2995 {
2996 ++xp->xp_pattern;
2997 xp->xp_context = cmdidx != CMD_let || got_eq
2998 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2999 }
3000 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003001 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003003 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3004 xp->xp_pattern += 2;
3005
3006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 }
3008 else if (c == '$')
3009 {
3010 /* environment variable */
3011 xp->xp_context = EXPAND_ENV_VARS;
3012 }
3013 else if (c == '=')
3014 {
3015 got_eq = TRUE;
3016 xp->xp_context = EXPAND_EXPRESSION;
3017 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003018 else if (c == '#'
3019 && xp->xp_context == EXPAND_EXPRESSION)
3020 {
3021 /* Autoload function/variable contains '#'. */
3022 break;
3023 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003024 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 && xp->xp_context == EXPAND_FUNCTIONS
3026 && vim_strchr(xp->xp_pattern, '(') == NULL)
3027 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003028 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 break;
3030 }
3031 else if (cmdidx != CMD_let || got_eq)
3032 {
3033 if (c == '"') /* string */
3034 {
3035 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3036 if (c == '\\' && xp->xp_pattern[1] != NUL)
3037 ++xp->xp_pattern;
3038 xp->xp_context = EXPAND_NOTHING;
3039 }
3040 else if (c == '\'') /* literal string */
3041 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003042 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3044 /* skip */ ;
3045 xp->xp_context = EXPAND_NOTHING;
3046 }
3047 else if (c == '|')
3048 {
3049 if (xp->xp_pattern[1] == '|')
3050 {
3051 ++xp->xp_pattern;
3052 xp->xp_context = EXPAND_EXPRESSION;
3053 }
3054 else
3055 xp->xp_context = EXPAND_COMMANDS;
3056 }
3057 else
3058 xp->xp_context = EXPAND_EXPRESSION;
3059 }
3060 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003061 /* Doesn't look like something valid, expand as an expression
3062 * anyway. */
3063 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 arg = xp->xp_pattern;
3065 if (*arg != NUL)
3066 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3067 /* skip */ ;
3068 }
3069 xp->xp_pattern = arg;
3070}
3071
3072#endif /* FEAT_CMDL_COMPL */
3073
3074/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 * ":unlet[!] var1 ... " command.
3076 */
3077 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003078ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003080 ex_unletlock(eap, eap->arg, 0);
3081}
3082
3083/*
3084 * ":lockvar" and ":unlockvar" commands
3085 */
3086 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003087ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003088{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003090 int deep = 2;
3091
3092 if (eap->forceit)
3093 deep = -1;
3094 else if (vim_isdigit(*arg))
3095 {
3096 deep = getdigits(&arg);
3097 arg = skipwhite(arg);
3098 }
3099
3100 ex_unletlock(eap, arg, deep);
3101}
3102
3103/*
3104 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3105 */
3106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003107ex_unletlock(
3108 exarg_T *eap,
3109 char_u *argstart,
3110 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003111{
3112 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003115 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116
3117 do
3118 {
Bram Moolenaar137374f2018-05-13 15:59:50 +02003119 if (*arg == '$')
3120 {
3121 char_u *name = ++arg;
3122
3123 if (get_env_len(&arg) == 0)
3124 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003125 semsg(_(e_invarg2), name - 1);
Bram Moolenaar137374f2018-05-13 15:59:50 +02003126 return;
3127 }
3128 vim_unsetenv(name);
3129 arg = skipwhite(arg);
3130 continue;
3131 }
3132
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003133 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003134 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003135 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003136 if (lv.ll_name == NULL)
3137 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003138 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003139 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003141 if (name_end != NULL)
3142 {
3143 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003144 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003145 }
3146 if (!(eap->skip || error))
3147 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 break;
3149 }
3150
3151 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003152 {
3153 if (eap->cmdidx == CMD_unlet)
3154 {
3155 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3156 error = TRUE;
3157 }
3158 else
3159 {
3160 if (do_lock_var(&lv, name_end, deep,
3161 eap->cmdidx == CMD_lockvar) == FAIL)
3162 error = TRUE;
3163 }
3164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003166 if (!eap->skip)
3167 clear_lval(&lv);
3168
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 arg = skipwhite(name_end);
3170 } while (!ends_excmd(*arg));
3171
3172 eap->nextcmd = check_nextcmd(arg);
3173}
3174
Bram Moolenaar8c711452005-01-14 21:53:12 +00003175 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003176do_unlet_var(
3177 lval_T *lp,
3178 char_u *name_end,
3179 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003180{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003181 int ret = OK;
3182 int cc;
3183
3184 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003185 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003186 cc = *name_end;
3187 *name_end = NUL;
3188
3189 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003190 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003191 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003192 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003193 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003194 else if ((lp->ll_list != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003195 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003196 || (lp->ll_dict != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003197 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003198 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003199 else if (lp->ll_range)
3200 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003201 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003202 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003203 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003204
3205 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3206 {
3207 li = ll_li->li_next;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003208 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003209 return FAIL;
3210 ll_li = li;
3211 ++ll_n1;
3212 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003213
3214 /* Delete a range of List items. */
3215 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3216 {
3217 li = lp->ll_li->li_next;
3218 listitem_remove(lp->ll_list, lp->ll_li);
3219 lp->ll_li = li;
3220 ++lp->ll_n1;
3221 }
3222 }
3223 else
3224 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003225 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003226 /* unlet a List item. */
3227 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003228 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003229 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003230 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003231 }
3232
3233 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003234}
3235
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236/*
3237 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003238 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 */
3240 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003241do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242{
Bram Moolenaar33570922005-01-25 22:26:29 +00003243 hashtab_T *ht;
3244 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003245 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003246 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003247 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248
Bram Moolenaar33570922005-01-25 22:26:29 +00003249 ht = find_var_ht(name, &varname);
3250 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003252 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003253 if (d == NULL)
3254 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003255 if (ht == &globvarht)
3256 d = &globvardict;
3257 else if (ht == &compat_hashtab)
3258 d = &vimvardict;
3259 else
3260 {
3261 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3262 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3263 }
3264 if (d == NULL)
3265 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003266 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003267 return FAIL;
3268 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003269 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003270 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003271 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003272 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003273 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003274 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003275 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003276 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003277 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003278 || var_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003279 return FAIL;
3280
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003281 delete_var(ht, hi);
3282 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003285 if (forceit)
3286 return OK;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003287 semsg(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 return FAIL;
3289}
3290
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003291/*
3292 * Lock or unlock variable indicated by "lp".
3293 * "deep" is the levels to go (-1 for unlimited);
3294 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3295 */
3296 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003297do_lock_var(
3298 lval_T *lp,
3299 char_u *name_end,
3300 int deep,
3301 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003302{
3303 int ret = OK;
3304 int cc;
3305 dictitem_T *di;
3306
3307 if (deep == 0) /* nothing to do */
3308 return OK;
3309
3310 if (lp->ll_tv == NULL)
3311 {
3312 cc = *name_end;
3313 *name_end = NUL;
3314
3315 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003316 di = find_var(lp->ll_name, NULL, TRUE);
3317 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003318 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01003319 else if ((di->di_flags & DI_FLAGS_FIX)
3320 && di->di_tv.v_type != VAR_DICT
3321 && di->di_tv.v_type != VAR_LIST)
3322 /* For historic reasons this error is not given for a list or dict.
3323 * E.g., the b: dict could be locked/unlocked. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003324 semsg(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003325 else
3326 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01003327 if (lock)
3328 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003329 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01003330 di->di_flags &= ~DI_FLAGS_LOCK;
3331 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003332 }
3333 *name_end = cc;
3334 }
3335 else if (lp->ll_range)
3336 {
3337 listitem_T *li = lp->ll_li;
3338
3339 /* (un)lock a range of List items. */
3340 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3341 {
3342 item_lock(&li->li_tv, deep, lock);
3343 li = li->li_next;
3344 ++lp->ll_n1;
3345 }
3346 }
3347 else if (lp->ll_list != NULL)
3348 /* (un)lock a List item. */
3349 item_lock(&lp->ll_li->li_tv, deep, lock);
3350 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003351 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003352 item_lock(&lp->ll_di->di_tv, deep, lock);
3353
3354 return ret;
3355}
3356
3357/*
3358 * Lock or unlock an item. "deep" is nr of levels to go.
3359 */
3360 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003361item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003362{
3363 static int recurse = 0;
3364 list_T *l;
3365 listitem_T *li;
3366 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003367 blob_T *b;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003368 hashitem_T *hi;
3369 int todo;
3370
3371 if (recurse >= DICT_MAXNEST)
3372 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003373 emsg(_("E743: variable nested too deep for (un)lock"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003374 return;
3375 }
3376 if (deep == 0)
3377 return;
3378 ++recurse;
3379
3380 /* lock/unlock the item itself */
3381 if (lock)
3382 tv->v_lock |= VAR_LOCKED;
3383 else
3384 tv->v_lock &= ~VAR_LOCKED;
3385
3386 switch (tv->v_type)
3387 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003388 case VAR_UNKNOWN:
3389 case VAR_NUMBER:
3390 case VAR_STRING:
3391 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003392 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003393 case VAR_FLOAT:
3394 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003395 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003396 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003397 break;
3398
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003399 case VAR_BLOB:
3400 if ((b = tv->vval.v_blob) != NULL)
3401 {
3402 if (lock)
3403 b->bv_lock |= VAR_LOCKED;
3404 else
3405 b->bv_lock &= ~VAR_LOCKED;
3406 }
3407 break;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003408 case VAR_LIST:
3409 if ((l = tv->vval.v_list) != NULL)
3410 {
3411 if (lock)
3412 l->lv_lock |= VAR_LOCKED;
3413 else
3414 l->lv_lock &= ~VAR_LOCKED;
3415 if (deep < 0 || deep > 1)
3416 /* recursive: lock/unlock the items the List contains */
3417 for (li = l->lv_first; li != NULL; li = li->li_next)
3418 item_lock(&li->li_tv, deep - 1, lock);
3419 }
3420 break;
3421 case VAR_DICT:
3422 if ((d = tv->vval.v_dict) != NULL)
3423 {
3424 if (lock)
3425 d->dv_lock |= VAR_LOCKED;
3426 else
3427 d->dv_lock &= ~VAR_LOCKED;
3428 if (deep < 0 || deep > 1)
3429 {
3430 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003431 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003432 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3433 {
3434 if (!HASHITEM_EMPTY(hi))
3435 {
3436 --todo;
3437 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3438 }
3439 }
3440 }
3441 }
3442 }
3443 --recurse;
3444}
3445
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3447/*
3448 * Delete all "menutrans_" variables.
3449 */
3450 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003451del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452{
Bram Moolenaar33570922005-01-25 22:26:29 +00003453 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003454 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455
Bram Moolenaar33570922005-01-25 22:26:29 +00003456 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003457 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003458 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003459 {
3460 if (!HASHITEM_EMPTY(hi))
3461 {
3462 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003463 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3464 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003465 }
3466 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003467 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468}
3469#endif
3470
3471#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3472
3473/*
3474 * Local string buffer for the next two functions to store a variable name
3475 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3476 * get_user_var_name().
3477 */
3478
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479static char_u *varnamebuf = NULL;
3480static int varnamebuflen = 0;
3481
3482/*
3483 * Function to concatenate a prefix and a variable name.
3484 */
3485 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003486cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487{
3488 int len;
3489
3490 len = (int)STRLEN(name) + 3;
3491 if (len > varnamebuflen)
3492 {
3493 vim_free(varnamebuf);
3494 len += 10; /* some additional space */
3495 varnamebuf = alloc(len);
3496 if (varnamebuf == NULL)
3497 {
3498 varnamebuflen = 0;
3499 return NULL;
3500 }
3501 varnamebuflen = len;
3502 }
3503 *varnamebuf = prefix;
3504 varnamebuf[1] = ':';
3505 STRCPY(varnamebuf + 2, name);
3506 return varnamebuf;
3507}
3508
3509/*
3510 * Function given to ExpandGeneric() to obtain the list of user defined
3511 * (global/buffer/window/built-in) variable names.
3512 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003514get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003516 static long_u gdone;
3517 static long_u bdone;
3518 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003519 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003520 static int vidx;
3521 static hashitem_T *hi;
3522 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523
3524 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003525 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003526 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003527 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003528 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003529
3530 /* Global variables */
3531 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003533 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003534 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003535 else
3536 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003537 while (HASHITEM_EMPTY(hi))
3538 ++hi;
3539 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3540 return cat_prefix_varname('g', hi->hi_key);
3541 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003543
3544 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003545 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003546 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003548 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003549 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003550 else
3551 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003552 while (HASHITEM_EMPTY(hi))
3553 ++hi;
3554 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003556
3557 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003558 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003559 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003561 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003562 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003563 else
3564 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003565 while (HASHITEM_EMPTY(hi))
3566 ++hi;
3567 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003569
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003570 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003571 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003572 if (tdone < ht->ht_used)
3573 {
3574 if (tdone++ == 0)
3575 hi = ht->ht_array;
3576 else
3577 ++hi;
3578 while (HASHITEM_EMPTY(hi))
3579 ++hi;
3580 return cat_prefix_varname('t', hi->hi_key);
3581 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003582
Bram Moolenaar33570922005-01-25 22:26:29 +00003583 /* v: variables */
3584 if (vidx < VV_LEN)
3585 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586
Bram Moolenaard23a8232018-02-10 18:45:26 +01003587 VIM_CLEAR(varnamebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 varnamebuflen = 0;
3589 return NULL;
3590}
3591
3592#endif /* FEAT_CMDL_COMPL */
3593
3594/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003595 * Return TRUE if "pat" matches "text".
3596 * Does not use 'cpo' and always uses 'magic'.
3597 */
3598 static int
3599pattern_match(char_u *pat, char_u *text, int ic)
3600{
3601 int matches = FALSE;
3602 char_u *save_cpo;
3603 regmatch_T regmatch;
3604
3605 /* avoid 'l' flag in 'cpoptions' */
3606 save_cpo = p_cpo;
3607 p_cpo = (char_u *)"";
3608 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3609 if (regmatch.regprog != NULL)
3610 {
3611 regmatch.rm_ic = ic;
3612 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3613 vim_regfree(regmatch.regprog);
3614 }
3615 p_cpo = save_cpo;
3616 return matches;
3617}
3618
3619/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003621 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3623 */
3624
3625/*
3626 * Handle zero level expression.
3627 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003628 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003629 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 * Return OK or FAIL.
3631 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003632 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003633eval0(
3634 char_u *arg,
3635 typval_T *rettv,
3636 char_u **nextcmd,
3637 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638{
3639 int ret;
3640 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003641 int did_emsg_before = did_emsg;
3642 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643
3644 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003645 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 if (ret == FAIL || !ends_excmd(*p))
3647 {
3648 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003649 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 /*
3651 * Report the invalid expression unless the expression evaluation has
3652 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003653 * exception, or we already gave a more specific error.
3654 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003656 if (!aborting() && did_emsg == did_emsg_before
3657 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003658 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 ret = FAIL;
3660 }
3661 if (nextcmd != NULL)
3662 *nextcmd = check_nextcmd(p);
3663
3664 return ret;
3665}
3666
3667/*
3668 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003669 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 *
3671 * "arg" must point to the first non-white of the expression.
3672 * "arg" is advanced to the next non-white after the recognized expression.
3673 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003674 * Note: "rettv.v_lock" is not set.
3675 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 * Return OK or FAIL.
3677 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003678 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003679eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680{
3681 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003682 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683
3684 /*
3685 * Get the first variable.
3686 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003687 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 return FAIL;
3689
3690 if ((*arg)[0] == '?')
3691 {
3692 result = FALSE;
3693 if (evaluate)
3694 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003695 int error = FALSE;
3696
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003697 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003699 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003700 if (error)
3701 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 }
3703
3704 /*
3705 * Get the second variable.
3706 */
3707 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003708 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 return FAIL;
3710
3711 /*
3712 * Check for the ":".
3713 */
3714 if ((*arg)[0] != ':')
3715 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003716 emsg(_("E109: Missing ':' after '?'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003718 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 return FAIL;
3720 }
3721
3722 /*
3723 * Get the third variable.
3724 */
3725 *arg = skipwhite(*arg + 1);
3726 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3727 {
3728 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003729 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 return FAIL;
3731 }
3732 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003733 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 }
3735
3736 return OK;
3737}
3738
3739/*
3740 * Handle first level expression:
3741 * expr2 || expr2 || expr2 logical OR
3742 *
3743 * "arg" must point to the first non-white of the expression.
3744 * "arg" is advanced to the next non-white after the recognized expression.
3745 *
3746 * Return OK or FAIL.
3747 */
3748 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003749eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750{
Bram Moolenaar33570922005-01-25 22:26:29 +00003751 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 long result;
3753 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003754 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755
3756 /*
3757 * Get the first variable.
3758 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003759 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 return FAIL;
3761
3762 /*
3763 * Repeat until there is no following "||".
3764 */
3765 first = TRUE;
3766 result = FALSE;
3767 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3768 {
3769 if (evaluate && first)
3770 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003771 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003773 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003774 if (error)
3775 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 first = FALSE;
3777 }
3778
3779 /*
3780 * Get the second variable.
3781 */
3782 *arg = skipwhite(*arg + 2);
3783 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3784 return FAIL;
3785
3786 /*
3787 * Compute the result.
3788 */
3789 if (evaluate && !result)
3790 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003791 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003793 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003794 if (error)
3795 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 }
3797 if (evaluate)
3798 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003799 rettv->v_type = VAR_NUMBER;
3800 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 }
3802 }
3803
3804 return OK;
3805}
3806
3807/*
3808 * Handle second level expression:
3809 * expr3 && expr3 && expr3 logical AND
3810 *
3811 * "arg" must point to the first non-white of the expression.
3812 * "arg" is advanced to the next non-white after the recognized expression.
3813 *
3814 * Return OK or FAIL.
3815 */
3816 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003817eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818{
Bram Moolenaar33570922005-01-25 22:26:29 +00003819 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 long result;
3821 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003822 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823
3824 /*
3825 * Get the first variable.
3826 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003827 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 return FAIL;
3829
3830 /*
3831 * Repeat until there is no following "&&".
3832 */
3833 first = TRUE;
3834 result = TRUE;
3835 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3836 {
3837 if (evaluate && first)
3838 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003839 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003841 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003842 if (error)
3843 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 first = FALSE;
3845 }
3846
3847 /*
3848 * Get the second variable.
3849 */
3850 *arg = skipwhite(*arg + 2);
3851 if (eval4(arg, &var2, evaluate && result) == FAIL)
3852 return FAIL;
3853
3854 /*
3855 * Compute the result.
3856 */
3857 if (evaluate && result)
3858 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003859 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003861 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003862 if (error)
3863 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 }
3865 if (evaluate)
3866 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003867 rettv->v_type = VAR_NUMBER;
3868 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 }
3870 }
3871
3872 return OK;
3873}
3874
3875/*
3876 * Handle third level expression:
3877 * var1 == var2
3878 * var1 =~ var2
3879 * var1 != var2
3880 * var1 !~ var2
3881 * var1 > var2
3882 * var1 >= var2
3883 * var1 < var2
3884 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003885 * var1 is var2
3886 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 *
3888 * "arg" must point to the first non-white of the expression.
3889 * "arg" is advanced to the next non-white after the recognized expression.
3890 *
3891 * Return OK or FAIL.
3892 */
3893 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003894eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895{
Bram Moolenaar33570922005-01-25 22:26:29 +00003896 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 char_u *p;
3898 int i;
3899 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003900 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903
3904 /*
3905 * Get the first variable.
3906 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003907 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 return FAIL;
3909
3910 p = *arg;
3911 switch (p[0])
3912 {
3913 case '=': if (p[1] == '=')
3914 type = TYPE_EQUAL;
3915 else if (p[1] == '~')
3916 type = TYPE_MATCH;
3917 break;
3918 case '!': if (p[1] == '=')
3919 type = TYPE_NEQUAL;
3920 else if (p[1] == '~')
3921 type = TYPE_NOMATCH;
3922 break;
3923 case '>': if (p[1] != '=')
3924 {
3925 type = TYPE_GREATER;
3926 len = 1;
3927 }
3928 else
3929 type = TYPE_GEQUAL;
3930 break;
3931 case '<': if (p[1] != '=')
3932 {
3933 type = TYPE_SMALLER;
3934 len = 1;
3935 }
3936 else
3937 type = TYPE_SEQUAL;
3938 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003939 case 'i': if (p[1] == 's')
3940 {
3941 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3942 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003943 i = p[len];
3944 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003945 {
3946 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3947 type_is = TRUE;
3948 }
3949 }
3950 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 }
3952
3953 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003954 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 */
3956 if (type != TYPE_UNKNOWN)
3957 {
3958 /* extra question mark appended: ignore case */
3959 if (p[len] == '?')
3960 {
3961 ic = TRUE;
3962 ++len;
3963 }
3964 /* extra '#' appended: match case */
3965 else if (p[len] == '#')
3966 {
3967 ic = FALSE;
3968 ++len;
3969 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003970 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 else
3972 ic = p_ic;
3973
3974 /*
3975 * Get the second variable.
3976 */
3977 *arg = skipwhite(p + len);
3978 if (eval5(arg, &var2, evaluate) == FAIL)
3979 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003980 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 return FAIL;
3982 }
Bram Moolenaar31988702018-02-13 12:57:42 +01003983 if (evaluate)
3984 {
3985 int ret = typval_compare(rettv, &var2, type, type_is, ic);
3986
3987 clear_tv(&var2);
3988 return ret;
3989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 }
3991
3992 return OK;
3993}
3994
3995/*
3996 * Handle fourth level expression:
3997 * + number addition
3998 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003999 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02004000 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 *
4002 * "arg" must point to the first non-white of the expression.
4003 * "arg" is advanced to the next non-white after the recognized expression.
4004 *
4005 * Return OK or FAIL.
4006 */
4007 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004008eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009{
Bram Moolenaar33570922005-01-25 22:26:29 +00004010 typval_T var2;
4011 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004013 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004014#ifdef FEAT_FLOAT
4015 float_T f1 = 0, f2 = 0;
4016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 char_u *s1, *s2;
4018 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4019 char_u *p;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004020 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021
4022 /*
4023 * Get the first variable.
4024 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004025 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 return FAIL;
4027
4028 /*
4029 * Repeat computing, until no '+', '-' or '.' is following.
4030 */
4031 for (;;)
4032 {
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004033 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 op = **arg;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004035 concat = op == '.'
4036 && (*(*arg + 1) == '.' || current_sctx.sc_version < 2);
4037 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 break;
4039
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004040 if ((op != '+' || (rettv->v_type != VAR_LIST
4041 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004042#ifdef FEAT_FLOAT
4043 && (op == '.' || rettv->v_type != VAR_FLOAT)
4044#endif
4045 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004046 {
4047 /* For "list + ...", an illegal use of the first operand as
4048 * a number cannot be determined before evaluating the 2nd
4049 * operand: if this is also a list, all is ok.
4050 * For "something . ...", "something - ..." or "non-list + ...",
4051 * we know that the first operand needs to be a string or number
4052 * without evaluating the 2nd operand. So check before to avoid
4053 * side effects after an error. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004054 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004055 {
4056 clear_tv(rettv);
4057 return FAIL;
4058 }
4059 }
4060
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 /*
4062 * Get the second variable.
4063 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02004064 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
4065 ++*arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004067 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004069 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 return FAIL;
4071 }
4072
4073 if (evaluate)
4074 {
4075 /*
4076 * Compute the result.
4077 */
4078 if (op == '.')
4079 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004080 s1 = tv_get_string_buf(rettv, buf1); /* already checked */
4081 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004082 if (s2 == NULL) /* type error ? */
4083 {
4084 clear_tv(rettv);
4085 clear_tv(&var2);
4086 return FAIL;
4087 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004088 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004089 clear_tv(rettv);
4090 rettv->v_type = VAR_STRING;
4091 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004093 else if (op == '+' && rettv->v_type == VAR_BLOB
4094 && var2.v_type == VAR_BLOB)
4095 {
4096 blob_T *b1 = rettv->vval.v_blob;
4097 blob_T *b2 = var2.vval.v_blob;
4098 blob_T *b = blob_alloc();
4099 int i;
4100
4101 if (b != NULL)
4102 {
4103 for (i = 0; i < blob_len(b1); i++)
4104 ga_append(&b->bv_ga, blob_get(b1, i));
4105 for (i = 0; i < blob_len(b2); i++)
4106 ga_append(&b->bv_ga, blob_get(b2, i));
4107
4108 clear_tv(rettv);
4109 rettv_blob_set(rettv, b);
4110 }
4111 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004112 else if (op == '+' && rettv->v_type == VAR_LIST
4113 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004114 {
4115 /* concatenate Lists */
4116 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4117 &var3) == FAIL)
4118 {
4119 clear_tv(rettv);
4120 clear_tv(&var2);
4121 return FAIL;
4122 }
4123 clear_tv(rettv);
4124 *rettv = var3;
4125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 else
4127 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004128 int error = FALSE;
4129
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004130#ifdef FEAT_FLOAT
4131 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004132 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004133 f1 = rettv->vval.v_float;
4134 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004135 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004136 else
4137#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004138 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004139 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004140 if (error)
4141 {
4142 /* This can only happen for "list + non-list". For
4143 * "non-list + ..." or "something - ...", we returned
4144 * before evaluating the 2nd operand. */
4145 clear_tv(rettv);
4146 return FAIL;
4147 }
4148#ifdef FEAT_FLOAT
4149 if (var2.v_type == VAR_FLOAT)
4150 f1 = n1;
4151#endif
4152 }
4153#ifdef FEAT_FLOAT
4154 if (var2.v_type == VAR_FLOAT)
4155 {
4156 f2 = var2.vval.v_float;
4157 n2 = 0;
4158 }
4159 else
4160#endif
4161 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004162 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004163 if (error)
4164 {
4165 clear_tv(rettv);
4166 clear_tv(&var2);
4167 return FAIL;
4168 }
4169#ifdef FEAT_FLOAT
4170 if (rettv->v_type == VAR_FLOAT)
4171 f2 = n2;
4172#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004173 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004174 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004175
4176#ifdef FEAT_FLOAT
4177 /* If there is a float on either side the result is a float. */
4178 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4179 {
4180 if (op == '+')
4181 f1 = f1 + f2;
4182 else
4183 f1 = f1 - f2;
4184 rettv->v_type = VAR_FLOAT;
4185 rettv->vval.v_float = f1;
4186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004188#endif
4189 {
4190 if (op == '+')
4191 n1 = n1 + n2;
4192 else
4193 n1 = n1 - n2;
4194 rettv->v_type = VAR_NUMBER;
4195 rettv->vval.v_number = n1;
4196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004198 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 }
4200 }
4201 return OK;
4202}
4203
4204/*
4205 * Handle fifth level expression:
4206 * * number multiplication
4207 * / number division
4208 * % number modulo
4209 *
4210 * "arg" must point to the first non-white of the expression.
4211 * "arg" is advanced to the next non-white after the recognized expression.
4212 *
4213 * Return OK or FAIL.
4214 */
4215 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004216eval6(
4217 char_u **arg,
4218 typval_T *rettv,
4219 int evaluate,
4220 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221{
Bram Moolenaar33570922005-01-25 22:26:29 +00004222 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004224 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004225#ifdef FEAT_FLOAT
4226 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02004227 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004228#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004229 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230
4231 /*
4232 * Get the first variable.
4233 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004234 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 return FAIL;
4236
4237 /*
4238 * Repeat computing, until no '*', '/' or '%' is following.
4239 */
4240 for (;;)
4241 {
4242 op = **arg;
4243 if (op != '*' && op != '/' && op != '%')
4244 break;
4245
4246 if (evaluate)
4247 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004248#ifdef FEAT_FLOAT
4249 if (rettv->v_type == VAR_FLOAT)
4250 {
4251 f1 = rettv->vval.v_float;
4252 use_float = TRUE;
4253 n1 = 0;
4254 }
4255 else
4256#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004257 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004258 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004259 if (error)
4260 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 }
4262 else
4263 n1 = 0;
4264
4265 /*
4266 * Get the second variable.
4267 */
4268 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004269 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 return FAIL;
4271
4272 if (evaluate)
4273 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004274#ifdef FEAT_FLOAT
4275 if (var2.v_type == VAR_FLOAT)
4276 {
4277 if (!use_float)
4278 {
4279 f1 = n1;
4280 use_float = TRUE;
4281 }
4282 f2 = var2.vval.v_float;
4283 n2 = 0;
4284 }
4285 else
4286#endif
4287 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004288 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004289 clear_tv(&var2);
4290 if (error)
4291 return FAIL;
4292#ifdef FEAT_FLOAT
4293 if (use_float)
4294 f2 = n2;
4295#endif
4296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297
4298 /*
4299 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004300 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004302#ifdef FEAT_FLOAT
4303 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004305 if (op == '*')
4306 f1 = f1 * f2;
4307 else if (op == '/')
4308 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004309# ifdef VMS
4310 /* VMS crashes on divide by zero, work around it */
4311 if (f2 == 0.0)
4312 {
4313 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004314 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004315 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004316 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004317 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004318 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004319 }
4320 else
4321 f1 = f1 / f2;
4322# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004323 /* We rely on the floating point library to handle divide
4324 * by zero to result in "inf" and not a crash. */
4325 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004326# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004329 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004330 emsg(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004331 return FAIL;
4332 }
4333 rettv->v_type = VAR_FLOAT;
4334 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 }
4336 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004339 if (op == '*')
4340 n1 = n1 * n2;
4341 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01004342 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01004344 n1 = num_modulus(n1, n2);
4345
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004346 rettv->v_type = VAR_NUMBER;
4347 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 }
4350 }
4351
4352 return OK;
4353}
4354
4355/*
4356 * Handle sixth level expression:
4357 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004358 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004359 * "string" string constant
4360 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 * &option-name option value
4362 * @r register contents
4363 * identifier variable value
4364 * function() function call
4365 * $VAR environment variable
4366 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004367 * [expr, expr] List
4368 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 *
4370 * Also handle:
4371 * ! in front logical NOT
4372 * - in front unary minus
4373 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004374 * trailing [] subscript in String or List
4375 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 *
4377 * "arg" must point to the first non-white of the expression.
4378 * "arg" is advanced to the next non-white after the recognized expression.
4379 *
4380 * Return OK or FAIL.
4381 */
4382 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004383eval7(
4384 char_u **arg,
4385 typval_T *rettv,
4386 int evaluate,
4387 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004389 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 int len;
4391 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 char_u *start_leader, *end_leader;
4393 int ret = OK;
4394 char_u *alias;
4395
4396 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004397 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004398 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004400 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401
4402 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004403 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 */
4405 start_leader = *arg;
4406 while (**arg == '!' || **arg == '-' || **arg == '+')
4407 *arg = skipwhite(*arg + 1);
4408 end_leader = *arg;
4409
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004410 if (**arg == '.' && (!isdigit(*(*arg + 1))
4411#ifdef FEAT_FLOAT
4412 || current_sctx.sc_version < 2
4413#endif
4414 ))
4415 {
4416 semsg(_(e_invexpr2), *arg);
4417 ++*arg;
4418 return FAIL;
4419 }
4420
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 switch (**arg)
4422 {
4423 /*
4424 * Number constant.
4425 */
4426 case '0':
4427 case '1':
4428 case '2':
4429 case '3':
4430 case '4':
4431 case '5':
4432 case '6':
4433 case '7':
4434 case '8':
4435 case '9':
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004436 case '.':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004437 {
4438#ifdef FEAT_FLOAT
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004439 char_u *p;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004440 int get_float = FALSE;
4441
4442 /* We accept a float when the format matches
4443 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004444 * strict to avoid backwards compatibility problems.
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004445 * With script version 2 and later the leading digit can be
4446 * omitted.
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004447 * Don't look for a float after the "." operator, so that
4448 * ":let vers = 1.2.3" doesn't fail. */
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004449 if (**arg == '.')
4450 p = *arg;
4451 else
4452 p = skipdigits(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004453 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004455 get_float = TRUE;
4456 p = skipdigits(p + 2);
4457 if (*p == 'e' || *p == 'E')
4458 {
4459 ++p;
4460 if (*p == '-' || *p == '+')
4461 ++p;
4462 if (!vim_isdigit(*p))
4463 get_float = FALSE;
4464 else
4465 p = skipdigits(p + 1);
4466 }
4467 if (ASCII_ISALPHA(*p) || *p == '.')
4468 get_float = FALSE;
4469 }
4470 if (get_float)
4471 {
4472 float_T f;
4473
4474 *arg += string2float(*arg, &f);
4475 if (evaluate)
4476 {
4477 rettv->v_type = VAR_FLOAT;
4478 rettv->vval.v_float = f;
4479 }
4480 }
4481 else
4482#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004483 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004484 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004485 char_u *bp;
Bram Moolenaare519dfd2019-01-13 15:42:02 +01004486 blob_T *blob = NULL; // init for gcc
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004487
4488 // Blob constant: 0z0123456789abcdef
4489 if (evaluate)
4490 blob = blob_alloc();
4491 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
4492 {
4493 if (!vim_isxdigit(bp[1]))
4494 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004495 if (blob != NULL)
4496 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004497 emsg(_("E973: Blob literal should have an even number of hex characters"));
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004498 ga_clear(&blob->bv_ga);
4499 VIM_CLEAR(blob);
4500 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004501 ret = FAIL;
4502 break;
4503 }
4504 if (blob != NULL)
4505 ga_append(&blob->bv_ga,
4506 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
Bram Moolenaar4131fd52019-01-17 16:32:53 +01004507 if (bp[2] == '.' && vim_isxdigit(bp[3]))
4508 ++bp;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004509 }
4510 if (blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004511 rettv_blob_set(rettv, blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004512 *arg = bp;
4513 }
4514 else
4515 {
4516 // decimal, hex or octal number
Bram Moolenaar16e9b852019-05-19 19:59:35 +02004517 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0, TRUE);
4518 if (len == 0)
4519 {
4520 semsg(_(e_invexpr2), *arg);
4521 ret = FAIL;
4522 break;
4523 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004524 *arg += len;
4525 if (evaluate)
4526 {
4527 rettv->v_type = VAR_NUMBER;
4528 rettv->vval.v_number = n;
4529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 }
4531 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533
4534 /*
4535 * String constant: "string".
4536 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004537 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 break;
4539
4540 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004541 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004543 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004544 break;
4545
4546 /*
4547 * List: [expr, expr]
4548 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004549 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 break;
4551
4552 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004553 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004554 * Dictionary: {key: val, key: val}
4555 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004556 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4557 if (ret == NOTDONE)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004558 ret = dict_get_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004559 break;
4560
4561 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004562 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004564 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 break;
4566
4567 /*
4568 * Environment variable: $VAR.
4569 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004570 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 break;
4572
4573 /*
4574 * Register contents: @r.
4575 */
4576 case '@': ++*arg;
4577 if (evaluate)
4578 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004579 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004580 rettv->vval.v_string = get_reg_contents(**arg,
4581 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 }
4583 if (**arg != NUL)
4584 ++*arg;
4585 break;
4586
4587 /*
4588 * nested expression: (expression).
4589 */
4590 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004591 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 if (**arg == ')')
4593 ++*arg;
4594 else if (ret == OK)
4595 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004596 emsg(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004597 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 ret = FAIL;
4599 }
4600 break;
4601
Bram Moolenaar8c711452005-01-14 21:53:12 +00004602 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 break;
4604 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004605
4606 if (ret == NOTDONE)
4607 {
4608 /*
4609 * Must be a variable or function name.
4610 * Can also be a curly-braces kind of name: {expr}.
4611 */
4612 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004613 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004614 if (alias != NULL)
4615 s = alias;
4616
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004617 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004618 ret = FAIL;
4619 else
4620 {
4621 if (**arg == '(') /* recursive! */
4622 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004623 partial_T *partial;
4624
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004625 if (!evaluate)
4626 check_vars(s, len);
4627
Bram Moolenaar8c711452005-01-14 21:53:12 +00004628 /* If "s" is the name of a variable of type VAR_FUNC
4629 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004630 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004631
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004632 /* Need to make a copy, in case evaluating the arguments makes
4633 * the name invalid. */
4634 s = vim_strsave(s);
4635 if (s == NULL)
4636 ret = FAIL;
4637 else
4638 /* Invoke the function. */
4639 ret = get_func_tv(s, len, rettv, arg,
4640 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4641 &len, evaluate, partial, NULL);
4642 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004643
4644 /* If evaluate is FALSE rettv->v_type was not set in
4645 * get_func_tv, but it's needed in handle_subscript() to parse
4646 * what follows. So set it here. */
4647 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4648 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004649 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004650 rettv->v_type = VAR_FUNC;
4651 }
4652
Bram Moolenaar8c711452005-01-14 21:53:12 +00004653 /* Stop the expression evaluation when immediately
4654 * aborting on error, or when an interrupt occurred or
4655 * an exception was thrown but not caught. */
Bram Moolenaar60640732019-06-07 23:15:22 +02004656 if (evaluate && aborting())
Bram Moolenaar8c711452005-01-14 21:53:12 +00004657 {
4658 if (ret == OK)
4659 clear_tv(rettv);
4660 ret = FAIL;
4661 }
4662 }
4663 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004664 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004665 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004666 {
4667 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004668 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004669 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004670 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004671 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004672 }
4673
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 *arg = skipwhite(*arg);
4675
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004676 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4677 * expr(expr). */
4678 if (ret == OK)
4679 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680
4681 /*
4682 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4683 */
4684 if (ret == OK && evaluate && end_leader > start_leader)
4685 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004686 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004687 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004688#ifdef FEAT_FLOAT
4689 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004690
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004691 if (rettv->v_type == VAR_FLOAT)
4692 f = rettv->vval.v_float;
4693 else
4694#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004695 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004696 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004698 clear_tv(rettv);
4699 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004701 else
4702 {
4703 while (end_leader > start_leader)
4704 {
4705 --end_leader;
4706 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004707 {
4708#ifdef FEAT_FLOAT
4709 if (rettv->v_type == VAR_FLOAT)
4710 f = !f;
4711 else
4712#endif
4713 val = !val;
4714 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004715 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004716 {
4717#ifdef FEAT_FLOAT
4718 if (rettv->v_type == VAR_FLOAT)
4719 f = -f;
4720 else
4721#endif
4722 val = -val;
4723 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004724 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004725#ifdef FEAT_FLOAT
4726 if (rettv->v_type == VAR_FLOAT)
4727 {
4728 clear_tv(rettv);
4729 rettv->vval.v_float = f;
4730 }
4731 else
4732#endif
4733 {
4734 clear_tv(rettv);
4735 rettv->v_type = VAR_NUMBER;
4736 rettv->vval.v_number = val;
4737 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 }
4740
4741 return ret;
4742}
4743
4744/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004745 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4746 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004747 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4748 */
4749 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004750eval_index(
4751 char_u **arg,
4752 typval_T *rettv,
4753 int evaluate,
4754 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004755{
4756 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004757 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004758 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004759 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004760 long len = -1;
4761 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004762 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004763 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004764
Bram Moolenaara03f2332016-02-06 18:09:59 +01004765 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004766 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004767 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004768 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004769 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004770 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004771 return FAIL;
4772 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004773#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004774 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004775 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004776 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004777#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004778 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004779 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004780 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004781 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004782 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004783 return FAIL;
4784 case VAR_UNKNOWN:
4785 if (evaluate)
4786 return FAIL;
4787 /* FALLTHROUGH */
4788
4789 case VAR_STRING:
4790 case VAR_NUMBER:
4791 case VAR_LIST:
4792 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004793 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004794 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004795 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004796
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004797 init_tv(&var1);
4798 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004799 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004800 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004801 /*
4802 * dict.name
4803 */
4804 key = *arg + 1;
4805 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4806 ;
4807 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004808 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004809 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004810 }
4811 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004812 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004813 /*
4814 * something[idx]
4815 *
4816 * Get the (first) variable from inside the [].
4817 */
4818 *arg = skipwhite(*arg + 1);
4819 if (**arg == ':')
4820 empty1 = TRUE;
4821 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4822 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004823 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004824 {
4825 /* not a number or string */
4826 clear_tv(&var1);
4827 return FAIL;
4828 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004829
4830 /*
4831 * Get the second variable from inside the [:].
4832 */
4833 if (**arg == ':')
4834 {
4835 range = TRUE;
4836 *arg = skipwhite(*arg + 1);
4837 if (**arg == ']')
4838 empty2 = TRUE;
4839 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4840 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004841 if (!empty1)
4842 clear_tv(&var1);
4843 return FAIL;
4844 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004845 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004846 {
4847 /* not a number or string */
4848 if (!empty1)
4849 clear_tv(&var1);
4850 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004851 return FAIL;
4852 }
4853 }
4854
4855 /* Check for the ']'. */
4856 if (**arg != ']')
4857 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004858 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004859 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004860 clear_tv(&var1);
4861 if (range)
4862 clear_tv(&var2);
4863 return FAIL;
4864 }
4865 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004866 }
4867
4868 if (evaluate)
4869 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004870 n1 = 0;
4871 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004872 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004873 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004874 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004875 }
4876 if (range)
4877 {
4878 if (empty2)
4879 n2 = -1;
4880 else
4881 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004882 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004883 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004884 }
4885 }
4886
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004887 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004888 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004889 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004890 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004891 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004892 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004893 case VAR_SPECIAL:
4894 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004895 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004896 break; /* not evaluating, skipping over subscript */
4897
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004898 case VAR_NUMBER:
4899 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004900 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004901 len = (long)STRLEN(s);
4902 if (range)
4903 {
4904 /* The resulting variable is a substring. If the indexes
4905 * are out of range the result is empty. */
4906 if (n1 < 0)
4907 {
4908 n1 = len + n1;
4909 if (n1 < 0)
4910 n1 = 0;
4911 }
4912 if (n2 < 0)
4913 n2 = len + n2;
4914 else if (n2 >= len)
4915 n2 = len;
4916 if (n1 >= len || n2 < 0 || n1 > n2)
4917 s = NULL;
4918 else
4919 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4920 }
4921 else
4922 {
4923 /* The resulting variable is a string of a single
4924 * character. If the index is too big or negative the
4925 * result is empty. */
4926 if (n1 >= len || n1 < 0)
4927 s = NULL;
4928 else
4929 s = vim_strnsave(s + n1, 1);
4930 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004931 clear_tv(rettv);
4932 rettv->v_type = VAR_STRING;
4933 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004934 break;
4935
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004936 case VAR_BLOB:
4937 len = blob_len(rettv->vval.v_blob);
4938 if (range)
4939 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01004940 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004941 // are out of range the result is empty.
4942 if (n1 < 0)
4943 {
4944 n1 = len + n1;
4945 if (n1 < 0)
4946 n1 = 0;
4947 }
4948 if (n2 < 0)
4949 n2 = len + n2;
4950 else if (n2 >= len)
4951 n2 = len - 1;
4952 if (n1 >= len || n2 < 0 || n1 > n2)
4953 {
4954 clear_tv(rettv);
4955 rettv->v_type = VAR_BLOB;
4956 rettv->vval.v_blob = NULL;
4957 }
4958 else
4959 {
4960 blob_T *blob = blob_alloc();
4961
4962 if (blob != NULL)
4963 {
4964 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
4965 {
4966 blob_free(blob);
4967 return FAIL;
4968 }
4969 blob->bv_ga.ga_len = n2 - n1 + 1;
4970 for (i = n1; i <= n2; i++)
4971 blob_set(blob, i - n1,
4972 blob_get(rettv->vval.v_blob, i));
4973
4974 clear_tv(rettv);
4975 rettv_blob_set(rettv, blob);
4976 }
4977 }
4978 }
4979 else
4980 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01004981 // The resulting variable is a byte value.
4982 // If the index is too big or negative that is an error.
4983 if (n1 < 0)
4984 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004985 if (n1 < len && n1 >= 0)
4986 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01004987 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004988
4989 clear_tv(rettv);
4990 rettv->v_type = VAR_NUMBER;
4991 rettv->vval.v_number = v;
4992 }
4993 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004994 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004995 }
4996 break;
4997
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004998 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004999 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005000 if (n1 < 0)
5001 n1 = len + n1;
5002 if (!empty1 && (n1 < 0 || n1 >= len))
5003 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005004 /* For a range we allow invalid values and return an empty
5005 * list. A list index out of range is an error. */
5006 if (!range)
5007 {
5008 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005009 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005010 return FAIL;
5011 }
5012 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005013 }
5014 if (range)
5015 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005016 list_T *l;
5017 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005018
5019 if (n2 < 0)
5020 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005021 else if (n2 >= len)
5022 n2 = len - 1;
5023 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005024 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005025 l = list_alloc();
5026 if (l == NULL)
5027 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005028 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005029 n1 <= n2; ++n1)
5030 {
5031 if (list_append_tv(l, &item->li_tv) == FAIL)
5032 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005033 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005034 return FAIL;
5035 }
5036 item = item->li_next;
5037 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005038 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005039 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005040 }
5041 else
5042 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005043 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005044 clear_tv(rettv);
5045 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005046 }
5047 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005048
5049 case VAR_DICT:
5050 if (range)
5051 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005052 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005053 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005054 if (len == -1)
5055 clear_tv(&var1);
5056 return FAIL;
5057 }
5058 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005059 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005060
5061 if (len == -1)
5062 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005063 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005064 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005065 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005066 clear_tv(&var1);
5067 return FAIL;
5068 }
5069 }
5070
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005071 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005072
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005073 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005074 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005075 if (len == -1)
5076 clear_tv(&var1);
5077 if (item == NULL)
5078 return FAIL;
5079
5080 copy_tv(&item->di_tv, &var1);
5081 clear_tv(rettv);
5082 *rettv = var1;
5083 }
5084 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005085 }
5086 }
5087
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005088 return OK;
5089}
5090
5091/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 * Get an option value.
5093 * "arg" points to the '&' or '+' before the option name.
5094 * "arg" is advanced to character after the option name.
5095 * Return OK or FAIL.
5096 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005097 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005098get_option_tv(
5099 char_u **arg,
5100 typval_T *rettv, /* when NULL, only check if option exists */
5101 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102{
5103 char_u *option_end;
5104 long numval;
5105 char_u *stringval;
5106 int opt_type;
5107 int c;
5108 int working = (**arg == '+'); /* has("+option") */
5109 int ret = OK;
5110 int opt_flags;
5111
5112 /*
5113 * Isolate the option name and find its value.
5114 */
5115 option_end = find_option_end(arg, &opt_flags);
5116 if (option_end == NULL)
5117 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005118 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005119 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 return FAIL;
5121 }
5122
5123 if (!evaluate)
5124 {
5125 *arg = option_end;
5126 return OK;
5127 }
5128
5129 c = *option_end;
5130 *option_end = NUL;
5131 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005132 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133
5134 if (opt_type == -3) /* invalid name */
5135 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005136 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005137 semsg(_("E113: Unknown option: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 ret = FAIL;
5139 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005140 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 {
5142 if (opt_type == -2) /* hidden string option */
5143 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005144 rettv->v_type = VAR_STRING;
5145 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 }
5147 else if (opt_type == -1) /* hidden number option */
5148 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005149 rettv->v_type = VAR_NUMBER;
5150 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 }
5152 else if (opt_type == 1) /* number option */
5153 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005154 rettv->v_type = VAR_NUMBER;
5155 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 }
5157 else /* string option */
5158 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005159 rettv->v_type = VAR_STRING;
5160 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 }
5162 }
5163 else if (working && (opt_type == -2 || opt_type == -1))
5164 ret = FAIL;
5165
5166 *option_end = c; /* put back for error messages */
5167 *arg = option_end;
5168
5169 return ret;
5170}
5171
5172/*
5173 * Allocate a variable for a string constant.
5174 * Return OK or FAIL.
5175 */
5176 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005177get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178{
5179 char_u *p;
5180 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 int extra = 0;
5182
5183 /*
5184 * Find the end of the string, skipping backslashed characters.
5185 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005186 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 {
5188 if (*p == '\\' && p[1] != NUL)
5189 {
5190 ++p;
5191 /* A "\<x>" form occupies at least 4 characters, and produces up
5192 * to 6 characters: reserve space for 2 extra */
5193 if (*p == '<')
5194 extra += 2;
5195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 }
5197
5198 if (*p != '"')
5199 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005200 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 return FAIL;
5202 }
5203
5204 /* If only parsing, set *arg and return here */
5205 if (!evaluate)
5206 {
5207 *arg = p + 1;
5208 return OK;
5209 }
5210
5211 /*
5212 * Copy the string into allocated memory, handling backslashed
5213 * characters.
5214 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005215 name = alloc(p - *arg + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 if (name == NULL)
5217 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005218 rettv->v_type = VAR_STRING;
5219 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220
Bram Moolenaar8c711452005-01-14 21:53:12 +00005221 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 {
5223 if (*p == '\\')
5224 {
5225 switch (*++p)
5226 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005227 case 'b': *name++ = BS; ++p; break;
5228 case 'e': *name++ = ESC; ++p; break;
5229 case 'f': *name++ = FF; ++p; break;
5230 case 'n': *name++ = NL; ++p; break;
5231 case 'r': *name++ = CAR; ++p; break;
5232 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233
5234 case 'X': /* hex: "\x1", "\x12" */
5235 case 'x':
5236 case 'u': /* Unicode: "\u0023" */
5237 case 'U':
5238 if (vim_isxdigit(p[1]))
5239 {
5240 int n, nr;
5241 int c = toupper(*p);
5242
5243 if (c == 'X')
5244 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005245 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005247 else
5248 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 nr = 0;
5250 while (--n >= 0 && vim_isxdigit(p[1]))
5251 {
5252 ++p;
5253 nr = (nr << 4) + hex2nr(*p);
5254 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005255 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 /* For "\u" store the number according to
5257 * 'encoding'. */
5258 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005259 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005261 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 break;
5264
5265 /* octal: "\1", "\12", "\123" */
5266 case '0':
5267 case '1':
5268 case '2':
5269 case '3':
5270 case '4':
5271 case '5':
5272 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005273 case '7': *name = *p++ - '0';
5274 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005276 *name = (*name << 3) + *p++ - '0';
5277 if (*p >= '0' && *p <= '7')
5278 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005280 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 break;
5282
5283 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005284 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 if (extra != 0)
5286 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005287 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 break;
5289 }
5290 /* FALLTHROUGH */
5291
Bram Moolenaar8c711452005-01-14 21:53:12 +00005292 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 break;
5294 }
5295 }
5296 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005297 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005300 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02005301 if (*p != NUL) /* just in case */
5302 ++p;
5303 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 return OK;
5306}
5307
5308/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005309 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 * Return OK or FAIL.
5311 */
5312 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005313get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314{
5315 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005316 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005317 int reduce = 0;
5318
5319 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005320 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005321 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005322 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005323 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005324 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005325 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005326 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005327 break;
5328 ++reduce;
5329 ++p;
5330 }
5331 }
5332
Bram Moolenaar8c711452005-01-14 21:53:12 +00005333 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005334 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005335 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005336 return FAIL;
5337 }
5338
Bram Moolenaar8c711452005-01-14 21:53:12 +00005339 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005340 if (!evaluate)
5341 {
5342 *arg = p + 1;
5343 return OK;
5344 }
5345
5346 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005347 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005348 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005349 str = alloc((p - *arg) - reduce);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005350 if (str == NULL)
5351 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005352 rettv->v_type = VAR_STRING;
5353 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005354
Bram Moolenaar8c711452005-01-14 21:53:12 +00005355 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005356 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005357 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005358 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005359 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005360 break;
5361 ++p;
5362 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005363 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005364 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005365 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005366 *arg = p + 1;
5367
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005368 return OK;
5369}
5370
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005371/*
5372 * Return the function name of the partial.
5373 */
5374 char_u *
5375partial_name(partial_T *pt)
5376{
5377 if (pt->pt_name != NULL)
5378 return pt->pt_name;
5379 return pt->pt_func->uf_name;
5380}
5381
Bram Moolenaarddecc252016-04-06 22:59:37 +02005382 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005383partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005384{
5385 int i;
5386
5387 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005388 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005389 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005390 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005391 if (pt->pt_name != NULL)
5392 {
5393 func_unref(pt->pt_name);
5394 vim_free(pt->pt_name);
5395 }
5396 else
5397 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005398 vim_free(pt);
5399}
5400
5401/*
5402 * Unreference a closure: decrement the reference count and free it when it
5403 * becomes zero.
5404 */
5405 void
5406partial_unref(partial_T *pt)
5407{
5408 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005409 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005410}
5411
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005412static int tv_equal_recurse_limit;
5413
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005414 static int
5415func_equal(
5416 typval_T *tv1,
5417 typval_T *tv2,
5418 int ic) /* ignore case */
5419{
5420 char_u *s1, *s2;
5421 dict_T *d1, *d2;
5422 int a1, a2;
5423 int i;
5424
5425 /* empty and NULL function name considered the same */
5426 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005427 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005428 if (s1 != NULL && *s1 == NUL)
5429 s1 = NULL;
5430 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005431 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005432 if (s2 != NULL && *s2 == NUL)
5433 s2 = NULL;
5434 if (s1 == NULL || s2 == NULL)
5435 {
5436 if (s1 != s2)
5437 return FALSE;
5438 }
5439 else if (STRCMP(s1, s2) != 0)
5440 return FALSE;
5441
5442 /* empty dict and NULL dict is different */
5443 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5444 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5445 if (d1 == NULL || d2 == NULL)
5446 {
5447 if (d1 != d2)
5448 return FALSE;
5449 }
5450 else if (!dict_equal(d1, d2, ic, TRUE))
5451 return FALSE;
5452
5453 /* empty list and no list considered the same */
5454 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5455 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5456 if (a1 != a2)
5457 return FALSE;
5458 for (i = 0; i < a1; ++i)
5459 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5460 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5461 return FALSE;
5462
5463 return TRUE;
5464}
5465
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005466/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005467 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005468 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005469 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005470 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005471 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005472tv_equal(
5473 typval_T *tv1,
5474 typval_T *tv2,
5475 int ic, /* ignore case */
5476 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005477{
5478 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005479 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005480 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005481 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005482
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005483 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005484 * recursiveness to a limit. We guess they are equal then.
5485 * A fixed limit has the problem of still taking an awful long time.
5486 * Reduce the limit every time running into it. That should work fine for
5487 * deeply linked structures that are not recursively linked and catch
5488 * recursiveness quickly. */
5489 if (!recursive)
5490 tv_equal_recurse_limit = 1000;
5491 if (recursive_cnt >= tv_equal_recurse_limit)
5492 {
5493 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005494 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005495 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005496
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005497 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5498 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005499 if ((tv1->v_type == VAR_FUNC
5500 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5501 && (tv2->v_type == VAR_FUNC
5502 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5503 {
5504 ++recursive_cnt;
5505 r = func_equal(tv1, tv2, ic);
5506 --recursive_cnt;
5507 return r;
5508 }
5509
5510 if (tv1->v_type != tv2->v_type)
5511 return FALSE;
5512
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005513 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005514 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005515 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005516 ++recursive_cnt;
5517 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5518 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005519 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005520
5521 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005522 ++recursive_cnt;
5523 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5524 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005525 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005526
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005527 case VAR_BLOB:
5528 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
5529
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005530 case VAR_NUMBER:
5531 return tv1->vval.v_number == tv2->vval.v_number;
5532
5533 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005534 s1 = tv_get_string_buf(tv1, buf1);
5535 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005536 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005537
5538 case VAR_SPECIAL:
5539 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005540
5541 case VAR_FLOAT:
5542#ifdef FEAT_FLOAT
5543 return tv1->vval.v_float == tv2->vval.v_float;
5544#endif
5545 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005546#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005547 return tv1->vval.v_job == tv2->vval.v_job;
5548#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005549 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005550#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005551 return tv1->vval.v_channel == tv2->vval.v_channel;
5552#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005553 case VAR_FUNC:
5554 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005555 case VAR_UNKNOWN:
5556 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005557 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005558
Bram Moolenaara03f2332016-02-06 18:09:59 +01005559 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5560 * does not equal anything, not even itself. */
5561 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005562}
5563
5564/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005565 * Return the next (unique) copy ID.
5566 * Used for serializing nested structures.
5567 */
5568 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005569get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005570{
5571 current_copyID += COPYID_INC;
5572 return current_copyID;
5573}
5574
5575/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005576 * Garbage collection for lists and dictionaries.
5577 *
5578 * We use reference counts to be able to free most items right away when they
5579 * are no longer used. But for composite items it's possible that it becomes
5580 * unused while the reference count is > 0: When there is a recursive
5581 * reference. Example:
5582 * :let l = [1, 2, 3]
5583 * :let d = {9: l}
5584 * :let l[1] = d
5585 *
5586 * Since this is quite unusual we handle this with garbage collection: every
5587 * once in a while find out which lists and dicts are not referenced from any
5588 * variable.
5589 *
5590 * Here is a good reference text about garbage collection (refers to Python
5591 * but it applies to all reference-counting mechanisms):
5592 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005593 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005594
5595/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005596 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005597 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005598 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005599 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005600 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005601garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005602{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005603 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005604 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005605 buf_T *buf;
5606 win_T *wp;
5607 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005608 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005609 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005610
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005611 if (!testing)
5612 {
5613 /* Only do this once. */
5614 want_garbage_collect = FALSE;
5615 may_garbage_collect = FALSE;
5616 garbage_collect_at_exit = FALSE;
5617 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005618
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005619 /* We advance by two because we add one for items referenced through
5620 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005621 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005622
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005623 /*
5624 * 1. Go through all accessible variables and mark all lists and dicts
5625 * with copyID.
5626 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005627
5628 /* Don't free variables in the previous_funccal list unless they are only
5629 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005630 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005631 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005632
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005633 /* script-local variables */
5634 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005635 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005636
5637 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005638 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005639 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5640 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005641
5642 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005643 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005644 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5645 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005646 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005647 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5648 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005649#ifdef FEAT_TEXT_PROP
5650 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
5651 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5652 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005653 FOR_ALL_TABPAGES(tp)
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02005654 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005655 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5656 NULL, NULL);
5657#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005658
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005659 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005660 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005661 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5662 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005663 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005664 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005665
5666 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005667 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005668
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005669 /* named functions (matters for closures) */
5670 abort = abort || set_ref_in_functions(copyID);
5671
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005672 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005673 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005674
Bram Moolenaard812df62008-11-09 12:46:09 +00005675 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005676 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005677
Bram Moolenaar1dced572012-04-05 16:54:08 +02005678#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005679 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005680#endif
5681
Bram Moolenaardb913952012-06-29 12:54:53 +02005682#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005683 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005684#endif
5685
5686#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005687 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005688#endif
5689
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005690#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005691 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005692 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005693#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005694#ifdef FEAT_NETBEANS_INTG
5695 abort = abort || set_ref_in_nb_channel(copyID);
5696#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005697
Bram Moolenaare3188e22016-05-31 21:13:04 +02005698#ifdef FEAT_TIMERS
5699 abort = abort || set_ref_in_timer(copyID);
5700#endif
5701
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005702#ifdef FEAT_QUICKFIX
5703 abort = abort || set_ref_in_quickfix(copyID);
5704#endif
5705
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005706#ifdef FEAT_TERMINAL
5707 abort = abort || set_ref_in_term(copyID);
5708#endif
5709
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005710 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005711 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005712 /*
5713 * 2. Free lists and dictionaries that are not referenced.
5714 */
5715 did_free = free_unref_items(copyID);
5716
5717 /*
5718 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005719 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005720 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005721 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005722 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005723 else if (p_verbose > 0)
5724 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005725 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005726 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005727
5728 return did_free;
5729}
5730
5731/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005732 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005733 */
5734 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005735free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005736{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005737 int did_free = FALSE;
5738
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005739 /* Let all "free" functions know that we are here. This means no
5740 * dictionaries, lists, channels or jobs are to be freed, because we will
5741 * do that here. */
5742 in_free_unref_items = TRUE;
5743
5744 /*
5745 * PASS 1: free the contents of the items. We don't free the items
5746 * themselves yet, so that it is possible to decrement refcount counters
5747 */
5748
Bram Moolenaarcd524592016-07-17 14:57:05 +02005749 /* Go through the list of dicts and free items without the copyID. */
5750 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005751
Bram Moolenaarda861d62016-07-17 15:46:27 +02005752 /* Go through the list of lists and free items without the copyID. */
5753 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005754
5755#ifdef FEAT_JOB_CHANNEL
5756 /* Go through the list of jobs and free items without the copyID. This
5757 * must happen before doing channels, because jobs refer to channels, but
5758 * the reference from the channel to the job isn't tracked. */
5759 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5760
5761 /* Go through the list of channels and free items without the copyID. */
5762 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5763#endif
5764
5765 /*
5766 * PASS 2: free the items themselves.
5767 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005768 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005769 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005770
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005771#ifdef FEAT_JOB_CHANNEL
5772 /* Go through the list of jobs and free items without the copyID. This
5773 * must happen before doing channels, because jobs refer to channels, but
5774 * the reference from the channel to the job isn't tracked. */
5775 free_unused_jobs(copyID, COPYID_MASK);
5776
5777 /* Go through the list of channels and free items without the copyID. */
5778 free_unused_channels(copyID, COPYID_MASK);
5779#endif
5780
5781 in_free_unref_items = FALSE;
5782
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005783 return did_free;
5784}
5785
5786/*
5787 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005788 * "list_stack" is used to add lists to be marked. Can be NULL.
5789 *
5790 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005791 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005792 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005793set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005794{
5795 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005796 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005797 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005798 hashtab_T *cur_ht;
5799 ht_stack_T *ht_stack = NULL;
5800 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005801
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005802 cur_ht = ht;
5803 for (;;)
5804 {
5805 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005806 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005807 /* Mark each item in the hashtab. If the item contains a hashtab
5808 * it is added to ht_stack, if it contains a list it is added to
5809 * list_stack. */
5810 todo = (int)cur_ht->ht_used;
5811 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5812 if (!HASHITEM_EMPTY(hi))
5813 {
5814 --todo;
5815 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5816 &ht_stack, list_stack);
5817 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005818 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005819
5820 if (ht_stack == NULL)
5821 break;
5822
5823 /* take an item from the stack */
5824 cur_ht = ht_stack->ht;
5825 tempitem = ht_stack;
5826 ht_stack = ht_stack->prev;
5827 free(tempitem);
5828 }
5829
5830 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005831}
5832
5833/*
5834 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005835 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5836 *
5837 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005838 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005839 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005840set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005841{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005842 listitem_T *li;
5843 int abort = FALSE;
5844 list_T *cur_l;
5845 list_stack_T *list_stack = NULL;
5846 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005847
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005848 cur_l = l;
5849 for (;;)
5850 {
5851 if (!abort)
5852 /* Mark each item in the list. If the item contains a hashtab
5853 * it is added to ht_stack, if it contains a list it is added to
5854 * list_stack. */
5855 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5856 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5857 ht_stack, &list_stack);
5858 if (list_stack == NULL)
5859 break;
5860
5861 /* take an item from the stack */
5862 cur_l = list_stack->list;
5863 tempitem = list_stack;
5864 list_stack = list_stack->prev;
5865 free(tempitem);
5866 }
5867
5868 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005869}
5870
5871/*
5872 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005873 * "list_stack" is used to add lists to be marked. Can be NULL.
5874 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5875 *
5876 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005877 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005878 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005879set_ref_in_item(
5880 typval_T *tv,
5881 int copyID,
5882 ht_stack_T **ht_stack,
5883 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005884{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005885 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005886
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005887 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005888 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005889 dict_T *dd = tv->vval.v_dict;
5890
Bram Moolenaara03f2332016-02-06 18:09:59 +01005891 if (dd != NULL && dd->dv_copyID != copyID)
5892 {
5893 /* Didn't see this dict yet. */
5894 dd->dv_copyID = copyID;
5895 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005896 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005897 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5898 }
5899 else
5900 {
5901 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5902 if (newitem == NULL)
5903 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005904 else
5905 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005906 newitem->ht = &dd->dv_hashtab;
5907 newitem->prev = *ht_stack;
5908 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005909 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005910 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005911 }
5912 }
5913 else if (tv->v_type == VAR_LIST)
5914 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005915 list_T *ll = tv->vval.v_list;
5916
Bram Moolenaara03f2332016-02-06 18:09:59 +01005917 if (ll != NULL && ll->lv_copyID != copyID)
5918 {
5919 /* Didn't see this list yet. */
5920 ll->lv_copyID = copyID;
5921 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005922 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005923 abort = set_ref_in_list(ll, copyID, ht_stack);
5924 }
5925 else
5926 {
5927 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005928 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005929 if (newitem == NULL)
5930 abort = TRUE;
5931 else
5932 {
5933 newitem->list = ll;
5934 newitem->prev = *list_stack;
5935 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005936 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005937 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005938 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005939 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005940 else if (tv->v_type == VAR_FUNC)
5941 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005942 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005943 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005944 else if (tv->v_type == VAR_PARTIAL)
5945 {
5946 partial_T *pt = tv->vval.v_partial;
5947 int i;
5948
5949 /* A partial does not have a copyID, because it cannot contain itself.
5950 */
5951 if (pt != NULL)
5952 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005953 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005954
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005955 if (pt->pt_dict != NULL)
5956 {
5957 typval_T dtv;
5958
5959 dtv.v_type = VAR_DICT;
5960 dtv.vval.v_dict = pt->pt_dict;
5961 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5962 }
5963
5964 for (i = 0; i < pt->pt_argc; ++i)
5965 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5966 ht_stack, list_stack);
5967 }
5968 }
5969#ifdef FEAT_JOB_CHANNEL
5970 else if (tv->v_type == VAR_JOB)
5971 {
5972 job_T *job = tv->vval.v_job;
5973 typval_T dtv;
5974
5975 if (job != NULL && job->jv_copyID != copyID)
5976 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005977 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005978 if (job->jv_channel != NULL)
5979 {
5980 dtv.v_type = VAR_CHANNEL;
5981 dtv.vval.v_channel = job->jv_channel;
5982 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5983 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005984 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005985 {
5986 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005987 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005988 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5989 }
5990 }
5991 }
5992 else if (tv->v_type == VAR_CHANNEL)
5993 {
5994 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005995 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005996 typval_T dtv;
5997 jsonq_T *jq;
5998 cbq_T *cq;
5999
6000 if (ch != NULL && ch->ch_copyID != copyID)
6001 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02006002 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02006003 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006004 {
6005 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
6006 jq = jq->jq_next)
6007 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
6008 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
6009 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006010 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006011 {
6012 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006013 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006014 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6015 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006016 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006017 {
6018 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006019 dtv.vval.v_partial =
6020 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006021 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6022 }
6023 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006024 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006025 {
6026 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006027 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006028 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6029 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006030 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006031 {
6032 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006033 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006034 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6035 }
6036 }
6037 }
6038#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006039 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006040}
6041
Bram Moolenaar17a13432016-01-24 14:22:10 +01006042 static char *
6043get_var_special_name(int nr)
6044{
6045 switch (nr)
6046 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01006047 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01006048 case VVAL_TRUE: return "v:true";
6049 case VVAL_NONE: return "v:none";
6050 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01006051 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01006052 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01006053 return "42";
6054}
6055
Bram Moolenaar8c711452005-01-14 21:53:12 +00006056/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006057 * Return a string with the string representation of a variable.
6058 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006059 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006060 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02006061 * When both "echo_style" and "composite_val" are FALSE, put quotes around
6062 * stings as "string()", otherwise does not put quotes around strings, as
6063 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006064 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6065 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006066 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006067 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006068 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006069echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006070 typval_T *tv,
6071 char_u **tofree,
6072 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006073 int copyID,
6074 int echo_style,
6075 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006076 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006077{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006078 static int recurse = 0;
6079 char_u *r = NULL;
6080
Bram Moolenaar33570922005-01-25 22:26:29 +00006081 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006082 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006083 if (!did_echo_string_emsg)
6084 {
6085 /* Only give this message once for a recursive call to avoid
6086 * flooding the user with errors. And stop iterating over lists
6087 * and dicts. */
6088 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006089 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006090 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006091 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006092 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006093 }
6094 ++recurse;
6095
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006096 switch (tv->v_type)
6097 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006098 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006099 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006100 {
6101 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02006102 r = tv->vval.v_string;
6103 if (r == NULL)
6104 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006105 }
6106 else
6107 {
6108 *tofree = string_quote(tv->vval.v_string, FALSE);
6109 r = *tofree;
6110 }
6111 break;
6112
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006113 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006114 if (echo_style)
6115 {
6116 *tofree = NULL;
6117 r = tv->vval.v_string;
6118 }
6119 else
6120 {
6121 *tofree = string_quote(tv->vval.v_string, TRUE);
6122 r = *tofree;
6123 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006124 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006125
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006126 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006127 {
6128 partial_T *pt = tv->vval.v_partial;
6129 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006130 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006131 garray_T ga;
6132 int i;
6133 char_u *tf;
6134
6135 ga_init2(&ga, 1, 100);
6136 ga_concat(&ga, (char_u *)"function(");
6137 if (fname != NULL)
6138 {
6139 ga_concat(&ga, fname);
6140 vim_free(fname);
6141 }
6142 if (pt != NULL && pt->pt_argc > 0)
6143 {
6144 ga_concat(&ga, (char_u *)", [");
6145 for (i = 0; i < pt->pt_argc; ++i)
6146 {
6147 if (i > 0)
6148 ga_concat(&ga, (char_u *)", ");
6149 ga_concat(&ga,
6150 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6151 vim_free(tf);
6152 }
6153 ga_concat(&ga, (char_u *)"]");
6154 }
6155 if (pt != NULL && pt->pt_dict != NULL)
6156 {
6157 typval_T dtv;
6158
6159 ga_concat(&ga, (char_u *)", ");
6160 dtv.v_type = VAR_DICT;
6161 dtv.vval.v_dict = pt->pt_dict;
6162 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6163 vim_free(tf);
6164 }
6165 ga_concat(&ga, (char_u *)")");
6166
6167 *tofree = ga.ga_data;
6168 r = *tofree;
6169 break;
6170 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006171
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006172 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006173 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006174 break;
6175
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006176 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006177 if (tv->vval.v_list == NULL)
6178 {
6179 *tofree = NULL;
6180 r = NULL;
6181 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006182 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6183 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006184 {
6185 *tofree = NULL;
6186 r = (char_u *)"[...]";
6187 }
6188 else
6189 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006190 int old_copyID = tv->vval.v_list->lv_copyID;
6191
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006192 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006193 *tofree = list2string(tv, copyID, restore_copyID);
6194 if (restore_copyID)
6195 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006196 r = *tofree;
6197 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006198 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006199
Bram Moolenaar8c711452005-01-14 21:53:12 +00006200 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006201 if (tv->vval.v_dict == NULL)
6202 {
6203 *tofree = NULL;
6204 r = NULL;
6205 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006206 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6207 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006208 {
6209 *tofree = NULL;
6210 r = (char_u *)"{...}";
6211 }
6212 else
6213 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006214 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006215 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006216 *tofree = dict2string(tv, copyID, restore_copyID);
6217 if (restore_copyID)
6218 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006219 r = *tofree;
6220 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006221 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006222
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006223 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006224 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006225 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006226 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006227 break;
6228
Bram Moolenaar835dc632016-02-07 14:27:38 +01006229 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006230 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006231 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006232 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006233 if (composite_val)
6234 {
6235 *tofree = string_quote(r, FALSE);
6236 r = *tofree;
6237 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006238 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006239
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006240 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006241#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006242 *tofree = NULL;
6243 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6244 r = numbuf;
6245 break;
6246#endif
6247
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006248 case VAR_SPECIAL:
6249 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006250 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006251 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006252 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006253
Bram Moolenaar8502c702014-06-17 12:51:16 +02006254 if (--recurse == 0)
6255 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006256 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006257}
6258
6259/*
6260 * Return a string with the string representation of a variable.
6261 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6262 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006263 * Does not put quotes around strings, as ":echo" displays values.
6264 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6265 * May return NULL.
6266 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006267 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006268echo_string(
6269 typval_T *tv,
6270 char_u **tofree,
6271 char_u *numbuf,
6272 int copyID)
6273{
6274 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6275}
6276
6277/*
6278 * Return a string with the string representation of a variable.
6279 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6280 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006281 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006282 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006283 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02006284 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006285tv2string(
6286 typval_T *tv,
6287 char_u **tofree,
6288 char_u *numbuf,
6289 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006290{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006291 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006292}
6293
6294/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006295 * Return string "str" in ' quotes, doubling ' characters.
6296 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006297 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006298 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006299 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006300string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006301{
Bram Moolenaar33570922005-01-25 22:26:29 +00006302 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006303 char_u *p, *r, *s;
6304
Bram Moolenaar33570922005-01-25 22:26:29 +00006305 len = (function ? 13 : 3);
6306 if (str != NULL)
6307 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006308 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006309 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00006310 if (*p == '\'')
6311 ++len;
6312 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006313 s = r = alloc(len);
6314 if (r != NULL)
6315 {
6316 if (function)
6317 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006318 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006319 r += 10;
6320 }
6321 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006322 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006323 if (str != NULL)
6324 for (p = str; *p != NUL; )
6325 {
6326 if (*p == '\'')
6327 *r++ = '\'';
6328 MB_COPY_CHAR(p, r);
6329 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006330 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006331 if (function)
6332 *r++ = ')';
6333 *r++ = NUL;
6334 }
6335 return s;
6336}
6337
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006338#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006339/*
6340 * Convert the string "text" to a floating point number.
6341 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
6342 * this always uses a decimal point.
6343 * Returns the length of the text that was consumed.
6344 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006345 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006346string2float(
6347 char_u *text,
6348 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006349{
6350 char *s = (char *)text;
6351 float_T f;
6352
Bram Moolenaar62473612017-01-08 19:25:40 +01006353 /* MS-Windows does not deal with "inf" and "nan" properly. */
6354 if (STRNICMP(text, "inf", 3) == 0)
6355 {
6356 *value = INFINITY;
6357 return 3;
6358 }
6359 if (STRNICMP(text, "-inf", 3) == 0)
6360 {
6361 *value = -INFINITY;
6362 return 4;
6363 }
6364 if (STRNICMP(text, "nan", 3) == 0)
6365 {
6366 *value = NAN;
6367 return 3;
6368 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006369 f = strtod(s, &s);
6370 *value = f;
6371 return (int)((char_u *)s - text);
6372}
6373#endif
6374
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006375/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 * Get the value of an environment variable.
6377 * "arg" is pointing to the '$'. It is advanced to after the name.
6378 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006379 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 */
6381 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006382get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383{
6384 char_u *string = NULL;
6385 int len;
6386 int cc;
6387 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006388 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389
6390 ++*arg;
6391 name = *arg;
6392 len = get_env_len(arg);
6393 if (evaluate)
6394 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006395 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006396 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006397
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006398 cc = name[len];
6399 name[len] = NUL;
6400 /* first try vim_getenv(), fast for normal environment vars */
6401 string = vim_getenv(name, &mustfree);
6402 if (string != NULL && *string != NUL)
6403 {
6404 if (!mustfree)
6405 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006407 else
6408 {
6409 if (mustfree)
6410 vim_free(string);
6411
6412 /* next try expanding things like $VIM and ${HOME} */
6413 string = expand_env_save(name - 1);
6414 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01006415 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006416 }
6417 name[len] = cc;
6418
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006419 rettv->v_type = VAR_STRING;
6420 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 }
6422
6423 return OK;
6424}
6425
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006426
6427
6428/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006430 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006432 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006433var2fpos(
6434 typval_T *varp,
6435 int dollar_lnum, /* TRUE when $ is last line */
6436 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006438 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006440 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441
Bram Moolenaara5525202006-03-02 22:52:09 +00006442 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006443 if (varp->v_type == VAR_LIST)
6444 {
6445 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006446 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006447 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006448 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006449
6450 l = varp->vval.v_list;
6451 if (l == NULL)
6452 return NULL;
6453
6454 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006455 pos.lnum = list_find_nr(l, 0L, &error);
6456 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006457 return NULL; /* invalid line number */
6458
6459 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006460 pos.col = list_find_nr(l, 1L, &error);
6461 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006462 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006463 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006464
6465 /* We accept "$" for the column number: last column. */
6466 li = list_find(l, 1L);
6467 if (li != NULL && li->li_tv.v_type == VAR_STRING
6468 && li->li_tv.vval.v_string != NULL
6469 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6470 pos.col = len + 1;
6471
Bram Moolenaara5525202006-03-02 22:52:09 +00006472 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006473 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006474 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006475 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006476
Bram Moolenaara5525202006-03-02 22:52:09 +00006477 /* Get the virtual offset. Defaults to zero. */
6478 pos.coladd = list_find_nr(l, 2L, &error);
6479 if (error)
6480 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006481
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006482 return &pos;
6483 }
6484
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006485 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006486 if (name == NULL)
6487 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006488 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006490 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6491 {
6492 if (VIsual_active)
6493 return &VIsual;
6494 return &curwin->w_cursor;
6495 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006496 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006498 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6500 return NULL;
6501 return pp;
6502 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006503
Bram Moolenaara5525202006-03-02 22:52:09 +00006504 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006505
Bram Moolenaar477933c2007-07-17 14:32:23 +00006506 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006507 {
6508 pos.col = 0;
6509 if (name[1] == '0') /* "w0": first visible line */
6510 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006511 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006512 /* In silent Ex mode topline is zero, but that's not a valid line
6513 * number; use one instead. */
6514 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006515 return &pos;
6516 }
6517 else if (name[1] == '$') /* "w$": last visible line */
6518 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006519 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006520 /* In silent Ex mode botline is zero, return zero then. */
6521 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006522 return &pos;
6523 }
6524 }
6525 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006527 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 {
6529 pos.lnum = curbuf->b_ml.ml_line_count;
6530 pos.col = 0;
6531 }
6532 else
6533 {
6534 pos.lnum = curwin->w_cursor.lnum;
6535 pos.col = (colnr_T)STRLEN(ml_get_curline());
6536 }
6537 return &pos;
6538 }
6539 return NULL;
6540}
6541
6542/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006543 * Convert list in "arg" into a position and optional file number.
6544 * When "fnump" is NULL there is no file number, only 3 items.
6545 * Note that the column is passed on as-is, the caller may want to decrement
6546 * it to use 1 for the first column.
6547 * Return FAIL when conversion is not possible, doesn't check the position for
6548 * validity.
6549 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006550 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006551list2fpos(
6552 typval_T *arg,
6553 pos_T *posp,
6554 int *fnump,
6555 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006556{
6557 list_T *l = arg->vval.v_list;
6558 long i = 0;
6559 long n;
6560
Bram Moolenaar493c1782014-05-28 14:34:46 +02006561 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6562 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006563 if (arg->v_type != VAR_LIST
6564 || l == NULL
6565 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006566 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006567 return FAIL;
6568
6569 if (fnump != NULL)
6570 {
6571 n = list_find_nr(l, i++, NULL); /* fnum */
6572 if (n < 0)
6573 return FAIL;
6574 if (n == 0)
6575 n = curbuf->b_fnum; /* current buffer */
6576 *fnump = n;
6577 }
6578
6579 n = list_find_nr(l, i++, NULL); /* lnum */
6580 if (n < 0)
6581 return FAIL;
6582 posp->lnum = n;
6583
6584 n = list_find_nr(l, i++, NULL); /* col */
6585 if (n < 0)
6586 return FAIL;
6587 posp->col = n;
6588
Bram Moolenaar493c1782014-05-28 14:34:46 +02006589 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006590 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006591 posp->coladd = 0;
6592 else
6593 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006594
Bram Moolenaar493c1782014-05-28 14:34:46 +02006595 if (curswantp != NULL)
6596 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6597
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006598 return OK;
6599}
6600
6601/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 * Get the length of an environment variable name.
6603 * Advance "arg" to the first character after the name.
6604 * Return 0 for error.
6605 */
6606 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006607get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608{
6609 char_u *p;
6610 int len;
6611
6612 for (p = *arg; vim_isIDc(*p); ++p)
6613 ;
6614 if (p == *arg) /* no name found */
6615 return 0;
6616
6617 len = (int)(p - *arg);
6618 *arg = p;
6619 return len;
6620}
6621
6622/*
6623 * Get the length of the name of a function or internal variable.
6624 * "arg" is advanced to the first non-white character after the name.
6625 * Return 0 if something is wrong.
6626 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006627 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006628get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629{
6630 char_u *p;
6631 int len;
6632
6633 /* Find the end of the name. */
6634 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006635 {
6636 if (*p == ':')
6637 {
6638 /* "s:" is start of "s:var", but "n:" is not and can be used in
6639 * slice "[n:]". Also "xx:" is not a namespace. */
6640 len = (int)(p - *arg);
6641 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6642 || len > 1)
6643 break;
6644 }
6645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 if (p == *arg) /* no name found */
6647 return 0;
6648
6649 len = (int)(p - *arg);
6650 *arg = skipwhite(p);
6651
6652 return len;
6653}
6654
6655/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006656 * Get the length of the name of a variable or function.
6657 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006659 * Return -1 if curly braces expansion failed.
6660 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 * If the name contains 'magic' {}'s, expand them and return the
6662 * expanded name in an allocated string via 'alias' - caller must free.
6663 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006664 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006665get_name_len(
6666 char_u **arg,
6667 char_u **alias,
6668 int evaluate,
6669 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670{
6671 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 char_u *p;
6673 char_u *expr_start;
6674 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675
6676 *alias = NULL; /* default to no alias */
6677
6678 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6679 && (*arg)[2] == (int)KE_SNR)
6680 {
6681 /* hard coded <SNR>, already translated */
6682 *arg += 3;
6683 return get_id_len(arg) + 3;
6684 }
6685 len = eval_fname_script(*arg);
6686 if (len > 0)
6687 {
6688 /* literal "<SID>", "s:" or "<SNR>" */
6689 *arg += len;
6690 }
6691
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006693 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006695 p = find_name_end(*arg, &expr_start, &expr_end,
6696 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 if (expr_start != NULL)
6698 {
6699 char_u *temp_string;
6700
6701 if (!evaluate)
6702 {
6703 len += (int)(p - *arg);
6704 *arg = skipwhite(p);
6705 return len;
6706 }
6707
6708 /*
6709 * Include any <SID> etc in the expanded string:
6710 * Thus the -len here.
6711 */
6712 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6713 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006714 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 *alias = temp_string;
6716 *arg = skipwhite(p);
6717 return (int)STRLEN(temp_string);
6718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719
6720 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006721 // Only give an error when there is something, otherwise it will be
6722 // reported at a higher level.
6723 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006724 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725
6726 return len;
6727}
6728
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006729/*
6730 * Find the end of a variable or function name, taking care of magic braces.
6731 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6732 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006733 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006734 * Return a pointer to just after the name. Equal to "arg" if there is no
6735 * valid name.
6736 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006737 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006738find_name_end(
6739 char_u *arg,
6740 char_u **expr_start,
6741 char_u **expr_end,
6742 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006744 int mb_nest = 0;
6745 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006747 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006749 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006751 *expr_start = NULL;
6752 *expr_end = NULL;
6753 }
6754
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006755 /* Quick check for valid starting character. */
6756 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6757 return arg;
6758
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006759 for (p = arg; *p != NUL
6760 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006761 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006762 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006763 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006764 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006765 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006766 if (*p == '\'')
6767 {
6768 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006769 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006770 ;
6771 if (*p == NUL)
6772 break;
6773 }
6774 else if (*p == '"')
6775 {
6776 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006777 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006778 if (*p == '\\' && p[1] != NUL)
6779 ++p;
6780 if (*p == NUL)
6781 break;
6782 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006783 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6784 {
6785 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006786 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006787 len = (int)(p - arg);
6788 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006789 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006790 break;
6791 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006792
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006793 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006795 if (*p == '[')
6796 ++br_nest;
6797 else if (*p == ']')
6798 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006800
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006801 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006803 if (*p == '{')
6804 {
6805 mb_nest++;
6806 if (expr_start != NULL && *expr_start == NULL)
6807 *expr_start = p;
6808 }
6809 else if (*p == '}')
6810 {
6811 mb_nest--;
6812 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6813 *expr_end = p;
6814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 }
6817
6818 return p;
6819}
6820
6821/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006822 * Expands out the 'magic' {}'s in a variable/function name.
6823 * Note that this can call itself recursively, to deal with
6824 * constructs like foo{bar}{baz}{bam}
6825 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6826 * "in_start" ^
6827 * "expr_start" ^
6828 * "expr_end" ^
6829 * "in_end" ^
6830 *
6831 * Returns a new allocated string, which the caller must free.
6832 * Returns NULL for failure.
6833 */
6834 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006835make_expanded_name(
6836 char_u *in_start,
6837 char_u *expr_start,
6838 char_u *expr_end,
6839 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006840{
6841 char_u c1;
6842 char_u *retval = NULL;
6843 char_u *temp_result;
6844 char_u *nextcmd = NULL;
6845
6846 if (expr_end == NULL || in_end == NULL)
6847 return NULL;
6848 *expr_start = NUL;
6849 *expr_end = NUL;
6850 c1 = *in_end;
6851 *in_end = NUL;
6852
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006853 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006854 if (temp_result != NULL && nextcmd == NULL)
6855 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006856 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6857 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006858 if (retval != NULL)
6859 {
6860 STRCPY(retval, in_start);
6861 STRCAT(retval, temp_result);
6862 STRCAT(retval, expr_end + 1);
6863 }
6864 }
6865 vim_free(temp_result);
6866
6867 *in_end = c1; /* put char back for error messages */
6868 *expr_start = '{';
6869 *expr_end = '}';
6870
6871 if (retval != NULL)
6872 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006873 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006874 if (expr_start != NULL)
6875 {
6876 /* Further expansion! */
6877 temp_result = make_expanded_name(retval, expr_start,
6878 expr_end, temp_result);
6879 vim_free(retval);
6880 retval = temp_result;
6881 }
6882 }
6883
6884 return retval;
6885}
6886
6887/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006889 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006891 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006892eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006894 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6895}
6896
6897/*
6898 * Return TRUE if character "c" can be used as the first character in a
6899 * variable or function name (excluding '{' and '}').
6900 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006901 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006902eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006903{
6904 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905}
6906
6907/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 * Set number v: variable to "val".
6909 */
6910 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006911set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006913 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914}
6915
6916/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006917 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006919 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006920get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006922 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923}
6924
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006925/*
6926 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01006927 * If the String variable has never been set, return an empty string.
6928 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006929 */
6930 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006931get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006932{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006933 return tv_get_string(&vimvars[idx].vv_tv);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006934}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006935
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006937 * Get List v: variable value. Caller must take care of reference count when
6938 * needed.
6939 */
6940 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006941get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006942{
6943 return vimvars[idx].vv_list;
6944}
6945
6946/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006947 * Get Dict v: variable value. Caller must take care of reference count when
6948 * needed.
6949 */
6950 dict_T *
6951get_vim_var_dict(int idx)
6952{
6953 return vimvars[idx].vv_dict;
6954}
6955
6956/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006957 * Set v:char to character "c".
6958 */
6959 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006960set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006961{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006962 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006963
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006964 if (has_mbyte)
6965 buf[(*mb_char2bytes)(c, buf)] = NUL;
6966 else
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006967 {
6968 buf[0] = c;
6969 buf[1] = NUL;
6970 }
6971 set_vim_var_string(VV_CHAR, buf, -1);
6972}
6973
6974/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006975 * Set v:count to "count" and v:count1 to "count1".
6976 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 */
6978 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006979set_vcount(
6980 long count,
6981 long count1,
6982 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006984 if (set_prevcount)
6985 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006986 vimvars[VV_COUNT].vv_nr = count;
6987 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988}
6989
6990/*
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02006991 * Save variables that might be changed as a side effect. Used when executing
6992 * a timer callback.
6993 */
6994 void
6995save_vimvars(vimvars_save_T *vvsave)
6996{
6997 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
6998 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
6999 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
7000}
7001
7002/*
7003 * Restore variables saved by save_vimvars().
7004 */
7005 void
7006restore_vimvars(vimvars_save_T *vvsave)
7007{
7008 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
7009 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
7010 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
7011}
7012
7013/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 * Set string v: variable to a copy of "val".
7015 */
7016 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007017set_vim_var_string(
7018 int idx,
7019 char_u *val,
7020 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021{
Bram Moolenaara542c682016-01-31 16:28:04 +01007022 clear_tv(&vimvars[idx].vv_di.di_tv);
7023 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007025 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007027 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00007029 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030}
7031
7032/*
Bram Moolenaard812df62008-11-09 12:46:09 +00007033 * Set List v: variable to "val".
7034 */
7035 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007036set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00007037{
Bram Moolenaara542c682016-01-31 16:28:04 +01007038 clear_tv(&vimvars[idx].vv_di.di_tv);
7039 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00007040 vimvars[idx].vv_list = val;
7041 if (val != NULL)
7042 ++val->lv_refcount;
7043}
7044
7045/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02007046 * Set Dictionary v: variable to "val".
7047 */
7048 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007049set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02007050{
Bram Moolenaara542c682016-01-31 16:28:04 +01007051 clear_tv(&vimvars[idx].vv_di.di_tv);
7052 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02007053 vimvars[idx].vv_dict = val;
7054 if (val != NULL)
7055 {
7056 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01007057 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02007058 }
7059}
7060
7061/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 * Set v:register if needed.
7063 */
7064 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007065set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066{
7067 char_u regname;
7068
7069 if (c == 0 || c == ' ')
7070 regname = '"';
7071 else
7072 regname = c;
7073 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007074 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 set_vim_var_string(VV_REG, &regname, 1);
7076}
7077
7078/*
7079 * Get or set v:exception. If "oldval" == NULL, return the current value.
7080 * Otherwise, restore the value to "oldval" and return NULL.
7081 * Must always be called in pairs to save and restore v:exception! Does not
7082 * take care of memory allocations.
7083 */
7084 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007085v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086{
7087 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007088 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089
Bram Moolenaare9a41262005-01-15 22:18:47 +00007090 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 return NULL;
7092}
7093
7094/*
7095 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
7096 * Otherwise, restore the value to "oldval" and return NULL.
7097 * Must always be called in pairs to save and restore v:throwpoint! Does not
7098 * take care of memory allocations.
7099 */
7100 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007101v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102{
7103 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007104 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105
Bram Moolenaare9a41262005-01-15 22:18:47 +00007106 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 return NULL;
7108}
7109
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110/*
7111 * Set v:cmdarg.
7112 * If "eap" != NULL, use "eap" to generate the value and return the old value.
7113 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
7114 * Must always be called in pairs!
7115 */
7116 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007117set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118{
7119 char_u *oldval;
7120 char_u *newval;
7121 unsigned len;
7122
Bram Moolenaare9a41262005-01-15 22:18:47 +00007123 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007124 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007126 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007127 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007128 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 }
7130
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007131 if (eap->force_bin == FORCE_BIN)
7132 len = 6;
7133 else if (eap->force_bin == FORCE_NOBIN)
7134 len = 8;
7135 else
7136 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007137
7138 if (eap->read_edit)
7139 len += 7;
7140
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007141 if (eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02007142 len += 10; /* " ++ff=unix" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007143 if (eap->force_enc != 0)
7144 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02007145 if (eap->bad_char != 0)
7146 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007147
7148 newval = alloc(len + 1);
7149 if (newval == NULL)
7150 return NULL;
7151
7152 if (eap->force_bin == FORCE_BIN)
7153 sprintf((char *)newval, " ++bin");
7154 else if (eap->force_bin == FORCE_NOBIN)
7155 sprintf((char *)newval, " ++nobin");
7156 else
7157 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007158
7159 if (eap->read_edit)
7160 STRCAT(newval, " ++edit");
7161
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007162 if (eap->force_ff != 0)
7163 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
Bram Moolenaar333b80a2018-04-04 22:57:29 +02007164 eap->force_ff == 'u' ? "unix"
7165 : eap->force_ff == 'd' ? "dos"
7166 : "mac");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007167 if (eap->force_enc != 0)
7168 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
7169 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02007170 if (eap->bad_char == BAD_KEEP)
7171 STRCPY(newval + STRLEN(newval), " ++bad=keep");
7172 else if (eap->bad_char == BAD_DROP)
7173 STRCPY(newval + STRLEN(newval), " ++bad=drop");
7174 else if (eap->bad_char != 0)
7175 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007176 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007177 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179
7180/*
7181 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01007182 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007184 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007185get_var_tv(
7186 char_u *name,
7187 int len, /* length of "name" */
7188 typval_T *rettv, /* NULL when only checking existence */
7189 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
7190 int verbose, /* may give error message */
7191 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192{
7193 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007194 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007195 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197
7198 /* truncate the name, so that we can use strcmp() */
7199 cc = name[len];
7200 name[len] = NUL;
7201
7202 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 * Check for user-defined variables.
7204 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01007205 v = find_var(name, NULL, no_autoload);
7206 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007208 tv = &v->di_tv;
7209 if (dip != NULL)
7210 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 }
7212
Bram Moolenaare9a41262005-01-15 22:18:47 +00007213 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007215 if (rettv != NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007216 semsg(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 ret = FAIL;
7218 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007219 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007220 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221
7222 name[len] = cc;
7223
7224 return ret;
7225}
7226
7227/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007228 * Check if variable "name[len]" is a local variable or an argument.
7229 * If so, "*eval_lavars_used" is set to TRUE.
7230 */
7231 static void
7232check_vars(char_u *name, int len)
7233{
7234 int cc;
7235 char_u *varname;
7236 hashtab_T *ht;
7237
7238 if (eval_lavars_used == NULL)
7239 return;
7240
7241 /* truncate the name, so that we can use strcmp() */
7242 cc = name[len];
7243 name[len] = NUL;
7244
7245 ht = find_var_ht(name, &varname);
7246 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
7247 {
7248 if (find_var(name, NULL, TRUE) != NULL)
7249 *eval_lavars_used = TRUE;
7250 }
7251
7252 name[len] = cc;
7253}
7254
7255/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007256 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
7257 * Also handle function call with Funcref variable: func(expr)
7258 * Can all be combined: dict.func(expr)[idx]['func'](expr)
7259 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007260 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007261handle_subscript(
7262 char_u **arg,
7263 typval_T *rettv,
7264 int evaluate, /* do more than finding the end */
7265 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007266{
7267 int ret = OK;
7268 dict_T *selfdict = NULL;
7269 char_u *s;
7270 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007271 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007272
7273 while (ret == OK
7274 && (**arg == '['
7275 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007276 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7277 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01007278 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007279 {
7280 if (**arg == '(')
7281 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007282 partial_T *pt = NULL;
7283
Bram Moolenaard9fba312005-06-26 22:34:35 +00007284 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007285 if (evaluate)
7286 {
7287 functv = *rettv;
7288 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007289
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007290 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007291 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007292 {
7293 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007294 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007295 }
7296 else
7297 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007298 }
7299 else
7300 s = (char_u *)"";
Bram Moolenaar6ed88192019-05-11 18:37:44 +02007301 ret = get_func_tv(s, -1, rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00007302 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007303 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007304
7305 /* Clear the funcref afterwards, so that deleting it while
7306 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007307 if (evaluate)
7308 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007309
7310 /* Stop the expression evaluation when immediately aborting on
7311 * error, or when an interrupt occurred or an exception was thrown
7312 * but not caught. */
7313 if (aborting())
7314 {
7315 if (ret == OK)
7316 clear_tv(rettv);
7317 ret = FAIL;
7318 }
7319 dict_unref(selfdict);
7320 selfdict = NULL;
7321 }
7322 else /* **arg == '[' || **arg == '.' */
7323 {
7324 dict_unref(selfdict);
7325 if (rettv->v_type == VAR_DICT)
7326 {
7327 selfdict = rettv->vval.v_dict;
7328 if (selfdict != NULL)
7329 ++selfdict->dv_refcount;
7330 }
7331 else
7332 selfdict = NULL;
7333 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
7334 {
7335 clear_tv(rettv);
7336 ret = FAIL;
7337 }
7338 }
7339 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007340
Bram Moolenaar1d429612016-05-24 15:44:17 +02007341 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
7342 * Don't do this when "Func" is already a partial that was bound
7343 * explicitly (pt_auto is FALSE). */
7344 if (selfdict != NULL
7345 && (rettv->v_type == VAR_FUNC
7346 || (rettv->v_type == VAR_PARTIAL
7347 && (rettv->vval.v_partial->pt_auto
7348 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007349 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007350
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007351 dict_unref(selfdict);
7352 return ret;
7353}
7354
7355/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007356 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007357 * value).
7358 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01007359 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007360alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007361{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007362 return ALLOC_CLEAR_ONE(typval_T);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007363}
7364
7365/*
7366 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 * The string "s" must have been allocated, it is consumed.
7368 * Return NULL for out of memory, the variable otherwise.
7369 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007370 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007371alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372{
Bram Moolenaar33570922005-01-25 22:26:29 +00007373 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007375 rettv = alloc_tv();
7376 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007378 rettv->v_type = VAR_STRING;
7379 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 }
7381 else
7382 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007383 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384}
7385
7386/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007387 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007389 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007390free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391{
7392 if (varp != NULL)
7393 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007394 switch (varp->v_type)
7395 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007396 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007397 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007398 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007399 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007400 vim_free(varp->vval.v_string);
7401 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007402 case VAR_PARTIAL:
7403 partial_unref(varp->vval.v_partial);
7404 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007405 case VAR_BLOB:
7406 blob_unref(varp->vval.v_blob);
7407 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007408 case VAR_LIST:
7409 list_unref(varp->vval.v_list);
7410 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007411 case VAR_DICT:
7412 dict_unref(varp->vval.v_dict);
7413 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007414 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007415#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007416 job_unref(varp->vval.v_job);
7417 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007418#endif
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 break;
7423#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007424 case VAR_NUMBER:
7425 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007426 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007427 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007428 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 vim_free(varp);
7431 }
7432}
7433
7434/*
7435 * Free the memory for a variable value and set the value to NULL or 0.
7436 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007437 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007438clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439{
7440 if (varp != NULL)
7441 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007442 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007444 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007445 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007446 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007447 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007448 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007449 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007450 case VAR_PARTIAL:
7451 partial_unref(varp->vval.v_partial);
7452 varp->vval.v_partial = NULL;
7453 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007454 case VAR_BLOB:
7455 blob_unref(varp->vval.v_blob);
7456 varp->vval.v_blob = NULL;
7457 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007458 case VAR_LIST:
7459 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007460 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007461 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007462 case VAR_DICT:
7463 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007464 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007465 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007466 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007467 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007468 varp->vval.v_number = 0;
7469 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007470 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007471#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007472 varp->vval.v_float = 0.0;
7473 break;
7474#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007475 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007476#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007477 job_unref(varp->vval.v_job);
7478 varp->vval.v_job = NULL;
7479#endif
7480 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007481 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007482#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007483 channel_unref(varp->vval.v_channel);
7484 varp->vval.v_channel = NULL;
7485#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007486 case VAR_UNKNOWN:
7487 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007489 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 }
7491}
7492
7493/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007494 * Set the value of a variable to NULL without freeing items.
7495 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007496 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007497init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007498{
7499 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007500 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007501}
7502
7503/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 * Get the number value of a variable.
7505 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007506 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007507 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007508 * caller of incompatible types: it sets *denote to TRUE if "denote"
7509 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007511 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007512tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007514 int error = FALSE;
7515
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007516 return tv_get_number_chk(varp, &error); /* return 0L on error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007517}
7518
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007519 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007520tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007521{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007522 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007524 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007526 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007527 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007528 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007529#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007530 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007531 break;
7532#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007533 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007534 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007535 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007536 break;
7537 case VAR_STRING:
7538 if (varp->vval.v_string != NULL)
7539 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02007540 STR2NR_ALL, &n, NULL, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007541 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007542 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007543 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007544 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007545 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007546 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007547 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007548 case VAR_SPECIAL:
7549 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7550 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007551 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007552#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007553 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007554 break;
7555#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007556 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007557#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007558 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007559 break;
7560#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007561 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007562 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007563 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007564 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007565 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007566 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007568 if (denote == NULL) /* useful for values that must be unsigned */
7569 n = -1;
7570 else
7571 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007572 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573}
7574
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007575#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007576 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007577tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007578{
7579 switch (varp->v_type)
7580 {
7581 case VAR_NUMBER:
7582 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007583 case VAR_FLOAT:
7584 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007585 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007586 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007587 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007588 break;
7589 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007590 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007591 break;
7592 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007593 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007594 break;
7595 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007596 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007597 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007598 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007599 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007600 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007601 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007602# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007603 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007604 break;
7605# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007606 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007607# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007608 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007609 break;
7610# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007611 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007612 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007613 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007614 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007615 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007616 break;
7617 }
7618 return 0;
7619}
7620#endif
7621
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 * Get the string value of a variable.
7624 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007625 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7626 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 * If the String variable has never been set, return an empty string.
7628 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007629 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007630 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007632 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007633tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634{
7635 static char_u mybuf[NUMBUFLEN];
7636
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007637 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638}
7639
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007640 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007641tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007643 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007644
7645 return res != NULL ? res : (char_u *)"";
7646}
7647
Bram Moolenaar7d647822014-04-05 21:28:56 +02007648/*
7649 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7650 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007651 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007652tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007653{
7654 static char_u mybuf[NUMBUFLEN];
7655
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007656 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007657}
7658
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007659 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007660tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007661{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007662 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007664 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007665 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007666 (long_long_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007667 return buf;
7668 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007669 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007670 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007671 break;
7672 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007673 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007674 break;
7675 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007676 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007677 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007678 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007679#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007680 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007681 break;
7682#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007683 case VAR_STRING:
7684 if (varp->vval.v_string != NULL)
7685 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007686 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007687 case VAR_SPECIAL:
7688 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7689 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007690 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007691 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007692 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007693 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007694#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007695 {
7696 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007697 char *status;
7698
7699 if (job == NULL)
7700 return (char_u *)"no process";
7701 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007702 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007703 : "run";
7704# ifdef UNIX
7705 vim_snprintf((char *)buf, NUMBUFLEN,
7706 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01007707# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007708 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007709 "process %ld %s",
7710 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007711 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007712# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007713 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007714 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7715# endif
7716 return buf;
7717 }
7718#endif
7719 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007720 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007721#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007722 {
7723 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007724 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007725
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007726 if (channel == NULL)
7727 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7728 else
7729 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007730 "channel %d %s", channel->ch_id, status);
7731 return buf;
7732 }
7733#endif
7734 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007735 case VAR_UNKNOWN:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007736 emsg(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007737 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007739 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740}
7741
7742/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007743 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
7744 * string() on Dict, List, etc.
7745 */
7746 char_u *
7747tv_stringify(typval_T *varp, char_u *buf)
7748{
7749 if (varp->v_type == VAR_LIST
7750 || varp->v_type == VAR_DICT
7751 || varp->v_type == VAR_FUNC
7752 || varp->v_type == VAR_PARTIAL
7753 || varp->v_type == VAR_FLOAT)
7754 {
7755 typval_T tmp;
7756
7757 f_string(varp, &tmp);
7758 tv_get_string_buf(&tmp, buf);
7759 clear_tv(varp);
7760 *varp = tmp;
7761 return tmp.vval.v_string;
7762 }
7763 return tv_get_string_buf(varp, buf);
7764}
7765
7766/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 * Find variable "name" in the list of variables.
7768 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007769 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007770 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007771 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007773 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007774find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007777 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007778 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779
Bram Moolenaara7043832005-01-21 11:56:39 +00007780 ht = find_var_ht(name, &varname);
7781 if (htp != NULL)
7782 *htp = ht;
7783 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007785 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7786 if (ret != NULL)
7787 return ret;
7788
7789 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007790 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791}
7792
7793/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007794 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007795 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007797 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007798find_var_in_ht(
7799 hashtab_T *ht,
7800 int htname,
7801 char_u *varname,
7802 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007803{
Bram Moolenaar33570922005-01-25 22:26:29 +00007804 hashitem_T *hi;
7805
7806 if (*varname == NUL)
7807 {
7808 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007809 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007810 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007811 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007812 case 'g': return &globvars_var;
7813 case 'v': return &vimvars_var;
7814 case 'b': return &curbuf->b_bufvar;
7815 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007816 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007817 case 'l': return get_funccal_local_var();
7818 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007819 }
7820 return NULL;
7821 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007822
7823 hi = hash_find(ht, varname);
7824 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007825 {
7826 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007827 * worked find the variable again. Don't auto-load a script if it was
7828 * loaded already, otherwise it would be loaded every time when
7829 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007830 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007831 {
7832 /* Note: script_autoload() may make "hi" invalid. It must either
7833 * be obtained again or not used. */
7834 if (!script_autoload(varname, FALSE) || aborting())
7835 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007836 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007837 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007838 if (HASHITEM_EMPTY(hi))
7839 return NULL;
7840 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007841 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007842}
7843
7844/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007845 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007846 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007847 * Set "varname" to the start of name without ':'.
7848 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007849 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007850find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007852 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007853 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007854
Bram Moolenaar73627d02015-08-11 15:46:09 +02007855 if (name[0] == NUL)
7856 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 if (name[1] != ':')
7858 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007859 /* The name must not start with a colon or #. */
7860 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 return NULL;
7862 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007863
Bram Moolenaard2e716e2019-04-20 14:39:52 +02007864 // "version" is "v:version" in all scopes if scriptversion < 3.
7865 // Same for a few other variables marked with VV_COMPAT.
7866 if (current_sctx.sc_version < 3)
7867 {
7868 hi = hash_find(&compat_hashtab, name);
7869 if (!HASHITEM_EMPTY(hi))
7870 return &compat_hashtab;
7871 }
Bram Moolenaar532c7802005-01-27 14:44:31 +00007872
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007873 ht = get_funccal_local_ht();
7874 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007875 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007876 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 }
7878 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007879 if (*name == 'g') /* global variable */
7880 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007881 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7882 */
7883 if (vim_strchr(name + 2, ':') != NULL
7884 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007885 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007887 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007889 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007890 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007891 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007892 if (*name == 'v') /* v: variable */
7893 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007894 if (*name == 'a') /* a: function argument */
7895 return get_funccal_args_ht();
7896 if (*name == 'l') /* l: local function variable */
7897 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 if (*name == 's' /* script variable */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007899 && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
7900 return &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 return NULL;
7902}
7903
7904/*
7905 * Get the string value of a (global/local) variable.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007906 * Note: see tv_get_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 * Returns NULL when it doesn't exist.
7908 */
7909 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007910get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911{
Bram Moolenaar33570922005-01-25 22:26:29 +00007912 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007914 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 if (v == NULL)
7916 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007917 return tv_get_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918}
7919
7920/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007921 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 * sourcing this script and when executing functions defined in the script.
7923 */
7924 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007925new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926{
Bram Moolenaara7043832005-01-21 11:56:39 +00007927 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007928 hashtab_T *ht;
7929 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007930
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7932 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007933 /* Re-allocating ga_data means that an ht_array pointing to
7934 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007935 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007936 for (i = 1; i <= ga_scripts.ga_len; ++i)
7937 {
7938 ht = &SCRIPT_VARS(i);
7939 if (ht->ht_mask == HT_INIT_SIZE - 1)
7940 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007941 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007942 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007943 }
7944
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 while (ga_scripts.ga_len < id)
7946 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007947 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007948 ALLOC_CLEAR_ONE(scriptvar_T);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007949 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 }
7952 }
7953}
7954
7955/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007956 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7957 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 */
7959 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007960init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961{
Bram Moolenaar33570922005-01-25 22:26:29 +00007962 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007963 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007964 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007965 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007966 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007967 dict_var->di_tv.vval.v_dict = dict;
7968 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007969 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007970 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7971 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972}
7973
7974/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007975 * Unreference a dictionary initialized by init_var_dict().
7976 */
7977 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007978unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007979{
7980 /* Now the dict needs to be freed if no one else is using it, go back to
7981 * normal reference counting. */
7982 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7983 dict_unref(dict);
7984}
7985
7986/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007988 * Frees all allocated variables and the value they contain.
7989 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 */
7991 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007992vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007993{
7994 vars_clear_ext(ht, TRUE);
7995}
7996
7997/*
7998 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7999 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008000 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008001vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002{
Bram Moolenaara7043832005-01-21 11:56:39 +00008003 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008004 hashitem_T *hi;
8005 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006
Bram Moolenaar33570922005-01-25 22:26:29 +00008007 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008008 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00008009 for (hi = ht->ht_array; todo > 0; ++hi)
8010 {
8011 if (!HASHITEM_EMPTY(hi))
8012 {
8013 --todo;
8014
Bram Moolenaar33570922005-01-25 22:26:29 +00008015 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00008016 * ht_array might change then. hash_clear() takes care of it
8017 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008018 v = HI2DI(hi);
8019 if (free_val)
8020 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02008021 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00008022 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00008023 }
8024 }
8025 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00008026 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027}
8028
Bram Moolenaara7043832005-01-21 11:56:39 +00008029/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008030 * Delete a variable from hashtab "ht" at item "hi".
8031 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00008032 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008034delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035{
Bram Moolenaar33570922005-01-25 22:26:29 +00008036 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00008037
8038 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00008039 clear_tv(&di->di_tv);
8040 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041}
8042
8043/*
8044 * List the value of one internal variable.
8045 */
8046 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01008047list_one_var(dictitem_T *v, char *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008049 char_u *tofree;
8050 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008051 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008052
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008053 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00008054 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00008055 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008056 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057}
8058
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008060list_one_var_a(
Bram Moolenaar32526b32019-01-19 17:43:09 +01008061 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01008062 char_u *name,
8063 int type,
8064 char_u *string,
8065 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066{
Bram Moolenaar31859182007-08-14 20:41:13 +00008067 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
8068 msg_start();
8069 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 if (name != NULL) /* "a:" vars don't have a name stored */
Bram Moolenaar32526b32019-01-19 17:43:09 +01008071 msg_puts((char *)name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 msg_putchar(' ');
8073 msg_advance(22);
8074 if (type == VAR_NUMBER)
8075 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008076 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008077 msg_putchar('*');
8078 else if (type == VAR_LIST)
8079 {
8080 msg_putchar('[');
8081 if (*string == '[')
8082 ++string;
8083 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008084 else if (type == VAR_DICT)
8085 {
8086 msg_putchar('{');
8087 if (*string == '{')
8088 ++string;
8089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 else
8091 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008092
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008094
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008095 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008096 msg_puts("()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00008097 if (*first)
8098 {
8099 msg_clr_eos();
8100 *first = FALSE;
8101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102}
8103
8104/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008105 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 * If the variable already exists, the value is updated.
8107 * Otherwise the variable is created.
8108 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008109 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008110set_var(
8111 char_u *name,
8112 typval_T *tv,
Bram Moolenaar9937a052019-06-15 15:45:06 +02008113 int copy) // make copy of value in "tv"
8114{
8115 set_var_const(name, tv, copy, FALSE);
8116}
8117
8118/*
8119 * Set variable "name" to value in "tv".
8120 * If the variable already exists and "is_const" is FALSE the value is updated.
8121 * Otherwise the variable is created.
8122 */
8123 static void
8124set_var_const(
8125 char_u *name,
8126 typval_T *tv,
8127 int copy, // make copy of value in "tv"
8128 int is_const) // disallow to modify existing variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129{
Bram Moolenaar33570922005-01-25 22:26:29 +00008130 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008132 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008134 ht = find_var_ht(name, &varname);
8135 if (ht == NULL || *varname == NUL)
8136 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008137 semsg(_(e_illvar), name);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008138 return;
8139 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02008140 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008141
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008142 /* Search in parent scope which is possible to reference from lambda */
8143 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02008144 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008145
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008146 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
8147 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008148 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008149
Bram Moolenaar33570922005-01-25 22:26:29 +00008150 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02008152 if (is_const)
8153 {
8154 emsg(_(e_cannot_mod));
8155 return;
8156 }
8157
Bram Moolenaar33570922005-01-25 22:26:29 +00008158 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02008159 if (var_check_ro(v->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008160 || var_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00008161 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008162
8163 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008164 * Handle setting internal v: variables separately where needed to
8165 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00008166 */
8167 if (ht == &vimvarht)
8168 {
8169 if (v->di_tv.v_type == VAR_STRING)
8170 {
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01008171 VIM_CLEAR(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008172 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01008173 {
8174 char_u *val = tv_get_string(tv);
8175
8176 // Careful: when assigning to v:errmsg and tv_get_string()
8177 // causes an error message the variable will alrady be set.
8178 if (v->di_tv.vval.v_string == NULL)
8179 v->di_tv.vval.v_string = vim_strsave(val);
8180 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008181 else
8182 {
8183 /* Take over the string to avoid an extra alloc/free. */
8184 v->di_tv.vval.v_string = tv->vval.v_string;
8185 tv->vval.v_string = NULL;
8186 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008187 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008188 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008189 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008190 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008191 v->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008192 if (STRCMP(varname, "searchforward") == 0)
8193 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01008194#ifdef FEAT_SEARCH_EXTRA
8195 else if (STRCMP(varname, "hlsearch") == 0)
8196 {
8197 no_hlsearch = !v->di_tv.vval.v_number;
8198 redraw_all_later(SOME_VALID);
8199 }
8200#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008201 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008202 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008203 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01008204 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008205 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01008206 return;
8207 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008208 }
8209
8210 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 }
8212 else /* add a new variable */
8213 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01008214 // Can't add "v:" or "a:" variable.
8215 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00008216 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008217 semsg(_(e_illvar), name);
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00008218 return;
8219 }
8220
Bram Moolenaar31b81602019-02-10 22:14:27 +01008221 // Make sure the variable name is valid.
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008222 if (!valid_varname(varname))
8223 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00008224
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008225 v = alloc(sizeof(dictitem_T) + STRLEN(varname));
Bram Moolenaara7043832005-01-21 11:56:39 +00008226 if (v == NULL)
8227 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008228 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00008229 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008231 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 return;
8233 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02008234 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar9937a052019-06-15 15:45:06 +02008235 if (is_const)
8236 v->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 }
Bram Moolenaara7043832005-01-21 11:56:39 +00008238
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008239 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00008240 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00008241 else
8242 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008243 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008244 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008245 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00008246 }
Bram Moolenaar9937a052019-06-15 15:45:06 +02008247
8248 if (is_const)
8249 v->di_tv.v_lock |= VAR_LOCKED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250}
8251
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008252/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008253 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00008254 * Also give an error message.
8255 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008256 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008257var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00008258{
8259 if (flags & DI_FLAGS_RO)
8260 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008261 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008262 return TRUE;
8263 }
8264 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
8265 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008266 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008267 return TRUE;
8268 }
8269 return FALSE;
8270}
8271
8272/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008273 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
8274 * Also give an error message.
8275 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008276 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008277var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008278{
8279 if (flags & DI_FLAGS_FIX)
8280 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008281 semsg(_("E795: Cannot delete variable %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008282 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008283 return TRUE;
8284 }
8285 return FALSE;
8286}
8287
8288/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008289 * Check if a funcref is assigned to a valid variable name.
8290 * Return TRUE and give an error if not.
8291 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008292 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008293var_check_func_name(
8294 char_u *name, /* points to start of variable name */
8295 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008296{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02008297 /* Allow for w: b: s: and t:. */
8298 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008299 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
8300 ? name[2] : name[0]))
8301 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008302 semsg(_("E704: Funcref variable name must start with a capital: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008303 name);
8304 return TRUE;
8305 }
8306 /* Don't allow hiding a function. When "v" is not NULL we might be
8307 * assigning another function to the same var, the type is checked
8308 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02008309 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008310 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008311 semsg(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008312 name);
8313 return TRUE;
8314 }
8315 return FALSE;
8316}
8317
8318/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008319 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02008320 * Also give an error message, using "name" or _("name") when use_gettext is
8321 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008322 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008323 int
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008324var_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008325{
8326 if (lock & VAR_LOCKED)
8327 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008328 semsg(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008329 name == NULL ? (char_u *)_("Unknown")
8330 : use_gettext ? (char_u *)_(name)
8331 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008332 return TRUE;
8333 }
8334 if (lock & VAR_FIXED)
8335 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008336 semsg(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008337 name == NULL ? (char_u *)_("Unknown")
8338 : use_gettext ? (char_u *)_(name)
8339 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008340 return TRUE;
8341 }
8342 return FALSE;
8343}
8344
8345/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008346 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
8347 * Also give an error message, using "name" or _("name") when use_gettext is
8348 * TRUE.
8349 */
8350 static int
8351tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
8352{
8353 int lock = 0;
8354
8355 switch (tv->v_type)
8356 {
8357 case VAR_BLOB:
8358 if (tv->vval.v_blob != NULL)
8359 lock = tv->vval.v_blob->bv_lock;
8360 break;
8361 case VAR_LIST:
8362 if (tv->vval.v_list != NULL)
8363 lock = tv->vval.v_list->lv_lock;
8364 break;
8365 case VAR_DICT:
8366 if (tv->vval.v_dict != NULL)
8367 lock = tv->vval.v_dict->dv_lock;
8368 break;
8369 default:
8370 break;
8371 }
8372 return var_check_lock(tv->v_lock, name, use_gettext)
8373 || (lock != 0 && var_check_lock(lock, name, use_gettext));
8374}
8375
8376/*
8377 * Check if a variable name is valid.
8378 * Return FALSE and give an error if not.
8379 */
8380 int
8381valid_varname(char_u *varname)
8382{
8383 char_u *p;
8384
8385 for (p = varname; *p != NUL; ++p)
8386 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
8387 && *p != AUTOLOAD_CHAR)
8388 {
8389 semsg(_(e_illvar), varname);
8390 return FALSE;
8391 }
8392 return TRUE;
8393}
8394
8395/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008396 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008397 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008398 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008399 * It is OK for "from" and "to" to point to the same item. This is used to
8400 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008401 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008402 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008403copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008405 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008406 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008407 switch (from->v_type)
8408 {
8409 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008410 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008411 to->vval.v_number = from->vval.v_number;
8412 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008413 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008414#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008415 to->vval.v_float = from->vval.v_float;
8416 break;
8417#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008418 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008419#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01008420 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01008421 if (to->vval.v_job != NULL)
8422 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01008423 break;
8424#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01008425 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008426#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01008427 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01008428 if (to->vval.v_channel != NULL)
8429 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01008430 break;
8431#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008432 case VAR_STRING:
8433 case VAR_FUNC:
8434 if (from->vval.v_string == NULL)
8435 to->vval.v_string = NULL;
8436 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008437 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008438 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008439 if (from->v_type == VAR_FUNC)
8440 func_ref(to->vval.v_string);
8441 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008442 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008443 case VAR_PARTIAL:
8444 if (from->vval.v_partial == NULL)
8445 to->vval.v_partial = NULL;
8446 else
8447 {
8448 to->vval.v_partial = from->vval.v_partial;
8449 ++to->vval.v_partial->pt_refcount;
8450 }
8451 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008452 case VAR_BLOB:
8453 if (from->vval.v_blob == NULL)
8454 to->vval.v_blob = NULL;
8455 else
8456 {
8457 to->vval.v_blob = from->vval.v_blob;
8458 ++to->vval.v_blob->bv_refcount;
8459 }
8460 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008461 case VAR_LIST:
8462 if (from->vval.v_list == NULL)
8463 to->vval.v_list = NULL;
8464 else
8465 {
8466 to->vval.v_list = from->vval.v_list;
8467 ++to->vval.v_list->lv_refcount;
8468 }
8469 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008470 case VAR_DICT:
8471 if (from->vval.v_dict == NULL)
8472 to->vval.v_dict = NULL;
8473 else
8474 {
8475 to->vval.v_dict = from->vval.v_dict;
8476 ++to->vval.v_dict->dv_refcount;
8477 }
8478 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008479 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008480 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008481 break;
8482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483}
8484
8485/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008486 * Make a copy of an item.
8487 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008488 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
8489 * reference to an already copied list/dict can be used.
8490 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008491 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008492 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008493item_copy(
8494 typval_T *from,
8495 typval_T *to,
8496 int deep,
8497 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008498{
8499 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008500 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008501
Bram Moolenaar33570922005-01-25 22:26:29 +00008502 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008503 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008504 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008505 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008506 }
8507 ++recurse;
8508
8509 switch (from->v_type)
8510 {
8511 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008512 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008513 case VAR_STRING:
8514 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008515 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008516 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008517 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008518 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008519 copy_tv(from, to);
8520 break;
8521 case VAR_LIST:
8522 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008523 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008524 if (from->vval.v_list == NULL)
8525 to->vval.v_list = NULL;
8526 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8527 {
8528 /* use the copy made earlier */
8529 to->vval.v_list = from->vval.v_list->lv_copylist;
8530 ++to->vval.v_list->lv_refcount;
8531 }
8532 else
8533 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8534 if (to->vval.v_list == NULL)
8535 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008536 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008537 case VAR_BLOB:
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008538 ret = blob_copy(from, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008539 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008540 case VAR_DICT:
8541 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008542 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008543 if (from->vval.v_dict == NULL)
8544 to->vval.v_dict = NULL;
8545 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8546 {
8547 /* use the copy made earlier */
8548 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8549 ++to->vval.v_dict->dv_refcount;
8550 }
8551 else
8552 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8553 if (to->vval.v_dict == NULL)
8554 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008555 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008556 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008557 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008558 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008559 }
8560 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008561 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008562}
8563
8564/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008565 * This function is used by f_input() and f_inputdialog() functions. The third
8566 * argument to f_input() specifies the type of completion to use at the
8567 * prompt. The third argument to f_inputdialog() specifies the value to return
8568 * when the user cancels the prompt.
8569 */
8570 void
8571get_user_input(
8572 typval_T *argvars,
8573 typval_T *rettv,
8574 int inputdialog,
8575 int secret)
8576{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008577 char_u *prompt = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008578 char_u *p = NULL;
8579 int c;
8580 char_u buf[NUMBUFLEN];
8581 int cmd_silent_save = cmd_silent;
8582 char_u *defstr = (char_u *)"";
8583 int xp_type = EXPAND_NOTHING;
8584 char_u *xp_arg = NULL;
8585
8586 rettv->v_type = VAR_STRING;
8587 rettv->vval.v_string = NULL;
8588
8589#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008590 /* While starting up, there is no place to enter text. When running tests
8591 * with --not-a-term we assume feedkeys() will be used. */
8592 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008593 return;
8594#endif
8595
8596 cmd_silent = FALSE; /* Want to see the prompt. */
8597 if (prompt != NULL)
8598 {
8599 /* Only the part of the message after the last NL is considered as
8600 * prompt for the command line */
8601 p = vim_strrchr(prompt, '\n');
8602 if (p == NULL)
8603 p = prompt;
8604 else
8605 {
8606 ++p;
8607 c = *p;
8608 *p = NUL;
8609 msg_start();
8610 msg_clr_eos();
Bram Moolenaar32526b32019-01-19 17:43:09 +01008611 msg_puts_attr((char *)prompt, echo_attr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008612 msg_didout = FALSE;
8613 msg_starthere();
8614 *p = c;
8615 }
8616 cmdline_row = msg_row;
8617
8618 if (argvars[1].v_type != VAR_UNKNOWN)
8619 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008620 defstr = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008621 if (defstr != NULL)
8622 stuffReadbuffSpec(defstr);
8623
8624 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8625 {
8626 char_u *xp_name;
8627 int xp_namelen;
8628 long argt;
8629
8630 /* input() with a third argument: completion */
8631 rettv->vval.v_string = NULL;
8632
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008633 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008634 if (xp_name == NULL)
8635 return;
8636
8637 xp_namelen = (int)STRLEN(xp_name);
8638
8639 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8640 &xp_arg) == FAIL)
8641 return;
8642 }
8643 }
8644
8645 if (defstr != NULL)
8646 {
8647 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02008648
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008649 ex_normal_busy = 0;
8650 rettv->vval.v_string =
8651 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8652 xp_type, xp_arg);
8653 ex_normal_busy = save_ex_normal_busy;
8654 }
8655 if (inputdialog && rettv->vval.v_string == NULL
8656 && argvars[1].v_type != VAR_UNKNOWN
8657 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008658 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008659 &argvars[2], buf));
8660
8661 vim_free(xp_arg);
8662
8663 /* since the user typed this, no need to wait for return */
8664 need_wait_return = FALSE;
8665 msg_didout = FALSE;
8666 }
8667 cmd_silent = cmd_silent_save;
8668}
8669
8670/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 * ":echo expr1 ..." print each argument separated with a space, add a
8672 * newline at the end.
8673 * ":echon expr1 ..." print each argument plain.
8674 */
8675 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008676ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677{
8678 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008679 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008680 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 char_u *p;
8682 int needclr = TRUE;
8683 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008684 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01008685 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008686 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687
8688 if (eap->skip)
8689 ++emsg_skip;
8690 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8691 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008692 /* If eval1() causes an error message the text from the command may
8693 * still need to be cleared. E.g., "echo 22,44". */
8694 need_clr_eos = needclr;
8695
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008697 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 {
8699 /*
8700 * Report the invalid expression unless the expression evaluation
8701 * has been cancelled due to an aborting error, an interrupt, or an
8702 * exception.
8703 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008704 if (!aborting() && did_emsg == did_emsg_before
8705 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008706 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008707 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 break;
8709 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008710 need_clr_eos = FALSE;
8711
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 if (!eap->skip)
8713 {
8714 if (atstart)
8715 {
8716 atstart = FALSE;
8717 /* Call msg_start() after eval1(), evaluating the expression
8718 * may cause a message to appear. */
8719 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008720 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008721 /* Mark the saved text as finishing the line, so that what
8722 * follows is displayed on a new line when scrolling back
8723 * at the more prompt. */
8724 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 }
8728 else if (eap->cmdidx == CMD_echo)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008729 msg_puts_attr(" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008730 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008731 if (p != NULL)
8732 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008734 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008736 if (*p != TAB && needclr)
8737 {
8738 /* remove any text still there from the command */
8739 msg_clr_eos();
8740 needclr = FALSE;
8741 }
8742 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 }
8744 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008745 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008746 if (has_mbyte)
8747 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008748 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008749
8750 (void)msg_outtrans_len_attr(p, i, echo_attr);
8751 p += i - 1;
8752 }
8753 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008754 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008757 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008759 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 arg = skipwhite(arg);
8761 }
8762 eap->nextcmd = check_nextcmd(arg);
8763
8764 if (eap->skip)
8765 --emsg_skip;
8766 else
8767 {
8768 /* remove text that may still be there from the command */
8769 if (needclr)
8770 msg_clr_eos();
8771 if (eap->cmdidx == CMD_echo)
8772 msg_end();
8773 }
8774}
8775
8776/*
8777 * ":echohl {name}".
8778 */
8779 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008780ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008782 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783}
8784
8785/*
8786 * ":execute expr1 ..." execute the result of an expression.
8787 * ":echomsg expr1 ..." Print a message
8788 * ":echoerr expr1 ..." Print an error
8789 * Each gets spaces around each argument and a newline at the end for
8790 * echo commands
8791 */
8792 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008793ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794{
8795 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008796 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797 int ret = OK;
8798 char_u *p;
8799 garray_T ga;
8800 int len;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01008801 int save_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802
8803 ga_init2(&ga, 1, 80);
8804
8805 if (eap->skip)
8806 ++emsg_skip;
8807 while (*arg != NUL && *arg != '|' && *arg != '\n')
8808 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01008809 ret = eval1_emsg(&arg, &rettv, !eap->skip);
8810 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812
8813 if (!eap->skip)
8814 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008815 char_u buf[NUMBUFLEN];
8816
8817 if (eap->cmdidx == CMD_execute)
8818 p = tv_get_string_buf(&rettv, buf);
8819 else
8820 p = tv_stringify(&rettv, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 len = (int)STRLEN(p);
8822 if (ga_grow(&ga, len + 2) == FAIL)
8823 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008824 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 ret = FAIL;
8826 break;
8827 }
8828 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 ga.ga_len += len;
8832 }
8833
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008834 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 arg = skipwhite(arg);
8836 }
8837
8838 if (ret != FAIL && ga.ga_data != NULL)
8839 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008840 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8841 {
8842 /* Mark the already saved text as finishing the line, so that what
8843 * follows is displayed on a new line when scrolling back at the
8844 * more prompt. */
8845 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008846 }
8847
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008849 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01008850 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008851 out_flush();
8852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 else if (eap->cmdidx == CMD_echoerr)
8854 {
8855 /* We don't want to abort following commands, restore did_emsg. */
8856 save_did_emsg = did_emsg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008857 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 if (!force_abort)
8859 did_emsg = save_did_emsg;
8860 }
8861 else if (eap->cmdidx == CMD_execute)
8862 do_cmdline((char_u *)ga.ga_data,
8863 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8864 }
8865
8866 ga_clear(&ga);
8867
8868 if (eap->skip)
8869 --emsg_skip;
8870
8871 eap->nextcmd = check_nextcmd(arg);
8872}
8873
8874/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008875 * Find window specified by "vp" in tabpage "tp".
8876 */
8877 win_T *
8878find_win_by_nr(
8879 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01008880 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008881{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008882 win_T *wp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008883 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008884
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008885 if (nr < 0)
8886 return NULL;
8887 if (nr == 0)
8888 return curwin;
8889
Bram Moolenaar29323592016-07-24 22:04:11 +02008890 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8891 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008892 if (nr >= LOWEST_WIN_ID)
8893 {
8894 if (wp->w_id == nr)
8895 return wp;
8896 }
8897 else if (--nr <= 0)
8898 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008899 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008900 if (nr >= LOWEST_WIN_ID)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008901 {
8902#ifdef FEAT_TEXT_PROP
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02008903 // check tab-local popup windows
8904 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008905 if (wp->w_id == nr)
8906 return wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02008907 // check global popup windows
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008908 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
8909 if (wp->w_id == nr)
8910 return wp;
8911#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008912 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008913 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008914 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008915}
8916
8917/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02008918 * Find a window: When using a Window ID in any tab page, when using a number
8919 * in the current tab page.
8920 */
8921 win_T *
8922find_win_by_nr_or_id(typval_T *vp)
8923{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008924 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02008925
8926 if (nr >= LOWEST_WIN_ID)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01008927 return win_id2wp(tv_get_number(vp));
Bram Moolenaare6e39892018-10-25 12:32:11 +02008928 return find_win_by_nr(vp, NULL);
8929}
8930
8931/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008932 * Find window specified by "wvp" in tabpage "tvp".
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008933 * Returns the tab page in 'ptp'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008934 */
8935 win_T *
8936find_tabwin(
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008937 typval_T *wvp, // VAR_UNKNOWN for current window
8938 typval_T *tvp, // VAR_UNKNOWN for current tab page
8939 tabpage_T **ptp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008940{
8941 win_T *wp = NULL;
8942 tabpage_T *tp = NULL;
8943 long n;
8944
8945 if (wvp->v_type != VAR_UNKNOWN)
8946 {
8947 if (tvp->v_type != VAR_UNKNOWN)
8948 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008949 n = (long)tv_get_number(tvp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008950 if (n >= 0)
8951 tp = find_tabpage(n);
8952 }
8953 else
8954 tp = curtab;
8955
8956 if (tp != NULL)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008957 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008958 wp = find_win_by_nr(wvp, tp);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008959 if (wp == NULL && wvp->v_type == VAR_NUMBER
8960 && wvp->vval.v_number != -1)
8961 // A window with the specified number is not found
8962 tp = NULL;
8963 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008964 }
8965 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008966 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008967 wp = curwin;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008968 tp = curtab;
8969 }
8970
8971 if (ptp != NULL)
8972 *ptp = tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008973
8974 return wp;
8975}
8976
8977/*
8978 * getwinvar() and gettabwinvar()
8979 */
8980 void
8981getwinvar(
8982 typval_T *argvars,
8983 typval_T *rettv,
8984 int off) /* 1 for gettabwinvar() */
8985{
8986 win_T *win;
8987 char_u *varname;
8988 dictitem_T *v;
8989 tabpage_T *tp = NULL;
8990 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008991 win_T *oldcurwin;
8992 tabpage_T *oldtabpage;
8993 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008994
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008995 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008996 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008997 else
8998 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008999 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009000 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009001 ++emsg_off;
9002
9003 rettv->v_type = VAR_STRING;
9004 rettv->vval.v_string = NULL;
9005
9006 if (win != NULL && varname != NULL)
9007 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009008 /* Set curwin to be our win, temporarily. Also set the tabpage,
9009 * otherwise the window is not valid. Only do this when needed,
9010 * autocommands get blocked. */
9011 need_switch_win = !(tp == curtab && win == curwin);
9012 if (!need_switch_win
9013 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009014 {
Bram Moolenaar30567352016-08-27 21:25:44 +02009015 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009016 {
Bram Moolenaar30567352016-08-27 21:25:44 +02009017 if (varname[1] == NUL)
9018 {
9019 /* get all window-local options in a dict */
9020 dict_T *opts = get_winbuf_options(FALSE);
9021
9022 if (opts != NULL)
9023 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009024 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02009025 done = TRUE;
9026 }
9027 }
9028 else if (get_option_tv(&varname, rettv, 1) == OK)
9029 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009030 done = TRUE;
9031 }
9032 else
9033 {
9034 /* Look up the variable. */
9035 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
9036 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
9037 varname, FALSE);
9038 if (v != NULL)
9039 {
9040 copy_tv(&v->di_tv, rettv);
9041 done = TRUE;
9042 }
9043 }
9044 }
9045
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009046 if (need_switch_win)
9047 /* restore previous notion of curwin */
9048 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009049 }
9050
9051 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
9052 /* use the default return value */
9053 copy_tv(&argvars[off + 2], rettv);
9054
9055 --emsg_off;
9056}
9057
9058/*
9059 * "setwinvar()" and "settabwinvar()" functions
9060 */
9061 void
9062setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
9063{
9064 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009065 win_T *save_curwin;
9066 tabpage_T *save_curtab;
9067 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009068 char_u *varname, *winvarname;
9069 typval_T *varp;
9070 char_u nbuf[NUMBUFLEN];
9071 tabpage_T *tp = NULL;
9072
Bram Moolenaare0fb7d12019-02-12 20:48:10 +01009073 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009074 return;
9075
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009076 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009077 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009078 else
9079 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009080 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009081 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009082 varp = &argvars[off + 2];
9083
9084 if (win != NULL && varname != NULL && varp != NULL)
9085 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009086 need_switch_win = !(tp == curtab && win == curwin);
9087 if (!need_switch_win
9088 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009089 {
9090 if (*varname == '&')
9091 {
9092 long numval;
9093 char_u *strval;
9094 int error = FALSE;
9095
9096 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009097 numval = (long)tv_get_number_chk(varp, &error);
9098 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009099 if (!error && strval != NULL)
9100 set_option_value(varname, numval, strval, OPT_LOCAL);
9101 }
9102 else
9103 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02009104 winvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009105 if (winvarname != NULL)
9106 {
9107 STRCPY(winvarname, "w:");
9108 STRCPY(winvarname + 2, varname);
9109 set_var(winvarname, varp, TRUE);
9110 vim_free(winvarname);
9111 }
9112 }
9113 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009114 if (need_switch_win)
9115 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009116 }
9117}
9118
9119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
9121 * "arg" points to the "&" or '+' when called, to "option" when returning.
9122 * Returns NULL when no option name found. Otherwise pointer to the char
9123 * after the option name.
9124 */
9125 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009126find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127{
9128 char_u *p = *arg;
9129
9130 ++p;
9131 if (*p == 'g' && p[1] == ':')
9132 {
9133 *opt_flags = OPT_GLOBAL;
9134 p += 2;
9135 }
9136 else if (*p == 'l' && p[1] == ':')
9137 {
9138 *opt_flags = OPT_LOCAL;
9139 p += 2;
9140 }
9141 else
9142 *opt_flags = 0;
9143
9144 if (!ASCII_ISALPHA(*p))
9145 return NULL;
9146 *arg = p;
9147
9148 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
9149 p += 4; /* termcap option */
9150 else
9151 while (ASCII_ISALPHA(*p))
9152 ++p;
9153 return p;
9154}
9155
9156/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009157 * Return the autoload script name for a function or variable name.
9158 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02009160 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009161autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02009162{
Bram Moolenaara1544c02013-05-30 12:35:52 +02009163 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009164 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02009165
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009166 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaar964b3742019-05-24 18:54:09 +02009167 scriptname = alloc(STRLEN(name) + 14);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009168 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02009169 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009170 STRCPY(scriptname, "autoload/");
9171 STRCAT(scriptname, name);
9172 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
9173 STRCAT(scriptname, ".vim");
9174 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
9175 *p = '/';
9176 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009177}
9178
9179/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009180 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009181 * Return TRUE if a package was loaded.
9182 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009183 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009184script_autoload(
9185 char_u *name,
9186 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009187{
9188 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009189 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009190 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009191 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009192
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009193 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00009194 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009195 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009196 return FALSE;
9197
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009198 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009199
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009200 /* Find the name in the list of previously loaded package names. Skip
9201 * "autoload/", it's always the same. */
9202 for (i = 0; i < ga_loaded.ga_len; ++i)
9203 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
9204 break;
9205 if (!reload && i < ga_loaded.ga_len)
9206 ret = FALSE; /* was loaded already */
9207 else
9208 {
9209 /* Remember the name if it wasn't loaded already. */
9210 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
9211 {
9212 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
9213 tofree = NULL;
9214 }
9215
9216 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009217 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009218 ret = TRUE;
9219 }
9220
9221 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009222 return ret;
9223}
9224
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
9226typedef enum
9227{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009228 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
9229 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
9230 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231} var_flavour_T;
9232
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01009234var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235{
9236 char_u *p = varname;
9237
9238 if (ASCII_ISUPPER(*p))
9239 {
9240 while (*(++p))
9241 if (ASCII_ISLOWER(*p))
9242 return VAR_FLAVOUR_SESSION;
9243 return VAR_FLAVOUR_VIMINFO;
9244 }
9245 else
9246 return VAR_FLAVOUR_DEFAULT;
9247}
9248#endif
9249
9250#if defined(FEAT_VIMINFO) || defined(PROTO)
9251/*
9252 * Restore global vars that start with a capital from the viminfo file
9253 */
9254 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009255read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256{
9257 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009258 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00009259 typval_T tv;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009260 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261
9262 if (!writing && (find_viminfo_parameter('!') != NULL))
9263 {
9264 tab = vim_strchr(virp->vir_line + 1, '\t');
9265 if (tab != NULL)
9266 {
9267 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009268 switch (*tab)
9269 {
9270 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009271#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009272 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009273#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009274 case 'D': type = VAR_DICT; break;
9275 case 'L': type = VAR_LIST; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009276 case 'B': type = VAR_BLOB; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009277 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279
9280 tab = vim_strchr(tab, '\t');
9281 if (tab != NULL)
9282 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009283 tv.v_type = type;
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009284 if (type == VAR_STRING || type == VAR_DICT
9285 || type == VAR_LIST || type == VAR_BLOB)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009286 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009288#ifdef FEAT_FLOAT
9289 else if (type == VAR_FLOAT)
9290 (void)string2float(tab + 1, &tv.vval.v_float);
9291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009293 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009294 if (type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009295 {
9296 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
9297
9298 if (etv == NULL)
9299 /* Failed to parse back the dict or list, use it as a
9300 * string. */
9301 tv.v_type = VAR_STRING;
9302 else
9303 {
9304 vim_free(tv.vval.v_string);
9305 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01009306 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009307 }
9308 }
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009309 else if (type == VAR_BLOB)
9310 {
9311 blob_T *blob = string2blob(tv.vval.v_string);
9312
9313 if (blob == NULL)
9314 // Failed to parse back the blob, use it as a string.
9315 tv.v_type = VAR_STRING;
9316 else
9317 {
9318 vim_free(tv.vval.v_string);
9319 tv.v_type = VAR_BLOB;
9320 tv.vval.v_blob = blob;
9321 }
9322 }
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009323
Bram Moolenaarb20e3342016-01-18 23:29:01 +01009324 /* when in a function use global variables */
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009325 save_funccal(&funccal_entry);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009326 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009327 restore_funccal();
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009328
9329 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009330 vim_free(tv.vval.v_string);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009331 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST ||
9332 tv.v_type == VAR_BLOB)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009333 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334 }
9335 }
9336 }
9337
9338 return viminfo_readline(virp);
9339}
9340
9341/*
9342 * Write global vars that start with a capital to the viminfo file
9343 */
9344 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009345write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346{
Bram Moolenaar33570922005-01-25 22:26:29 +00009347 hashitem_T *hi;
9348 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009349 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01009350 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009351 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009352 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009353 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354
9355 if (find_viminfo_parameter('!') == NULL)
9356 return;
9357
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02009358 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00009359
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009360 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009361 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009363 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009365 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009366 this_var = HI2DI(hi);
9367 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009368 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009369 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00009370 {
9371 case VAR_STRING: s = "STR"; break;
9372 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009373 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009374 case VAR_DICT: s = "DIC"; break;
9375 case VAR_LIST: s = "LIS"; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009376 case VAR_BLOB: s = "BLO"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009377 case VAR_SPECIAL: s = "XPL"; break;
9378
9379 case VAR_UNKNOWN:
9380 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009381 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01009382 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01009383 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01009384 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00009385 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009386 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009387 if (this_var->di_tv.v_type == VAR_SPECIAL)
9388 {
9389 sprintf((char *)numbuf, "%ld",
9390 (long)this_var->di_tv.vval.v_number);
9391 p = numbuf;
9392 tofree = NULL;
9393 }
9394 else
9395 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009396 if (p != NULL)
9397 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00009398 vim_free(tofree);
9399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400 }
9401 }
9402}
9403#endif
9404
9405#if defined(FEAT_SESSION) || defined(PROTO)
9406 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009407store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408{
Bram Moolenaar33570922005-01-25 22:26:29 +00009409 hashitem_T *hi;
9410 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009411 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 char_u *p, *t;
9413
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009414 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009415 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009417 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009419 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009420 this_var = HI2DI(hi);
9421 if ((this_var->di_tv.v_type == VAR_NUMBER
9422 || this_var->di_tv.v_type == VAR_STRING)
9423 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009424 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009425 /* Escape special characters with a backslash. Turn a LF and
9426 * CR into \n and \r. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009427 p = vim_strsave_escaped(tv_get_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00009428 (char_u *)"\\\"\n\r");
9429 if (p == NULL) /* out of memory */
9430 break;
9431 for (t = p; *t != NUL; ++t)
9432 if (*t == '\n')
9433 *t = 'n';
9434 else if (*t == '\r')
9435 *t = 'r';
9436 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00009437 this_var->di_key,
9438 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9439 : ' ',
9440 p,
9441 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9442 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00009443 || put_eol(fd) == FAIL)
9444 {
9445 vim_free(p);
9446 return FAIL;
9447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 vim_free(p);
9449 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009450#ifdef FEAT_FLOAT
9451 else if (this_var->di_tv.v_type == VAR_FLOAT
9452 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
9453 {
9454 float_T f = this_var->di_tv.vval.v_float;
9455 int sign = ' ';
9456
9457 if (f < 0)
9458 {
9459 f = -f;
9460 sign = '-';
9461 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01009462 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009463 this_var->di_key, sign, f) < 0)
9464 || put_eol(fd) == FAIL)
9465 return FAIL;
9466 }
9467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468 }
9469 }
9470 return OK;
9471}
9472#endif
9473
Bram Moolenaar661b1822005-07-28 22:36:45 +00009474/*
9475 * Display script name where an item was last set.
9476 * Should only be invoked when 'verbose' is non-zero.
9477 */
9478 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009479last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009480{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009481 char_u *p;
9482
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009483 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009484 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009485 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009486 if (p != NULL)
9487 {
9488 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01009489 msg_puts(_("\n\tLast set from "));
9490 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009491 if (script_ctx.sc_lnum > 0)
9492 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01009493 msg_puts(_(" line "));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009494 msg_outnum((long)script_ctx.sc_lnum);
9495 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009496 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009497 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009498 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00009499 }
9500}
9501
Bram Moolenaar61be3762019-03-19 23:04:17 +01009502/*
9503 * Reset v:option_new, v:option_old and v:option_type.
9504 */
Bram Moolenaar53744302015-07-17 17:38:22 +02009505 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009506reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02009507{
9508 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
9509 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
9510 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
9511}
9512
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009513/*
9514 * Prepare "gap" for an assert error and add the sourcing position.
9515 */
9516 void
9517prepare_assert_error(garray_T *gap)
9518{
9519 char buf[NUMBUFLEN];
9520
9521 ga_init2(gap, 1, 100);
9522 if (sourcing_name != NULL)
9523 {
9524 ga_concat(gap, sourcing_name);
9525 if (sourcing_lnum > 0)
9526 ga_concat(gap, (char_u *)" ");
9527 }
9528 if (sourcing_lnum > 0)
9529 {
9530 sprintf(buf, "line %ld", (long)sourcing_lnum);
9531 ga_concat(gap, (char_u *)buf);
9532 }
9533 if (sourcing_name != NULL || sourcing_lnum > 0)
9534 ga_concat(gap, (char_u *)": ");
9535}
9536
9537/*
9538 * Add an assert error to v:errors.
9539 */
9540 void
9541assert_error(garray_T *gap)
9542{
9543 struct vimvar *vp = &vimvars[VV_ERRORS];
9544
9545 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9546 /* Make sure v:errors is a list. */
9547 set_vim_var_list(VV_ERRORS, list_alloc());
9548 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9549}
9550
Bram Moolenaar65a54642018-04-28 16:56:53 +02009551 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009552assert_equal_common(typval_T *argvars, assert_type_T atype)
9553{
9554 garray_T ga;
9555
9556 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9557 != (atype == ASSERT_EQUAL))
9558 {
9559 prepare_assert_error(&ga);
9560 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9561 atype);
9562 assert_error(&ga);
9563 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009564 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009565 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009566 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009567}
9568
Bram Moolenaar65a54642018-04-28 16:56:53 +02009569 int
Bram Moolenaard96ff162018-02-18 22:13:29 +01009570assert_equalfile(typval_T *argvars)
9571{
9572 char_u buf1[NUMBUFLEN];
9573 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009574 char_u *fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
9575 char_u *fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009576 garray_T ga;
9577 FILE *fd1;
9578 FILE *fd2;
9579
9580 if (fname1 == NULL || fname2 == NULL)
Bram Moolenaar65a54642018-04-28 16:56:53 +02009581 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009582
9583 IObuff[0] = NUL;
9584 fd1 = mch_fopen((char *)fname1, READBIN);
9585 if (fd1 == NULL)
9586 {
9587 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname1);
9588 }
9589 else
9590 {
9591 fd2 = mch_fopen((char *)fname2, READBIN);
9592 if (fd2 == NULL)
9593 {
9594 fclose(fd1);
9595 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname2);
9596 }
9597 else
9598 {
9599 int c1, c2;
9600 long count = 0;
9601
9602 for (;;)
9603 {
9604 c1 = fgetc(fd1);
9605 c2 = fgetc(fd2);
9606 if (c1 == EOF)
9607 {
9608 if (c2 != EOF)
9609 STRCPY(IObuff, "first file is shorter");
9610 break;
9611 }
9612 else if (c2 == EOF)
9613 {
9614 STRCPY(IObuff, "second file is shorter");
9615 break;
9616 }
9617 else if (c1 != c2)
9618 {
9619 vim_snprintf((char *)IObuff, IOSIZE,
9620 "difference at byte %ld", count);
9621 break;
9622 }
9623 ++count;
9624 }
Bram Moolenaar30494182018-02-20 21:46:05 +01009625 fclose(fd1);
9626 fclose(fd2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009627 }
9628 }
9629 if (IObuff[0] != NUL)
9630 {
9631 prepare_assert_error(&ga);
9632 ga_concat(&ga, IObuff);
9633 assert_error(&ga);
9634 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009635 return 1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009636 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009637 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009638}
9639
Bram Moolenaar65a54642018-04-28 16:56:53 +02009640 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009641assert_match_common(typval_T *argvars, assert_type_T atype)
9642{
9643 garray_T ga;
9644 char_u buf1[NUMBUFLEN];
9645 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009646 char_u *pat = tv_get_string_buf_chk(&argvars[0], buf1);
9647 char_u *text = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009648
9649 if (pat == NULL || text == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009650 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009651 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
9652 {
9653 prepare_assert_error(&ga);
9654 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9655 atype);
9656 assert_error(&ga);
9657 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009658 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009659 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009660 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009661}
9662
Bram Moolenaar65a54642018-04-28 16:56:53 +02009663 int
Bram Moolenaar61c04492016-07-23 15:35:35 +02009664assert_inrange(typval_T *argvars)
9665{
9666 garray_T ga;
9667 int error = FALSE;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009668 char_u *tofree;
9669 char msg[200];
9670 char_u numbuf[NUMBUFLEN];
9671
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009672#ifdef FEAT_FLOAT
9673 if (argvars[0].v_type == VAR_FLOAT
9674 || argvars[1].v_type == VAR_FLOAT
9675 || argvars[2].v_type == VAR_FLOAT)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009676 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009677 float_T flower = tv_get_float(&argvars[0]);
9678 float_T fupper = tv_get_float(&argvars[1]);
9679 float_T factual = tv_get_float(&argvars[2]);
9680
9681 if (factual < flower || factual > fupper)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009682 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009683 prepare_assert_error(&ga);
9684 if (argvars[3].v_type != VAR_UNKNOWN)
9685 {
9686 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9687 vim_free(tofree);
9688 }
9689 else
9690 {
9691 vim_snprintf(msg, 200, "Expected range %g - %g, but got %g",
9692 flower, fupper, factual);
9693 ga_concat(&ga, (char_u *)msg);
9694 }
9695 assert_error(&ga);
9696 ga_clear(&ga);
9697 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009698 }
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009699 }
9700 else
9701#endif
9702 {
9703 varnumber_T lower = tv_get_number_chk(&argvars[0], &error);
9704 varnumber_T upper = tv_get_number_chk(&argvars[1], &error);
9705 varnumber_T actual = tv_get_number_chk(&argvars[2], &error);
9706
9707 if (error)
9708 return 0;
9709 if (actual < lower || actual > upper)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009710 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009711 prepare_assert_error(&ga);
9712 if (argvars[3].v_type != VAR_UNKNOWN)
9713 {
9714 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9715 vim_free(tofree);
9716 }
9717 else
9718 {
9719 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
Bram Moolenaar61c04492016-07-23 15:35:35 +02009720 (long)lower, (long)upper, (long)actual);
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009721 ga_concat(&ga, (char_u *)msg);
9722 }
9723 assert_error(&ga);
9724 ga_clear(&ga);
9725 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009726 }
Bram Moolenaar61c04492016-07-23 15:35:35 +02009727 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009728 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009729}
9730
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009731/*
9732 * Common for assert_true() and assert_false().
Bram Moolenaar65a54642018-04-28 16:56:53 +02009733 * Return non-zero for failure.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009734 */
Bram Moolenaar65a54642018-04-28 16:56:53 +02009735 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009736assert_bool(typval_T *argvars, int isTrue)
9737{
9738 int error = FALSE;
9739 garray_T ga;
9740
9741 if (argvars[0].v_type == VAR_SPECIAL
9742 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar65a54642018-04-28 16:56:53 +02009743 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009744 if (argvars[0].v_type != VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009745 || (tv_get_number_chk(&argvars[0], &error) == 0) == isTrue
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009746 || error)
9747 {
9748 prepare_assert_error(&ga);
9749 fill_assert_error(&ga, &argvars[1],
9750 (char_u *)(isTrue ? "True" : "False"),
9751 NULL, &argvars[0], ASSERT_OTHER);
9752 assert_error(&ga);
9753 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009754 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009755 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009756 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009757}
9758
Bram Moolenaar65a54642018-04-28 16:56:53 +02009759 int
Bram Moolenaar42205552017-03-18 19:42:22 +01009760assert_report(typval_T *argvars)
9761{
9762 garray_T ga;
9763
9764 prepare_assert_error(&ga);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009765 ga_concat(&ga, tv_get_string(&argvars[0]));
Bram Moolenaar42205552017-03-18 19:42:22 +01009766 assert_error(&ga);
9767 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009768 return 1;
Bram Moolenaar42205552017-03-18 19:42:22 +01009769}
9770
Bram Moolenaar65a54642018-04-28 16:56:53 +02009771 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009772assert_exception(typval_T *argvars)
9773{
9774 garray_T ga;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009775 char_u *error = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009776
9777 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9778 {
9779 prepare_assert_error(&ga);
9780 ga_concat(&ga, (char_u *)"v:exception is not set");
9781 assert_error(&ga);
9782 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009783 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009784 }
9785 else if (error != NULL
9786 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9787 {
9788 prepare_assert_error(&ga);
9789 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9790 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9791 assert_error(&ga);
9792 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009793 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009794 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009795 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009796}
9797
Bram Moolenaar65a54642018-04-28 16:56:53 +02009798 int
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009799assert_beeps(typval_T *argvars)
9800{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009801 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009802 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009803 int ret = 0;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009804
9805 called_vim_beep = FALSE;
9806 suppress_errthrow = TRUE;
9807 emsg_silent = FALSE;
9808 do_cmdline_cmd(cmd);
9809 if (!called_vim_beep)
9810 {
9811 prepare_assert_error(&ga);
9812 ga_concat(&ga, (char_u *)"command did not beep: ");
9813 ga_concat(&ga, cmd);
9814 assert_error(&ga);
9815 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009816 ret = 1;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009817 }
9818
9819 suppress_errthrow = FALSE;
9820 emsg_on_display = FALSE;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009821 return ret;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009822}
9823
Bram Moolenaar25190db2019-05-04 15:05:28 +02009824 static void
9825assert_append_cmd_or_arg(garray_T *gap, typval_T *argvars, char_u *cmd)
9826{
9827 char_u *tofree;
9828 char_u numbuf[NUMBUFLEN];
9829
9830 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9831 {
9832 ga_concat(gap, echo_string(&argvars[2], &tofree, numbuf, 0));
9833 vim_free(tofree);
9834 }
9835 else
9836 ga_concat(gap, cmd);
9837}
9838
Bram Moolenaar65a54642018-04-28 16:56:53 +02009839 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009840assert_fails(typval_T *argvars)
9841{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009842 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009843 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009844 int ret = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009845
9846 called_emsg = FALSE;
9847 suppress_errthrow = TRUE;
9848 emsg_silent = TRUE;
9849 do_cmdline_cmd(cmd);
9850 if (!called_emsg)
9851 {
9852 prepare_assert_error(&ga);
9853 ga_concat(&ga, (char_u *)"command did not fail: ");
Bram Moolenaar25190db2019-05-04 15:05:28 +02009854 assert_append_cmd_or_arg(&ga, argvars, cmd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009855 assert_error(&ga);
9856 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009857 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009858 }
9859 else if (argvars[1].v_type != VAR_UNKNOWN)
9860 {
9861 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009862 char *error = (char *)tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009863
9864 if (error == NULL
9865 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9866 {
9867 prepare_assert_error(&ga);
9868 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9869 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaar25190db2019-05-04 15:05:28 +02009870 ga_concat(&ga, (char_u *)": ");
9871 assert_append_cmd_or_arg(&ga, argvars, cmd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009872 assert_error(&ga);
9873 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009874 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009875 }
9876 }
9877
9878 called_emsg = FALSE;
9879 suppress_errthrow = FALSE;
9880 emsg_silent = FALSE;
9881 emsg_on_display = FALSE;
9882 set_vim_var_string(VV_ERRMSG, NULL, 0);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009883 return ret;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009884}
9885
9886/*
Bram Moolenaar86576712019-01-25 20:48:33 +01009887 * Append "p[clen]" to "gap", escaping unprintable characters.
9888 * Changes NL to \n, CR to \r, etc.
9889 */
9890 static void
9891ga_concat_esc(garray_T *gap, char_u *p, int clen)
9892{
9893 char_u buf[NUMBUFLEN];
9894
9895 if (clen > 1)
9896 {
9897 mch_memmove(buf, p, clen);
9898 buf[clen] = NUL;
9899 ga_concat(gap, buf);
9900 }
9901 else switch (*p)
9902 {
9903 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9904 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9905 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9906 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9907 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9908 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9909 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9910 default:
9911 if (*p < ' ')
9912 {
9913 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9914 ga_concat(gap, buf);
9915 }
9916 else
9917 ga_append(gap, *p);
9918 break;
9919 }
9920}
9921
9922/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009923 * Append "str" to "gap", escaping unprintable characters.
9924 * Changes NL to \n, CR to \r, etc.
9925 */
9926 static void
Bram Moolenaar86576712019-01-25 20:48:33 +01009927ga_concat_shorten_esc(garray_T *gap, char_u *str)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009928{
9929 char_u *p;
Bram Moolenaar86576712019-01-25 20:48:33 +01009930 char_u *s;
9931 int c;
9932 int clen;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009933 char_u buf[NUMBUFLEN];
Bram Moolenaar86576712019-01-25 20:48:33 +01009934 int same_len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009935
9936 if (str == NULL)
9937 {
9938 ga_concat(gap, (char_u *)"NULL");
9939 return;
9940 }
9941
9942 for (p = str; *p != NUL; ++p)
Bram Moolenaar86576712019-01-25 20:48:33 +01009943 {
9944 same_len = 1;
9945 s = p;
9946 c = mb_ptr2char_adv(&s);
9947 clen = s - p;
9948 while (*s != NUL && c == mb_ptr2char(s))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009949 {
Bram Moolenaar86576712019-01-25 20:48:33 +01009950 ++same_len;
9951 s += clen;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009952 }
Bram Moolenaar86576712019-01-25 20:48:33 +01009953 if (same_len > 20)
9954 {
9955 ga_concat(gap, (char_u *)"\\[");
9956 ga_concat_esc(gap, p, clen);
9957 ga_concat(gap, (char_u *)" occurs ");
9958 vim_snprintf((char *)buf, NUMBUFLEN, "%d", same_len);
9959 ga_concat(gap, buf);
9960 ga_concat(gap, (char_u *)" times]");
9961 p = s - 1;
9962 }
9963 else
9964 ga_concat_esc(gap, p, clen);
9965 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009966}
9967
9968/*
9969 * Fill "gap" with information about an assert error.
9970 */
9971 void
9972fill_assert_error(
9973 garray_T *gap,
9974 typval_T *opt_msg_tv,
9975 char_u *exp_str,
9976 typval_T *exp_tv,
9977 typval_T *got_tv,
9978 assert_type_T atype)
9979{
9980 char_u numbuf[NUMBUFLEN];
9981 char_u *tofree;
9982
9983 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9984 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009985 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9986 vim_free(tofree);
9987 ga_concat(gap, (char_u *)": ");
9988 }
9989
9990 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9991 ga_concat(gap, (char_u *)"Pattern ");
9992 else if (atype == ASSERT_NOTEQUAL)
9993 ga_concat(gap, (char_u *)"Expected not equal to ");
9994 else
9995 ga_concat(gap, (char_u *)"Expected ");
9996 if (exp_str == NULL)
9997 {
Bram Moolenaar86576712019-01-25 20:48:33 +01009998 ga_concat_shorten_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009999 vim_free(tofree);
10000 }
10001 else
Bram Moolenaar86576712019-01-25 20:48:33 +010010002 ga_concat_shorten_esc(gap, exp_str);
Bram Moolenaarc7b831c2017-01-28 18:08:12 +010010003 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010004 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +010010005 if (atype == ASSERT_MATCH)
10006 ga_concat(gap, (char_u *)" does not match ");
10007 else if (atype == ASSERT_NOTMATCH)
10008 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010009 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +010010010 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar86576712019-01-25 20:48:33 +010010011 ga_concat_shorten_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarc7b831c2017-01-28 18:08:12 +010010012 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010013 }
10014}
10015
Bram Moolenaar31988702018-02-13 12:57:42 +010010016/*
10017 * Compare "typ1" and "typ2". Put the result in "typ1".
10018 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010019 int
10020typval_compare(
10021 typval_T *typ1, /* first operand */
10022 typval_T *typ2, /* second operand */
10023 exptype_T type, /* operator */
10024 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +010010025 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010026{
10027 int i;
10028 varnumber_T n1, n2;
10029 char_u *s1, *s2;
10030 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
10031
Bram Moolenaar31988702018-02-13 12:57:42 +010010032 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010033 {
Bram Moolenaar31988702018-02-13 12:57:42 +010010034 /* For "is" a different type always means FALSE, for "notis"
10035 * it means TRUE. */
10036 n1 = (type == TYPE_NEQUAL);
10037 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010038 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
10039 {
10040 if (type_is)
10041 {
10042 n1 = (typ1->v_type == typ2->v_type
10043 && typ1->vval.v_blob == typ2->vval.v_blob);
10044 if (type == TYPE_NEQUAL)
10045 n1 = !n1;
10046 }
10047 else if (typ1->v_type != typ2->v_type
10048 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
10049 {
10050 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010051 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010052 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010053 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010054 clear_tv(typ1);
10055 return FAIL;
10056 }
10057 else
10058 {
10059 // Compare two Blobs for being equal or unequal.
10060 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
10061 if (type == TYPE_NEQUAL)
10062 n1 = !n1;
10063 }
10064 }
Bram Moolenaar31988702018-02-13 12:57:42 +010010065 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
10066 {
10067 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010068 {
Bram Moolenaar31988702018-02-13 12:57:42 +010010069 n1 = (typ1->v_type == typ2->v_type
10070 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010071 if (type == TYPE_NEQUAL)
10072 n1 = !n1;
10073 }
Bram Moolenaar31988702018-02-13 12:57:42 +010010074 else if (typ1->v_type != typ2->v_type
10075 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010076 {
Bram Moolenaar31988702018-02-13 12:57:42 +010010077 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010078 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010079 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010080 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +010010081 clear_tv(typ1);
10082 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010083 }
10084 else
10085 {
Bram Moolenaar31988702018-02-13 12:57:42 +010010086 /* Compare two Lists for being equal or unequal. */
10087 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
10088 ic, FALSE);
10089 if (type == TYPE_NEQUAL)
10090 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010091 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010092 }
Bram Moolenaar31988702018-02-13 12:57:42 +010010093
10094 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
10095 {
10096 if (type_is)
10097 {
10098 n1 = (typ1->v_type == typ2->v_type
10099 && typ1->vval.v_dict == typ2->vval.v_dict);
10100 if (type == TYPE_NEQUAL)
10101 n1 = !n1;
10102 }
10103 else if (typ1->v_type != typ2->v_type
10104 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
10105 {
10106 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010107 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +010010108 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010109 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +010010110 clear_tv(typ1);
10111 return FAIL;
10112 }
10113 else
10114 {
10115 /* Compare two Dictionaries for being equal or unequal. */
10116 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
10117 ic, FALSE);
10118 if (type == TYPE_NEQUAL)
10119 n1 = !n1;
10120 }
10121 }
10122
10123 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
10124 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
10125 {
10126 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
10127 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010128 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +010010129 clear_tv(typ1);
10130 return FAIL;
10131 }
10132 if ((typ1->v_type == VAR_PARTIAL
10133 && typ1->vval.v_partial == NULL)
10134 || (typ2->v_type == VAR_PARTIAL
10135 && typ2->vval.v_partial == NULL))
10136 /* when a partial is NULL assume not equal */
10137 n1 = FALSE;
10138 else if (type_is)
10139 {
10140 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
10141 /* strings are considered the same if their value is
10142 * the same */
10143 n1 = tv_equal(typ1, typ2, ic, FALSE);
10144 else if (typ1->v_type == VAR_PARTIAL
10145 && typ2->v_type == VAR_PARTIAL)
10146 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
10147 else
10148 n1 = FALSE;
10149 }
10150 else
10151 n1 = tv_equal(typ1, typ2, ic, FALSE);
10152 if (type == TYPE_NEQUAL)
10153 n1 = !n1;
10154 }
10155
10156#ifdef FEAT_FLOAT
10157 /*
10158 * If one of the two variables is a float, compare as a float.
10159 * When using "=~" or "!~", always compare as string.
10160 */
10161 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
10162 && type != TYPE_MATCH && type != TYPE_NOMATCH)
10163 {
10164 float_T f1, f2;
10165
Bram Moolenaar38f08e72019-02-20 22:04:32 +010010166 f1 = tv_get_float(typ1);
10167 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +010010168 n1 = FALSE;
10169 switch (type)
10170 {
10171 case TYPE_EQUAL: n1 = (f1 == f2); break;
10172 case TYPE_NEQUAL: n1 = (f1 != f2); break;
10173 case TYPE_GREATER: n1 = (f1 > f2); break;
10174 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
10175 case TYPE_SMALLER: n1 = (f1 < f2); break;
10176 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
10177 case TYPE_UNKNOWN:
10178 case TYPE_MATCH:
10179 case TYPE_NOMATCH: break; /* avoid gcc warning */
10180 }
10181 }
10182#endif
10183
10184 /*
10185 * If one of the two variables is a number, compare as a number.
10186 * When using "=~" or "!~", always compare as string.
10187 */
10188 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
10189 && type != TYPE_MATCH && type != TYPE_NOMATCH)
10190 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010191 n1 = tv_get_number(typ1);
10192 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +010010193 switch (type)
10194 {
10195 case TYPE_EQUAL: n1 = (n1 == n2); break;
10196 case TYPE_NEQUAL: n1 = (n1 != n2); break;
10197 case TYPE_GREATER: n1 = (n1 > n2); break;
10198 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
10199 case TYPE_SMALLER: n1 = (n1 < n2); break;
10200 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
10201 case TYPE_UNKNOWN:
10202 case TYPE_MATCH:
10203 case TYPE_NOMATCH: break; /* avoid gcc warning */
10204 }
10205 }
10206 else
10207 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010208 s1 = tv_get_string_buf(typ1, buf1);
10209 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar31988702018-02-13 12:57:42 +010010210 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
10211 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
10212 else
10213 i = 0;
10214 n1 = FALSE;
10215 switch (type)
10216 {
10217 case TYPE_EQUAL: n1 = (i == 0); break;
10218 case TYPE_NEQUAL: n1 = (i != 0); break;
10219 case TYPE_GREATER: n1 = (i > 0); break;
10220 case TYPE_GEQUAL: n1 = (i >= 0); break;
10221 case TYPE_SMALLER: n1 = (i < 0); break;
10222 case TYPE_SEQUAL: n1 = (i <= 0); break;
10223
10224 case TYPE_MATCH:
10225 case TYPE_NOMATCH:
10226 n1 = pattern_match(s2, s1, ic);
10227 if (type == TYPE_NOMATCH)
10228 n1 = !n1;
10229 break;
10230
10231 case TYPE_UNKNOWN: break; /* avoid gcc warning */
10232 }
10233 }
10234 clear_tv(typ1);
10235 typ1->v_type = VAR_NUMBER;
10236 typ1->vval.v_number = n1;
10237
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010238 return OK;
10239}
10240
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010241 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010242typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010243{
10244 char_u *tofree;
10245 char_u numbuf[NUMBUFLEN];
10246 char_u *ret = NULL;
10247
10248 if (arg == NULL)
10249 return vim_strsave((char_u *)"(does not exist)");
10250 ret = tv2string(arg, &tofree, numbuf, 0);
10251 /* Make a copy if we have a value but it's not in allocated memory. */
10252 if (ret != NULL && tofree == NULL)
10253 ret = vim_strsave(ret);
10254 return ret;
10255}
10256
10257 int
10258var_exists(char_u *var)
10259{
10260 char_u *name;
10261 char_u *tofree;
10262 typval_T tv;
10263 int len = 0;
10264 int n = FALSE;
10265
10266 /* get_name_len() takes care of expanding curly braces */
10267 name = var;
10268 len = get_name_len(&var, &tofree, TRUE, FALSE);
10269 if (len > 0)
10270 {
10271 if (tofree != NULL)
10272 name = tofree;
10273 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
10274 if (n)
10275 {
10276 /* handle d.key, l[idx], f(expr) */
10277 n = (handle_subscript(&var, &tv, TRUE, FALSE) == OK);
10278 if (n)
10279 clear_tv(&tv);
10280 }
10281 }
10282 if (*var != NUL)
10283 n = FALSE;
10284
10285 vim_free(tofree);
10286 return n;
10287}
10288
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289#endif /* FEAT_EVAL */
10290
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010292#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293
Bram Moolenaar4f974752019-02-17 17:44:42 +010010294#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295/*
10296 * Functions for ":8" filename modifier: get 8.3 version of a filename.
10297 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298
10299/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010300 * Get the short path (8.3) for the filename in "fnamep".
10301 * Only works for a valid file name.
10302 * When the path gets longer "fnamep" is changed and the allocated buffer
10303 * is put in "bufp".
10304 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
10305 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010306 */
10307 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010308get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010310 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010311 char_u *newbuf;
10312
10313 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010314 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315 if (l > len - 1)
10316 {
10317 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010318 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319 newbuf = vim_strnsave(*fnamep, l);
10320 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010321 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322
10323 vim_free(*bufp);
10324 *fnamep = *bufp = newbuf;
10325
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010326 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010327 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328 }
10329
10330 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010331 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332}
10333
10334/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010335 * Get the short path (8.3) for the filename in "fname". The converted
10336 * path is returned in "bufp".
10337 *
10338 * Some of the directories specified in "fname" may not exist. This function
10339 * will shorten the existing directories at the beginning of the path and then
10340 * append the remaining non-existing path.
10341 *
10342 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020010343 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010344 * bufp - Pointer to an allocated buffer for the filename.
10345 * fnamelen - Length of the filename pointed to by fname
10346 *
10347 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348 */
10349 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010350shortpath_for_invalid_fname(
10351 char_u **fname,
10352 char_u **bufp,
10353 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010355 char_u *short_fname, *save_fname, *pbuf_unused;
10356 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010358 int old_len, len;
10359 int new_len, sfx_len;
10360 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361
10362 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010363 old_len = *fnamelen;
10364 save_fname = vim_strnsave(*fname, old_len);
10365 pbuf_unused = NULL;
10366 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010367
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010368 endp = save_fname + old_len - 1; /* Find the end of the copy */
10369 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010371 /*
10372 * Try shortening the supplied path till it succeeds by removing one
10373 * directory at a time from the tail of the path.
10374 */
10375 len = 0;
10376 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010378 /* go back one path-separator */
10379 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
10380 --endp;
10381 if (endp <= save_fname)
10382 break; /* processed the complete path */
10383
10384 /*
10385 * Replace the path separator with a NUL and try to shorten the
10386 * resulting path.
10387 */
10388 ch = *endp;
10389 *endp = 0;
10390 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010391 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010392 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
10393 {
10394 retval = FAIL;
10395 goto theend;
10396 }
10397 *endp = ch; /* preserve the string */
10398
10399 if (len > 0)
10400 break; /* successfully shortened the path */
10401
10402 /* failed to shorten the path. Skip the path separator */
10403 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404 }
10405
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010406 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010408 /*
10409 * Succeeded in shortening the path. Now concatenate the shortened
10410 * path with the remaining path at the tail.
10411 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010412
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010413 /* Compute the length of the new path. */
10414 sfx_len = (int)(save_endp - endp) + 1;
10415 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010417 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010419 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010420 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010421 /* There is not enough space in the currently allocated string,
10422 * copy it to a buffer big enough. */
10423 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010425 {
10426 retval = FAIL;
10427 goto theend;
10428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429 }
10430 else
10431 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010432 /* Transfer short_fname to the main buffer (it's big enough),
10433 * unless get_short_pathname() did its work in-place. */
10434 *fname = *bufp = save_fname;
10435 if (short_fname != save_fname)
10436 vim_strncpy(save_fname, short_fname, len);
10437 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010438 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010439
10440 /* concat the not-shortened part of the path */
10441 vim_strncpy(*fname + len, endp, sfx_len);
10442 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010444
10445theend:
10446 vim_free(pbuf_unused);
10447 vim_free(save_fname);
10448
10449 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450}
10451
10452/*
10453 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010454 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455 */
10456 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010457shortpath_for_partial(
10458 char_u **fnamep,
10459 char_u **bufp,
10460 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461{
10462 int sepcount, len, tflen;
10463 char_u *p;
10464 char_u *pbuf, *tfname;
10465 int hasTilde;
10466
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010467 /* Count up the path separators from the RHS.. so we know which part
10468 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010470 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471 if (vim_ispathsep(*p))
10472 ++sepcount;
10473
10474 /* Need full path first (use expand_env() to remove a "~/") */
10475 hasTilde = (**fnamep == '~');
10476 if (hasTilde)
10477 pbuf = tfname = expand_env_save(*fnamep);
10478 else
10479 pbuf = tfname = FullName_save(*fnamep, FALSE);
10480
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010481 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010483 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
10484 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485
10486 if (len == 0)
10487 {
10488 /* Don't have a valid filename, so shorten the rest of the
10489 * path if we can. This CAN give us invalid 8.3 filenames, but
10490 * there's not a lot of point in guessing what it might be.
10491 */
10492 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010493 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
10494 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495 }
10496
10497 /* Count the paths backward to find the beginning of the desired string. */
10498 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010499 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010500 if (has_mbyte)
10501 p -= mb_head_off(tfname, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502 if (vim_ispathsep(*p))
10503 {
10504 if (sepcount == 0 || (hasTilde && sepcount == 1))
10505 break;
10506 else
10507 sepcount --;
10508 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510 if (hasTilde)
10511 {
10512 --p;
10513 if (p >= tfname)
10514 *p = '~';
10515 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010516 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517 }
10518 else
10519 ++p;
10520
10521 /* Copy in the string - p indexes into tfname - allocated at pbuf */
10522 vim_free(*bufp);
10523 *fnamelen = (int)STRLEN(p);
10524 *bufp = pbuf;
10525 *fnamep = p;
10526
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010527 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528}
Bram Moolenaar4f974752019-02-17 17:44:42 +010010529#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530
10531/*
10532 * Adjust a filename, according to a string of modifiers.
10533 * *fnamep must be NUL terminated when called. When returning, the length is
10534 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010535 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536 * When there is an error, *fnamep is set to NULL.
10537 */
10538 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010539modify_fname(
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010540 char_u *src, // string with modifiers
10541 int tilde_file, // "~" is a file name, not $HOME
10542 int *usedlen, // characters after src that are used
10543 char_u **fnamep, // file name so far
10544 char_u **bufp, // buffer for allocated file name or NULL
10545 int *fnamelen) // length of fnamep
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546{
10547 int valid = 0;
10548 char_u *tail;
10549 char_u *s, *p, *pbuf;
10550 char_u dirname[MAXPATHL];
10551 int c;
10552 int has_fullname = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010553#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010554 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555 int has_shortname = 0;
10556#endif
10557
10558repeat:
10559 /* ":p" - full path/file_name */
10560 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
10561 {
10562 has_fullname = 1;
10563
10564 valid |= VALID_PATH;
10565 *usedlen += 2;
10566
10567 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
10568 if ((*fnamep)[0] == '~'
10569#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
10570 && ((*fnamep)[1] == '/'
10571# ifdef BACKSLASH_IN_FILENAME
10572 || (*fnamep)[1] == '\\'
10573# endif
10574 || (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010576 && !(tilde_file && (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577 )
10578 {
10579 *fnamep = expand_env_save(*fnamep);
10580 vim_free(*bufp); /* free any allocated file name */
10581 *bufp = *fnamep;
10582 if (*fnamep == NULL)
10583 return -1;
10584 }
10585
10586 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010587 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588 {
10589 if (vim_ispathsep(*p)
10590 && p[1] == '.'
10591 && (p[2] == NUL
10592 || vim_ispathsep(p[2])
10593 || (p[2] == '.'
10594 && (p[3] == NUL || vim_ispathsep(p[3])))))
10595 break;
10596 }
10597
10598 /* FullName_save() is slow, don't use it when not needed. */
10599 if (*p != NUL || !vim_isAbsName(*fnamep))
10600 {
10601 *fnamep = FullName_save(*fnamep, *p != NUL);
10602 vim_free(*bufp); /* free any allocated file name */
10603 *bufp = *fnamep;
10604 if (*fnamep == NULL)
10605 return -1;
10606 }
10607
Bram Moolenaar4f974752019-02-17 17:44:42 +010010608#ifdef MSWIN
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010609# if _WIN32_WINNT >= 0x0500
10610 if (vim_strchr(*fnamep, '~') != NULL)
10611 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010612 // Expand 8.3 filename to full path. Needed to make sure the same
10613 // file does not have two different names.
10614 // Note: problem does not occur if _WIN32_WINNT < 0x0500.
10615 WCHAR *wfname = enc_to_utf16(*fnamep, NULL);
10616 WCHAR buf[_MAX_PATH];
10617
10618 if (wfname != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010619 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010620 if (GetLongPathNameW(wfname, buf, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010621 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010622 char_u *p = utf16_to_enc(buf, NULL);
10623
10624 if (p != NULL)
10625 {
10626 vim_free(*bufp); // free any allocated file name
10627 *bufp = *fnamep = p;
10628 }
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010629 }
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010630 vim_free(wfname);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010631 }
10632 }
10633# endif
10634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635 /* Append a path separator to a directory. */
10636 if (mch_isdir(*fnamep))
10637 {
10638 /* Make room for one or two extra characters. */
10639 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
10640 vim_free(*bufp); /* free any allocated file name */
10641 *bufp = *fnamep;
10642 if (*fnamep == NULL)
10643 return -1;
10644 add_pathsep(*fnamep);
10645 }
10646 }
10647
10648 /* ":." - path relative to the current directory */
10649 /* ":~" - path relative to the home directory */
10650 /* ":8" - shortname path - postponed till after */
10651 while (src[*usedlen] == ':'
10652 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
10653 {
10654 *usedlen += 2;
10655 if (c == '8')
10656 {
Bram Moolenaar4f974752019-02-17 17:44:42 +010010657#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658 has_shortname = 1; /* Postpone this. */
10659#endif
10660 continue;
10661 }
10662 pbuf = NULL;
10663 /* Need full path first (use expand_env() to remove a "~/") */
10664 if (!has_fullname)
10665 {
10666 if (c == '.' && **fnamep == '~')
10667 p = pbuf = expand_env_save(*fnamep);
10668 else
10669 p = pbuf = FullName_save(*fnamep, FALSE);
10670 }
10671 else
10672 p = *fnamep;
10673
10674 has_fullname = 0;
10675
10676 if (p != NULL)
10677 {
10678 if (c == '.')
10679 {
10680 mch_dirname(dirname, MAXPATHL);
10681 s = shorten_fname(p, dirname);
10682 if (s != NULL)
10683 {
10684 *fnamep = s;
10685 if (pbuf != NULL)
10686 {
10687 vim_free(*bufp); /* free any allocated file name */
10688 *bufp = pbuf;
10689 pbuf = NULL;
10690 }
10691 }
10692 }
10693 else
10694 {
10695 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
10696 /* Only replace it when it starts with '~' */
10697 if (*dirname == '~')
10698 {
10699 s = vim_strsave(dirname);
10700 if (s != NULL)
10701 {
10702 *fnamep = s;
10703 vim_free(*bufp);
10704 *bufp = s;
10705 }
10706 }
10707 }
10708 vim_free(pbuf);
10709 }
10710 }
10711
10712 tail = gettail(*fnamep);
10713 *fnamelen = (int)STRLEN(*fnamep);
10714
10715 /* ":h" - head, remove "/file_name", can be repeated */
10716 /* Don't remove the first "/" or "c:\" */
10717 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
10718 {
10719 valid |= VALID_HEAD;
10720 *usedlen += 2;
10721 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010722 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010723 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724 *fnamelen = (int)(tail - *fnamep);
10725#ifdef VMS
10726 if (*fnamelen > 0)
10727 *fnamelen += 1; /* the path separator is part of the path */
10728#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010729 if (*fnamelen == 0)
10730 {
10731 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
10732 p = vim_strsave((char_u *)".");
10733 if (p == NULL)
10734 return -1;
10735 vim_free(*bufp);
10736 *bufp = *fnamep = tail = p;
10737 *fnamelen = 1;
10738 }
10739 else
10740 {
10741 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010742 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744 }
10745
10746 /* ":8" - shortname */
10747 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
10748 {
10749 *usedlen += 2;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010750#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751 has_shortname = 1;
10752#endif
10753 }
10754
Bram Moolenaar4f974752019-02-17 17:44:42 +010010755#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010756 /*
10757 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010758 */
10759 if (has_shortname)
10760 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010761 /* Copy the string if it is shortened by :h and when it wasn't copied
10762 * yet, because we are going to change it in place. Avoids changing
10763 * the buffer name for "%:8". */
10764 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765 {
10766 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020010767 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010768 return -1;
10769 vim_free(*bufp);
10770 *bufp = *fnamep = p;
10771 }
10772
10773 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020010774 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775 if (!has_fullname && !vim_isAbsName(*fnamep))
10776 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010777 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778 return -1;
10779 }
10780 else
10781 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010782 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010783
Bram Moolenaardc935552011-08-17 15:23:23 +020010784 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010786 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787 return -1;
10788
10789 if (l == 0)
10790 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010791 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010793 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010794 return -1;
10795 }
10796 *fnamelen = l;
10797 }
10798 }
Bram Moolenaar4f974752019-02-17 17:44:42 +010010799#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800
10801 /* ":t" - tail, just the basename */
10802 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
10803 {
10804 *usedlen += 2;
10805 *fnamelen -= (int)(tail - *fnamep);
10806 *fnamep = tail;
10807 }
10808
10809 /* ":e" - extension, can be repeated */
10810 /* ":r" - root, without extension, can be repeated */
10811 while (src[*usedlen] == ':'
10812 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
10813 {
10814 /* find a '.' in the tail:
10815 * - for second :e: before the current fname
10816 * - otherwise: The last '.'
10817 */
10818 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
10819 s = *fnamep - 2;
10820 else
10821 s = *fnamep + *fnamelen - 1;
10822 for ( ; s > tail; --s)
10823 if (s[0] == '.')
10824 break;
10825 if (src[*usedlen + 1] == 'e') /* :e */
10826 {
10827 if (s > tail)
10828 {
10829 *fnamelen += (int)(*fnamep - (s + 1));
10830 *fnamep = s + 1;
10831#ifdef VMS
10832 /* cut version from the extension */
10833 s = *fnamep + *fnamelen - 1;
10834 for ( ; s > *fnamep; --s)
10835 if (s[0] == ';')
10836 break;
10837 if (s > *fnamep)
10838 *fnamelen = s - *fnamep;
10839#endif
10840 }
10841 else if (*fnamep <= tail)
10842 *fnamelen = 0;
10843 }
10844 else /* :r */
10845 {
10846 if (s > tail) /* remove one extension */
10847 *fnamelen = (int)(s - *fnamep);
10848 }
10849 *usedlen += 2;
10850 }
10851
10852 /* ":s?pat?foo?" - substitute */
10853 /* ":gs?pat?foo?" - global substitute */
10854 if (src[*usedlen] == ':'
10855 && (src[*usedlen + 1] == 's'
10856 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
10857 {
10858 char_u *str;
10859 char_u *pat;
10860 char_u *sub;
10861 int sep;
10862 char_u *flags;
10863 int didit = FALSE;
10864
10865 flags = (char_u *)"";
10866 s = src + *usedlen + 2;
10867 if (src[*usedlen + 1] == 'g')
10868 {
10869 flags = (char_u *)"g";
10870 ++s;
10871 }
10872
10873 sep = *s++;
10874 if (sep)
10875 {
10876 /* find end of pattern */
10877 p = vim_strchr(s, sep);
10878 if (p != NULL)
10879 {
10880 pat = vim_strnsave(s, (int)(p - s));
10881 if (pat != NULL)
10882 {
10883 s = p + 1;
10884 /* find end of substitution */
10885 p = vim_strchr(s, sep);
10886 if (p != NULL)
10887 {
10888 sub = vim_strnsave(s, (int)(p - s));
10889 str = vim_strnsave(*fnamep, *fnamelen);
10890 if (sub != NULL && str != NULL)
10891 {
10892 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010893 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894 if (s != NULL)
10895 {
10896 *fnamep = s;
10897 *fnamelen = (int)STRLEN(s);
10898 vim_free(*bufp);
10899 *bufp = s;
10900 didit = TRUE;
10901 }
10902 }
10903 vim_free(sub);
10904 vim_free(str);
10905 }
10906 vim_free(pat);
10907 }
10908 }
10909 /* after using ":s", repeat all the modifiers */
10910 if (didit)
10911 goto repeat;
10912 }
10913 }
10914
Bram Moolenaar26df0922014-02-23 23:39:13 +010010915 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10916 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010917 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010918 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010919 if (c != NUL)
10920 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010921 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010922 if (c != NUL)
10923 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010924 if (p == NULL)
10925 return -1;
10926 vim_free(*bufp);
10927 *bufp = *fnamep = p;
10928 *fnamelen = (int)STRLEN(p);
10929 *usedlen += 2;
10930 }
10931
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932 return valid;
10933}
10934
10935/*
10936 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010937 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938 * "flags" can be "g" to do a global substitute.
10939 * Returns an allocated string, NULL for error.
10940 */
10941 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010942do_string_sub(
10943 char_u *str,
10944 char_u *pat,
10945 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010946 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010947 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948{
10949 int sublen;
10950 regmatch_T regmatch;
10951 int i;
10952 int do_all;
10953 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010954 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955 garray_T ga;
10956 char_u *ret;
10957 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010958 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959
10960 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
10961 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010962 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963
10964 ga_init2(&ga, 1, 200);
10965
10966 do_all = (flags[0] == 'g');
10967
10968 regmatch.rm_ic = p_ic;
10969 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10970 if (regmatch.regprog != NULL)
10971 {
10972 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010973 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
10975 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010010976 /* Skip empty match except for first match. */
10977 if (regmatch.startp[0] == regmatch.endp[0])
10978 {
10979 if (zero_width == regmatch.startp[0])
10980 {
10981 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020010982 i = MB_PTR2LEN(tail);
10983 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
10984 (size_t)i);
10985 ga.ga_len += i;
10986 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010987 continue;
10988 }
10989 zero_width = regmatch.startp[0];
10990 }
10991
Bram Moolenaar071d4272004-06-13 20:20:40 +000010992 /*
10993 * Get some space for a temporary buffer to do the substitution
10994 * into. It will contain:
10995 * - The text up to where the match is.
10996 * - The substituted text.
10997 * - The text after the match.
10998 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010999 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010011000 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
11002 {
11003 ga_clear(&ga);
11004 break;
11005 }
11006
11007 /* copy the text up to where the match is */
11008 i = (int)(regmatch.startp[0] - tail);
11009 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
11010 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011011 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000011012 + ga.ga_len + i, TRUE, TRUE, FALSE);
11013 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020011014 tail = regmatch.endp[0];
11015 if (*tail == NUL)
11016 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017 if (!do_all)
11018 break;
11019 }
11020
11021 if (ga.ga_data != NULL)
11022 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
11023
Bram Moolenaar473de612013-06-08 18:19:48 +020011024 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025 }
11026
11027 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
11028 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000011029 if (p_cpo == empty_option)
11030 p_cpo = save_cpo;
11031 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011032 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000011033 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034
11035 return ret;
11036}
11037
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011038 static int
11039filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
11040{
11041 typval_T rettv;
11042 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011043 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011044
11045 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
11046 argv[0] = vimvars[VV_KEY].vv_tv;
11047 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010011048 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
11049 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011050 if (map)
11051 {
11052 /* map(): replace the list item value */
11053 clear_tv(tv);
11054 rettv.v_lock = 0;
11055 *tv = rettv;
11056 }
11057 else
11058 {
11059 int error = FALSE;
11060
11061 /* filter(): when expr is zero remove the item */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011062 *remp = (tv_get_number_chk(&rettv, &error) == 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011063 clear_tv(&rettv);
11064 /* On type error, nothing has been removed; return FAIL to stop the
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011065 * loop. The error message was given by tv_get_number_chk(). */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011066 if (error)
11067 goto theend;
11068 }
11069 retval = OK;
11070theend:
11071 clear_tv(&vimvars[VV_VAL].vv_tv);
11072 return retval;
11073}
11074
11075
11076/*
11077 * Implementation of map() and filter().
11078 */
11079 void
11080filter_map(typval_T *argvars, typval_T *rettv, int map)
11081{
11082 typval_T *expr;
11083 listitem_T *li, *nli;
11084 list_T *l = NULL;
11085 dictitem_T *di;
11086 hashtab_T *ht;
11087 hashitem_T *hi;
11088 dict_T *d = NULL;
11089 typval_T save_val;
11090 typval_T save_key;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010011091 blob_T *b = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011092 int rem;
11093 int todo;
11094 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
11095 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
11096 : N_("filter() argument"));
11097 int save_did_emsg;
11098 int idx = 0;
11099
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010011100 if (argvars[0].v_type == VAR_BLOB)
11101 {
11102 if ((b = argvars[0].vval.v_blob) == NULL)
11103 return;
11104 }
11105 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011106 {
11107 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011108 || (!map && var_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011109 return;
11110 }
11111 else if (argvars[0].v_type == VAR_DICT)
11112 {
11113 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011114 || (!map && var_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011115 return;
11116 }
11117 else
11118 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011119 semsg(_(e_listdictarg), ermsg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011120 return;
11121 }
11122
11123 expr = &argvars[1];
11124 /* On type errors, the preceding call has already displayed an error
11125 * message. Avoid a misleading error message for an empty string that
11126 * was not passed as argument. */
11127 if (expr->v_type != VAR_UNKNOWN)
11128 {
11129 prepare_vimvar(VV_VAL, &save_val);
11130
11131 /* We reset "did_emsg" to be able to detect whether an error
11132 * occurred during evaluation of the expression. */
11133 save_did_emsg = did_emsg;
11134 did_emsg = FALSE;
11135
11136 prepare_vimvar(VV_KEY, &save_key);
11137 if (argvars[0].v_type == VAR_DICT)
11138 {
11139 vimvars[VV_KEY].vv_type = VAR_STRING;
11140
11141 ht = &d->dv_hashtab;
11142 hash_lock(ht);
11143 todo = (int)ht->ht_used;
11144 for (hi = ht->ht_array; todo > 0; ++hi)
11145 {
11146 if (!HASHITEM_EMPTY(hi))
11147 {
11148 int r;
11149
11150 --todo;
11151 di = HI2DI(hi);
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011152 if (map && (var_check_lock(di->di_tv.v_lock,
11153 arg_errmsg, TRUE)
11154 || var_check_ro(di->di_flags,
11155 arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011156 break;
11157 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
11158 r = filter_map_one(&di->di_tv, expr, map, &rem);
11159 clear_tv(&vimvars[VV_KEY].vv_tv);
11160 if (r == FAIL || did_emsg)
11161 break;
11162 if (!map && rem)
11163 {
11164 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11165 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
11166 break;
11167 dictitem_remove(d, di);
11168 }
11169 }
11170 }
11171 hash_unlock(ht);
11172 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010011173 else if (argvars[0].v_type == VAR_BLOB)
11174 {
11175 int i;
11176 typval_T tv;
11177
11178 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11179 for (i = 0; i < b->bv_ga.ga_len; i++)
11180 {
11181 tv.v_type = VAR_NUMBER;
11182 tv.vval.v_number = blob_get(b, i);
11183 vimvars[VV_KEY].vv_nr = idx;
11184 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg)
11185 break;
11186 if (tv.v_type != VAR_NUMBER)
11187 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011188 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010011189 return;
11190 }
11191 tv.v_type = VAR_NUMBER;
11192 blob_set(b, i, tv.vval.v_number);
11193 if (!map && rem)
11194 {
11195 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
11196
11197 mch_memmove(p + idx, p + i + 1,
11198 (size_t)b->bv_ga.ga_len - i - 1);
11199 --b->bv_ga.ga_len;
11200 --i;
11201 }
11202 }
11203 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011204 else
11205 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +010011206 // argvars[0].v_type == VAR_LIST
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011207 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11208
11209 for (li = l->lv_first; li != NULL; li = nli)
11210 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011211 if (map && var_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011212 break;
11213 nli = li->li_next;
11214 vimvars[VV_KEY].vv_nr = idx;
11215 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
11216 || did_emsg)
11217 break;
11218 if (!map && rem)
11219 listitem_remove(l, li);
11220 ++idx;
11221 }
11222 }
11223
11224 restore_vimvar(VV_KEY, &save_key);
11225 restore_vimvar(VV_VAL, &save_val);
11226
11227 did_emsg |= save_did_emsg;
11228 }
11229
11230 copy_tv(&argvars[0], rettv);
11231}
11232
Bram Moolenaar071d4272004-06-13 20:20:40 +000011233#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */