blob: ef25a3fc9d62233e085a37fb57d212927622b4d3 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar33570922005-01-25 22:26:29 +000023#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026static char *e_undefvar = N_("E121: Undefined variable: %s");
27static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000028static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
29static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar92124a32005-06-17 22:03:40 +000030static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020031#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020032static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020033#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000034
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010035#define NAMESPACE_CHAR (char_u *)"abglstvw"
36
Bram Moolenaar230bb3f2013-04-24 14:07:45 +020037static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +000038#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +000039
40/*
Bram Moolenaar532c7802005-01-27 14:44:31 +000041 * Old Vim variables such as "v:version" are also available without the "v:".
42 * Also in functions. We need a special hashtable for them.
43 */
Bram Moolenaar4debb442005-06-01 21:57:40 +000044static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +000045
46/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000047 * When recursively copying lists and dicts we need to remember which ones we
48 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000049 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000050 */
51static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020052
Bram Moolenaard9fba312005-06-26 22:34:35 +000053/*
Bram Moolenaar33570922005-01-25 22:26:29 +000054 * Array to hold the hashtab with variables local to each sourced script.
55 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 */
Bram Moolenaar33570922005-01-25 22:26:29 +000057typedef struct
58{
59 dictitem_T sv_var;
60 dict_T sv_dict;
61} scriptvar_T;
62
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020063static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
64#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
65#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +000066
67static int echo_attr = 0; /* attributes used for ":echo" */
68
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000069/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000070static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
71
Bram Moolenaar071d4272004-06-13 20:20:40 +000072/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000073 * Info used by a ":for" loop.
74 */
Bram Moolenaar33570922005-01-25 22:26:29 +000075typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000076{
77 int fi_semicolon; /* TRUE if ending in '; var]' */
78 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +000079 listwatch_T fi_lw; /* keep an eye on the item used. */
80 list_T *fi_list; /* list being used */
81} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000082
Bram Moolenaara7043832005-01-21 11:56:39 +000083
84/*
Bram Moolenaar33570922005-01-25 22:26:29 +000085 * Array to hold the value of v: variables.
86 * The value is in a dictitem, so that it can also be used in the v: scope.
87 * The reason to use this table anyway is for very quick access to the
88 * variables with the VV_ defines.
89 */
Bram Moolenaar33570922005-01-25 22:26:29 +000090
91/* values for vv_flags: */
92#define VV_COMPAT 1 /* compatible, also used without "v:" */
93#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000094#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +000095
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +010096#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +000097
98static struct vimvar
99{
100 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100101 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000102 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
103} vimvars[VV_LEN] =
104{
105 /*
106 * The order here must match the VV_ defines in vim.h!
107 * Initializing a union does not work, leave tv.vval empty to get zero's.
108 */
109 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
110 {VV_NAME("count1", VAR_NUMBER), VV_RO},
111 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
112 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
113 {VV_NAME("warningmsg", VAR_STRING), 0},
114 {VV_NAME("statusmsg", VAR_STRING), 0},
115 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
116 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
117 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
118 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
119 {VV_NAME("termresponse", VAR_STRING), VV_RO},
120 {VV_NAME("fname", VAR_STRING), VV_RO},
121 {VV_NAME("lang", VAR_STRING), VV_RO},
122 {VV_NAME("lc_time", VAR_STRING), VV_RO},
123 {VV_NAME("ctype", VAR_STRING), VV_RO},
124 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
125 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
126 {VV_NAME("fname_in", VAR_STRING), VV_RO},
127 {VV_NAME("fname_out", VAR_STRING), VV_RO},
128 {VV_NAME("fname_new", VAR_STRING), VV_RO},
129 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
130 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
131 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
132 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
133 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
134 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
135 {VV_NAME("progname", VAR_STRING), VV_RO},
136 {VV_NAME("servername", VAR_STRING), VV_RO},
137 {VV_NAME("dying", VAR_NUMBER), VV_RO},
138 {VV_NAME("exception", VAR_STRING), VV_RO},
139 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
140 {VV_NAME("register", VAR_STRING), VV_RO},
141 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
142 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000143 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
144 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000145 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000146 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
147 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000148 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
149 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
Bram Moolenaarc9721bd2016-06-04 17:41:03 +0200150 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000151 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
152 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
153 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000154 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000155 {VV_NAME("swapname", VAR_STRING), VV_RO},
156 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000157 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200158 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000159 {VV_NAME("mouse_win", VAR_NUMBER), 0},
Bram Moolenaar511972d2016-06-04 18:09:59 +0200160 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000161 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
162 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000163 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000164 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100165 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000166 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200167 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200168 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200169 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200170 {VV_NAME("option_new", VAR_STRING), VV_RO},
171 {VV_NAME("option_old", VAR_STRING), VV_RO},
172 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100173 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100174 {VV_NAME("false", VAR_SPECIAL), VV_RO},
175 {VV_NAME("true", VAR_SPECIAL), VV_RO},
176 {VV_NAME("null", VAR_SPECIAL), VV_RO},
177 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100178 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200179 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaarf562e722016-07-19 17:25:25 +0200180 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
181 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
182 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
183 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
184 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
185 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
186 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
187 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
188 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
189 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200190 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
191 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
Bram Moolenaarf3af54e2017-08-30 14:53:06 +0200192 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
193 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
194 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100195 {VV_NAME("event", VAR_DICT), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000196};
197
198/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000199#define vv_type vv_di.di_tv.v_type
200#define vv_nr vv_di.di_tv.vval.v_number
201#define vv_float vv_di.di_tv.vval.v_float
202#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000203#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200204#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000205#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000206
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200207static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000208#define vimvarht vimvardict.dv_hashtab
209
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100210static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
211static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
212static char_u *skip_var_one(char_u *arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100213static void list_glob_vars(int *first);
214static void list_buf_vars(int *first);
215static void list_win_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100216static void list_tab_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100217static void list_vim_vars(int *first);
218static void list_script_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100219static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
220static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100221static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
222static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100223static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
224static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
225static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
226static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000227
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100228static int eval2(char_u **arg, typval_T *rettv, int evaluate);
229static int eval3(char_u **arg, typval_T *rettv, int evaluate);
230static int eval4(char_u **arg, typval_T *rettv, int evaluate);
231static int eval5(char_u **arg, typval_T *rettv, int evaluate);
232static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
233static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000234
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100235static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100236static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
237static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100238static int free_unref_items(int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100239static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100240static int get_env_len(char_u **arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100241static 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 +0200242static void check_vars(char_u *name, int len);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100243static typval_T *alloc_string_tv(char_u *string);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100244static void delete_var(hashtab_T *ht, hashitem_T *hi);
245static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
246static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100247static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200248
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200249/* for VIM_VERSION_ defines */
250#include "version.h"
251
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100252
253#if defined(EBCDIC) || defined(PROTO)
254/*
255 * Compare struct fst by function name.
256 */
257 static int
258compare_func_name(const void *s1, const void *s2)
259{
260 struct fst *p1 = (struct fst *)s1;
261 struct fst *p2 = (struct fst *)s2;
262
263 return STRCMP(p1->f_name, p2->f_name);
264}
265
266/*
267 * Sort the function table by function name.
268 * The sorting of the table above is ASCII dependant.
269 * On machines using EBCDIC we have to sort it.
270 */
271 static void
272sortFunctions(void)
273{
274 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
275
276 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
277}
278#endif
279
280
Bram Moolenaar33570922005-01-25 22:26:29 +0000281/*
282 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000283 */
284 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100285eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000286{
Bram Moolenaar33570922005-01-25 22:26:29 +0000287 int i;
288 struct vimvar *p;
289
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200290 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
291 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200292 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000293 hash_init(&compat_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200294 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000295
296 for (i = 0; i < VV_LEN; ++i)
297 {
298 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200299 if (STRLEN(p->vv_name) > 16)
300 {
Bram Moolenaarde330112017-01-08 20:50:52 +0100301 IEMSG("INTERNAL: name too long, increase size of dictitem16_T");
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200302 getout(1);
303 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000304 STRCPY(p->vv_di.di_key, p->vv_name);
305 if (p->vv_flags & VV_RO)
306 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
307 else if (p->vv_flags & VV_RO_SBX)
308 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
309 else
310 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000311
312 /* add to v: scope dict, unless the value is not always available */
313 if (p->vv_type != VAR_UNKNOWN)
314 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000315 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000316 /* add to compat scope dict */
317 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000318 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100319 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
320
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000321 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100322 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100323 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100324 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100325 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100326
327 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
328 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
329 set_vim_var_nr(VV_NONE, VVAL_NONE);
330 set_vim_var_nr(VV_NULL, VVAL_NULL);
331
Bram Moolenaarf562e722016-07-19 17:25:25 +0200332 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
333 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
334 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
335 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
336 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
337 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
338 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
339 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
340 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
341 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
342
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200343 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200344
345#ifdef EBCDIC
346 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100347 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200348 */
349 sortFunctions();
350#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000351}
352
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000353#if defined(EXITFREE) || defined(PROTO)
354 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100355eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000356{
357 int i;
358 struct vimvar *p;
359
360 for (i = 0; i < VV_LEN; ++i)
361 {
362 p = &vimvars[i];
363 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100364 VIM_CLEAR(p->vv_str);
Bram Moolenaard812df62008-11-09 12:46:09 +0000365 else if (p->vv_di.di_tv.v_type == VAR_LIST)
366 {
367 list_unref(p->vv_list);
368 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000369 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000370 }
371 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000372 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000373 hash_clear(&compat_hashtab);
374
Bram Moolenaard9fba312005-06-26 22:34:35 +0000375 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100376# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200377 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100378# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000379
380 /* global variables */
381 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000382
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000383 /* autoloaded script names */
384 ga_clear_strings(&ga_loaded);
385
Bram Moolenaarcca74132013-09-25 21:00:28 +0200386 /* Script-local variables. First clear all the variables and in a second
387 * loop free the scriptvar_T, because a variable in one script might hold
388 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200389 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200390 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200391 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200392 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200393 ga_clear(&ga_scripts);
394
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000395 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200396 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000397
398 /* functions */
399 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000400}
401#endif
402
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403
404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 * Set an internal variable to a string value. Creates the variable if it does
406 * not already exist.
407 */
408 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100409set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000411 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000412 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413
414 val = vim_strsave(value);
415 if (val != NULL)
416 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000417 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000418 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000420 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000421 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 }
423 }
424}
425
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000426static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200427#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000428static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000429static char_u *redir_endp = NULL;
430static char_u *redir_varname = NULL;
431
432/*
433 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100434 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000435 * Returns OK if successfully completed the setup. FAIL otherwise.
436 */
437 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100438var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000439{
440 int save_emsg;
441 int err;
442 typval_T tv;
443
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000444 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000445 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000446 {
447 EMSG(_(e_invarg));
448 return FAIL;
449 }
450
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000451 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000452 redir_varname = vim_strsave(name);
453 if (redir_varname == NULL)
454 return FAIL;
455
456 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
457 if (redir_lval == NULL)
458 {
459 var_redir_stop();
460 return FAIL;
461 }
462
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000463 /* The output is stored in growarray "redir_ga" until redirection ends. */
464 ga_init2(&redir_ga, (int)sizeof(char), 500);
465
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000466 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100467 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000468 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000469 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
470 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200471 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000472 if (redir_endp != NULL && *redir_endp != NUL)
473 /* Trailing characters are present after the variable name */
474 EMSG(_(e_trailing));
475 else
476 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000477 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000478 var_redir_stop();
479 return FAIL;
480 }
481
482 /* check if we can write to the variable: set it to or append an empty
483 * string */
484 save_emsg = did_emsg;
485 did_emsg = FALSE;
486 tv.v_type = VAR_STRING;
487 tv.vval.v_string = (char_u *)"";
488 if (append)
489 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
490 else
491 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200492 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000493 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000494 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000495 if (err)
496 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000497 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000498 var_redir_stop();
499 return FAIL;
500 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000501
502 return OK;
503}
504
505/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000506 * Append "value[value_len]" to the variable set by var_redir_start().
507 * The actual appending is postponed until redirection ends, because the value
508 * appended may in fact be the string we write to, changing it may cause freed
509 * memory to be used:
510 * :redir => foo
511 * :let foo
512 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000513 */
514 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100515var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000516{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000517 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000518
519 if (redir_lval == NULL)
520 return;
521
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000522 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000523 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000524 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000525 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000526
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000527 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000528 {
529 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000530 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000531 }
532 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000533 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000534}
535
536/*
537 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000538 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000539 */
540 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100541var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000542{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000543 typval_T tv;
544
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200545 if (EVALCMD_BUSY)
546 {
547 redir_lval = NULL;
548 return;
549 }
550
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000551 if (redir_lval != NULL)
552 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000553 /* If there was no error: assign the text to the variable. */
554 if (redir_endp != NULL)
555 {
556 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
557 tv.v_type = VAR_STRING;
558 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200559 /* Call get_lval() again, if it's inside a Dict or List it may
560 * have changed. */
561 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100562 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200563 if (redir_endp != NULL && redir_lval->ll_name != NULL)
564 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
565 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000566 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000567
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000568 /* free the collected output */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100569 VIM_CLEAR(redir_ga.ga_data);
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000570
Bram Moolenaard23a8232018-02-10 18:45:26 +0100571 VIM_CLEAR(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000572 }
Bram Moolenaard23a8232018-02-10 18:45:26 +0100573 VIM_CLEAR(redir_varname);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000574}
575
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576# if defined(FEAT_MBYTE) || defined(PROTO)
577 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100578eval_charconvert(
579 char_u *enc_from,
580 char_u *enc_to,
581 char_u *fname_from,
582 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583{
584 int err = FALSE;
585
586 set_vim_var_string(VV_CC_FROM, enc_from, -1);
587 set_vim_var_string(VV_CC_TO, enc_to, -1);
588 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
589 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
590 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
591 err = TRUE;
592 set_vim_var_string(VV_CC_FROM, NULL, -1);
593 set_vim_var_string(VV_CC_TO, NULL, -1);
594 set_vim_var_string(VV_FNAME_IN, NULL, -1);
595 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
596
597 if (err)
598 return FAIL;
599 return OK;
600}
601# endif
602
603# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
604 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100605eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606{
607 int err = FALSE;
608
609 set_vim_var_string(VV_FNAME_IN, fname, -1);
610 set_vim_var_string(VV_CMDARG, args, -1);
611 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
612 err = TRUE;
613 set_vim_var_string(VV_FNAME_IN, NULL, -1);
614 set_vim_var_string(VV_CMDARG, NULL, -1);
615
616 if (err)
617 {
618 mch_remove(fname);
619 return FAIL;
620 }
621 return OK;
622}
623# endif
624
625# if defined(FEAT_DIFF) || defined(PROTO)
626 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100627eval_diff(
628 char_u *origfile,
629 char_u *newfile,
630 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631{
632 int err = FALSE;
633
634 set_vim_var_string(VV_FNAME_IN, origfile, -1);
635 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
636 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
637 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
638 set_vim_var_string(VV_FNAME_IN, NULL, -1);
639 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
640 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
641}
642
643 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100644eval_patch(
645 char_u *origfile,
646 char_u *difffile,
647 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648{
649 int err;
650
651 set_vim_var_string(VV_FNAME_IN, origfile, -1);
652 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
653 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
654 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
655 set_vim_var_string(VV_FNAME_IN, NULL, -1);
656 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
657 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
658}
659# endif
660
661/*
662 * Top level evaluation function, returning a boolean.
663 * Sets "error" to TRUE if there was an error.
664 * Return TRUE or FALSE.
665 */
666 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100667eval_to_bool(
668 char_u *arg,
669 int *error,
670 char_u **nextcmd,
671 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672{
Bram Moolenaar33570922005-01-25 22:26:29 +0000673 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200674 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675
676 if (skip)
677 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000678 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 else
681 {
682 *error = FALSE;
683 if (!skip)
684 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000685 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000686 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 }
688 }
689 if (skip)
690 --emsg_skip;
691
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200692 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693}
694
Bram Moolenaar48570482017-10-30 21:48:41 +0100695 static int
696eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
697{
698 char_u *s;
699 int dummy;
700 char_u buf[NUMBUFLEN];
701
702 if (expr->v_type == VAR_FUNC)
703 {
704 s = expr->vval.v_string;
705 if (s == NULL || *s == NUL)
706 return FAIL;
707 if (call_func(s, (int)STRLEN(s), rettv, argc, argv, NULL,
708 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
709 return FAIL;
710 }
711 else if (expr->v_type == VAR_PARTIAL)
712 {
713 partial_T *partial = expr->vval.v_partial;
714
715 s = partial_name(partial);
716 if (s == NULL || *s == NUL)
717 return FAIL;
718 if (call_func(s, (int)STRLEN(s), rettv, argc, argv, NULL,
719 0L, 0L, &dummy, TRUE, partial, NULL) == FAIL)
720 return FAIL;
721 }
722 else
723 {
724 s = get_tv_string_buf_chk(expr, buf);
725 if (s == NULL)
726 return FAIL;
727 s = skipwhite(s);
728 if (eval1(&s, rettv, TRUE) == FAIL)
729 return FAIL;
730 if (*s != NUL) /* check for trailing chars after expr */
731 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200732 clear_tv(rettv);
Bram Moolenaar48570482017-10-30 21:48:41 +0100733 EMSG2(_(e_invexpr2), s);
734 return FAIL;
735 }
736 }
737 return OK;
738}
739
740/*
741 * Like eval_to_bool() but using a typval_T instead of a string.
742 * Works for string, funcref and partial.
743 */
744 int
745eval_expr_to_bool(typval_T *expr, int *error)
746{
747 typval_T rettv;
748 int res;
749
750 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
751 {
752 *error = TRUE;
753 return FALSE;
754 }
755 res = (get_tv_number_chk(&rettv, error) != 0);
756 clear_tv(&rettv);
757 return res;
758}
759
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760/*
761 * Top level evaluation function, returning a string. If "skip" is TRUE,
762 * only parsing to "nextcmd" is done, without reporting errors. Return
763 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
764 */
765 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100766eval_to_string_skip(
767 char_u *arg,
768 char_u **nextcmd,
769 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770{
Bram Moolenaar33570922005-01-25 22:26:29 +0000771 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 char_u *retval;
773
774 if (skip)
775 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000776 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 retval = NULL;
778 else
779 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000780 retval = vim_strsave(get_tv_string(&tv));
781 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 }
783 if (skip)
784 --emsg_skip;
785
786 return retval;
787}
788
789/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000790 * Skip over an expression at "*pp".
791 * Return FAIL for an error, OK otherwise.
792 */
793 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100794skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000795{
Bram Moolenaar33570922005-01-25 22:26:29 +0000796 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000797
798 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000799 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000800}
801
802/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000804 * When "convert" is TRUE convert a List into a sequence of lines and convert
805 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 * Return pointer to allocated memory, or NULL for failure.
807 */
808 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100809eval_to_string(
810 char_u *arg,
811 char_u **nextcmd,
812 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813{
Bram Moolenaar33570922005-01-25 22:26:29 +0000814 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000816 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000817#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000818 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000821 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 retval = NULL;
823 else
824 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000825 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000826 {
827 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000828 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200829 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200830 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200831 if (tv.vval.v_list->lv_len > 0)
832 ga_append(&ga, NL);
833 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000834 ga_append(&ga, NUL);
835 retval = (char_u *)ga.ga_data;
836 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000837#ifdef FEAT_FLOAT
838 else if (convert && tv.v_type == VAR_FLOAT)
839 {
840 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
841 retval = vim_strsave(numbuf);
842 }
843#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000844 else
845 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000846 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 }
848
849 return retval;
850}
851
852/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000853 * Call eval_to_string() without using current local variables and using
854 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 */
856 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100857eval_to_string_safe(
858 char_u *arg,
859 char_u **nextcmd,
860 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861{
862 char_u *retval;
863 void *save_funccalp;
864
865 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000866 if (use_sandbox)
867 ++sandbox;
868 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000869 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000870 if (use_sandbox)
871 --sandbox;
872 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 restore_funccal(save_funccalp);
874 return retval;
875}
876
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877/*
878 * Top level evaluation function, returning a number.
879 * Evaluates "expr" silently.
880 * Returns -1 for an error.
881 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200882 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100883eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884{
Bram Moolenaar33570922005-01-25 22:26:29 +0000885 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200886 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000887 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888
889 ++emsg_off;
890
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000891 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 retval = -1;
893 else
894 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000895 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000896 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 }
898 --emsg_off;
899
900 return retval;
901}
902
Bram Moolenaara40058a2005-07-11 22:42:07 +0000903/*
904 * Prepare v: variable "idx" to be used.
905 * Save the current typeval in "save_tv".
906 * When not used yet add the variable to the v: hashtable.
907 */
908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100909prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000910{
911 *save_tv = vimvars[idx].vv_tv;
912 if (vimvars[idx].vv_type == VAR_UNKNOWN)
913 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
914}
915
916/*
917 * Restore v: variable "idx" to typeval "save_tv".
918 * When no longer defined, remove the variable from the v: hashtable.
919 */
920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100921restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000922{
923 hashitem_T *hi;
924
Bram Moolenaara40058a2005-07-11 22:42:07 +0000925 vimvars[idx].vv_tv = *save_tv;
926 if (vimvars[idx].vv_type == VAR_UNKNOWN)
927 {
928 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
929 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100930 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +0000931 else
932 hash_remove(&vimvarht, hi);
933 }
934}
935
Bram Moolenaar3c56a962006-03-12 22:19:04 +0000936#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000937/*
938 * Evaluate an expression to a list with suggestions.
939 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000940 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000941 */
942 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100943eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000944{
945 typval_T save_val;
946 typval_T rettv;
947 list_T *list = NULL;
948 char_u *p = skipwhite(expr);
949
950 /* Set "v:val" to the bad word. */
951 prepare_vimvar(VV_VAL, &save_val);
952 vimvars[VV_VAL].vv_type = VAR_STRING;
953 vimvars[VV_VAL].vv_str = badword;
954 if (p_verbose == 0)
955 ++emsg_off;
956
957 if (eval1(&p, &rettv, TRUE) == OK)
958 {
959 if (rettv.v_type != VAR_LIST)
960 clear_tv(&rettv);
961 else
962 list = rettv.vval.v_list;
963 }
964
965 if (p_verbose == 0)
966 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000967 restore_vimvar(VV_VAL, &save_val);
968
969 return list;
970}
971
972/*
973 * "list" is supposed to contain two items: a word and a number. Return the
974 * word in "pp" and the number as the return value.
975 * Return -1 if anything isn't right.
976 * Used to get the good word and score from the eval_spell_expr() result.
977 */
978 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100979get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000980{
981 listitem_T *li;
982
983 li = list->lv_first;
984 if (li == NULL)
985 return -1;
986 *pp = get_tv_string(&li->li_tv);
987
988 li = li->li_next;
989 if (li == NULL)
990 return -1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200991 return (int)get_tv_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000992}
993#endif
994
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000995/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000996 * Top level evaluation function.
997 * Returns an allocated typval_T with the result.
998 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000999 */
1000 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001001eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001002{
1003 typval_T *tv;
1004
1005 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001006 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001007 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001008
1009 return tv;
1010}
1011
1012
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001014 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +02001015 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
1016 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001017 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001019 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001020call_vim_function(
1021 char_u *func,
1022 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +02001023 typval_T *argv,
1024 typval_T *rettv,
1025 int safe) /* use the sandbox */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 int doesrange;
1028 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001029 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 if (safe)
1032 {
1033 save_funccalp = save_funccal();
1034 ++sandbox;
1035 }
1036
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001037 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaarffa96842018-06-12 22:05:14 +02001038 ret = call_func(func, (int)STRLEN(func), rettv, argc, argv, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001040 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 if (safe)
1042 {
1043 --sandbox;
1044 restore_funccal(save_funccalp);
1045 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001046
1047 if (ret == FAIL)
1048 clear_tv(rettv);
1049
1050 return ret;
1051}
1052
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001053/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001054 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001055 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001056 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1057 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001058 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001059 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001060call_func_retnr(
1061 char_u *func,
1062 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +02001063 typval_T *argv,
Bram Moolenaar7454a062016-01-30 15:14:10 +01001064 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001065{
1066 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001067 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001068
Bram Moolenaarffa96842018-06-12 22:05:14 +02001069 if (call_vim_function(func, argc, argv, &rettv, safe) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001070 return -1;
1071
1072 retval = get_tv_number_chk(&rettv, NULL);
1073 clear_tv(&rettv);
1074 return retval;
1075}
1076
1077#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1078 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1079
Bram Moolenaar4f688582007-07-24 12:34:30 +00001080# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001081/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001082 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001083 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001084 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1085 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001086 */
1087 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001088call_func_retstr(
1089 char_u *func,
1090 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +02001091 typval_T *argv,
Bram Moolenaar7454a062016-01-30 15:14:10 +01001092 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001093{
1094 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001095 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001096
Bram Moolenaarffa96842018-06-12 22:05:14 +02001097 if (call_vim_function(func, argc, argv, &rettv, safe) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001098 return NULL;
1099
1100 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001101 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 return retval;
1103}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001104# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001105
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001106/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001107 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001108 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1109 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001110 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001111 */
1112 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001113call_func_retlist(
1114 char_u *func,
1115 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +02001116 typval_T *argv,
Bram Moolenaar7454a062016-01-30 15:14:10 +01001117 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001118{
1119 typval_T rettv;
1120
Bram Moolenaarffa96842018-06-12 22:05:14 +02001121 if (call_vim_function(func, argc, argv, &rettv, safe) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001122 return NULL;
1123
1124 if (rettv.v_type != VAR_LIST)
1125 {
1126 clear_tv(&rettv);
1127 return NULL;
1128 }
1129
1130 return rettv.vval.v_list;
1131}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132#endif
1133
Bram Moolenaar05159a02005-02-26 23:04:13 +00001134
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135#ifdef FEAT_FOLDING
1136/*
1137 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1138 * it in "*cp". Doesn't give error messages.
1139 */
1140 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001141eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142{
Bram Moolenaar33570922005-01-25 22:26:29 +00001143 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001144 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001146 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1147 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148
1149 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001150 if (use_sandbox)
1151 ++sandbox;
1152 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001154 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 retval = 0;
1156 else
1157 {
1158 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001159 if (tv.v_type == VAR_NUMBER)
1160 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001161 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 retval = 0;
1163 else
1164 {
1165 /* If the result is a string, check if there is a non-digit before
1166 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001167 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 if (!VIM_ISDIGIT(*s) && *s != '-')
1169 *cp = *s++;
1170 retval = atol((char *)s);
1171 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001172 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 }
1174 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001175 if (use_sandbox)
1176 --sandbox;
1177 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001179 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180}
1181#endif
1182
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001184 * ":let" list all variable values
1185 * ":let var1 var2" list variable values
1186 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001187 * ":let var += expr" assignment command.
1188 * ":let var -= expr" assignment command.
1189 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001190 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 */
1192 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001193ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194{
1195 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001196 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001197 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001199 int var_count = 0;
1200 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001201 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001202 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001203 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204
Bram Moolenaardb552d602006-03-23 22:59:57 +00001205 argend = skip_var_list(arg, &var_count, &semicolon);
1206 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001207 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001208 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1209 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001210 expr = skipwhite(argend);
1211 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1212 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001214 /*
1215 * ":let" without "=": list variables
1216 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001217 if (*arg == '[')
1218 EMSG(_(e_invarg));
1219 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001220 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001221 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001222 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001223 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001224 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001225 list_glob_vars(&first);
1226 list_buf_vars(&first);
1227 list_win_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001228 list_tab_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001229 list_script_vars(&first);
1230 list_func_vars(&first);
1231 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 eap->nextcmd = check_nextcmd(arg);
1234 }
1235 else
1236 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001237 op[0] = '=';
1238 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001239 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001240 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001241 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1242 op[0] = *expr; /* +=, -= or .= */
1243 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001244 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001245 else
1246 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001247
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 if (eap->skip)
1249 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001250 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 if (eap->skip)
1252 {
1253 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001254 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 --emsg_skip;
1256 }
1257 else if (i != FAIL)
1258 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001259 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001260 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001261 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 }
1263 }
1264}
1265
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001266/*
1267 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1268 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001269 * When "nextchars" is not NULL it points to a string with characters that
1270 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1271 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001272 * Returns OK or FAIL;
1273 */
1274 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001275ex_let_vars(
1276 char_u *arg_start,
1277 typval_T *tv,
1278 int copy, /* copy values from "tv", don't move */
1279 int semicolon, /* from skip_var_list() */
1280 int var_count, /* from skip_var_list() */
1281 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001282{
1283 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001284 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001285 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001286 listitem_T *item;
1287 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001288
1289 if (*arg != '[')
1290 {
1291 /*
1292 * ":let var = expr" or ":for var in list"
1293 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001294 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001295 return FAIL;
1296 return OK;
1297 }
1298
1299 /*
1300 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1301 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001302 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001303 {
1304 EMSG(_(e_listreq));
1305 return FAIL;
1306 }
1307
1308 i = list_len(l);
1309 if (semicolon == 0 && var_count < i)
1310 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001311 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001312 return FAIL;
1313 }
1314 if (var_count - semicolon > i)
1315 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001316 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001317 return FAIL;
1318 }
1319
1320 item = l->lv_first;
1321 while (*arg != ']')
1322 {
1323 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001324 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001325 item = item->li_next;
1326 if (arg == NULL)
1327 return FAIL;
1328
1329 arg = skipwhite(arg);
1330 if (*arg == ';')
1331 {
1332 /* Put the rest of the list (may be empty) in the var after ';'.
1333 * Create a new list for this. */
1334 l = list_alloc();
1335 if (l == NULL)
1336 return FAIL;
1337 while (item != NULL)
1338 {
1339 list_append_tv(l, &item->li_tv);
1340 item = item->li_next;
1341 }
1342
1343 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001344 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001345 ltv.vval.v_list = l;
1346 l->lv_refcount = 1;
1347
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001348 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1349 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001350 clear_tv(&ltv);
1351 if (arg == NULL)
1352 return FAIL;
1353 break;
1354 }
1355 else if (*arg != ',' && *arg != ']')
1356 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001357 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001358 return FAIL;
1359 }
1360 }
1361
1362 return OK;
1363}
1364
1365/*
1366 * Skip over assignable variable "var" or list of variables "[var, var]".
1367 * Used for ":let varvar = expr" and ":for varvar in expr".
1368 * For "[var, var]" increment "*var_count" for each variable.
1369 * for "[var, var; var]" set "semicolon".
1370 * Return NULL for an error.
1371 */
1372 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001373skip_var_list(
1374 char_u *arg,
1375 int *var_count,
1376 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001377{
1378 char_u *p, *s;
1379
1380 if (*arg == '[')
1381 {
1382 /* "[var, var]": find the matching ']'. */
1383 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001384 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001385 {
1386 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1387 s = skip_var_one(p);
1388 if (s == p)
1389 {
1390 EMSG2(_(e_invarg2), p);
1391 return NULL;
1392 }
1393 ++*var_count;
1394
1395 p = skipwhite(s);
1396 if (*p == ']')
1397 break;
1398 else if (*p == ';')
1399 {
1400 if (*semicolon == 1)
1401 {
1402 EMSG(_("Double ; in list of variables"));
1403 return NULL;
1404 }
1405 *semicolon = 1;
1406 }
1407 else if (*p != ',')
1408 {
1409 EMSG2(_(e_invarg2), p);
1410 return NULL;
1411 }
1412 }
1413 return p + 1;
1414 }
1415 else
1416 return skip_var_one(arg);
1417}
1418
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001419/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001420 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001421 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001422 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001423 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001424skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001425{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001426 if (*arg == '@' && arg[1] != NUL)
1427 return arg + 2;
1428 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1429 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001430}
1431
Bram Moolenaara7043832005-01-21 11:56:39 +00001432/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001433 * List variables for hashtab "ht" with prefix "prefix".
1434 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001435 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001436 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001437list_hashtable_vars(
1438 hashtab_T *ht,
1439 char_u *prefix,
1440 int empty,
1441 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001442{
Bram Moolenaar33570922005-01-25 22:26:29 +00001443 hashitem_T *hi;
1444 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001445 int todo;
1446
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001447 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001448 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1449 {
1450 if (!HASHITEM_EMPTY(hi))
1451 {
1452 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001453 di = HI2DI(hi);
1454 if (empty || di->di_tv.v_type != VAR_STRING
1455 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001456 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001457 }
1458 }
1459}
1460
1461/*
1462 * List global variables.
1463 */
1464 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001465list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001466{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001467 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001468}
1469
1470/*
1471 * List buffer variables.
1472 */
1473 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001474list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001475{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001476 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001477 TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001478}
1479
1480/*
1481 * List window variables.
1482 */
1483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001484list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001485{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001486 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001487 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001488}
1489
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001490/*
1491 * List tab page variables.
1492 */
1493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001494list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001495{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001496 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001497 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001498}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001499
Bram Moolenaara7043832005-01-21 11:56:39 +00001500/*
1501 * List Vim variables.
1502 */
1503 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001504list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001505{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001506 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001507}
1508
1509/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001510 * List script-local variables, if there is a script.
1511 */
1512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001513list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001514{
1515 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001516 list_hashtable_vars(&SCRIPT_VARS(current_SID),
1517 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001518}
1519
1520/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001521 * List variables in "arg".
1522 */
1523 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001524list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001525{
1526 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001527 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001528 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001529 char_u *name_start;
1530 char_u *arg_subsc;
1531 char_u *tofree;
1532 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001533
1534 while (!ends_excmd(*arg) && !got_int)
1535 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001536 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001537 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001538 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001539 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001540 {
1541 emsg_severe = TRUE;
1542 EMSG(_(e_trailing));
1543 break;
1544 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001545 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001546 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001547 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001548 /* get_name_len() takes care of expanding curly braces */
1549 name_start = name = arg;
1550 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1551 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001552 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001553 /* This is mainly to keep test 49 working: when expanding
1554 * curly braces fails overrule the exception error message. */
1555 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001556 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001557 emsg_severe = TRUE;
1558 EMSG2(_(e_invarg2), arg);
1559 break;
1560 }
1561 error = TRUE;
1562 }
1563 else
1564 {
1565 if (tofree != NULL)
1566 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001567 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001568 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001569 else
1570 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001571 /* handle d.key, l[idx], f(expr) */
1572 arg_subsc = arg;
1573 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001574 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001575 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001576 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001577 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001578 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001579 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001580 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001581 case 'g': list_glob_vars(first); break;
1582 case 'b': list_buf_vars(first); break;
1583 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001584 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001585 case 'v': list_vim_vars(first); break;
1586 case 's': list_script_vars(first); break;
1587 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001588 default:
1589 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001590 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001591 }
1592 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001593 {
1594 char_u numbuf[NUMBUFLEN];
1595 char_u *tf;
1596 int c;
1597 char_u *s;
1598
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001599 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001600 c = *arg;
1601 *arg = NUL;
1602 list_one_var_a((char_u *)"",
1603 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001604 tv.v_type,
1605 s == NULL ? (char_u *)"" : s,
1606 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001607 *arg = c;
1608 vim_free(tf);
1609 }
1610 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001611 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001612 }
1613 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001614
1615 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001616 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001617
1618 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001619 }
1620
1621 return arg;
1622}
1623
1624/*
1625 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1626 * Returns a pointer to the char just after the var name.
1627 * Returns NULL if there is an error.
1628 */
1629 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001630ex_let_one(
1631 char_u *arg, /* points to variable name */
1632 typval_T *tv, /* value to assign to variable */
1633 int copy, /* copy value from "tv" */
1634 char_u *endchars, /* valid chars after variable name or NULL */
1635 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001636{
1637 int c1;
1638 char_u *name;
1639 char_u *p;
1640 char_u *arg_end = NULL;
1641 int len;
1642 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001643 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001644
1645 /*
1646 * ":let $VAR = expr": Set environment variable.
1647 */
1648 if (*arg == '$')
1649 {
1650 /* Find the end of the name. */
1651 ++arg;
1652 name = arg;
1653 len = get_env_len(&arg);
1654 if (len == 0)
1655 EMSG2(_(e_invarg2), name - 1);
1656 else
1657 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001658 if (op != NULL && (*op == '+' || *op == '-'))
1659 EMSG2(_(e_letwrong), op);
1660 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001661 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001662 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001663 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001664 {
1665 c1 = name[len];
1666 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001667 p = get_tv_string_chk(tv);
1668 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001669 {
1670 int mustfree = FALSE;
1671 char_u *s = vim_getenv(name, &mustfree);
1672
1673 if (s != NULL)
1674 {
1675 p = tofree = concat_str(s, p);
1676 if (mustfree)
1677 vim_free(s);
1678 }
1679 }
1680 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001681 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001682 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001683 if (STRICMP(name, "HOME") == 0)
1684 init_homedir();
1685 else if (didset_vim && STRICMP(name, "VIM") == 0)
1686 didset_vim = FALSE;
1687 else if (didset_vimruntime
1688 && STRICMP(name, "VIMRUNTIME") == 0)
1689 didset_vimruntime = FALSE;
1690 arg_end = arg;
1691 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001692 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001693 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001694 }
1695 }
1696 }
1697
1698 /*
1699 * ":let &option = expr": Set option value.
1700 * ":let &l:option = expr": Set local option value.
1701 * ":let &g:option = expr": Set global option value.
1702 */
1703 else if (*arg == '&')
1704 {
1705 /* Find the end of the name. */
1706 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001707 if (p == NULL || (endchars != NULL
1708 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001709 EMSG(_(e_letunexp));
1710 else
1711 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001712 long n;
1713 int opt_type;
1714 long numval;
1715 char_u *stringval = NULL;
1716 char_u *s;
1717
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001718 c1 = *p;
1719 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001720
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001721 n = (long)get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001722 s = get_tv_string_chk(tv); /* != NULL if number or string */
1723 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001724 {
1725 opt_type = get_option_value(arg, &numval,
1726 &stringval, opt_flags);
1727 if ((opt_type == 1 && *op == '.')
1728 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001729 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001730 EMSG2(_(e_letwrong), op);
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001731 s = NULL; /* don't set the value */
1732 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001733 else
1734 {
1735 if (opt_type == 1) /* number */
1736 {
1737 if (*op == '+')
1738 n = numval + n;
1739 else
1740 n = numval - n;
1741 }
1742 else if (opt_type == 0 && stringval != NULL) /* string */
1743 {
1744 s = concat_str(stringval, s);
1745 vim_free(stringval);
1746 stringval = s;
1747 }
1748 }
1749 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001750 if (s != NULL)
1751 {
1752 set_option_value(arg, n, s, opt_flags);
1753 arg_end = p;
1754 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001755 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001756 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001757 }
1758 }
1759
1760 /*
1761 * ":let @r = expr": Set register contents.
1762 */
1763 else if (*arg == '@')
1764 {
1765 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001766 if (op != NULL && (*op == '+' || *op == '-'))
1767 EMSG2(_(e_letwrong), op);
1768 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001769 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001770 EMSG(_(e_letunexp));
1771 else
1772 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001773 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001774 char_u *s;
1775
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001776 p = get_tv_string_chk(tv);
1777 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001778 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02001779 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001780 if (s != NULL)
1781 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001782 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001783 vim_free(s);
1784 }
1785 }
1786 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001787 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001788 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001789 arg_end = arg + 1;
1790 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001791 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001792 }
1793 }
1794
1795 /*
1796 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001797 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001798 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001799 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001800 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001801 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001802
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001803 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001804 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001805 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001806 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1807 EMSG(_(e_letunexp));
1808 else
1809 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001810 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001811 arg_end = p;
1812 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001813 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001814 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001815 }
1816
1817 else
1818 EMSG2(_(e_invarg2), arg);
1819
1820 return arg_end;
1821}
1822
1823/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001824 * Get an lval: variable, Dict item or List item that can be assigned a value
1825 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1826 * "name.key", "name.key[expr]" etc.
1827 * Indexing only works if "name" is an existing List or Dictionary.
1828 * "name" points to the start of the name.
1829 * If "rettv" is not NULL it points to the value to be assigned.
1830 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1831 * wrong; must end in space or cmd separator.
1832 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001833 * flags:
1834 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001835 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001836 * GLV_NO_AUTOLOAD: do not use script autoloading
1837 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001838 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001839 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001840 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001841 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001842 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001843get_lval(
1844 char_u *name,
1845 typval_T *rettv,
1846 lval_T *lp,
1847 int unlet,
1848 int skip,
1849 int flags, /* GLV_ values */
1850 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001851{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001852 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001853 char_u *expr_start, *expr_end;
1854 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001855 dictitem_T *v;
1856 typval_T var1;
1857 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001858 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001859 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001860 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001861 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001862 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001863 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001864
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001865 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001866 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001867
1868 if (skip)
1869 {
1870 /* When skipping just find the end of the name. */
1871 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001872 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001873 }
1874
1875 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001876 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001877 if (expr_start != NULL)
1878 {
1879 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001880 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001881 && *p != '[' && *p != '.')
1882 {
1883 EMSG(_(e_trailing));
1884 return NULL;
1885 }
1886
1887 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1888 if (lp->ll_exp_name == NULL)
1889 {
1890 /* Report an invalid expression in braces, unless the
1891 * expression evaluation has been cancelled due to an
1892 * aborting error, an interrupt, or an exception. */
1893 if (!aborting() && !quiet)
1894 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001895 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001896 EMSG2(_(e_invarg2), name);
1897 return NULL;
1898 }
1899 }
1900 lp->ll_name = lp->ll_exp_name;
1901 }
1902 else
1903 lp->ll_name = name;
1904
1905 /* Without [idx] or .key we are done. */
1906 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1907 return p;
1908
1909 cc = *p;
1910 *p = NUL;
Bram Moolenaar6e65d592017-12-07 22:11:27 +01001911 /* Only pass &ht when we would write to the variable, it prevents autoload
1912 * as well. */
1913 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
1914 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001915 if (v == NULL && !quiet)
1916 EMSG2(_(e_undefvar), lp->ll_name);
1917 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001918 if (v == NULL)
1919 return NULL;
1920
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001921 /*
1922 * Loop until no more [idx] or .key is following.
1923 */
Bram Moolenaar33570922005-01-25 22:26:29 +00001924 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001925 var1.v_type = VAR_UNKNOWN;
1926 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001927 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001928 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001929 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1930 && !(lp->ll_tv->v_type == VAR_DICT
1931 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001932 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001933 if (!quiet)
1934 EMSG(_("E689: Can only index a List or Dictionary"));
1935 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001936 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001937 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001938 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001939 if (!quiet)
1940 EMSG(_("E708: [:] must come last"));
1941 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001942 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001943
Bram Moolenaar8c711452005-01-14 21:53:12 +00001944 len = -1;
1945 if (*p == '.')
1946 {
1947 key = p + 1;
1948 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1949 ;
1950 if (len == 0)
1951 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001952 if (!quiet)
1953 EMSG(_(e_emptykey));
1954 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001955 }
1956 p = key + len;
1957 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001958 else
1959 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001960 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001961 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001962 if (*p == ':')
1963 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001964 else
1965 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001966 empty1 = FALSE;
1967 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001968 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001969 if (get_tv_string_chk(&var1) == NULL)
1970 {
1971 /* not a number or string */
1972 clear_tv(&var1);
1973 return NULL;
1974 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001975 }
1976
1977 /* Optionally get the second index [ :expr]. */
1978 if (*p == ':')
1979 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001980 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001981 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001982 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001983 EMSG(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001984 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001985 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001986 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001987 if (rettv != NULL && (rettv->v_type != VAR_LIST
1988 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001989 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001990 if (!quiet)
1991 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001992 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001993 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001994 }
1995 p = skipwhite(p + 1);
1996 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001997 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001998 else
1999 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002000 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002001 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2002 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002003 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002004 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002005 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002006 if (get_tv_string_chk(&var2) == NULL)
2007 {
2008 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002009 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002010 clear_tv(&var2);
2011 return NULL;
2012 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002013 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002014 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002015 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002016 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002017 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002018
Bram Moolenaar8c711452005-01-14 21:53:12 +00002019 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002020 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002021 if (!quiet)
2022 EMSG(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002023 clear_tv(&var1);
2024 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002025 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002026 }
2027
2028 /* Skip to past ']'. */
2029 ++p;
2030 }
2031
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002032 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002033 {
2034 if (len == -1)
2035 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002036 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002037 key = get_tv_string_chk(&var1); /* is number or string */
2038 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002039 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002040 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002041 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002042 }
2043 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002044 lp->ll_list = NULL;
2045 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002046 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002047
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002048 /* When assigning to a scope dictionary check that a function and
2049 * variable name is valid (only variable name unless it is l: or
2050 * g: dictionary). Disallow overwriting a builtin function. */
2051 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002052 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002053 int prevval;
2054 int wrong;
2055
2056 if (len != -1)
2057 {
2058 prevval = key[len];
2059 key[len] = NUL;
2060 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002061 else
2062 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002063 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2064 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002065 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002066 || !valid_varname(key);
2067 if (len != -1)
2068 key[len] = prevval;
2069 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002070 return NULL;
2071 }
2072
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002073 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002074 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002075 /* Can't add "v:" variable. */
2076 if (lp->ll_dict == &vimvardict)
2077 {
2078 EMSG2(_(e_illvar), name);
2079 return NULL;
2080 }
2081
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002082 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002083 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002084 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002085 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002086 EMSG2(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002087 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002088 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002089 }
2090 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002091 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002092 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002093 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002094 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002095 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002096 p = NULL;
2097 break;
2098 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002099 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002100 else if ((flags & GLV_READ_ONLY) == 0
2101 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002102 {
2103 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002104 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002105 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002106
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002107 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002108 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002109 }
2110 else
2111 {
2112 /*
2113 * Get the number and item for the only or first index of the List.
2114 */
2115 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002116 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002117 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002118 /* is number or string */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002119 lp->ll_n1 = (long)get_tv_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002120 clear_tv(&var1);
2121
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002122 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002123 lp->ll_list = lp->ll_tv->vval.v_list;
2124 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2125 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002126 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002127 if (lp->ll_n1 < 0)
2128 {
2129 lp->ll_n1 = 0;
2130 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2131 }
2132 }
2133 if (lp->ll_li == NULL)
2134 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002135 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002136 if (!quiet)
2137 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002138 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002139 }
2140
2141 /*
2142 * May need to find the item or absolute index for the second
2143 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002144 * When no index given: "lp->ll_empty2" is TRUE.
2145 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002146 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002147 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002148 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002149 lp->ll_n2 = (long)get_tv_number(&var2);
2150 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002151 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002152 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002153 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002154 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002155 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002156 {
2157 if (!quiet)
2158 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002159 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002160 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002161 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002162 }
2163
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002164 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2165 if (lp->ll_n1 < 0)
2166 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2167 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002168 {
2169 if (!quiet)
2170 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002171 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002172 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002173 }
2174
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002175 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002176 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002177 }
2178
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002179 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002180 return p;
2181}
2182
2183/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002184 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002185 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002186 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002187clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002188{
2189 vim_free(lp->ll_exp_name);
2190 vim_free(lp->ll_newkey);
2191}
2192
2193/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002194 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002195 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002196 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002197 */
2198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002199set_var_lval(
2200 lval_T *lp,
2201 char_u *endp,
2202 typval_T *rettv,
2203 int copy,
2204 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002205{
2206 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002207 listitem_T *ri;
2208 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002209
2210 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002212 cc = *endp;
2213 *endp = NUL;
2214 if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002215 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002216 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002217
Bram Moolenaar79518e22017-02-17 16:31:35 +01002218 /* handle +=, -= and .= */
2219 di = NULL;
2220 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2221 &tv, &di, TRUE, FALSE) == OK)
2222 {
2223 if ((di == NULL
2224 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2225 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2226 FALSE)))
2227 && tv_op(&tv, rettv, op) == OK)
2228 set_var(lp->ll_name, &tv, FALSE);
2229 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002230 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002231 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002232 else
2233 set_var(lp->ll_name, rettv, copy);
2234 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002235 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002236 else if (tv_check_lock(lp->ll_newkey == NULL
2237 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002238 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002239 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002240 else if (lp->ll_range)
2241 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002242 listitem_T *ll_li = lp->ll_li;
2243 int ll_n1 = lp->ll_n1;
2244
2245 /*
2246 * Check whether any of the list items is locked
2247 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002248 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002249 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002250 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002251 return;
2252 ri = ri->li_next;
2253 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2254 break;
2255 ll_li = ll_li->li_next;
2256 ++ll_n1;
2257 }
2258
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002259 /*
2260 * Assign the List values to the list items.
2261 */
2262 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002263 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002264 if (op != NULL && *op != '=')
2265 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2266 else
2267 {
2268 clear_tv(&lp->ll_li->li_tv);
2269 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2270 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002271 ri = ri->li_next;
2272 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2273 break;
2274 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002275 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002276 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002277 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002278 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002279 ri = NULL;
2280 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002281 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002282 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002283 lp->ll_li = lp->ll_li->li_next;
2284 ++lp->ll_n1;
2285 }
2286 if (ri != NULL)
2287 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002288 else if (lp->ll_empty2
2289 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002290 : lp->ll_n1 != lp->ll_n2)
2291 EMSG(_("E711: List value has not enough items"));
2292 }
2293 else
2294 {
2295 /*
2296 * Assign to a List or Dictionary item.
2297 */
2298 if (lp->ll_newkey != NULL)
2299 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002300 if (op != NULL && *op != '=')
2301 {
2302 EMSG2(_(e_letwrong), op);
2303 return;
2304 }
2305
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002306 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002307 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002308 if (di == NULL)
2309 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002310 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2311 {
2312 vim_free(di);
2313 return;
2314 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002315 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002316 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002317 else if (op != NULL && *op != '=')
2318 {
2319 tv_op(lp->ll_tv, rettv, op);
2320 return;
2321 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002322 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002323 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002324
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002325 /*
2326 * Assign the value to the variable or list item.
2327 */
2328 if (copy)
2329 copy_tv(rettv, lp->ll_tv);
2330 else
2331 {
2332 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002333 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002334 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002335 }
2336 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002337}
2338
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002339/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002340 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2341 * Returns OK or FAIL.
2342 */
2343 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002344tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002345{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002346 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002347 char_u numbuf[NUMBUFLEN];
2348 char_u *s;
2349
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002350 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2351 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2352 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002353 {
2354 switch (tv1->v_type)
2355 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002356 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002357 case VAR_DICT:
2358 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002359 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002360 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002361 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002362 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002363 break;
2364
2365 case VAR_LIST:
2366 if (*op != '+' || tv2->v_type != VAR_LIST)
2367 break;
2368 /* List += List */
2369 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2370 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2371 return OK;
2372
2373 case VAR_NUMBER:
2374 case VAR_STRING:
2375 if (tv2->v_type == VAR_LIST)
2376 break;
2377 if (*op == '+' || *op == '-')
2378 {
2379 /* nr += nr or nr -= nr*/
2380 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002381#ifdef FEAT_FLOAT
2382 if (tv2->v_type == VAR_FLOAT)
2383 {
2384 float_T f = n;
2385
2386 if (*op == '+')
2387 f += tv2->vval.v_float;
2388 else
2389 f -= tv2->vval.v_float;
2390 clear_tv(tv1);
2391 tv1->v_type = VAR_FLOAT;
2392 tv1->vval.v_float = f;
2393 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002394 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002395#endif
2396 {
2397 if (*op == '+')
2398 n += get_tv_number(tv2);
2399 else
2400 n -= get_tv_number(tv2);
2401 clear_tv(tv1);
2402 tv1->v_type = VAR_NUMBER;
2403 tv1->vval.v_number = n;
2404 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002405 }
2406 else
2407 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002408 if (tv2->v_type == VAR_FLOAT)
2409 break;
2410
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002411 /* str .= str */
2412 s = get_tv_string(tv1);
2413 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2414 clear_tv(tv1);
2415 tv1->v_type = VAR_STRING;
2416 tv1->vval.v_string = s;
2417 }
2418 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002419
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002420 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002421#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002422 {
2423 float_T f;
2424
2425 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2426 && tv2->v_type != VAR_NUMBER
2427 && tv2->v_type != VAR_STRING))
2428 break;
2429 if (tv2->v_type == VAR_FLOAT)
2430 f = tv2->vval.v_float;
2431 else
2432 f = get_tv_number(tv2);
2433 if (*op == '+')
2434 tv1->vval.v_float += f;
2435 else
2436 tv1->vval.v_float -= f;
2437 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002438#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002439 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002440 }
2441 }
2442
2443 EMSG2(_(e_letwrong), op);
2444 return FAIL;
2445}
2446
2447/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002448 * Evaluate the expression used in a ":for var in expr" command.
2449 * "arg" points to "var".
2450 * Set "*errp" to TRUE for an error, FALSE otherwise;
2451 * Return a pointer that holds the info. Null when there is an error.
2452 */
2453 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002454eval_for_line(
2455 char_u *arg,
2456 int *errp,
2457 char_u **nextcmdp,
2458 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002459{
Bram Moolenaar33570922005-01-25 22:26:29 +00002460 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002461 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002462 typval_T tv;
2463 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002464
2465 *errp = TRUE; /* default: there is an error */
2466
Bram Moolenaar33570922005-01-25 22:26:29 +00002467 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002468 if (fi == NULL)
2469 return NULL;
2470
2471 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2472 if (expr == NULL)
2473 return fi;
2474
2475 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002476 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002477 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002478 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002479 return fi;
2480 }
2481
2482 if (skip)
2483 ++emsg_skip;
2484 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2485 {
2486 *errp = FALSE;
2487 if (!skip)
2488 {
2489 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002490 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002491 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002492 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002493 clear_tv(&tv);
2494 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002495 else if (l == NULL)
2496 {
2497 /* a null list is like an empty list: do nothing */
2498 clear_tv(&tv);
2499 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002500 else
2501 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002502 /* No need to increment the refcount, it's already set for the
2503 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002504 fi->fi_list = l;
2505 list_add_watch(l, &fi->fi_lw);
2506 fi->fi_lw.lw_item = l->lv_first;
2507 }
2508 }
2509 }
2510 if (skip)
2511 --emsg_skip;
2512
2513 return fi;
2514}
2515
2516/*
2517 * Use the first item in a ":for" list. Advance to the next.
2518 * Assign the values to the variable (list). "arg" points to the first one.
2519 * Return TRUE when a valid item was found, FALSE when at end of list or
2520 * something wrong.
2521 */
2522 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002523next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002524{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002525 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002526 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002527 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002528
2529 item = fi->fi_lw.lw_item;
2530 if (item == NULL)
2531 result = FALSE;
2532 else
2533 {
2534 fi->fi_lw.lw_item = item->li_next;
2535 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2536 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2537 }
2538 return result;
2539}
2540
2541/*
2542 * Free the structure used to store info used by ":for".
2543 */
2544 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002545free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002546{
Bram Moolenaar33570922005-01-25 22:26:29 +00002547 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002548
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002549 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002550 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002551 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002552 list_unref(fi->fi_list);
2553 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002554 vim_free(fi);
2555}
2556
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2558
2559 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002560set_context_for_expression(
2561 expand_T *xp,
2562 char_u *arg,
2563 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564{
2565 int got_eq = FALSE;
2566 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002567 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002569 if (cmdidx == CMD_let)
2570 {
2571 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002572 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002573 {
2574 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002575 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002576 {
2577 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002578 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002579 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002580 break;
2581 }
2582 return;
2583 }
2584 }
2585 else
2586 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2587 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 while ((xp->xp_pattern = vim_strpbrk(arg,
2589 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2590 {
2591 c = *xp->xp_pattern;
2592 if (c == '&')
2593 {
2594 c = xp->xp_pattern[1];
2595 if (c == '&')
2596 {
2597 ++xp->xp_pattern;
2598 xp->xp_context = cmdidx != CMD_let || got_eq
2599 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2600 }
2601 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002602 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002604 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2605 xp->xp_pattern += 2;
2606
2607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 }
2609 else if (c == '$')
2610 {
2611 /* environment variable */
2612 xp->xp_context = EXPAND_ENV_VARS;
2613 }
2614 else if (c == '=')
2615 {
2616 got_eq = TRUE;
2617 xp->xp_context = EXPAND_EXPRESSION;
2618 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002619 else if (c == '#'
2620 && xp->xp_context == EXPAND_EXPRESSION)
2621 {
2622 /* Autoload function/variable contains '#'. */
2623 break;
2624 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002625 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 && xp->xp_context == EXPAND_FUNCTIONS
2627 && vim_strchr(xp->xp_pattern, '(') == NULL)
2628 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002629 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 break;
2631 }
2632 else if (cmdidx != CMD_let || got_eq)
2633 {
2634 if (c == '"') /* string */
2635 {
2636 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2637 if (c == '\\' && xp->xp_pattern[1] != NUL)
2638 ++xp->xp_pattern;
2639 xp->xp_context = EXPAND_NOTHING;
2640 }
2641 else if (c == '\'') /* literal string */
2642 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002643 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2645 /* skip */ ;
2646 xp->xp_context = EXPAND_NOTHING;
2647 }
2648 else if (c == '|')
2649 {
2650 if (xp->xp_pattern[1] == '|')
2651 {
2652 ++xp->xp_pattern;
2653 xp->xp_context = EXPAND_EXPRESSION;
2654 }
2655 else
2656 xp->xp_context = EXPAND_COMMANDS;
2657 }
2658 else
2659 xp->xp_context = EXPAND_EXPRESSION;
2660 }
2661 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002662 /* Doesn't look like something valid, expand as an expression
2663 * anyway. */
2664 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 arg = xp->xp_pattern;
2666 if (*arg != NUL)
2667 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2668 /* skip */ ;
2669 }
2670 xp->xp_pattern = arg;
2671}
2672
2673#endif /* FEAT_CMDL_COMPL */
2674
2675/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 * ":unlet[!] var1 ... " command.
2677 */
2678 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002679ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002681 ex_unletlock(eap, eap->arg, 0);
2682}
2683
2684/*
2685 * ":lockvar" and ":unlockvar" commands
2686 */
2687 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002688ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002689{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002691 int deep = 2;
2692
2693 if (eap->forceit)
2694 deep = -1;
2695 else if (vim_isdigit(*arg))
2696 {
2697 deep = getdigits(&arg);
2698 arg = skipwhite(arg);
2699 }
2700
2701 ex_unletlock(eap, arg, deep);
2702}
2703
2704/*
2705 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2706 */
2707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002708ex_unletlock(
2709 exarg_T *eap,
2710 char_u *argstart,
2711 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002712{
2713 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002716 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717
2718 do
2719 {
Bram Moolenaar137374f2018-05-13 15:59:50 +02002720 if (*arg == '$')
2721 {
2722 char_u *name = ++arg;
2723
2724 if (get_env_len(&arg) == 0)
2725 {
2726 EMSG2(_(e_invarg2), name - 1);
2727 return;
2728 }
2729 vim_unsetenv(name);
2730 arg = skipwhite(arg);
2731 continue;
2732 }
2733
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002735 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002736 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002737 if (lv.ll_name == NULL)
2738 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002739 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002740 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 if (name_end != NULL)
2743 {
2744 emsg_severe = TRUE;
2745 EMSG(_(e_trailing));
2746 }
2747 if (!(eap->skip || error))
2748 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 break;
2750 }
2751
2752 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002753 {
2754 if (eap->cmdidx == CMD_unlet)
2755 {
2756 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2757 error = TRUE;
2758 }
2759 else
2760 {
2761 if (do_lock_var(&lv, name_end, deep,
2762 eap->cmdidx == CMD_lockvar) == FAIL)
2763 error = TRUE;
2764 }
2765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002767 if (!eap->skip)
2768 clear_lval(&lv);
2769
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 arg = skipwhite(name_end);
2771 } while (!ends_excmd(*arg));
2772
2773 eap->nextcmd = check_nextcmd(arg);
2774}
2775
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002777do_unlet_var(
2778 lval_T *lp,
2779 char_u *name_end,
2780 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002781{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 int ret = OK;
2783 int cc;
2784
2785 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002787 cc = *name_end;
2788 *name_end = NUL;
2789
2790 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002791 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002794 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002795 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002796 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002797 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002798 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002799 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 else if (lp->ll_range)
2801 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002802 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002803 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01002804 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002805
2806 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
2807 {
2808 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02002809 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002810 return FAIL;
2811 ll_li = li;
2812 ++ll_n1;
2813 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814
2815 /* Delete a range of List items. */
2816 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2817 {
2818 li = lp->ll_li->li_next;
2819 listitem_remove(lp->ll_list, lp->ll_li);
2820 lp->ll_li = li;
2821 ++lp->ll_n1;
2822 }
2823 }
2824 else
2825 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 /* unlet a List item. */
2828 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002831 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 }
2833
2834 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002835}
2836
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837/*
2838 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002839 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 */
2841 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002842do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843{
Bram Moolenaar33570922005-01-25 22:26:29 +00002844 hashtab_T *ht;
2845 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002846 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002847 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002848 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849
Bram Moolenaar33570922005-01-25 22:26:29 +00002850 ht = find_var_ht(name, &varname);
2851 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002853 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002854 if (d == NULL)
2855 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002856 if (ht == &globvarht)
2857 d = &globvardict;
2858 else if (ht == &compat_hashtab)
2859 d = &vimvardict;
2860 else
2861 {
2862 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2863 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2864 }
2865 if (d == NULL)
2866 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01002867 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002868 return FAIL;
2869 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002870 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002871 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002872 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02002873 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002874 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00002875 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002876 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02002877 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002878 || var_check_ro(di->di_flags, name, FALSE)
2879 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01002880 return FAIL;
2881
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002882 delete_var(ht, hi);
2883 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00002884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002886 if (forceit)
2887 return OK;
2888 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 return FAIL;
2890}
2891
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002892/*
2893 * Lock or unlock variable indicated by "lp".
2894 * "deep" is the levels to go (-1 for unlimited);
2895 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2896 */
2897 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002898do_lock_var(
2899 lval_T *lp,
2900 char_u *name_end,
2901 int deep,
2902 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002903{
2904 int ret = OK;
2905 int cc;
2906 dictitem_T *di;
2907
2908 if (deep == 0) /* nothing to do */
2909 return OK;
2910
2911 if (lp->ll_tv == NULL)
2912 {
2913 cc = *name_end;
2914 *name_end = NUL;
2915
2916 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002917 di = find_var(lp->ll_name, NULL, TRUE);
2918 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002919 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01002920 else if ((di->di_flags & DI_FLAGS_FIX)
2921 && di->di_tv.v_type != VAR_DICT
2922 && di->di_tv.v_type != VAR_LIST)
2923 /* For historic reasons this error is not given for a list or dict.
2924 * E.g., the b: dict could be locked/unlocked. */
2925 EMSG2(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002926 else
2927 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002928 if (lock)
2929 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002930 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01002931 di->di_flags &= ~DI_FLAGS_LOCK;
2932 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002933 }
2934 *name_end = cc;
2935 }
2936 else if (lp->ll_range)
2937 {
2938 listitem_T *li = lp->ll_li;
2939
2940 /* (un)lock a range of List items. */
2941 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2942 {
2943 item_lock(&li->li_tv, deep, lock);
2944 li = li->li_next;
2945 ++lp->ll_n1;
2946 }
2947 }
2948 else if (lp->ll_list != NULL)
2949 /* (un)lock a List item. */
2950 item_lock(&lp->ll_li->li_tv, deep, lock);
2951 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02002952 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002953 item_lock(&lp->ll_di->di_tv, deep, lock);
2954
2955 return ret;
2956}
2957
2958/*
2959 * Lock or unlock an item. "deep" is nr of levels to go.
2960 */
2961 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002962item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002963{
2964 static int recurse = 0;
2965 list_T *l;
2966 listitem_T *li;
2967 dict_T *d;
2968 hashitem_T *hi;
2969 int todo;
2970
2971 if (recurse >= DICT_MAXNEST)
2972 {
2973 EMSG(_("E743: variable nested too deep for (un)lock"));
2974 return;
2975 }
2976 if (deep == 0)
2977 return;
2978 ++recurse;
2979
2980 /* lock/unlock the item itself */
2981 if (lock)
2982 tv->v_lock |= VAR_LOCKED;
2983 else
2984 tv->v_lock &= ~VAR_LOCKED;
2985
2986 switch (tv->v_type)
2987 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01002988 case VAR_UNKNOWN:
2989 case VAR_NUMBER:
2990 case VAR_STRING:
2991 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002992 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002993 case VAR_FLOAT:
2994 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002995 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002996 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002997 break;
2998
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002999 case VAR_LIST:
3000 if ((l = tv->vval.v_list) != NULL)
3001 {
3002 if (lock)
3003 l->lv_lock |= VAR_LOCKED;
3004 else
3005 l->lv_lock &= ~VAR_LOCKED;
3006 if (deep < 0 || deep > 1)
3007 /* recursive: lock/unlock the items the List contains */
3008 for (li = l->lv_first; li != NULL; li = li->li_next)
3009 item_lock(&li->li_tv, deep - 1, lock);
3010 }
3011 break;
3012 case VAR_DICT:
3013 if ((d = tv->vval.v_dict) != NULL)
3014 {
3015 if (lock)
3016 d->dv_lock |= VAR_LOCKED;
3017 else
3018 d->dv_lock &= ~VAR_LOCKED;
3019 if (deep < 0 || deep > 1)
3020 {
3021 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003022 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003023 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3024 {
3025 if (!HASHITEM_EMPTY(hi))
3026 {
3027 --todo;
3028 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3029 }
3030 }
3031 }
3032 }
3033 }
3034 --recurse;
3035}
3036
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3038/*
3039 * Delete all "menutrans_" variables.
3040 */
3041 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003042del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043{
Bram Moolenaar33570922005-01-25 22:26:29 +00003044 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003045 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046
Bram Moolenaar33570922005-01-25 22:26:29 +00003047 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003048 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003049 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003050 {
3051 if (!HASHITEM_EMPTY(hi))
3052 {
3053 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003054 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3055 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003056 }
3057 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003058 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059}
3060#endif
3061
3062#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3063
3064/*
3065 * Local string buffer for the next two functions to store a variable name
3066 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3067 * get_user_var_name().
3068 */
3069
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003070static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071
3072static char_u *varnamebuf = NULL;
3073static int varnamebuflen = 0;
3074
3075/*
3076 * Function to concatenate a prefix and a variable name.
3077 */
3078 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003079cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080{
3081 int len;
3082
3083 len = (int)STRLEN(name) + 3;
3084 if (len > varnamebuflen)
3085 {
3086 vim_free(varnamebuf);
3087 len += 10; /* some additional space */
3088 varnamebuf = alloc(len);
3089 if (varnamebuf == NULL)
3090 {
3091 varnamebuflen = 0;
3092 return NULL;
3093 }
3094 varnamebuflen = len;
3095 }
3096 *varnamebuf = prefix;
3097 varnamebuf[1] = ':';
3098 STRCPY(varnamebuf + 2, name);
3099 return varnamebuf;
3100}
3101
3102/*
3103 * Function given to ExpandGeneric() to obtain the list of user defined
3104 * (global/buffer/window/built-in) variable names.
3105 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003107get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003109 static long_u gdone;
3110 static long_u bdone;
3111 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003112 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003113 static int vidx;
3114 static hashitem_T *hi;
3115 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116
3117 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003118 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003119 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003120 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003121 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003122
3123 /* Global variables */
3124 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003126 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003127 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003128 else
3129 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003130 while (HASHITEM_EMPTY(hi))
3131 ++hi;
3132 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3133 return cat_prefix_varname('g', hi->hi_key);
3134 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003136
3137 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003138 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003139 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003141 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003142 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003143 else
3144 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003145 while (HASHITEM_EMPTY(hi))
3146 ++hi;
3147 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003149
3150 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003151 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003152 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003154 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003155 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003156 else
3157 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003158 while (HASHITEM_EMPTY(hi))
3159 ++hi;
3160 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003162
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003163 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003164 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003165 if (tdone < ht->ht_used)
3166 {
3167 if (tdone++ == 0)
3168 hi = ht->ht_array;
3169 else
3170 ++hi;
3171 while (HASHITEM_EMPTY(hi))
3172 ++hi;
3173 return cat_prefix_varname('t', hi->hi_key);
3174 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003175
Bram Moolenaar33570922005-01-25 22:26:29 +00003176 /* v: variables */
3177 if (vidx < VV_LEN)
3178 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179
Bram Moolenaard23a8232018-02-10 18:45:26 +01003180 VIM_CLEAR(varnamebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 varnamebuflen = 0;
3182 return NULL;
3183}
3184
3185#endif /* FEAT_CMDL_COMPL */
3186
3187/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003188 * Return TRUE if "pat" matches "text".
3189 * Does not use 'cpo' and always uses 'magic'.
3190 */
3191 static int
3192pattern_match(char_u *pat, char_u *text, int ic)
3193{
3194 int matches = FALSE;
3195 char_u *save_cpo;
3196 regmatch_T regmatch;
3197
3198 /* avoid 'l' flag in 'cpoptions' */
3199 save_cpo = p_cpo;
3200 p_cpo = (char_u *)"";
3201 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3202 if (regmatch.regprog != NULL)
3203 {
3204 regmatch.rm_ic = ic;
3205 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3206 vim_regfree(regmatch.regprog);
3207 }
3208 p_cpo = save_cpo;
3209 return matches;
3210}
3211
3212/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003214 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3216 */
3217
3218/*
3219 * Handle zero level expression.
3220 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003221 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003222 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 * Return OK or FAIL.
3224 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003225 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003226eval0(
3227 char_u *arg,
3228 typval_T *rettv,
3229 char_u **nextcmd,
3230 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231{
3232 int ret;
3233 char_u *p;
3234
3235 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003236 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 if (ret == FAIL || !ends_excmd(*p))
3238 {
3239 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003240 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 /*
3242 * Report the invalid expression unless the expression evaluation has
3243 * been cancelled due to an aborting error, an interrupt, or an
3244 * exception.
3245 */
3246 if (!aborting())
3247 EMSG2(_(e_invexpr2), arg);
3248 ret = FAIL;
3249 }
3250 if (nextcmd != NULL)
3251 *nextcmd = check_nextcmd(p);
3252
3253 return ret;
3254}
3255
3256/*
3257 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003258 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 *
3260 * "arg" must point to the first non-white of the expression.
3261 * "arg" is advanced to the next non-white after the recognized expression.
3262 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003263 * Note: "rettv.v_lock" is not set.
3264 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 * Return OK or FAIL.
3266 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003267 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003268eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269{
3270 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003271 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272
3273 /*
3274 * Get the first variable.
3275 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003276 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 return FAIL;
3278
3279 if ((*arg)[0] == '?')
3280 {
3281 result = FALSE;
3282 if (evaluate)
3283 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003284 int error = FALSE;
3285
3286 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003288 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003289 if (error)
3290 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 }
3292
3293 /*
3294 * Get the second variable.
3295 */
3296 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003297 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 return FAIL;
3299
3300 /*
3301 * Check for the ":".
3302 */
3303 if ((*arg)[0] != ':')
3304 {
3305 EMSG(_("E109: Missing ':' after '?'"));
3306 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003307 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 return FAIL;
3309 }
3310
3311 /*
3312 * Get the third variable.
3313 */
3314 *arg = skipwhite(*arg + 1);
3315 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3316 {
3317 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003318 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 return FAIL;
3320 }
3321 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003322 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 }
3324
3325 return OK;
3326}
3327
3328/*
3329 * Handle first level expression:
3330 * expr2 || expr2 || expr2 logical OR
3331 *
3332 * "arg" must point to the first non-white of the expression.
3333 * "arg" is advanced to the next non-white after the recognized expression.
3334 *
3335 * Return OK or FAIL.
3336 */
3337 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003338eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339{
Bram Moolenaar33570922005-01-25 22:26:29 +00003340 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 long result;
3342 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003343 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344
3345 /*
3346 * Get the first variable.
3347 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003348 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 return FAIL;
3350
3351 /*
3352 * Repeat until there is no following "||".
3353 */
3354 first = TRUE;
3355 result = FALSE;
3356 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3357 {
3358 if (evaluate && first)
3359 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003360 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003362 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003363 if (error)
3364 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 first = FALSE;
3366 }
3367
3368 /*
3369 * Get the second variable.
3370 */
3371 *arg = skipwhite(*arg + 2);
3372 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3373 return FAIL;
3374
3375 /*
3376 * Compute the result.
3377 */
3378 if (evaluate && !result)
3379 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003380 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003382 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003383 if (error)
3384 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 }
3386 if (evaluate)
3387 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003388 rettv->v_type = VAR_NUMBER;
3389 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 }
3391 }
3392
3393 return OK;
3394}
3395
3396/*
3397 * Handle second level expression:
3398 * expr3 && expr3 && expr3 logical AND
3399 *
3400 * "arg" must point to the first non-white of the expression.
3401 * "arg" is advanced to the next non-white after the recognized expression.
3402 *
3403 * Return OK or FAIL.
3404 */
3405 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003406eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407{
Bram Moolenaar33570922005-01-25 22:26:29 +00003408 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 long result;
3410 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003411 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412
3413 /*
3414 * Get the first variable.
3415 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003416 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 return FAIL;
3418
3419 /*
3420 * Repeat until there is no following "&&".
3421 */
3422 first = TRUE;
3423 result = TRUE;
3424 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3425 {
3426 if (evaluate && first)
3427 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003428 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003430 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003431 if (error)
3432 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 first = FALSE;
3434 }
3435
3436 /*
3437 * Get the second variable.
3438 */
3439 *arg = skipwhite(*arg + 2);
3440 if (eval4(arg, &var2, evaluate && result) == FAIL)
3441 return FAIL;
3442
3443 /*
3444 * Compute the result.
3445 */
3446 if (evaluate && result)
3447 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003448 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003450 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003451 if (error)
3452 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 }
3454 if (evaluate)
3455 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003456 rettv->v_type = VAR_NUMBER;
3457 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 }
3459 }
3460
3461 return OK;
3462}
3463
3464/*
3465 * Handle third level expression:
3466 * var1 == var2
3467 * var1 =~ var2
3468 * var1 != var2
3469 * var1 !~ var2
3470 * var1 > var2
3471 * var1 >= var2
3472 * var1 < var2
3473 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003474 * var1 is var2
3475 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 *
3477 * "arg" must point to the first non-white of the expression.
3478 * "arg" is advanced to the next non-white after the recognized expression.
3479 *
3480 * Return OK or FAIL.
3481 */
3482 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003483eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484{
Bram Moolenaar33570922005-01-25 22:26:29 +00003485 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 char_u *p;
3487 int i;
3488 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003489 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492
3493 /*
3494 * Get the first variable.
3495 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003496 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 return FAIL;
3498
3499 p = *arg;
3500 switch (p[0])
3501 {
3502 case '=': if (p[1] == '=')
3503 type = TYPE_EQUAL;
3504 else if (p[1] == '~')
3505 type = TYPE_MATCH;
3506 break;
3507 case '!': if (p[1] == '=')
3508 type = TYPE_NEQUAL;
3509 else if (p[1] == '~')
3510 type = TYPE_NOMATCH;
3511 break;
3512 case '>': if (p[1] != '=')
3513 {
3514 type = TYPE_GREATER;
3515 len = 1;
3516 }
3517 else
3518 type = TYPE_GEQUAL;
3519 break;
3520 case '<': if (p[1] != '=')
3521 {
3522 type = TYPE_SMALLER;
3523 len = 1;
3524 }
3525 else
3526 type = TYPE_SEQUAL;
3527 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003528 case 'i': if (p[1] == 's')
3529 {
3530 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3531 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003532 i = p[len];
3533 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003534 {
3535 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3536 type_is = TRUE;
3537 }
3538 }
3539 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 }
3541
3542 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003543 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 */
3545 if (type != TYPE_UNKNOWN)
3546 {
3547 /* extra question mark appended: ignore case */
3548 if (p[len] == '?')
3549 {
3550 ic = TRUE;
3551 ++len;
3552 }
3553 /* extra '#' appended: match case */
3554 else if (p[len] == '#')
3555 {
3556 ic = FALSE;
3557 ++len;
3558 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003559 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 else
3561 ic = p_ic;
3562
3563 /*
3564 * Get the second variable.
3565 */
3566 *arg = skipwhite(p + len);
3567 if (eval5(arg, &var2, evaluate) == FAIL)
3568 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003569 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 return FAIL;
3571 }
Bram Moolenaar31988702018-02-13 12:57:42 +01003572 if (evaluate)
3573 {
3574 int ret = typval_compare(rettv, &var2, type, type_is, ic);
3575
3576 clear_tv(&var2);
3577 return ret;
3578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 }
3580
3581 return OK;
3582}
3583
3584/*
3585 * Handle fourth level expression:
3586 * + number addition
3587 * - number subtraction
3588 * . string concatenation
3589 *
3590 * "arg" must point to the first non-white of the expression.
3591 * "arg" is advanced to the next non-white after the recognized expression.
3592 *
3593 * Return OK or FAIL.
3594 */
3595 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003596eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597{
Bram Moolenaar33570922005-01-25 22:26:29 +00003598 typval_T var2;
3599 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003601 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003602#ifdef FEAT_FLOAT
3603 float_T f1 = 0, f2 = 0;
3604#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 char_u *s1, *s2;
3606 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3607 char_u *p;
3608
3609 /*
3610 * Get the first variable.
3611 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003612 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 return FAIL;
3614
3615 /*
3616 * Repeat computing, until no '+', '-' or '.' is following.
3617 */
3618 for (;;)
3619 {
3620 op = **arg;
3621 if (op != '+' && op != '-' && op != '.')
3622 break;
3623
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003624 if ((op != '+' || rettv->v_type != VAR_LIST)
3625#ifdef FEAT_FLOAT
3626 && (op == '.' || rettv->v_type != VAR_FLOAT)
3627#endif
3628 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003629 {
3630 /* For "list + ...", an illegal use of the first operand as
3631 * a number cannot be determined before evaluating the 2nd
3632 * operand: if this is also a list, all is ok.
3633 * For "something . ...", "something - ..." or "non-list + ...",
3634 * we know that the first operand needs to be a string or number
3635 * without evaluating the 2nd operand. So check before to avoid
3636 * side effects after an error. */
3637 if (evaluate && get_tv_string_chk(rettv) == NULL)
3638 {
3639 clear_tv(rettv);
3640 return FAIL;
3641 }
3642 }
3643
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 /*
3645 * Get the second variable.
3646 */
3647 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003648 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003650 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 return FAIL;
3652 }
3653
3654 if (evaluate)
3655 {
3656 /*
3657 * Compute the result.
3658 */
3659 if (op == '.')
3660 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003661 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
3662 s2 = get_tv_string_buf_chk(&var2, buf2);
3663 if (s2 == NULL) /* type error ? */
3664 {
3665 clear_tv(rettv);
3666 clear_tv(&var2);
3667 return FAIL;
3668 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003669 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003670 clear_tv(rettv);
3671 rettv->v_type = VAR_STRING;
3672 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003674 else if (op == '+' && rettv->v_type == VAR_LIST
3675 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003676 {
3677 /* concatenate Lists */
3678 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3679 &var3) == FAIL)
3680 {
3681 clear_tv(rettv);
3682 clear_tv(&var2);
3683 return FAIL;
3684 }
3685 clear_tv(rettv);
3686 *rettv = var3;
3687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 else
3689 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003690 int error = FALSE;
3691
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003692#ifdef FEAT_FLOAT
3693 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003694 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003695 f1 = rettv->vval.v_float;
3696 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003697 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003698 else
3699#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003700 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003701 n1 = get_tv_number_chk(rettv, &error);
3702 if (error)
3703 {
3704 /* This can only happen for "list + non-list". For
3705 * "non-list + ..." or "something - ...", we returned
3706 * before evaluating the 2nd operand. */
3707 clear_tv(rettv);
3708 return FAIL;
3709 }
3710#ifdef FEAT_FLOAT
3711 if (var2.v_type == VAR_FLOAT)
3712 f1 = n1;
3713#endif
3714 }
3715#ifdef FEAT_FLOAT
3716 if (var2.v_type == VAR_FLOAT)
3717 {
3718 f2 = var2.vval.v_float;
3719 n2 = 0;
3720 }
3721 else
3722#endif
3723 {
3724 n2 = get_tv_number_chk(&var2, &error);
3725 if (error)
3726 {
3727 clear_tv(rettv);
3728 clear_tv(&var2);
3729 return FAIL;
3730 }
3731#ifdef FEAT_FLOAT
3732 if (rettv->v_type == VAR_FLOAT)
3733 f2 = n2;
3734#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003735 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003736 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003737
3738#ifdef FEAT_FLOAT
3739 /* If there is a float on either side the result is a float. */
3740 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3741 {
3742 if (op == '+')
3743 f1 = f1 + f2;
3744 else
3745 f1 = f1 - f2;
3746 rettv->v_type = VAR_FLOAT;
3747 rettv->vval.v_float = f1;
3748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003750#endif
3751 {
3752 if (op == '+')
3753 n1 = n1 + n2;
3754 else
3755 n1 = n1 - n2;
3756 rettv->v_type = VAR_NUMBER;
3757 rettv->vval.v_number = n1;
3758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003760 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 }
3762 }
3763 return OK;
3764}
3765
3766/*
3767 * Handle fifth level expression:
3768 * * number multiplication
3769 * / number division
3770 * % number modulo
3771 *
3772 * "arg" must point to the first non-white of the expression.
3773 * "arg" is advanced to the next non-white after the recognized expression.
3774 *
3775 * Return OK or FAIL.
3776 */
3777 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003778eval6(
3779 char_u **arg,
3780 typval_T *rettv,
3781 int evaluate,
3782 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783{
Bram Moolenaar33570922005-01-25 22:26:29 +00003784 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003786 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003787#ifdef FEAT_FLOAT
3788 int use_float = FALSE;
3789 float_T f1 = 0, f2;
3790#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003791 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792
3793 /*
3794 * Get the first variable.
3795 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003796 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 return FAIL;
3798
3799 /*
3800 * Repeat computing, until no '*', '/' or '%' is following.
3801 */
3802 for (;;)
3803 {
3804 op = **arg;
3805 if (op != '*' && op != '/' && op != '%')
3806 break;
3807
3808 if (evaluate)
3809 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003810#ifdef FEAT_FLOAT
3811 if (rettv->v_type == VAR_FLOAT)
3812 {
3813 f1 = rettv->vval.v_float;
3814 use_float = TRUE;
3815 n1 = 0;
3816 }
3817 else
3818#endif
3819 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003820 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003821 if (error)
3822 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 }
3824 else
3825 n1 = 0;
3826
3827 /*
3828 * Get the second variable.
3829 */
3830 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003831 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 return FAIL;
3833
3834 if (evaluate)
3835 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003836#ifdef FEAT_FLOAT
3837 if (var2.v_type == VAR_FLOAT)
3838 {
3839 if (!use_float)
3840 {
3841 f1 = n1;
3842 use_float = TRUE;
3843 }
3844 f2 = var2.vval.v_float;
3845 n2 = 0;
3846 }
3847 else
3848#endif
3849 {
3850 n2 = get_tv_number_chk(&var2, &error);
3851 clear_tv(&var2);
3852 if (error)
3853 return FAIL;
3854#ifdef FEAT_FLOAT
3855 if (use_float)
3856 f2 = n2;
3857#endif
3858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859
3860 /*
3861 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003862 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003864#ifdef FEAT_FLOAT
3865 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003867 if (op == '*')
3868 f1 = f1 * f2;
3869 else if (op == '/')
3870 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003871# ifdef VMS
3872 /* VMS crashes on divide by zero, work around it */
3873 if (f2 == 0.0)
3874 {
3875 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003876 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003877 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003878 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003879 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003880 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003881 }
3882 else
3883 f1 = f1 / f2;
3884# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003885 /* We rely on the floating point library to handle divide
3886 * by zero to result in "inf" and not a crash. */
3887 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003888# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003891 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00003892 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003893 return FAIL;
3894 }
3895 rettv->v_type = VAR_FLOAT;
3896 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 }
3898 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003901 if (op == '*')
3902 n1 = n1 * n2;
3903 else if (op == '/')
3904 {
3905 if (n2 == 0) /* give an error message? */
3906 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003907 if (n1 == 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003908 n1 = VARNUM_MIN; /* similar to NaN */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003909 else if (n1 < 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003910 n1 = -VARNUM_MAX;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003911 else
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003912 n1 = VARNUM_MAX;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003913 }
3914 else
3915 n1 = n1 / n2;
3916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003918 {
3919 if (n2 == 0) /* give an error message? */
3920 n1 = 0;
3921 else
3922 n1 = n1 % n2;
3923 }
3924 rettv->v_type = VAR_NUMBER;
3925 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 }
3928 }
3929
3930 return OK;
3931}
3932
3933/*
3934 * Handle sixth level expression:
3935 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003936 * "string" string constant
3937 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 * &option-name option value
3939 * @r register contents
3940 * identifier variable value
3941 * function() function call
3942 * $VAR environment variable
3943 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003944 * [expr, expr] List
3945 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 *
3947 * Also handle:
3948 * ! in front logical NOT
3949 * - in front unary minus
3950 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003951 * trailing [] subscript in String or List
3952 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 *
3954 * "arg" must point to the first non-white of the expression.
3955 * "arg" is advanced to the next non-white after the recognized expression.
3956 *
3957 * Return OK or FAIL.
3958 */
3959 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003960eval7(
3961 char_u **arg,
3962 typval_T *rettv,
3963 int evaluate,
3964 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003966 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 int len;
3968 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 char_u *start_leader, *end_leader;
3970 int ret = OK;
3971 char_u *alias;
3972
3973 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003974 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003975 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003977 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978
3979 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003980 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 */
3982 start_leader = *arg;
3983 while (**arg == '!' || **arg == '-' || **arg == '+')
3984 *arg = skipwhite(*arg + 1);
3985 end_leader = *arg;
3986
3987 switch (**arg)
3988 {
3989 /*
3990 * Number constant.
3991 */
3992 case '0':
3993 case '1':
3994 case '2':
3995 case '3':
3996 case '4':
3997 case '5':
3998 case '6':
3999 case '7':
4000 case '8':
4001 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004002 {
4003#ifdef FEAT_FLOAT
4004 char_u *p = skipdigits(*arg + 1);
4005 int get_float = FALSE;
4006
4007 /* We accept a float when the format matches
4008 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004009 * strict to avoid backwards compatibility problems.
4010 * Don't look for a float after the "." operator, so that
4011 * ":let vers = 1.2.3" doesn't fail. */
4012 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004014 get_float = TRUE;
4015 p = skipdigits(p + 2);
4016 if (*p == 'e' || *p == 'E')
4017 {
4018 ++p;
4019 if (*p == '-' || *p == '+')
4020 ++p;
4021 if (!vim_isdigit(*p))
4022 get_float = FALSE;
4023 else
4024 p = skipdigits(p + 1);
4025 }
4026 if (ASCII_ISALPHA(*p) || *p == '.')
4027 get_float = FALSE;
4028 }
4029 if (get_float)
4030 {
4031 float_T f;
4032
4033 *arg += string2float(*arg, &f);
4034 if (evaluate)
4035 {
4036 rettv->v_type = VAR_FLOAT;
4037 rettv->vval.v_float = f;
4038 }
4039 }
4040 else
4041#endif
4042 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01004043 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004044 *arg += len;
4045 if (evaluate)
4046 {
4047 rettv->v_type = VAR_NUMBER;
4048 rettv->vval.v_number = n;
4049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 }
4051 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053
4054 /*
4055 * String constant: "string".
4056 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004057 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 break;
4059
4060 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004061 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004063 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004064 break;
4065
4066 /*
4067 * List: [expr, expr]
4068 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004069 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 break;
4071
4072 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004073 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004074 * Dictionary: {key: val, key: val}
4075 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004076 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4077 if (ret == NOTDONE)
4078 ret = get_dict_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004079 break;
4080
4081 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004082 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004084 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 break;
4086
4087 /*
4088 * Environment variable: $VAR.
4089 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004090 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 break;
4092
4093 /*
4094 * Register contents: @r.
4095 */
4096 case '@': ++*arg;
4097 if (evaluate)
4098 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004099 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004100 rettv->vval.v_string = get_reg_contents(**arg,
4101 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 }
4103 if (**arg != NUL)
4104 ++*arg;
4105 break;
4106
4107 /*
4108 * nested expression: (expression).
4109 */
4110 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004111 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 if (**arg == ')')
4113 ++*arg;
4114 else if (ret == OK)
4115 {
4116 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004117 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 ret = FAIL;
4119 }
4120 break;
4121
Bram Moolenaar8c711452005-01-14 21:53:12 +00004122 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 break;
4124 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004125
4126 if (ret == NOTDONE)
4127 {
4128 /*
4129 * Must be a variable or function name.
4130 * Can also be a curly-braces kind of name: {expr}.
4131 */
4132 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004133 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004134 if (alias != NULL)
4135 s = alias;
4136
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004137 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004138 ret = FAIL;
4139 else
4140 {
4141 if (**arg == '(') /* recursive! */
4142 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004143 partial_T *partial;
4144
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004145 if (!evaluate)
4146 check_vars(s, len);
4147
Bram Moolenaar8c711452005-01-14 21:53:12 +00004148 /* If "s" is the name of a variable of type VAR_FUNC
4149 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004150 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004151
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004152 /* Need to make a copy, in case evaluating the arguments makes
4153 * the name invalid. */
4154 s = vim_strsave(s);
4155 if (s == NULL)
4156 ret = FAIL;
4157 else
4158 /* Invoke the function. */
4159 ret = get_func_tv(s, len, rettv, arg,
4160 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4161 &len, evaluate, partial, NULL);
4162 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004163
4164 /* If evaluate is FALSE rettv->v_type was not set in
4165 * get_func_tv, but it's needed in handle_subscript() to parse
4166 * what follows. So set it here. */
4167 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4168 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004169 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004170 rettv->v_type = VAR_FUNC;
4171 }
4172
Bram Moolenaar8c711452005-01-14 21:53:12 +00004173 /* Stop the expression evaluation when immediately
4174 * aborting on error, or when an interrupt occurred or
4175 * an exception was thrown but not caught. */
4176 if (aborting())
4177 {
4178 if (ret == OK)
4179 clear_tv(rettv);
4180 ret = FAIL;
4181 }
4182 }
4183 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004184 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004185 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004186 {
4187 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004188 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004189 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004190 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004191 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004192 }
4193
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 *arg = skipwhite(*arg);
4195
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004196 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4197 * expr(expr). */
4198 if (ret == OK)
4199 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200
4201 /*
4202 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4203 */
4204 if (ret == OK && evaluate && end_leader > start_leader)
4205 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004206 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004207 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004208#ifdef FEAT_FLOAT
4209 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004210
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004211 if (rettv->v_type == VAR_FLOAT)
4212 f = rettv->vval.v_float;
4213 else
4214#endif
4215 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004216 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004218 clear_tv(rettv);
4219 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004221 else
4222 {
4223 while (end_leader > start_leader)
4224 {
4225 --end_leader;
4226 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004227 {
4228#ifdef FEAT_FLOAT
4229 if (rettv->v_type == VAR_FLOAT)
4230 f = !f;
4231 else
4232#endif
4233 val = !val;
4234 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004235 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004236 {
4237#ifdef FEAT_FLOAT
4238 if (rettv->v_type == VAR_FLOAT)
4239 f = -f;
4240 else
4241#endif
4242 val = -val;
4243 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004244 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004245#ifdef FEAT_FLOAT
4246 if (rettv->v_type == VAR_FLOAT)
4247 {
4248 clear_tv(rettv);
4249 rettv->vval.v_float = f;
4250 }
4251 else
4252#endif
4253 {
4254 clear_tv(rettv);
4255 rettv->v_type = VAR_NUMBER;
4256 rettv->vval.v_number = val;
4257 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 }
4260
4261 return ret;
4262}
4263
4264/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004265 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4266 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004267 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4268 */
4269 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004270eval_index(
4271 char_u **arg,
4272 typval_T *rettv,
4273 int evaluate,
4274 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004275{
4276 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004277 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004278 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004279 long len = -1;
4280 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004281 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004282 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004283
Bram Moolenaara03f2332016-02-06 18:09:59 +01004284 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004285 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004286 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004287 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004288 if (verbose)
4289 EMSG(_("E695: Cannot index a Funcref"));
4290 return FAIL;
4291 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004292#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004293 if (verbose)
4294 EMSG(_(e_float_as_string));
4295 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004296#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004297 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004298 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004299 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004300 if (verbose)
4301 EMSG(_("E909: Cannot index a special variable"));
4302 return FAIL;
4303 case VAR_UNKNOWN:
4304 if (evaluate)
4305 return FAIL;
4306 /* FALLTHROUGH */
4307
4308 case VAR_STRING:
4309 case VAR_NUMBER:
4310 case VAR_LIST:
4311 case VAR_DICT:
4312 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004313 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004314
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004315 init_tv(&var1);
4316 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004317 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004318 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004319 /*
4320 * dict.name
4321 */
4322 key = *arg + 1;
4323 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4324 ;
4325 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004326 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004327 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004328 }
4329 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004330 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004331 /*
4332 * something[idx]
4333 *
4334 * Get the (first) variable from inside the [].
4335 */
4336 *arg = skipwhite(*arg + 1);
4337 if (**arg == ':')
4338 empty1 = TRUE;
4339 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4340 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004341 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4342 {
4343 /* not a number or string */
4344 clear_tv(&var1);
4345 return FAIL;
4346 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004347
4348 /*
4349 * Get the second variable from inside the [:].
4350 */
4351 if (**arg == ':')
4352 {
4353 range = TRUE;
4354 *arg = skipwhite(*arg + 1);
4355 if (**arg == ']')
4356 empty2 = TRUE;
4357 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4358 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004359 if (!empty1)
4360 clear_tv(&var1);
4361 return FAIL;
4362 }
4363 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4364 {
4365 /* not a number or string */
4366 if (!empty1)
4367 clear_tv(&var1);
4368 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004369 return FAIL;
4370 }
4371 }
4372
4373 /* Check for the ']'. */
4374 if (**arg != ']')
4375 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004376 if (verbose)
4377 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004378 clear_tv(&var1);
4379 if (range)
4380 clear_tv(&var2);
4381 return FAIL;
4382 }
4383 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004384 }
4385
4386 if (evaluate)
4387 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004388 n1 = 0;
4389 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004390 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004391 n1 = get_tv_number(&var1);
4392 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004393 }
4394 if (range)
4395 {
4396 if (empty2)
4397 n2 = -1;
4398 else
4399 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004400 n2 = get_tv_number(&var2);
4401 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004402 }
4403 }
4404
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004405 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004406 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004407 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004408 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004409 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004410 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004411 case VAR_SPECIAL:
4412 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004413 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004414 break; /* not evaluating, skipping over subscript */
4415
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004416 case VAR_NUMBER:
4417 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004418 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004419 len = (long)STRLEN(s);
4420 if (range)
4421 {
4422 /* The resulting variable is a substring. If the indexes
4423 * are out of range the result is empty. */
4424 if (n1 < 0)
4425 {
4426 n1 = len + n1;
4427 if (n1 < 0)
4428 n1 = 0;
4429 }
4430 if (n2 < 0)
4431 n2 = len + n2;
4432 else if (n2 >= len)
4433 n2 = len;
4434 if (n1 >= len || n2 < 0 || n1 > n2)
4435 s = NULL;
4436 else
4437 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4438 }
4439 else
4440 {
4441 /* The resulting variable is a string of a single
4442 * character. If the index is too big or negative the
4443 * result is empty. */
4444 if (n1 >= len || n1 < 0)
4445 s = NULL;
4446 else
4447 s = vim_strnsave(s + n1, 1);
4448 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004449 clear_tv(rettv);
4450 rettv->v_type = VAR_STRING;
4451 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004452 break;
4453
4454 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004455 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004456 if (n1 < 0)
4457 n1 = len + n1;
4458 if (!empty1 && (n1 < 0 || n1 >= len))
4459 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004460 /* For a range we allow invalid values and return an empty
4461 * list. A list index out of range is an error. */
4462 if (!range)
4463 {
4464 if (verbose)
4465 EMSGN(_(e_listidx), n1);
4466 return FAIL;
4467 }
4468 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004469 }
4470 if (range)
4471 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004472 list_T *l;
4473 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004474
4475 if (n2 < 0)
4476 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004477 else if (n2 >= len)
4478 n2 = len - 1;
4479 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004480 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004481 l = list_alloc();
4482 if (l == NULL)
4483 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004484 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004485 n1 <= n2; ++n1)
4486 {
4487 if (list_append_tv(l, &item->li_tv) == FAIL)
4488 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004489 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004490 return FAIL;
4491 }
4492 item = item->li_next;
4493 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004494 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004495 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004496 }
4497 else
4498 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004499 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004500 clear_tv(rettv);
4501 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004502 }
4503 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004504
4505 case VAR_DICT:
4506 if (range)
4507 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004508 if (verbose)
4509 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004510 if (len == -1)
4511 clear_tv(&var1);
4512 return FAIL;
4513 }
4514 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004515 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004516
4517 if (len == -1)
4518 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02004519 key = get_tv_string_chk(&var1);
4520 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004521 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004522 clear_tv(&var1);
4523 return FAIL;
4524 }
4525 }
4526
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004527 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004528
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004529 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004530 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004531 if (len == -1)
4532 clear_tv(&var1);
4533 if (item == NULL)
4534 return FAIL;
4535
4536 copy_tv(&item->di_tv, &var1);
4537 clear_tv(rettv);
4538 *rettv = var1;
4539 }
4540 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004541 }
4542 }
4543
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004544 return OK;
4545}
4546
4547/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 * Get an option value.
4549 * "arg" points to the '&' or '+' before the option name.
4550 * "arg" is advanced to character after the option name.
4551 * Return OK or FAIL.
4552 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004553 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004554get_option_tv(
4555 char_u **arg,
4556 typval_T *rettv, /* when NULL, only check if option exists */
4557 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558{
4559 char_u *option_end;
4560 long numval;
4561 char_u *stringval;
4562 int opt_type;
4563 int c;
4564 int working = (**arg == '+'); /* has("+option") */
4565 int ret = OK;
4566 int opt_flags;
4567
4568 /*
4569 * Isolate the option name and find its value.
4570 */
4571 option_end = find_option_end(arg, &opt_flags);
4572 if (option_end == NULL)
4573 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004574 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 EMSG2(_("E112: Option name missing: %s"), *arg);
4576 return FAIL;
4577 }
4578
4579 if (!evaluate)
4580 {
4581 *arg = option_end;
4582 return OK;
4583 }
4584
4585 c = *option_end;
4586 *option_end = NUL;
4587 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004588 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589
4590 if (opt_type == -3) /* invalid name */
4591 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004592 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 EMSG2(_("E113: Unknown option: %s"), *arg);
4594 ret = FAIL;
4595 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004596 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 {
4598 if (opt_type == -2) /* hidden string option */
4599 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004600 rettv->v_type = VAR_STRING;
4601 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 }
4603 else if (opt_type == -1) /* hidden number option */
4604 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004605 rettv->v_type = VAR_NUMBER;
4606 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 }
4608 else if (opt_type == 1) /* number option */
4609 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004610 rettv->v_type = VAR_NUMBER;
4611 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 }
4613 else /* string option */
4614 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004615 rettv->v_type = VAR_STRING;
4616 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 }
4618 }
4619 else if (working && (opt_type == -2 || opt_type == -1))
4620 ret = FAIL;
4621
4622 *option_end = c; /* put back for error messages */
4623 *arg = option_end;
4624
4625 return ret;
4626}
4627
4628/*
4629 * Allocate a variable for a string constant.
4630 * Return OK or FAIL.
4631 */
4632 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004633get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634{
4635 char_u *p;
4636 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 int extra = 0;
4638
4639 /*
4640 * Find the end of the string, skipping backslashed characters.
4641 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004642 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 {
4644 if (*p == '\\' && p[1] != NUL)
4645 {
4646 ++p;
4647 /* A "\<x>" form occupies at least 4 characters, and produces up
4648 * to 6 characters: reserve space for 2 extra */
4649 if (*p == '<')
4650 extra += 2;
4651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 }
4653
4654 if (*p != '"')
4655 {
4656 EMSG2(_("E114: Missing quote: %s"), *arg);
4657 return FAIL;
4658 }
4659
4660 /* If only parsing, set *arg and return here */
4661 if (!evaluate)
4662 {
4663 *arg = p + 1;
4664 return OK;
4665 }
4666
4667 /*
4668 * Copy the string into allocated memory, handling backslashed
4669 * characters.
4670 */
4671 name = alloc((unsigned)(p - *arg + extra));
4672 if (name == NULL)
4673 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004674 rettv->v_type = VAR_STRING;
4675 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676
Bram Moolenaar8c711452005-01-14 21:53:12 +00004677 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 {
4679 if (*p == '\\')
4680 {
4681 switch (*++p)
4682 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004683 case 'b': *name++ = BS; ++p; break;
4684 case 'e': *name++ = ESC; ++p; break;
4685 case 'f': *name++ = FF; ++p; break;
4686 case 'n': *name++ = NL; ++p; break;
4687 case 'r': *name++ = CAR; ++p; break;
4688 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689
4690 case 'X': /* hex: "\x1", "\x12" */
4691 case 'x':
4692 case 'u': /* Unicode: "\u0023" */
4693 case 'U':
4694 if (vim_isxdigit(p[1]))
4695 {
4696 int n, nr;
4697 int c = toupper(*p);
4698
4699 if (c == 'X')
4700 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004701 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004703 else
4704 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 nr = 0;
4706 while (--n >= 0 && vim_isxdigit(p[1]))
4707 {
4708 ++p;
4709 nr = (nr << 4) + hex2nr(*p);
4710 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004711 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712#ifdef FEAT_MBYTE
4713 /* For "\u" store the number according to
4714 * 'encoding'. */
4715 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004716 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 else
4718#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004719 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 break;
4722
4723 /* octal: "\1", "\12", "\123" */
4724 case '0':
4725 case '1':
4726 case '2':
4727 case '3':
4728 case '4':
4729 case '5':
4730 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004731 case '7': *name = *p++ - '0';
4732 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004734 *name = (*name << 3) + *p++ - '0';
4735 if (*p >= '0' && *p <= '7')
4736 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004738 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 break;
4740
4741 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02004742 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 if (extra != 0)
4744 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004745 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 break;
4747 }
4748 /* FALLTHROUGH */
4749
Bram Moolenaar8c711452005-01-14 21:53:12 +00004750 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 break;
4752 }
4753 }
4754 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004755 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004758 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02004759 if (*p != NUL) /* just in case */
4760 ++p;
4761 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 return OK;
4764}
4765
4766/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004767 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 * Return OK or FAIL.
4769 */
4770 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004771get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772{
4773 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004774 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004775 int reduce = 0;
4776
4777 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004778 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004779 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004780 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004781 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004782 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004783 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004784 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004785 break;
4786 ++reduce;
4787 ++p;
4788 }
4789 }
4790
Bram Moolenaar8c711452005-01-14 21:53:12 +00004791 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004792 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004793 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004794 return FAIL;
4795 }
4796
Bram Moolenaar8c711452005-01-14 21:53:12 +00004797 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004798 if (!evaluate)
4799 {
4800 *arg = p + 1;
4801 return OK;
4802 }
4803
4804 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004805 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004806 */
4807 str = alloc((unsigned)((p - *arg) - reduce));
4808 if (str == NULL)
4809 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004810 rettv->v_type = VAR_STRING;
4811 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004812
Bram Moolenaar8c711452005-01-14 21:53:12 +00004813 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004814 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004815 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004816 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004817 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004818 break;
4819 ++p;
4820 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004821 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004822 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004823 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004824 *arg = p + 1;
4825
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004826 return OK;
4827}
4828
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004829/*
4830 * Return the function name of the partial.
4831 */
4832 char_u *
4833partial_name(partial_T *pt)
4834{
4835 if (pt->pt_name != NULL)
4836 return pt->pt_name;
4837 return pt->pt_func->uf_name;
4838}
4839
Bram Moolenaarddecc252016-04-06 22:59:37 +02004840 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004841partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004842{
4843 int i;
4844
4845 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004846 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004847 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004848 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004849 if (pt->pt_name != NULL)
4850 {
4851 func_unref(pt->pt_name);
4852 vim_free(pt->pt_name);
4853 }
4854 else
4855 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004856 vim_free(pt);
4857}
4858
4859/*
4860 * Unreference a closure: decrement the reference count and free it when it
4861 * becomes zero.
4862 */
4863 void
4864partial_unref(partial_T *pt)
4865{
4866 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004867 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004868}
4869
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004870static int tv_equal_recurse_limit;
4871
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004872 static int
4873func_equal(
4874 typval_T *tv1,
4875 typval_T *tv2,
4876 int ic) /* ignore case */
4877{
4878 char_u *s1, *s2;
4879 dict_T *d1, *d2;
4880 int a1, a2;
4881 int i;
4882
4883 /* empty and NULL function name considered the same */
4884 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004885 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004886 if (s1 != NULL && *s1 == NUL)
4887 s1 = NULL;
4888 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004889 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004890 if (s2 != NULL && *s2 == NUL)
4891 s2 = NULL;
4892 if (s1 == NULL || s2 == NULL)
4893 {
4894 if (s1 != s2)
4895 return FALSE;
4896 }
4897 else if (STRCMP(s1, s2) != 0)
4898 return FALSE;
4899
4900 /* empty dict and NULL dict is different */
4901 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
4902 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
4903 if (d1 == NULL || d2 == NULL)
4904 {
4905 if (d1 != d2)
4906 return FALSE;
4907 }
4908 else if (!dict_equal(d1, d2, ic, TRUE))
4909 return FALSE;
4910
4911 /* empty list and no list considered the same */
4912 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
4913 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
4914 if (a1 != a2)
4915 return FALSE;
4916 for (i = 0; i < a1; ++i)
4917 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
4918 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
4919 return FALSE;
4920
4921 return TRUE;
4922}
4923
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004924/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004925 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004926 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004927 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004928 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004929 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004930tv_equal(
4931 typval_T *tv1,
4932 typval_T *tv2,
4933 int ic, /* ignore case */
4934 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004935{
4936 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004937 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004938 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00004939 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004940
Bram Moolenaar8b402a02006-10-17 13:16:39 +00004941 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004942 * recursiveness to a limit. We guess they are equal then.
4943 * A fixed limit has the problem of still taking an awful long time.
4944 * Reduce the limit every time running into it. That should work fine for
4945 * deeply linked structures that are not recursively linked and catch
4946 * recursiveness quickly. */
4947 if (!recursive)
4948 tv_equal_recurse_limit = 1000;
4949 if (recursive_cnt >= tv_equal_recurse_limit)
4950 {
4951 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00004952 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004953 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004954
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02004955 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
4956 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004957 if ((tv1->v_type == VAR_FUNC
4958 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
4959 && (tv2->v_type == VAR_FUNC
4960 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
4961 {
4962 ++recursive_cnt;
4963 r = func_equal(tv1, tv2, ic);
4964 --recursive_cnt;
4965 return r;
4966 }
4967
4968 if (tv1->v_type != tv2->v_type)
4969 return FALSE;
4970
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004971 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004972 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004973 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004974 ++recursive_cnt;
4975 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
4976 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00004977 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004978
4979 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004980 ++recursive_cnt;
4981 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
4982 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00004983 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004984
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004985 case VAR_NUMBER:
4986 return tv1->vval.v_number == tv2->vval.v_number;
4987
4988 case VAR_STRING:
4989 s1 = get_tv_string_buf(tv1, buf1);
4990 s2 = get_tv_string_buf(tv2, buf2);
4991 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004992
4993 case VAR_SPECIAL:
4994 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01004995
4996 case VAR_FLOAT:
4997#ifdef FEAT_FLOAT
4998 return tv1->vval.v_float == tv2->vval.v_float;
4999#endif
5000 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005001#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005002 return tv1->vval.v_job == tv2->vval.v_job;
5003#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005004 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005005#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005006 return tv1->vval.v_channel == tv2->vval.v_channel;
5007#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005008 case VAR_FUNC:
5009 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005010 case VAR_UNKNOWN:
5011 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005012 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005013
Bram Moolenaara03f2332016-02-06 18:09:59 +01005014 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5015 * does not equal anything, not even itself. */
5016 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005017}
5018
5019/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005020 * Return the next (unique) copy ID.
5021 * Used for serializing nested structures.
5022 */
5023 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005024get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005025{
5026 current_copyID += COPYID_INC;
5027 return current_copyID;
5028}
5029
5030/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005031 * Garbage collection for lists and dictionaries.
5032 *
5033 * We use reference counts to be able to free most items right away when they
5034 * are no longer used. But for composite items it's possible that it becomes
5035 * unused while the reference count is > 0: When there is a recursive
5036 * reference. Example:
5037 * :let l = [1, 2, 3]
5038 * :let d = {9: l}
5039 * :let l[1] = d
5040 *
5041 * Since this is quite unusual we handle this with garbage collection: every
5042 * once in a while find out which lists and dicts are not referenced from any
5043 * variable.
5044 *
5045 * Here is a good reference text about garbage collection (refers to Python
5046 * but it applies to all reference-counting mechanisms):
5047 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005048 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005049
5050/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005051 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005052 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005053 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005054 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005055 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005056garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005057{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005058 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005059 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005060 buf_T *buf;
5061 win_T *wp;
5062 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005063 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005064 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005065
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005066 if (!testing)
5067 {
5068 /* Only do this once. */
5069 want_garbage_collect = FALSE;
5070 may_garbage_collect = FALSE;
5071 garbage_collect_at_exit = FALSE;
5072 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005073
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005074 /* We advance by two because we add one for items referenced through
5075 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005076 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005077
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005078 /*
5079 * 1. Go through all accessible variables and mark all lists and dicts
5080 * with copyID.
5081 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005082
5083 /* Don't free variables in the previous_funccal list unless they are only
5084 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005085 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005086 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005087
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005088 /* script-local variables */
5089 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005090 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005091
5092 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005093 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005094 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5095 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005096
5097 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005098 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005099 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5100 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005101 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005102 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5103 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005104
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005105 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005106 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005107 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5108 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005109 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005110 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005111
5112 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005113 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005114
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005115 /* named functions (matters for closures) */
5116 abort = abort || set_ref_in_functions(copyID);
5117
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005118 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005119 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005120
Bram Moolenaard812df62008-11-09 12:46:09 +00005121 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005122 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005123
Bram Moolenaar1dced572012-04-05 16:54:08 +02005124#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005125 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005126#endif
5127
Bram Moolenaardb913952012-06-29 12:54:53 +02005128#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005129 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005130#endif
5131
5132#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005133 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005134#endif
5135
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005136#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005137 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005138 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005139#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005140#ifdef FEAT_NETBEANS_INTG
5141 abort = abort || set_ref_in_nb_channel(copyID);
5142#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005143
Bram Moolenaare3188e22016-05-31 21:13:04 +02005144#ifdef FEAT_TIMERS
5145 abort = abort || set_ref_in_timer(copyID);
5146#endif
5147
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005148#ifdef FEAT_QUICKFIX
5149 abort = abort || set_ref_in_quickfix(copyID);
5150#endif
5151
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005152#ifdef FEAT_TERMINAL
5153 abort = abort || set_ref_in_term(copyID);
5154#endif
5155
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005156 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005157 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005158 /*
5159 * 2. Free lists and dictionaries that are not referenced.
5160 */
5161 did_free = free_unref_items(copyID);
5162
5163 /*
5164 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005165 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005166 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005167 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005168 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005169 else if (p_verbose > 0)
5170 {
5171 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
5172 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005173
5174 return did_free;
5175}
5176
5177/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005178 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005179 */
5180 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005181free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005182{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005183 int did_free = FALSE;
5184
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005185 /* Let all "free" functions know that we are here. This means no
5186 * dictionaries, lists, channels or jobs are to be freed, because we will
5187 * do that here. */
5188 in_free_unref_items = TRUE;
5189
5190 /*
5191 * PASS 1: free the contents of the items. We don't free the items
5192 * themselves yet, so that it is possible to decrement refcount counters
5193 */
5194
Bram Moolenaarcd524592016-07-17 14:57:05 +02005195 /* Go through the list of dicts and free items without the copyID. */
5196 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005197
Bram Moolenaarda861d62016-07-17 15:46:27 +02005198 /* Go through the list of lists and free items without the copyID. */
5199 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005200
5201#ifdef FEAT_JOB_CHANNEL
5202 /* Go through the list of jobs and free items without the copyID. This
5203 * must happen before doing channels, because jobs refer to channels, but
5204 * the reference from the channel to the job isn't tracked. */
5205 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5206
5207 /* Go through the list of channels and free items without the copyID. */
5208 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5209#endif
5210
5211 /*
5212 * PASS 2: free the items themselves.
5213 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005214 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005215 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005216
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005217#ifdef FEAT_JOB_CHANNEL
5218 /* Go through the list of jobs and free items without the copyID. This
5219 * must happen before doing channels, because jobs refer to channels, but
5220 * the reference from the channel to the job isn't tracked. */
5221 free_unused_jobs(copyID, COPYID_MASK);
5222
5223 /* Go through the list of channels and free items without the copyID. */
5224 free_unused_channels(copyID, COPYID_MASK);
5225#endif
5226
5227 in_free_unref_items = FALSE;
5228
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005229 return did_free;
5230}
5231
5232/*
5233 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005234 * "list_stack" is used to add lists to be marked. Can be NULL.
5235 *
5236 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005237 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005238 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005239set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005240{
5241 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005242 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005243 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005244 hashtab_T *cur_ht;
5245 ht_stack_T *ht_stack = NULL;
5246 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005247
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005248 cur_ht = ht;
5249 for (;;)
5250 {
5251 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005252 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005253 /* Mark each item in the hashtab. If the item contains a hashtab
5254 * it is added to ht_stack, if it contains a list it is added to
5255 * list_stack. */
5256 todo = (int)cur_ht->ht_used;
5257 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5258 if (!HASHITEM_EMPTY(hi))
5259 {
5260 --todo;
5261 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5262 &ht_stack, list_stack);
5263 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005264 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005265
5266 if (ht_stack == NULL)
5267 break;
5268
5269 /* take an item from the stack */
5270 cur_ht = ht_stack->ht;
5271 tempitem = ht_stack;
5272 ht_stack = ht_stack->prev;
5273 free(tempitem);
5274 }
5275
5276 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005277}
5278
5279/*
5280 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005281 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5282 *
5283 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005284 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005285 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005286set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005287{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005288 listitem_T *li;
5289 int abort = FALSE;
5290 list_T *cur_l;
5291 list_stack_T *list_stack = NULL;
5292 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005293
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005294 cur_l = l;
5295 for (;;)
5296 {
5297 if (!abort)
5298 /* Mark each item in the list. If the item contains a hashtab
5299 * it is added to ht_stack, if it contains a list it is added to
5300 * list_stack. */
5301 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5302 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5303 ht_stack, &list_stack);
5304 if (list_stack == NULL)
5305 break;
5306
5307 /* take an item from the stack */
5308 cur_l = list_stack->list;
5309 tempitem = list_stack;
5310 list_stack = list_stack->prev;
5311 free(tempitem);
5312 }
5313
5314 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005315}
5316
5317/*
5318 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005319 * "list_stack" is used to add lists to be marked. Can be NULL.
5320 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5321 *
5322 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005323 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005324 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005325set_ref_in_item(
5326 typval_T *tv,
5327 int copyID,
5328 ht_stack_T **ht_stack,
5329 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005330{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005331 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005332
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005333 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005334 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005335 dict_T *dd = tv->vval.v_dict;
5336
Bram Moolenaara03f2332016-02-06 18:09:59 +01005337 if (dd != NULL && dd->dv_copyID != copyID)
5338 {
5339 /* Didn't see this dict yet. */
5340 dd->dv_copyID = copyID;
5341 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005342 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005343 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5344 }
5345 else
5346 {
5347 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5348 if (newitem == NULL)
5349 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005350 else
5351 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005352 newitem->ht = &dd->dv_hashtab;
5353 newitem->prev = *ht_stack;
5354 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005355 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005356 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005357 }
5358 }
5359 else if (tv->v_type == VAR_LIST)
5360 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005361 list_T *ll = tv->vval.v_list;
5362
Bram Moolenaara03f2332016-02-06 18:09:59 +01005363 if (ll != NULL && ll->lv_copyID != copyID)
5364 {
5365 /* Didn't see this list yet. */
5366 ll->lv_copyID = copyID;
5367 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005368 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005369 abort = set_ref_in_list(ll, copyID, ht_stack);
5370 }
5371 else
5372 {
5373 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005374 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005375 if (newitem == NULL)
5376 abort = TRUE;
5377 else
5378 {
5379 newitem->list = ll;
5380 newitem->prev = *list_stack;
5381 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005382 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005383 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005384 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005385 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005386 else if (tv->v_type == VAR_FUNC)
5387 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005388 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005389 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005390 else if (tv->v_type == VAR_PARTIAL)
5391 {
5392 partial_T *pt = tv->vval.v_partial;
5393 int i;
5394
5395 /* A partial does not have a copyID, because it cannot contain itself.
5396 */
5397 if (pt != NULL)
5398 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005399 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005400
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005401 if (pt->pt_dict != NULL)
5402 {
5403 typval_T dtv;
5404
5405 dtv.v_type = VAR_DICT;
5406 dtv.vval.v_dict = pt->pt_dict;
5407 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5408 }
5409
5410 for (i = 0; i < pt->pt_argc; ++i)
5411 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5412 ht_stack, list_stack);
5413 }
5414 }
5415#ifdef FEAT_JOB_CHANNEL
5416 else if (tv->v_type == VAR_JOB)
5417 {
5418 job_T *job = tv->vval.v_job;
5419 typval_T dtv;
5420
5421 if (job != NULL && job->jv_copyID != copyID)
5422 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005423 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005424 if (job->jv_channel != NULL)
5425 {
5426 dtv.v_type = VAR_CHANNEL;
5427 dtv.vval.v_channel = job->jv_channel;
5428 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5429 }
5430 if (job->jv_exit_partial != NULL)
5431 {
5432 dtv.v_type = VAR_PARTIAL;
5433 dtv.vval.v_partial = job->jv_exit_partial;
5434 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5435 }
5436 }
5437 }
5438 else if (tv->v_type == VAR_CHANNEL)
5439 {
5440 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005441 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005442 typval_T dtv;
5443 jsonq_T *jq;
5444 cbq_T *cq;
5445
5446 if (ch != NULL && ch->ch_copyID != copyID)
5447 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005448 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005449 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005450 {
5451 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5452 jq = jq->jq_next)
5453 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5454 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5455 cq = cq->cq_next)
5456 if (cq->cq_partial != NULL)
5457 {
5458 dtv.v_type = VAR_PARTIAL;
5459 dtv.vval.v_partial = cq->cq_partial;
5460 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5461 }
5462 if (ch->ch_part[part].ch_partial != NULL)
5463 {
5464 dtv.v_type = VAR_PARTIAL;
5465 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5466 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5467 }
5468 }
5469 if (ch->ch_partial != NULL)
5470 {
5471 dtv.v_type = VAR_PARTIAL;
5472 dtv.vval.v_partial = ch->ch_partial;
5473 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5474 }
5475 if (ch->ch_close_partial != NULL)
5476 {
5477 dtv.v_type = VAR_PARTIAL;
5478 dtv.vval.v_partial = ch->ch_close_partial;
5479 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5480 }
5481 }
5482 }
5483#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005484 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005485}
5486
Bram Moolenaar17a13432016-01-24 14:22:10 +01005487 static char *
5488get_var_special_name(int nr)
5489{
5490 switch (nr)
5491 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005492 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005493 case VVAL_TRUE: return "v:true";
5494 case VVAL_NONE: return "v:none";
5495 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005496 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01005497 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01005498 return "42";
5499}
5500
Bram Moolenaar8c711452005-01-14 21:53:12 +00005501/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005502 * Return a string with the string representation of a variable.
5503 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005504 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005505 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005506 * When both "echo_style" and "composite_val" are FALSE, put quotes around
5507 * stings as "string()", otherwise does not put quotes around strings, as
5508 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005509 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5510 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005511 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005512 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005513 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005514echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005515 typval_T *tv,
5516 char_u **tofree,
5517 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005518 int copyID,
5519 int echo_style,
5520 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005521 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005522{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005523 static int recurse = 0;
5524 char_u *r = NULL;
5525
Bram Moolenaar33570922005-01-25 22:26:29 +00005526 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005527 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005528 if (!did_echo_string_emsg)
5529 {
5530 /* Only give this message once for a recursive call to avoid
5531 * flooding the user with errors. And stop iterating over lists
5532 * and dicts. */
5533 did_echo_string_emsg = TRUE;
5534 EMSG(_("E724: variable nested too deep for displaying"));
5535 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005536 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005537 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005538 }
5539 ++recurse;
5540
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005541 switch (tv->v_type)
5542 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005543 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005544 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005545 {
5546 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005547 r = tv->vval.v_string;
5548 if (r == NULL)
5549 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005550 }
5551 else
5552 {
5553 *tofree = string_quote(tv->vval.v_string, FALSE);
5554 r = *tofree;
5555 }
5556 break;
5557
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005558 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005559 if (echo_style)
5560 {
5561 *tofree = NULL;
5562 r = tv->vval.v_string;
5563 }
5564 else
5565 {
5566 *tofree = string_quote(tv->vval.v_string, TRUE);
5567 r = *tofree;
5568 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005569 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005570
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005571 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005572 {
5573 partial_T *pt = tv->vval.v_partial;
5574 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005575 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005576 garray_T ga;
5577 int i;
5578 char_u *tf;
5579
5580 ga_init2(&ga, 1, 100);
5581 ga_concat(&ga, (char_u *)"function(");
5582 if (fname != NULL)
5583 {
5584 ga_concat(&ga, fname);
5585 vim_free(fname);
5586 }
5587 if (pt != NULL && pt->pt_argc > 0)
5588 {
5589 ga_concat(&ga, (char_u *)", [");
5590 for (i = 0; i < pt->pt_argc; ++i)
5591 {
5592 if (i > 0)
5593 ga_concat(&ga, (char_u *)", ");
5594 ga_concat(&ga,
5595 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5596 vim_free(tf);
5597 }
5598 ga_concat(&ga, (char_u *)"]");
5599 }
5600 if (pt != NULL && pt->pt_dict != NULL)
5601 {
5602 typval_T dtv;
5603
5604 ga_concat(&ga, (char_u *)", ");
5605 dtv.v_type = VAR_DICT;
5606 dtv.vval.v_dict = pt->pt_dict;
5607 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5608 vim_free(tf);
5609 }
5610 ga_concat(&ga, (char_u *)")");
5611
5612 *tofree = ga.ga_data;
5613 r = *tofree;
5614 break;
5615 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005616
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005617 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005618 if (tv->vval.v_list == NULL)
5619 {
5620 *tofree = NULL;
5621 r = NULL;
5622 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005623 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5624 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005625 {
5626 *tofree = NULL;
5627 r = (char_u *)"[...]";
5628 }
5629 else
5630 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005631 int old_copyID = tv->vval.v_list->lv_copyID;
5632
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005633 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005634 *tofree = list2string(tv, copyID, restore_copyID);
5635 if (restore_copyID)
5636 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005637 r = *tofree;
5638 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005639 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005640
Bram Moolenaar8c711452005-01-14 21:53:12 +00005641 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005642 if (tv->vval.v_dict == NULL)
5643 {
5644 *tofree = NULL;
5645 r = NULL;
5646 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005647 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5648 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005649 {
5650 *tofree = NULL;
5651 r = (char_u *)"{...}";
5652 }
5653 else
5654 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005655 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005656 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005657 *tofree = dict2string(tv, copyID, restore_copyID);
5658 if (restore_copyID)
5659 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005660 r = *tofree;
5661 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005662 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005663
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005664 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005665 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005666 *tofree = NULL;
5667 r = get_tv_string_buf(tv, numbuf);
5668 break;
5669
Bram Moolenaar835dc632016-02-07 14:27:38 +01005670 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005671 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005672 *tofree = NULL;
5673 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005674 if (composite_val)
5675 {
5676 *tofree = string_quote(r, FALSE);
5677 r = *tofree;
5678 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005679 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005680
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005681 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005682#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005683 *tofree = NULL;
5684 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5685 r = numbuf;
5686 break;
5687#endif
5688
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005689 case VAR_SPECIAL:
5690 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005691 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005692 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005693 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005694
Bram Moolenaar8502c702014-06-17 12:51:16 +02005695 if (--recurse == 0)
5696 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005697 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005698}
5699
5700/*
5701 * Return a string with the string representation of a variable.
5702 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5703 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005704 * Does not put quotes around strings, as ":echo" displays values.
5705 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5706 * May return NULL.
5707 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005708 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005709echo_string(
5710 typval_T *tv,
5711 char_u **tofree,
5712 char_u *numbuf,
5713 int copyID)
5714{
5715 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5716}
5717
5718/*
5719 * Return a string with the string representation of a variable.
5720 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5721 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005722 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005723 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005724 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02005725 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005726tv2string(
5727 typval_T *tv,
5728 char_u **tofree,
5729 char_u *numbuf,
5730 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005731{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005732 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005733}
5734
5735/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005736 * Return string "str" in ' quotes, doubling ' characters.
5737 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005738 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005739 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005740 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005741string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005742{
Bram Moolenaar33570922005-01-25 22:26:29 +00005743 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005744 char_u *p, *r, *s;
5745
Bram Moolenaar33570922005-01-25 22:26:29 +00005746 len = (function ? 13 : 3);
5747 if (str != NULL)
5748 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005749 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005750 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005751 if (*p == '\'')
5752 ++len;
5753 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005754 s = r = alloc(len);
5755 if (r != NULL)
5756 {
5757 if (function)
5758 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005759 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005760 r += 10;
5761 }
5762 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005763 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005764 if (str != NULL)
5765 for (p = str; *p != NUL; )
5766 {
5767 if (*p == '\'')
5768 *r++ = '\'';
5769 MB_COPY_CHAR(p, r);
5770 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005771 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005772 if (function)
5773 *r++ = ')';
5774 *r++ = NUL;
5775 }
5776 return s;
5777}
5778
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005779#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005780/*
5781 * Convert the string "text" to a floating point number.
5782 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5783 * this always uses a decimal point.
5784 * Returns the length of the text that was consumed.
5785 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005786 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005787string2float(
5788 char_u *text,
5789 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005790{
5791 char *s = (char *)text;
5792 float_T f;
5793
Bram Moolenaar62473612017-01-08 19:25:40 +01005794 /* MS-Windows does not deal with "inf" and "nan" properly. */
5795 if (STRNICMP(text, "inf", 3) == 0)
5796 {
5797 *value = INFINITY;
5798 return 3;
5799 }
5800 if (STRNICMP(text, "-inf", 3) == 0)
5801 {
5802 *value = -INFINITY;
5803 return 4;
5804 }
5805 if (STRNICMP(text, "nan", 3) == 0)
5806 {
5807 *value = NAN;
5808 return 3;
5809 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005810 f = strtod(s, &s);
5811 *value = f;
5812 return (int)((char_u *)s - text);
5813}
5814#endif
5815
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005816/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 * Get the value of an environment variable.
5818 * "arg" is pointing to the '$'. It is advanced to after the name.
5819 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005820 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 */
5822 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005823get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824{
5825 char_u *string = NULL;
5826 int len;
5827 int cc;
5828 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005829 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830
5831 ++*arg;
5832 name = *arg;
5833 len = get_env_len(arg);
5834 if (evaluate)
5835 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005836 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01005837 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005838
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005839 cc = name[len];
5840 name[len] = NUL;
5841 /* first try vim_getenv(), fast for normal environment vars */
5842 string = vim_getenv(name, &mustfree);
5843 if (string != NULL && *string != NUL)
5844 {
5845 if (!mustfree)
5846 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005848 else
5849 {
5850 if (mustfree)
5851 vim_free(string);
5852
5853 /* next try expanding things like $VIM and ${HOME} */
5854 string = expand_env_save(name - 1);
5855 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01005856 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005857 }
5858 name[len] = cc;
5859
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005860 rettv->v_type = VAR_STRING;
5861 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 }
5863
5864 return OK;
5865}
5866
Bram Moolenaard6e256c2011-12-14 15:32:50 +01005867
5868
5869/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005871 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005873 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005874var2fpos(
5875 typval_T *varp,
5876 int dollar_lnum, /* TRUE when $ is last line */
5877 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005879 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005881 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882
Bram Moolenaara5525202006-03-02 22:52:09 +00005883 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005884 if (varp->v_type == VAR_LIST)
5885 {
5886 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005887 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005888 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005889 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005890
5891 l = varp->vval.v_list;
5892 if (l == NULL)
5893 return NULL;
5894
5895 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00005896 pos.lnum = list_find_nr(l, 0L, &error);
5897 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005898 return NULL; /* invalid line number */
5899
5900 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00005901 pos.col = list_find_nr(l, 1L, &error);
5902 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005903 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005904 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005905
5906 /* We accept "$" for the column number: last column. */
5907 li = list_find(l, 1L);
5908 if (li != NULL && li->li_tv.v_type == VAR_STRING
5909 && li->li_tv.vval.v_string != NULL
5910 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
5911 pos.col = len + 1;
5912
Bram Moolenaara5525202006-03-02 22:52:09 +00005913 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005914 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005915 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00005916 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005917
Bram Moolenaara5525202006-03-02 22:52:09 +00005918#ifdef FEAT_VIRTUALEDIT
5919 /* Get the virtual offset. Defaults to zero. */
5920 pos.coladd = list_find_nr(l, 2L, &error);
5921 if (error)
5922 pos.coladd = 0;
5923#endif
5924
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005925 return &pos;
5926 }
5927
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005928 name = get_tv_string_chk(varp);
5929 if (name == NULL)
5930 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005931 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005933 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
5934 {
5935 if (VIsual_active)
5936 return &VIsual;
5937 return &curwin->w_cursor;
5938 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005939 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005941 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5943 return NULL;
5944 return pp;
5945 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005946
5947#ifdef FEAT_VIRTUALEDIT
5948 pos.coladd = 0;
5949#endif
5950
Bram Moolenaar477933c2007-07-17 14:32:23 +00005951 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005952 {
5953 pos.col = 0;
5954 if (name[1] == '0') /* "w0": first visible line */
5955 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005956 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005957 /* In silent Ex mode topline is zero, but that's not a valid line
5958 * number; use one instead. */
5959 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005960 return &pos;
5961 }
5962 else if (name[1] == '$') /* "w$": last visible line */
5963 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005964 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005965 /* In silent Ex mode botline is zero, return zero then. */
5966 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005967 return &pos;
5968 }
5969 }
5970 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005972 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 {
5974 pos.lnum = curbuf->b_ml.ml_line_count;
5975 pos.col = 0;
5976 }
5977 else
5978 {
5979 pos.lnum = curwin->w_cursor.lnum;
5980 pos.col = (colnr_T)STRLEN(ml_get_curline());
5981 }
5982 return &pos;
5983 }
5984 return NULL;
5985}
5986
5987/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005988 * Convert list in "arg" into a position and optional file number.
5989 * When "fnump" is NULL there is no file number, only 3 items.
5990 * Note that the column is passed on as-is, the caller may want to decrement
5991 * it to use 1 for the first column.
5992 * Return FAIL when conversion is not possible, doesn't check the position for
5993 * validity.
5994 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005995 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005996list2fpos(
5997 typval_T *arg,
5998 pos_T *posp,
5999 int *fnump,
6000 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006001{
6002 list_T *l = arg->vval.v_list;
6003 long i = 0;
6004 long n;
6005
Bram Moolenaar493c1782014-05-28 14:34:46 +02006006 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6007 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006008 if (arg->v_type != VAR_LIST
6009 || l == NULL
6010 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006011 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006012 return FAIL;
6013
6014 if (fnump != NULL)
6015 {
6016 n = list_find_nr(l, i++, NULL); /* fnum */
6017 if (n < 0)
6018 return FAIL;
6019 if (n == 0)
6020 n = curbuf->b_fnum; /* current buffer */
6021 *fnump = n;
6022 }
6023
6024 n = list_find_nr(l, i++, NULL); /* lnum */
6025 if (n < 0)
6026 return FAIL;
6027 posp->lnum = n;
6028
6029 n = list_find_nr(l, i++, NULL); /* col */
6030 if (n < 0)
6031 return FAIL;
6032 posp->col = n;
6033
6034#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +02006035 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006036 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006037 posp->coladd = 0;
6038 else
6039 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006040#endif
6041
Bram Moolenaar493c1782014-05-28 14:34:46 +02006042 if (curswantp != NULL)
6043 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6044
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006045 return OK;
6046}
6047
6048/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 * Get the length of an environment variable name.
6050 * Advance "arg" to the first character after the name.
6051 * Return 0 for error.
6052 */
6053 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006054get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055{
6056 char_u *p;
6057 int len;
6058
6059 for (p = *arg; vim_isIDc(*p); ++p)
6060 ;
6061 if (p == *arg) /* no name found */
6062 return 0;
6063
6064 len = (int)(p - *arg);
6065 *arg = p;
6066 return len;
6067}
6068
6069/*
6070 * Get the length of the name of a function or internal variable.
6071 * "arg" is advanced to the first non-white character after the name.
6072 * Return 0 if something is wrong.
6073 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006074 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006075get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076{
6077 char_u *p;
6078 int len;
6079
6080 /* Find the end of the name. */
6081 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006082 {
6083 if (*p == ':')
6084 {
6085 /* "s:" is start of "s:var", but "n:" is not and can be used in
6086 * slice "[n:]". Also "xx:" is not a namespace. */
6087 len = (int)(p - *arg);
6088 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6089 || len > 1)
6090 break;
6091 }
6092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 if (p == *arg) /* no name found */
6094 return 0;
6095
6096 len = (int)(p - *arg);
6097 *arg = skipwhite(p);
6098
6099 return len;
6100}
6101
6102/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006103 * Get the length of the name of a variable or function.
6104 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006106 * Return -1 if curly braces expansion failed.
6107 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 * If the name contains 'magic' {}'s, expand them and return the
6109 * expanded name in an allocated string via 'alias' - caller must free.
6110 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006111 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006112get_name_len(
6113 char_u **arg,
6114 char_u **alias,
6115 int evaluate,
6116 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117{
6118 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 char_u *p;
6120 char_u *expr_start;
6121 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122
6123 *alias = NULL; /* default to no alias */
6124
6125 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6126 && (*arg)[2] == (int)KE_SNR)
6127 {
6128 /* hard coded <SNR>, already translated */
6129 *arg += 3;
6130 return get_id_len(arg) + 3;
6131 }
6132 len = eval_fname_script(*arg);
6133 if (len > 0)
6134 {
6135 /* literal "<SID>", "s:" or "<SNR>" */
6136 *arg += len;
6137 }
6138
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006140 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006142 p = find_name_end(*arg, &expr_start, &expr_end,
6143 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 if (expr_start != NULL)
6145 {
6146 char_u *temp_string;
6147
6148 if (!evaluate)
6149 {
6150 len += (int)(p - *arg);
6151 *arg = skipwhite(p);
6152 return len;
6153 }
6154
6155 /*
6156 * Include any <SID> etc in the expanded string:
6157 * Thus the -len here.
6158 */
6159 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6160 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006161 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 *alias = temp_string;
6163 *arg = skipwhite(p);
6164 return (int)STRLEN(temp_string);
6165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166
6167 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006168 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 EMSG2(_(e_invexpr2), *arg);
6170
6171 return len;
6172}
6173
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006174/*
6175 * Find the end of a variable or function name, taking care of magic braces.
6176 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6177 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006178 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006179 * Return a pointer to just after the name. Equal to "arg" if there is no
6180 * valid name.
6181 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006182 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006183find_name_end(
6184 char_u *arg,
6185 char_u **expr_start,
6186 char_u **expr_end,
6187 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006189 int mb_nest = 0;
6190 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006192 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006194 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006196 *expr_start = NULL;
6197 *expr_end = NULL;
6198 }
6199
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006200 /* Quick check for valid starting character. */
6201 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6202 return arg;
6203
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006204 for (p = arg; *p != NUL
6205 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006206 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006207 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006208 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006209 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006210 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006211 if (*p == '\'')
6212 {
6213 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006214 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006215 ;
6216 if (*p == NUL)
6217 break;
6218 }
6219 else if (*p == '"')
6220 {
6221 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006222 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006223 if (*p == '\\' && p[1] != NUL)
6224 ++p;
6225 if (*p == NUL)
6226 break;
6227 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006228 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6229 {
6230 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006231 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006232 len = (int)(p - arg);
6233 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006234 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006235 break;
6236 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006237
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006238 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006240 if (*p == '[')
6241 ++br_nest;
6242 else if (*p == ']')
6243 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006245
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006246 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006248 if (*p == '{')
6249 {
6250 mb_nest++;
6251 if (expr_start != NULL && *expr_start == NULL)
6252 *expr_start = p;
6253 }
6254 else if (*p == '}')
6255 {
6256 mb_nest--;
6257 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6258 *expr_end = p;
6259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 }
6262
6263 return p;
6264}
6265
6266/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006267 * Expands out the 'magic' {}'s in a variable/function name.
6268 * Note that this can call itself recursively, to deal with
6269 * constructs like foo{bar}{baz}{bam}
6270 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6271 * "in_start" ^
6272 * "expr_start" ^
6273 * "expr_end" ^
6274 * "in_end" ^
6275 *
6276 * Returns a new allocated string, which the caller must free.
6277 * Returns NULL for failure.
6278 */
6279 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006280make_expanded_name(
6281 char_u *in_start,
6282 char_u *expr_start,
6283 char_u *expr_end,
6284 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006285{
6286 char_u c1;
6287 char_u *retval = NULL;
6288 char_u *temp_result;
6289 char_u *nextcmd = NULL;
6290
6291 if (expr_end == NULL || in_end == NULL)
6292 return NULL;
6293 *expr_start = NUL;
6294 *expr_end = NUL;
6295 c1 = *in_end;
6296 *in_end = NUL;
6297
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006298 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006299 if (temp_result != NULL && nextcmd == NULL)
6300 {
6301 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6302 + (in_end - expr_end) + 1));
6303 if (retval != NULL)
6304 {
6305 STRCPY(retval, in_start);
6306 STRCAT(retval, temp_result);
6307 STRCAT(retval, expr_end + 1);
6308 }
6309 }
6310 vim_free(temp_result);
6311
6312 *in_end = c1; /* put char back for error messages */
6313 *expr_start = '{';
6314 *expr_end = '}';
6315
6316 if (retval != NULL)
6317 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006318 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006319 if (expr_start != NULL)
6320 {
6321 /* Further expansion! */
6322 temp_result = make_expanded_name(retval, expr_start,
6323 expr_end, temp_result);
6324 vim_free(retval);
6325 retval = temp_result;
6326 }
6327 }
6328
6329 return retval;
6330}
6331
6332/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006334 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006336 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006337eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006339 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6340}
6341
6342/*
6343 * Return TRUE if character "c" can be used as the first character in a
6344 * variable or function name (excluding '{' and '}').
6345 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006346 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006347eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006348{
6349 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350}
6351
6352/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 * Set number v: variable to "val".
6354 */
6355 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006356set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006358 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359}
6360
6361/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006362 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006364 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006365get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006367 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368}
6369
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006370/*
6371 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01006372 * If the String variable has never been set, return an empty string.
6373 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006374 */
6375 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006376get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006377{
6378 return get_tv_string(&vimvars[idx].vv_tv);
6379}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006380
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006382 * Get List v: variable value. Caller must take care of reference count when
6383 * needed.
6384 */
6385 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006386get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006387{
6388 return vimvars[idx].vv_list;
6389}
6390
6391/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006392 * Get Dict v: variable value. Caller must take care of reference count when
6393 * needed.
6394 */
6395 dict_T *
6396get_vim_var_dict(int idx)
6397{
6398 return vimvars[idx].vv_dict;
6399}
6400
6401/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006402 * Set v:char to character "c".
6403 */
6404 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006405set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006406{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006407 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006408
6409#ifdef FEAT_MBYTE
6410 if (has_mbyte)
6411 buf[(*mb_char2bytes)(c, buf)] = NUL;
6412 else
6413#endif
6414 {
6415 buf[0] = c;
6416 buf[1] = NUL;
6417 }
6418 set_vim_var_string(VV_CHAR, buf, -1);
6419}
6420
6421/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006422 * Set v:count to "count" and v:count1 to "count1".
6423 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 */
6425 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006426set_vcount(
6427 long count,
6428 long count1,
6429 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006431 if (set_prevcount)
6432 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006433 vimvars[VV_COUNT].vv_nr = count;
6434 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435}
6436
6437/*
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02006438 * Save variables that might be changed as a side effect. Used when executing
6439 * a timer callback.
6440 */
6441 void
6442save_vimvars(vimvars_save_T *vvsave)
6443{
6444 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
6445 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
6446 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
6447}
6448
6449/*
6450 * Restore variables saved by save_vimvars().
6451 */
6452 void
6453restore_vimvars(vimvars_save_T *vvsave)
6454{
6455 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
6456 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
6457 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
6458}
6459
6460/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 * Set string v: variable to a copy of "val".
6462 */
6463 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006464set_vim_var_string(
6465 int idx,
6466 char_u *val,
6467 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468{
Bram Moolenaara542c682016-01-31 16:28:04 +01006469 clear_tv(&vimvars[idx].vv_di.di_tv);
6470 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006472 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006474 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006476 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477}
6478
6479/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006480 * Set List v: variable to "val".
6481 */
6482 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006483set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006484{
Bram Moolenaara542c682016-01-31 16:28:04 +01006485 clear_tv(&vimvars[idx].vv_di.di_tv);
6486 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006487 vimvars[idx].vv_list = val;
6488 if (val != NULL)
6489 ++val->lv_refcount;
6490}
6491
6492/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006493 * Set Dictionary v: variable to "val".
6494 */
6495 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006496set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006497{
Bram Moolenaara542c682016-01-31 16:28:04 +01006498 clear_tv(&vimvars[idx].vv_di.di_tv);
6499 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006500 vimvars[idx].vv_dict = val;
6501 if (val != NULL)
6502 {
6503 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006504 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02006505 }
6506}
6507
6508/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 * Set v:register if needed.
6510 */
6511 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006512set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513{
6514 char_u regname;
6515
6516 if (c == 0 || c == ' ')
6517 regname = '"';
6518 else
6519 regname = c;
6520 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006521 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 set_vim_var_string(VV_REG, &regname, 1);
6523}
6524
6525/*
6526 * Get or set v:exception. If "oldval" == NULL, return the current value.
6527 * Otherwise, restore the value to "oldval" and return NULL.
6528 * Must always be called in pairs to save and restore v:exception! Does not
6529 * take care of memory allocations.
6530 */
6531 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006532v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533{
6534 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006535 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536
Bram Moolenaare9a41262005-01-15 22:18:47 +00006537 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 return NULL;
6539}
6540
6541/*
6542 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
6543 * Otherwise, restore the value to "oldval" and return NULL.
6544 * Must always be called in pairs to save and restore v:throwpoint! Does not
6545 * take care of memory allocations.
6546 */
6547 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006548v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549{
6550 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006551 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552
Bram Moolenaare9a41262005-01-15 22:18:47 +00006553 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 return NULL;
6555}
6556
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557/*
6558 * Set v:cmdarg.
6559 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6560 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6561 * Must always be called in pairs!
6562 */
6563 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006564set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565{
6566 char_u *oldval;
6567 char_u *newval;
6568 unsigned len;
6569
Bram Moolenaare9a41262005-01-15 22:18:47 +00006570 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006571 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006573 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006574 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006575 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 }
6577
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006578 if (eap->force_bin == FORCE_BIN)
6579 len = 6;
6580 else if (eap->force_bin == FORCE_NOBIN)
6581 len = 8;
6582 else
6583 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006584
6585 if (eap->read_edit)
6586 len += 7;
6587
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006588 if (eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006589 len += 10; /* " ++ff=unix" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006590# ifdef FEAT_MBYTE
6591 if (eap->force_enc != 0)
6592 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006593 if (eap->bad_char != 0)
6594 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006595# endif
6596
6597 newval = alloc(len + 1);
6598 if (newval == NULL)
6599 return NULL;
6600
6601 if (eap->force_bin == FORCE_BIN)
6602 sprintf((char *)newval, " ++bin");
6603 else if (eap->force_bin == FORCE_NOBIN)
6604 sprintf((char *)newval, " ++nobin");
6605 else
6606 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006607
6608 if (eap->read_edit)
6609 STRCAT(newval, " ++edit");
6610
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006611 if (eap->force_ff != 0)
6612 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006613 eap->force_ff == 'u' ? "unix"
6614 : eap->force_ff == 'd' ? "dos"
6615 : "mac");
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006616#ifdef FEAT_MBYTE
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006617 if (eap->force_enc != 0)
6618 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6619 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006620 if (eap->bad_char == BAD_KEEP)
6621 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6622 else if (eap->bad_char == BAD_DROP)
6623 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6624 else if (eap->bad_char != 0)
6625 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006626#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00006627 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006628 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630
6631/*
6632 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006633 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006635 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006636get_var_tv(
6637 char_u *name,
6638 int len, /* length of "name" */
6639 typval_T *rettv, /* NULL when only checking existence */
6640 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
6641 int verbose, /* may give error message */
6642 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643{
6644 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006645 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006646 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648
6649 /* truncate the name, so that we can use strcmp() */
6650 cc = name[len];
6651 name[len] = NUL;
6652
6653 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 * Check for user-defined variables.
6655 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01006656 v = find_var(name, NULL, no_autoload);
6657 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006659 tv = &v->di_tv;
6660 if (dip != NULL)
6661 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 }
6663
Bram Moolenaare9a41262005-01-15 22:18:47 +00006664 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006666 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006667 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 ret = FAIL;
6669 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006670 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006671 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672
6673 name[len] = cc;
6674
6675 return ret;
6676}
6677
6678/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006679 * Check if variable "name[len]" is a local variable or an argument.
6680 * If so, "*eval_lavars_used" is set to TRUE.
6681 */
6682 static void
6683check_vars(char_u *name, int len)
6684{
6685 int cc;
6686 char_u *varname;
6687 hashtab_T *ht;
6688
6689 if (eval_lavars_used == NULL)
6690 return;
6691
6692 /* truncate the name, so that we can use strcmp() */
6693 cc = name[len];
6694 name[len] = NUL;
6695
6696 ht = find_var_ht(name, &varname);
6697 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
6698 {
6699 if (find_var(name, NULL, TRUE) != NULL)
6700 *eval_lavars_used = TRUE;
6701 }
6702
6703 name[len] = cc;
6704}
6705
6706/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006707 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
6708 * Also handle function call with Funcref variable: func(expr)
6709 * Can all be combined: dict.func(expr)[idx]['func'](expr)
6710 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006711 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006712handle_subscript(
6713 char_u **arg,
6714 typval_T *rettv,
6715 int evaluate, /* do more than finding the end */
6716 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006717{
6718 int ret = OK;
6719 dict_T *selfdict = NULL;
6720 char_u *s;
6721 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006722 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006723
6724 while (ret == OK
6725 && (**arg == '['
6726 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006727 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6728 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01006729 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006730 {
6731 if (**arg == '(')
6732 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006733 partial_T *pt = NULL;
6734
Bram Moolenaard9fba312005-06-26 22:34:35 +00006735 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006736 if (evaluate)
6737 {
6738 functv = *rettv;
6739 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006740
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006741 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006742 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006743 {
6744 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006745 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006746 }
6747 else
6748 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006749 }
6750 else
6751 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006752 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00006753 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006754 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006755
6756 /* Clear the funcref afterwards, so that deleting it while
6757 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006758 if (evaluate)
6759 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006760
6761 /* Stop the expression evaluation when immediately aborting on
6762 * error, or when an interrupt occurred or an exception was thrown
6763 * but not caught. */
6764 if (aborting())
6765 {
6766 if (ret == OK)
6767 clear_tv(rettv);
6768 ret = FAIL;
6769 }
6770 dict_unref(selfdict);
6771 selfdict = NULL;
6772 }
6773 else /* **arg == '[' || **arg == '.' */
6774 {
6775 dict_unref(selfdict);
6776 if (rettv->v_type == VAR_DICT)
6777 {
6778 selfdict = rettv->vval.v_dict;
6779 if (selfdict != NULL)
6780 ++selfdict->dv_refcount;
6781 }
6782 else
6783 selfdict = NULL;
6784 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
6785 {
6786 clear_tv(rettv);
6787 ret = FAIL;
6788 }
6789 }
6790 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006791
Bram Moolenaar1d429612016-05-24 15:44:17 +02006792 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
6793 * Don't do this when "Func" is already a partial that was bound
6794 * explicitly (pt_auto is FALSE). */
6795 if (selfdict != NULL
6796 && (rettv->v_type == VAR_FUNC
6797 || (rettv->v_type == VAR_PARTIAL
6798 && (rettv->vval.v_partial->pt_auto
6799 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006800 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006801
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006802 dict_unref(selfdict);
6803 return ret;
6804}
6805
6806/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006807 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006808 * value).
6809 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01006810 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006811alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006812{
Bram Moolenaar33570922005-01-25 22:26:29 +00006813 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006814}
6815
6816/*
6817 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 * The string "s" must have been allocated, it is consumed.
6819 * Return NULL for out of memory, the variable otherwise.
6820 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006821 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006822alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823{
Bram Moolenaar33570922005-01-25 22:26:29 +00006824 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006826 rettv = alloc_tv();
6827 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006829 rettv->v_type = VAR_STRING;
6830 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 }
6832 else
6833 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006834 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835}
6836
6837/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006838 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006840 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006841free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842{
6843 if (varp != NULL)
6844 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006845 switch (varp->v_type)
6846 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006847 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006848 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02006849 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006850 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006851 vim_free(varp->vval.v_string);
6852 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006853 case VAR_PARTIAL:
6854 partial_unref(varp->vval.v_partial);
6855 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006856 case VAR_LIST:
6857 list_unref(varp->vval.v_list);
6858 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006859 case VAR_DICT:
6860 dict_unref(varp->vval.v_dict);
6861 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006862 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006863#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006864 job_unref(varp->vval.v_job);
6865 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006866#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006867 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006868#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006869 channel_unref(varp->vval.v_channel);
6870 break;
6871#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006872 case VAR_NUMBER:
6873 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00006874 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01006875 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00006876 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 vim_free(varp);
6879 }
6880}
6881
6882/*
6883 * Free the memory for a variable value and set the value to NULL or 0.
6884 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006885 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006886clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887{
6888 if (varp != NULL)
6889 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006890 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006892 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006893 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02006894 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006895 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01006896 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006897 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006898 case VAR_PARTIAL:
6899 partial_unref(varp->vval.v_partial);
6900 varp->vval.v_partial = NULL;
6901 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006902 case VAR_LIST:
6903 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006904 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006905 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006906 case VAR_DICT:
6907 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006908 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006909 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006910 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006911 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006912 varp->vval.v_number = 0;
6913 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006914 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006915#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006916 varp->vval.v_float = 0.0;
6917 break;
6918#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006919 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006920#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006921 job_unref(varp->vval.v_job);
6922 varp->vval.v_job = NULL;
6923#endif
6924 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01006925 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006926#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006927 channel_unref(varp->vval.v_channel);
6928 varp->vval.v_channel = NULL;
6929#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006930 case VAR_UNKNOWN:
6931 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006933 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 }
6935}
6936
6937/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006938 * Set the value of a variable to NULL without freeing items.
6939 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006940 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006941init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006942{
6943 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00006944 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006945}
6946
6947/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 * Get the number value of a variable.
6949 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006950 * For incompatible types, return 0.
6951 * get_tv_number_chk() is similar to get_tv_number(), but informs the
6952 * caller of incompatible types: it sets *denote to TRUE if "denote"
6953 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006955 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006956get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006958 int error = FALSE;
6959
6960 return get_tv_number_chk(varp, &error); /* return 0L on error */
6961}
6962
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006963 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006964get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006965{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006966 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006968 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006970 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006971 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006972 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006973#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +00006974 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006975 break;
6976#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006977 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006978 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +00006979 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006980 break;
6981 case VAR_STRING:
6982 if (varp->vval.v_string != NULL)
6983 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006984 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006985 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006986 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +00006987 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006988 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006989 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +00006990 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006991 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006992 case VAR_SPECIAL:
6993 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
6994 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006995 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006996#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006997 EMSG(_("E910: Using a Job as a Number"));
6998 break;
6999#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007000 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007001#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007002 EMSG(_("E913: Using a Channel as a Number"));
7003 break;
7004#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007005 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007006 internal_error("get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007007 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007009 if (denote == NULL) /* useful for values that must be unsigned */
7010 n = -1;
7011 else
7012 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007013 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014}
7015
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007016#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007017 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007018get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007019{
7020 switch (varp->v_type)
7021 {
7022 case VAR_NUMBER:
7023 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007024 case VAR_FLOAT:
7025 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007026 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007027 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007028 EMSG(_("E891: Using a Funcref as a Float"));
7029 break;
7030 case VAR_STRING:
7031 EMSG(_("E892: Using a String as a Float"));
7032 break;
7033 case VAR_LIST:
7034 EMSG(_("E893: Using a List as a Float"));
7035 break;
7036 case VAR_DICT:
7037 EMSG(_("E894: Using a Dictionary as a Float"));
7038 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007039 case VAR_SPECIAL:
7040 EMSG(_("E907: Using a special value as a Float"));
7041 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007042 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007043# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007044 EMSG(_("E911: Using a Job as a Float"));
7045 break;
7046# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007047 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007048# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007049 EMSG(_("E914: Using a Channel as a Float"));
7050 break;
7051# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007052 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007053 internal_error("get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007054 break;
7055 }
7056 return 0;
7057}
7058#endif
7059
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 * Get the string value of a variable.
7062 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +00007063 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7064 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 * If the String variable has never been set, return an empty string.
7066 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007067 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
7068 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007070 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007071get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072{
7073 static char_u mybuf[NUMBUFLEN];
7074
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007075 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076}
7077
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007078 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007079get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007081 char_u *res = get_tv_string_buf_chk(varp, buf);
7082
7083 return res != NULL ? res : (char_u *)"";
7084}
7085
Bram Moolenaar7d647822014-04-05 21:28:56 +02007086/*
7087 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7088 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007089 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007090get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007091{
7092 static char_u mybuf[NUMBUFLEN];
7093
7094 return get_tv_string_buf_chk(varp, mybuf);
7095}
7096
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007097 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007098get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007099{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007100 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007102 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007103 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaarea391762018-04-08 13:07:22 +02007104 (long long)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007105 return buf;
7106 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007107 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007108 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007109 break;
7110 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007111 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007112 break;
7113 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007114 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007115 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007116 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007117#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +02007118 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007119 break;
7120#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007121 case VAR_STRING:
7122 if (varp->vval.v_string != NULL)
7123 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007124 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007125 case VAR_SPECIAL:
7126 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7127 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007128 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007129#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007130 {
7131 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007132 char *status;
7133
7134 if (job == NULL)
7135 return (char_u *)"no process";
7136 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007137 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007138 : "run";
7139# ifdef UNIX
7140 vim_snprintf((char *)buf, NUMBUFLEN,
7141 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007142# elif defined(WIN32)
7143 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007144 "process %ld %s",
7145 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007146 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007147# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007148 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007149 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7150# endif
7151 return buf;
7152 }
7153#endif
7154 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007155 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007156#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007157 {
7158 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007159 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007160
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007161 if (channel == NULL)
7162 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7163 else
7164 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007165 "channel %d %s", channel->ch_id, status);
7166 return buf;
7167 }
7168#endif
7169 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007170 case VAR_UNKNOWN:
7171 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007172 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007174 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175}
7176
7177/*
7178 * Find variable "name" in the list of variables.
7179 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007180 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007181 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007182 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007184 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007185find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007188 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007189 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190
Bram Moolenaara7043832005-01-21 11:56:39 +00007191 ht = find_var_ht(name, &varname);
7192 if (htp != NULL)
7193 *htp = ht;
7194 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007196 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7197 if (ret != NULL)
7198 return ret;
7199
7200 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007201 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202}
7203
7204/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007205 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007206 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007208 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007209find_var_in_ht(
7210 hashtab_T *ht,
7211 int htname,
7212 char_u *varname,
7213 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007214{
Bram Moolenaar33570922005-01-25 22:26:29 +00007215 hashitem_T *hi;
7216
7217 if (*varname == NUL)
7218 {
7219 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007220 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007221 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007222 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007223 case 'g': return &globvars_var;
7224 case 'v': return &vimvars_var;
7225 case 'b': return &curbuf->b_bufvar;
7226 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007227 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007228 case 'l': return get_funccal_local_var();
7229 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007230 }
7231 return NULL;
7232 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007233
7234 hi = hash_find(ht, varname);
7235 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007236 {
7237 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007238 * worked find the variable again. Don't auto-load a script if it was
7239 * loaded already, otherwise it would be loaded every time when
7240 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007241 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007242 {
7243 /* Note: script_autoload() may make "hi" invalid. It must either
7244 * be obtained again or not used. */
7245 if (!script_autoload(varname, FALSE) || aborting())
7246 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007247 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007248 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007249 if (HASHITEM_EMPTY(hi))
7250 return NULL;
7251 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007252 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007253}
7254
7255/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007256 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007257 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007258 * Set "varname" to the start of name without ':'.
7259 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007260 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007261find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007263 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007264 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007265
Bram Moolenaar73627d02015-08-11 15:46:09 +02007266 if (name[0] == NUL)
7267 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 if (name[1] != ':')
7269 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007270 /* The name must not start with a colon or #. */
7271 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 return NULL;
7273 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007274
7275 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007276 hi = hash_find(&compat_hashtab, name);
7277 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +00007278 return &compat_hashtab;
7279
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007280 ht = get_funccal_local_ht();
7281 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007282 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007283 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 }
7285 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007286 if (*name == 'g') /* global variable */
7287 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007288 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7289 */
7290 if (vim_strchr(name + 2, ':') != NULL
7291 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007292 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007294 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007296 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007297 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007298 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007299 if (*name == 'v') /* v: variable */
7300 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007301 if (*name == 'a') /* a: function argument */
7302 return get_funccal_args_ht();
7303 if (*name == 'l') /* l: local function variable */
7304 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 if (*name == 's' /* script variable */
7306 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
7307 return &SCRIPT_VARS(current_SID);
7308 return NULL;
7309}
7310
7311/*
7312 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +02007313 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 * Returns NULL when it doesn't exist.
7315 */
7316 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007317get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318{
Bram Moolenaar33570922005-01-25 22:26:29 +00007319 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007321 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 if (v == NULL)
7323 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007324 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325}
7326
7327/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007328 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 * sourcing this script and when executing functions defined in the script.
7330 */
7331 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007332new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333{
Bram Moolenaara7043832005-01-21 11:56:39 +00007334 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007335 hashtab_T *ht;
7336 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007337
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7339 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007340 /* Re-allocating ga_data means that an ht_array pointing to
7341 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007342 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007343 for (i = 1; i <= ga_scripts.ga_len; ++i)
7344 {
7345 ht = &SCRIPT_VARS(i);
7346 if (ht->ht_mask == HT_INIT_SIZE - 1)
7347 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007348 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007349 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007350 }
7351
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 while (ga_scripts.ga_len < id)
7353 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007354 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007355 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007356 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 }
7359 }
7360}
7361
7362/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007363 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7364 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 */
7366 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007367init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368{
Bram Moolenaar33570922005-01-25 22:26:29 +00007369 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007370 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007371 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007372 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007373 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007374 dict_var->di_tv.vval.v_dict = dict;
7375 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007376 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007377 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7378 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379}
7380
7381/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007382 * Unreference a dictionary initialized by init_var_dict().
7383 */
7384 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007385unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007386{
7387 /* Now the dict needs to be freed if no one else is using it, go back to
7388 * normal reference counting. */
7389 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7390 dict_unref(dict);
7391}
7392
7393/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007395 * Frees all allocated variables and the value they contain.
7396 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 */
7398 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007399vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007400{
7401 vars_clear_ext(ht, TRUE);
7402}
7403
7404/*
7405 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7406 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007407 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007408vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409{
Bram Moolenaara7043832005-01-21 11:56:39 +00007410 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007411 hashitem_T *hi;
7412 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413
Bram Moolenaar33570922005-01-25 22:26:29 +00007414 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007415 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007416 for (hi = ht->ht_array; todo > 0; ++hi)
7417 {
7418 if (!HASHITEM_EMPTY(hi))
7419 {
7420 --todo;
7421
Bram Moolenaar33570922005-01-25 22:26:29 +00007422 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007423 * ht_array might change then. hash_clear() takes care of it
7424 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007425 v = HI2DI(hi);
7426 if (free_val)
7427 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007428 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007429 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007430 }
7431 }
7432 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007433 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434}
7435
Bram Moolenaara7043832005-01-21 11:56:39 +00007436/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007437 * Delete a variable from hashtab "ht" at item "hi".
7438 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007439 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007441delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442{
Bram Moolenaar33570922005-01-25 22:26:29 +00007443 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007444
7445 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007446 clear_tv(&di->di_tv);
7447 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448}
7449
7450/*
7451 * List the value of one internal variable.
7452 */
7453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007454list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007456 char_u *tofree;
7457 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007458 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007459
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007460 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007461 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007462 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007463 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464}
7465
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007467list_one_var_a(
7468 char_u *prefix,
7469 char_u *name,
7470 int type,
7471 char_u *string,
7472 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473{
Bram Moolenaar31859182007-08-14 20:41:13 +00007474 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7475 msg_start();
7476 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 if (name != NULL) /* "a:" vars don't have a name stored */
7478 msg_puts(name);
7479 msg_putchar(' ');
7480 msg_advance(22);
7481 if (type == VAR_NUMBER)
7482 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007483 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007484 msg_putchar('*');
7485 else if (type == VAR_LIST)
7486 {
7487 msg_putchar('[');
7488 if (*string == '[')
7489 ++string;
7490 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007491 else if (type == VAR_DICT)
7492 {
7493 msg_putchar('{');
7494 if (*string == '{')
7495 ++string;
7496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 else
7498 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007499
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007501
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007502 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007503 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007504 if (*first)
7505 {
7506 msg_clr_eos();
7507 *first = FALSE;
7508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509}
7510
7511/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007512 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 * If the variable already exists, the value is updated.
7514 * Otherwise the variable is created.
7515 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007516 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007517set_var(
7518 char_u *name,
7519 typval_T *tv,
7520 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521{
Bram Moolenaar33570922005-01-25 22:26:29 +00007522 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007524 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007526 ht = find_var_ht(name, &varname);
7527 if (ht == NULL || *varname == NUL)
7528 {
7529 EMSG2(_(e_illvar), name);
7530 return;
7531 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007532 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007533
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007534 /* Search in parent scope which is possible to reference from lambda */
7535 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007536 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007537
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007538 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7539 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007540 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007541
Bram Moolenaar33570922005-01-25 22:26:29 +00007542 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007544 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007545 if (var_check_ro(v->di_flags, name, FALSE)
7546 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007547 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007548
7549 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007550 * Handle setting internal v: variables separately where needed to
7551 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007552 */
7553 if (ht == &vimvarht)
7554 {
7555 if (v->di_tv.v_type == VAR_STRING)
7556 {
7557 vim_free(v->di_tv.vval.v_string);
7558 if (copy || tv->v_type != VAR_STRING)
7559 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
7560 else
7561 {
7562 /* Take over the string to avoid an extra alloc/free. */
7563 v->di_tv.vval.v_string = tv->vval.v_string;
7564 tv->vval.v_string = NULL;
7565 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007566 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007567 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007568 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007569 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007570 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007571 if (STRCMP(varname, "searchforward") == 0)
7572 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007573#ifdef FEAT_SEARCH_EXTRA
7574 else if (STRCMP(varname, "hlsearch") == 0)
7575 {
7576 no_hlsearch = !v->di_tv.vval.v_number;
7577 redraw_all_later(SOME_VALID);
7578 }
7579#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007580 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007581 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007582 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar95f09602016-11-10 20:01:45 +01007583 internal_error("set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +00007584 }
7585
7586 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 }
7588 else /* add a new variable */
7589 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007590 /* Can't add "v:" variable. */
7591 if (ht == &vimvarht)
7592 {
7593 EMSG2(_(e_illvar), name);
7594 return;
7595 }
7596
Bram Moolenaar92124a32005-06-17 22:03:40 +00007597 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007598 if (!valid_varname(varname))
7599 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00007600
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007601 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7602 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00007603 if (v == NULL)
7604 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007605 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00007606 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007608 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 return;
7610 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007611 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007613
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007614 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00007615 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007616 else
7617 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007618 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007619 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007620 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622}
7623
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007624/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007625 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00007626 * Also give an error message.
7627 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007628 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007629var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00007630{
7631 if (flags & DI_FLAGS_RO)
7632 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007633 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007634 return TRUE;
7635 }
7636 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
7637 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007638 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007639 return TRUE;
7640 }
7641 return FALSE;
7642}
7643
7644/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007645 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
7646 * Also give an error message.
7647 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007648 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007649var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007650{
7651 if (flags & DI_FLAGS_FIX)
7652 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007653 EMSG2(_("E795: Cannot delete variable %s"),
7654 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007655 return TRUE;
7656 }
7657 return FALSE;
7658}
7659
7660/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007661 * Check if a funcref is assigned to a valid variable name.
7662 * Return TRUE and give an error if not.
7663 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007664 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007665var_check_func_name(
7666 char_u *name, /* points to start of variable name */
7667 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007668{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02007669 /* Allow for w: b: s: and t:. */
7670 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007671 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
7672 ? name[2] : name[0]))
7673 {
7674 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
7675 name);
7676 return TRUE;
7677 }
7678 /* Don't allow hiding a function. When "v" is not NULL we might be
7679 * assigning another function to the same var, the type is checked
7680 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02007681 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007682 {
7683 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
7684 name);
7685 return TRUE;
7686 }
7687 return FALSE;
7688}
7689
7690/*
7691 * Check if a variable name is valid.
7692 * Return FALSE and give an error if not.
7693 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007694 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007695valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007696{
7697 char_u *p;
7698
7699 for (p = varname; *p != NUL; ++p)
7700 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
7701 && *p != AUTOLOAD_CHAR)
7702 {
7703 EMSG2(_(e_illvar), varname);
7704 return FALSE;
7705 }
7706 return TRUE;
7707}
7708
7709/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007710 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02007711 * Also give an error message, using "name" or _("name") when use_gettext is
7712 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007713 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007714 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007715tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007716{
7717 if (lock & VAR_LOCKED)
7718 {
7719 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007720 name == NULL ? (char_u *)_("Unknown")
7721 : use_gettext ? (char_u *)_(name)
7722 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007723 return TRUE;
7724 }
7725 if (lock & VAR_FIXED)
7726 {
7727 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007728 name == NULL ? (char_u *)_("Unknown")
7729 : use_gettext ? (char_u *)_(name)
7730 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007731 return TRUE;
7732 }
7733 return FALSE;
7734}
7735
7736/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007737 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007738 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007739 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007740 * It is OK for "from" and "to" to point to the same item. This is used to
7741 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007742 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007743 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007744copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007746 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007747 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007748 switch (from->v_type)
7749 {
7750 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007751 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007752 to->vval.v_number = from->vval.v_number;
7753 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007754 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007755#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007756 to->vval.v_float = from->vval.v_float;
7757 break;
7758#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007759 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007760#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007761 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007762 if (to->vval.v_job != NULL)
7763 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007764 break;
7765#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007766 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007767#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007768 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007769 if (to->vval.v_channel != NULL)
7770 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01007771 break;
7772#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007773 case VAR_STRING:
7774 case VAR_FUNC:
7775 if (from->vval.v_string == NULL)
7776 to->vval.v_string = NULL;
7777 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007778 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007779 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007780 if (from->v_type == VAR_FUNC)
7781 func_ref(to->vval.v_string);
7782 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007783 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007784 case VAR_PARTIAL:
7785 if (from->vval.v_partial == NULL)
7786 to->vval.v_partial = NULL;
7787 else
7788 {
7789 to->vval.v_partial = from->vval.v_partial;
7790 ++to->vval.v_partial->pt_refcount;
7791 }
7792 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007793 case VAR_LIST:
7794 if (from->vval.v_list == NULL)
7795 to->vval.v_list = NULL;
7796 else
7797 {
7798 to->vval.v_list = from->vval.v_list;
7799 ++to->vval.v_list->lv_refcount;
7800 }
7801 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007802 case VAR_DICT:
7803 if (from->vval.v_dict == NULL)
7804 to->vval.v_dict = NULL;
7805 else
7806 {
7807 to->vval.v_dict = from->vval.v_dict;
7808 ++to->vval.v_dict->dv_refcount;
7809 }
7810 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007811 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007812 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007813 break;
7814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815}
7816
7817/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007818 * Make a copy of an item.
7819 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007820 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7821 * reference to an already copied list/dict can be used.
7822 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007823 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007824 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007825item_copy(
7826 typval_T *from,
7827 typval_T *to,
7828 int deep,
7829 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007830{
7831 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007832 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007833
Bram Moolenaar33570922005-01-25 22:26:29 +00007834 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007835 {
7836 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007837 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007838 }
7839 ++recurse;
7840
7841 switch (from->v_type)
7842 {
7843 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007844 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007845 case VAR_STRING:
7846 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007847 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007848 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007849 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007850 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007851 copy_tv(from, to);
7852 break;
7853 case VAR_LIST:
7854 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007855 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007856 if (from->vval.v_list == NULL)
7857 to->vval.v_list = NULL;
7858 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
7859 {
7860 /* use the copy made earlier */
7861 to->vval.v_list = from->vval.v_list->lv_copylist;
7862 ++to->vval.v_list->lv_refcount;
7863 }
7864 else
7865 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
7866 if (to->vval.v_list == NULL)
7867 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007868 break;
7869 case VAR_DICT:
7870 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007871 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007872 if (from->vval.v_dict == NULL)
7873 to->vval.v_dict = NULL;
7874 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7875 {
7876 /* use the copy made earlier */
7877 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7878 ++to->vval.v_dict->dv_refcount;
7879 }
7880 else
7881 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
7882 if (to->vval.v_dict == NULL)
7883 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007884 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007885 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007886 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007887 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007888 }
7889 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007890 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007891}
7892
7893/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007894 * This function is used by f_input() and f_inputdialog() functions. The third
7895 * argument to f_input() specifies the type of completion to use at the
7896 * prompt. The third argument to f_inputdialog() specifies the value to return
7897 * when the user cancels the prompt.
7898 */
7899 void
7900get_user_input(
7901 typval_T *argvars,
7902 typval_T *rettv,
7903 int inputdialog,
7904 int secret)
7905{
7906 char_u *prompt = get_tv_string_chk(&argvars[0]);
7907 char_u *p = NULL;
7908 int c;
7909 char_u buf[NUMBUFLEN];
7910 int cmd_silent_save = cmd_silent;
7911 char_u *defstr = (char_u *)"";
7912 int xp_type = EXPAND_NOTHING;
7913 char_u *xp_arg = NULL;
7914
7915 rettv->v_type = VAR_STRING;
7916 rettv->vval.v_string = NULL;
7917
7918#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007919 /* While starting up, there is no place to enter text. When running tests
7920 * with --not-a-term we assume feedkeys() will be used. */
7921 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007922 return;
7923#endif
7924
7925 cmd_silent = FALSE; /* Want to see the prompt. */
7926 if (prompt != NULL)
7927 {
7928 /* Only the part of the message after the last NL is considered as
7929 * prompt for the command line */
7930 p = vim_strrchr(prompt, '\n');
7931 if (p == NULL)
7932 p = prompt;
7933 else
7934 {
7935 ++p;
7936 c = *p;
7937 *p = NUL;
7938 msg_start();
7939 msg_clr_eos();
7940 msg_puts_attr(prompt, echo_attr);
7941 msg_didout = FALSE;
7942 msg_starthere();
7943 *p = c;
7944 }
7945 cmdline_row = msg_row;
7946
7947 if (argvars[1].v_type != VAR_UNKNOWN)
7948 {
7949 defstr = get_tv_string_buf_chk(&argvars[1], buf);
7950 if (defstr != NULL)
7951 stuffReadbuffSpec(defstr);
7952
7953 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
7954 {
7955 char_u *xp_name;
7956 int xp_namelen;
7957 long argt;
7958
7959 /* input() with a third argument: completion */
7960 rettv->vval.v_string = NULL;
7961
7962 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
7963 if (xp_name == NULL)
7964 return;
7965
7966 xp_namelen = (int)STRLEN(xp_name);
7967
7968 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
7969 &xp_arg) == FAIL)
7970 return;
7971 }
7972 }
7973
7974 if (defstr != NULL)
7975 {
7976 int save_ex_normal_busy = ex_normal_busy;
7977 ex_normal_busy = 0;
7978 rettv->vval.v_string =
7979 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
7980 xp_type, xp_arg);
7981 ex_normal_busy = save_ex_normal_busy;
7982 }
7983 if (inputdialog && rettv->vval.v_string == NULL
7984 && argvars[1].v_type != VAR_UNKNOWN
7985 && argvars[2].v_type != VAR_UNKNOWN)
7986 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
7987 &argvars[2], buf));
7988
7989 vim_free(xp_arg);
7990
7991 /* since the user typed this, no need to wait for return */
7992 need_wait_return = FALSE;
7993 msg_didout = FALSE;
7994 }
7995 cmd_silent = cmd_silent_save;
7996}
7997
7998/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 * ":echo expr1 ..." print each argument separated with a space, add a
8000 * newline at the end.
8001 * ":echon expr1 ..." print each argument plain.
8002 */
8003 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008004ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005{
8006 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008007 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008008 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 char_u *p;
8010 int needclr = TRUE;
8011 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008012 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013
8014 if (eap->skip)
8015 ++emsg_skip;
8016 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8017 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008018 /* If eval1() causes an error message the text from the command may
8019 * still need to be cleared. E.g., "echo 22,44". */
8020 need_clr_eos = needclr;
8021
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008023 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 {
8025 /*
8026 * Report the invalid expression unless the expression evaluation
8027 * has been cancelled due to an aborting error, an interrupt, or an
8028 * exception.
8029 */
8030 if (!aborting())
8031 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008032 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 break;
8034 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008035 need_clr_eos = FALSE;
8036
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 if (!eap->skip)
8038 {
8039 if (atstart)
8040 {
8041 atstart = FALSE;
8042 /* Call msg_start() after eval1(), evaluating the expression
8043 * may cause a message to appear. */
8044 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008045 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008046 /* Mark the saved text as finishing the line, so that what
8047 * follows is displayed on a new line when scrolling back
8048 * at the more prompt. */
8049 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 }
8053 else if (eap->cmdidx == CMD_echo)
8054 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008055 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008056 if (p != NULL)
8057 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008059 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008061 if (*p != TAB && needclr)
8062 {
8063 /* remove any text still there from the command */
8064 msg_clr_eos();
8065 needclr = FALSE;
8066 }
8067 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 }
8069 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008070 {
8071#ifdef FEAT_MBYTE
8072 if (has_mbyte)
8073 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008074 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008075
8076 (void)msg_outtrans_len_attr(p, i, echo_attr);
8077 p += i - 1;
8078 }
8079 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008081 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008084 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008086 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 arg = skipwhite(arg);
8088 }
8089 eap->nextcmd = check_nextcmd(arg);
8090
8091 if (eap->skip)
8092 --emsg_skip;
8093 else
8094 {
8095 /* remove text that may still be there from the command */
8096 if (needclr)
8097 msg_clr_eos();
8098 if (eap->cmdidx == CMD_echo)
8099 msg_end();
8100 }
8101}
8102
8103/*
8104 * ":echohl {name}".
8105 */
8106 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008107ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008109 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110}
8111
8112/*
8113 * ":execute expr1 ..." execute the result of an expression.
8114 * ":echomsg expr1 ..." Print a message
8115 * ":echoerr expr1 ..." Print an error
8116 * Each gets spaces around each argument and a newline at the end for
8117 * echo commands
8118 */
8119 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008120ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121{
8122 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008123 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 int ret = OK;
8125 char_u *p;
8126 garray_T ga;
8127 int len;
8128 int save_did_emsg;
8129
8130 ga_init2(&ga, 1, 80);
8131
8132 if (eap->skip)
8133 ++emsg_skip;
8134 while (*arg != NUL && *arg != '|' && *arg != '\n')
8135 {
8136 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008137 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 {
8139 /*
8140 * Report the invalid expression unless the expression evaluation
8141 * has been cancelled due to an aborting error, an interrupt, or an
8142 * exception.
8143 */
8144 if (!aborting())
8145 EMSG2(_(e_invexpr2), p);
8146 ret = FAIL;
8147 break;
8148 }
8149
8150 if (!eap->skip)
8151 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008152 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 len = (int)STRLEN(p);
8154 if (ga_grow(&ga, len + 2) == FAIL)
8155 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008156 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 ret = FAIL;
8158 break;
8159 }
8160 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 ga.ga_len += len;
8164 }
8165
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008166 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 arg = skipwhite(arg);
8168 }
8169
8170 if (ret != FAIL && ga.ga_data != NULL)
8171 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008172 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8173 {
8174 /* Mark the already saved text as finishing the line, so that what
8175 * follows is displayed on a new line when scrolling back at the
8176 * more prompt. */
8177 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008178 }
8179
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008181 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008183 out_flush();
8184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 else if (eap->cmdidx == CMD_echoerr)
8186 {
8187 /* We don't want to abort following commands, restore did_emsg. */
8188 save_did_emsg = did_emsg;
8189 EMSG((char_u *)ga.ga_data);
8190 if (!force_abort)
8191 did_emsg = save_did_emsg;
8192 }
8193 else if (eap->cmdidx == CMD_execute)
8194 do_cmdline((char_u *)ga.ga_data,
8195 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8196 }
8197
8198 ga_clear(&ga);
8199
8200 if (eap->skip)
8201 --emsg_skip;
8202
8203 eap->nextcmd = check_nextcmd(arg);
8204}
8205
8206/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008207 * Find window specified by "vp" in tabpage "tp".
8208 */
8209 win_T *
8210find_win_by_nr(
8211 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01008212 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008213{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008214 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008215 int nr;
8216
8217 nr = (int)get_tv_number_chk(vp, NULL);
8218
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008219 if (nr < 0)
8220 return NULL;
8221 if (nr == 0)
8222 return curwin;
8223
Bram Moolenaar29323592016-07-24 22:04:11 +02008224 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8225 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008226 if (nr >= LOWEST_WIN_ID)
8227 {
8228 if (wp->w_id == nr)
8229 return wp;
8230 }
8231 else if (--nr <= 0)
8232 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008233 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008234 if (nr >= LOWEST_WIN_ID)
8235 return NULL;
8236 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008237}
8238
8239/*
8240 * Find window specified by "wvp" in tabpage "tvp".
8241 */
8242 win_T *
8243find_tabwin(
8244 typval_T *wvp, /* VAR_UNKNOWN for current window */
8245 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
8246{
8247 win_T *wp = NULL;
8248 tabpage_T *tp = NULL;
8249 long n;
8250
8251 if (wvp->v_type != VAR_UNKNOWN)
8252 {
8253 if (tvp->v_type != VAR_UNKNOWN)
8254 {
8255 n = (long)get_tv_number(tvp);
8256 if (n >= 0)
8257 tp = find_tabpage(n);
8258 }
8259 else
8260 tp = curtab;
8261
8262 if (tp != NULL)
8263 wp = find_win_by_nr(wvp, tp);
8264 }
8265 else
8266 wp = curwin;
8267
8268 return wp;
8269}
8270
8271/*
8272 * getwinvar() and gettabwinvar()
8273 */
8274 void
8275getwinvar(
8276 typval_T *argvars,
8277 typval_T *rettv,
8278 int off) /* 1 for gettabwinvar() */
8279{
8280 win_T *win;
8281 char_u *varname;
8282 dictitem_T *v;
8283 tabpage_T *tp = NULL;
8284 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008285 win_T *oldcurwin;
8286 tabpage_T *oldtabpage;
8287 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008288
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008289 if (off == 1)
8290 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8291 else
8292 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008293 win = find_win_by_nr(&argvars[off], tp);
8294 varname = get_tv_string_chk(&argvars[off + 1]);
8295 ++emsg_off;
8296
8297 rettv->v_type = VAR_STRING;
8298 rettv->vval.v_string = NULL;
8299
8300 if (win != NULL && varname != NULL)
8301 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008302 /* Set curwin to be our win, temporarily. Also set the tabpage,
8303 * otherwise the window is not valid. Only do this when needed,
8304 * autocommands get blocked. */
8305 need_switch_win = !(tp == curtab && win == curwin);
8306 if (!need_switch_win
8307 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008308 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008309 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008310 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008311 if (varname[1] == NUL)
8312 {
8313 /* get all window-local options in a dict */
8314 dict_T *opts = get_winbuf_options(FALSE);
8315
8316 if (opts != NULL)
8317 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008318 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02008319 done = TRUE;
8320 }
8321 }
8322 else if (get_option_tv(&varname, rettv, 1) == OK)
8323 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008324 done = TRUE;
8325 }
8326 else
8327 {
8328 /* Look up the variable. */
8329 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8330 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8331 varname, FALSE);
8332 if (v != NULL)
8333 {
8334 copy_tv(&v->di_tv, rettv);
8335 done = TRUE;
8336 }
8337 }
8338 }
8339
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008340 if (need_switch_win)
8341 /* restore previous notion of curwin */
8342 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008343 }
8344
8345 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8346 /* use the default return value */
8347 copy_tv(&argvars[off + 2], rettv);
8348
8349 --emsg_off;
8350}
8351
8352/*
8353 * "setwinvar()" and "settabwinvar()" functions
8354 */
8355 void
8356setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8357{
8358 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008359 win_T *save_curwin;
8360 tabpage_T *save_curtab;
8361 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008362 char_u *varname, *winvarname;
8363 typval_T *varp;
8364 char_u nbuf[NUMBUFLEN];
8365 tabpage_T *tp = NULL;
8366
8367 if (check_restricted() || check_secure())
8368 return;
8369
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008370 if (off == 1)
8371 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8372 else
8373 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008374 win = find_win_by_nr(&argvars[off], tp);
8375 varname = get_tv_string_chk(&argvars[off + 1]);
8376 varp = &argvars[off + 2];
8377
8378 if (win != NULL && varname != NULL && varp != NULL)
8379 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008380 need_switch_win = !(tp == curtab && win == curwin);
8381 if (!need_switch_win
8382 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008383 {
8384 if (*varname == '&')
8385 {
8386 long numval;
8387 char_u *strval;
8388 int error = FALSE;
8389
8390 ++varname;
8391 numval = (long)get_tv_number_chk(varp, &error);
8392 strval = get_tv_string_buf_chk(varp, nbuf);
8393 if (!error && strval != NULL)
8394 set_option_value(varname, numval, strval, OPT_LOCAL);
8395 }
8396 else
8397 {
8398 winvarname = alloc((unsigned)STRLEN(varname) + 3);
8399 if (winvarname != NULL)
8400 {
8401 STRCPY(winvarname, "w:");
8402 STRCPY(winvarname + 2, varname);
8403 set_var(winvarname, varp, TRUE);
8404 vim_free(winvarname);
8405 }
8406 }
8407 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008408 if (need_switch_win)
8409 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008410 }
8411}
8412
8413/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8415 * "arg" points to the "&" or '+' when called, to "option" when returning.
8416 * Returns NULL when no option name found. Otherwise pointer to the char
8417 * after the option name.
8418 */
8419 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008420find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421{
8422 char_u *p = *arg;
8423
8424 ++p;
8425 if (*p == 'g' && p[1] == ':')
8426 {
8427 *opt_flags = OPT_GLOBAL;
8428 p += 2;
8429 }
8430 else if (*p == 'l' && p[1] == ':')
8431 {
8432 *opt_flags = OPT_LOCAL;
8433 p += 2;
8434 }
8435 else
8436 *opt_flags = 0;
8437
8438 if (!ASCII_ISALPHA(*p))
8439 return NULL;
8440 *arg = p;
8441
8442 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
8443 p += 4; /* termcap option */
8444 else
8445 while (ASCII_ISALPHA(*p))
8446 ++p;
8447 return p;
8448}
8449
8450/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008451 * Return the autoload script name for a function or variable name.
8452 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02008454 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008455autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02008456{
Bram Moolenaara1544c02013-05-30 12:35:52 +02008457 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008458 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02008459
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008460 /* Get the script file name: replace '#' with '/', append ".vim". */
8461 scriptname = alloc((unsigned)(STRLEN(name) + 14));
8462 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008463 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008464 STRCPY(scriptname, "autoload/");
8465 STRCAT(scriptname, name);
8466 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
8467 STRCAT(scriptname, ".vim");
8468 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
8469 *p = '/';
8470 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008471}
8472
8473/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008474 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008475 * Return TRUE if a package was loaded.
8476 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008477 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008478script_autoload(
8479 char_u *name,
8480 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008481{
8482 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008483 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008484 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008485 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008486
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008487 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008488 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008489 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008490 return FALSE;
8491
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008492 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008493
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008494 /* Find the name in the list of previously loaded package names. Skip
8495 * "autoload/", it's always the same. */
8496 for (i = 0; i < ga_loaded.ga_len; ++i)
8497 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
8498 break;
8499 if (!reload && i < ga_loaded.ga_len)
8500 ret = FALSE; /* was loaded already */
8501 else
8502 {
8503 /* Remember the name if it wasn't loaded already. */
8504 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
8505 {
8506 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
8507 tofree = NULL;
8508 }
8509
8510 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008511 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008512 ret = TRUE;
8513 }
8514
8515 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008516 return ret;
8517}
8518
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
8520typedef enum
8521{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008522 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
8523 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
8524 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525} var_flavour_T;
8526
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008527static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528
8529 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01008530var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531{
8532 char_u *p = varname;
8533
8534 if (ASCII_ISUPPER(*p))
8535 {
8536 while (*(++p))
8537 if (ASCII_ISLOWER(*p))
8538 return VAR_FLAVOUR_SESSION;
8539 return VAR_FLAVOUR_VIMINFO;
8540 }
8541 else
8542 return VAR_FLAVOUR_DEFAULT;
8543}
8544#endif
8545
8546#if defined(FEAT_VIMINFO) || defined(PROTO)
8547/*
8548 * Restore global vars that start with a capital from the viminfo file
8549 */
8550 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008551read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552{
8553 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008554 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00008555 typval_T tv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008556 void *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557
8558 if (!writing && (find_viminfo_parameter('!') != NULL))
8559 {
8560 tab = vim_strchr(virp->vir_line + 1, '\t');
8561 if (tab != NULL)
8562 {
8563 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008564 switch (*tab)
8565 {
8566 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008567#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008568 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008569#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008570 case 'D': type = VAR_DICT; break;
8571 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008572 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574
8575 tab = vim_strchr(tab, '\t');
8576 if (tab != NULL)
8577 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008578 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008579 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008580 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008582#ifdef FEAT_FLOAT
8583 else if (type == VAR_FLOAT)
8584 (void)string2float(tab + 1, &tv.vval.v_float);
8585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008587 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008588 if (type == VAR_DICT || type == VAR_LIST)
8589 {
8590 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
8591
8592 if (etv == NULL)
8593 /* Failed to parse back the dict or list, use it as a
8594 * string. */
8595 tv.v_type = VAR_STRING;
8596 else
8597 {
8598 vim_free(tv.vval.v_string);
8599 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01008600 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008601 }
8602 }
8603
Bram Moolenaarb20e3342016-01-18 23:29:01 +01008604 /* when in a function use global variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008605 save_funccal = clear_current_funccal();
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008606 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008607 restore_current_funccal(save_funccal);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008608
8609 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008610 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008611 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
8612 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 }
8614 }
8615 }
8616
8617 return viminfo_readline(virp);
8618}
8619
8620/*
8621 * Write global vars that start with a capital to the viminfo file
8622 */
8623 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008624write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625{
Bram Moolenaar33570922005-01-25 22:26:29 +00008626 hashitem_T *hi;
8627 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008628 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01008629 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008630 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008631 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008632 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633
8634 if (find_viminfo_parameter('!') == NULL)
8635 return;
8636
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02008637 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00008638
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008639 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008640 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008642 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008644 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008645 this_var = HI2DI(hi);
8646 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008647 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008648 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00008649 {
8650 case VAR_STRING: s = "STR"; break;
8651 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008652 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008653 case VAR_DICT: s = "DIC"; break;
8654 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008655 case VAR_SPECIAL: s = "XPL"; break;
8656
8657 case VAR_UNKNOWN:
8658 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008659 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008660 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008661 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008662 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00008663 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008664 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008665 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008666 if (p != NULL)
8667 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00008668 vim_free(tofree);
8669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 }
8671 }
8672}
8673#endif
8674
8675#if defined(FEAT_SESSION) || defined(PROTO)
8676 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008677store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678{
Bram Moolenaar33570922005-01-25 22:26:29 +00008679 hashitem_T *hi;
8680 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008681 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 char_u *p, *t;
8683
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008684 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008685 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008687 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008689 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008690 this_var = HI2DI(hi);
8691 if ((this_var->di_tv.v_type == VAR_NUMBER
8692 || this_var->di_tv.v_type == VAR_STRING)
8693 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008694 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008695 /* Escape special characters with a backslash. Turn a LF and
8696 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008697 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00008698 (char_u *)"\\\"\n\r");
8699 if (p == NULL) /* out of memory */
8700 break;
8701 for (t = p; *t != NUL; ++t)
8702 if (*t == '\n')
8703 *t = 'n';
8704 else if (*t == '\r')
8705 *t = 'r';
8706 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00008707 this_var->di_key,
8708 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8709 : ' ',
8710 p,
8711 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8712 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00008713 || put_eol(fd) == FAIL)
8714 {
8715 vim_free(p);
8716 return FAIL;
8717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 vim_free(p);
8719 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008720#ifdef FEAT_FLOAT
8721 else if (this_var->di_tv.v_type == VAR_FLOAT
8722 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
8723 {
8724 float_T f = this_var->di_tv.vval.v_float;
8725 int sign = ' ';
8726
8727 if (f < 0)
8728 {
8729 f = -f;
8730 sign = '-';
8731 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01008732 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008733 this_var->di_key, sign, f) < 0)
8734 || put_eol(fd) == FAIL)
8735 return FAIL;
8736 }
8737#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 }
8739 }
8740 return OK;
8741}
8742#endif
8743
Bram Moolenaar661b1822005-07-28 22:36:45 +00008744/*
8745 * Display script name where an item was last set.
8746 * Should only be invoked when 'verbose' is non-zero.
8747 */
8748 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008749last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +00008750{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008751 char_u *p;
8752
Bram Moolenaar661b1822005-07-28 22:36:45 +00008753 if (scriptID != 0)
8754 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008755 p = home_replace_save(NULL, get_scriptname(scriptID));
8756 if (p != NULL)
8757 {
8758 verbose_enter();
8759 MSG_PUTS(_("\n\tLast set from "));
8760 MSG_PUTS(p);
8761 vim_free(p);
8762 verbose_leave();
8763 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00008764 }
8765}
8766
Bram Moolenaar53744302015-07-17 17:38:22 +02008767/* reset v:option_new, v:option_old and v:option_type */
8768 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008769reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02008770{
8771 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
8772 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
8773 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
8774}
8775
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008776/*
8777 * Prepare "gap" for an assert error and add the sourcing position.
8778 */
8779 void
8780prepare_assert_error(garray_T *gap)
8781{
8782 char buf[NUMBUFLEN];
8783
8784 ga_init2(gap, 1, 100);
8785 if (sourcing_name != NULL)
8786 {
8787 ga_concat(gap, sourcing_name);
8788 if (sourcing_lnum > 0)
8789 ga_concat(gap, (char_u *)" ");
8790 }
8791 if (sourcing_lnum > 0)
8792 {
8793 sprintf(buf, "line %ld", (long)sourcing_lnum);
8794 ga_concat(gap, (char_u *)buf);
8795 }
8796 if (sourcing_name != NULL || sourcing_lnum > 0)
8797 ga_concat(gap, (char_u *)": ");
8798}
8799
8800/*
8801 * Add an assert error to v:errors.
8802 */
8803 void
8804assert_error(garray_T *gap)
8805{
8806 struct vimvar *vp = &vimvars[VV_ERRORS];
8807
8808 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
8809 /* Make sure v:errors is a list. */
8810 set_vim_var_list(VV_ERRORS, list_alloc());
8811 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
8812}
8813
Bram Moolenaar65a54642018-04-28 16:56:53 +02008814 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008815assert_equal_common(typval_T *argvars, assert_type_T atype)
8816{
8817 garray_T ga;
8818
8819 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
8820 != (atype == ASSERT_EQUAL))
8821 {
8822 prepare_assert_error(&ga);
8823 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
8824 atype);
8825 assert_error(&ga);
8826 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02008827 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008828 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02008829 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008830}
8831
Bram Moolenaar65a54642018-04-28 16:56:53 +02008832 int
Bram Moolenaard96ff162018-02-18 22:13:29 +01008833assert_equalfile(typval_T *argvars)
8834{
8835 char_u buf1[NUMBUFLEN];
8836 char_u buf2[NUMBUFLEN];
8837 char_u *fname1 = get_tv_string_buf_chk(&argvars[0], buf1);
8838 char_u *fname2 = get_tv_string_buf_chk(&argvars[1], buf2);
8839 garray_T ga;
8840 FILE *fd1;
8841 FILE *fd2;
8842
8843 if (fname1 == NULL || fname2 == NULL)
Bram Moolenaar65a54642018-04-28 16:56:53 +02008844 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01008845
8846 IObuff[0] = NUL;
8847 fd1 = mch_fopen((char *)fname1, READBIN);
8848 if (fd1 == NULL)
8849 {
8850 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname1);
8851 }
8852 else
8853 {
8854 fd2 = mch_fopen((char *)fname2, READBIN);
8855 if (fd2 == NULL)
8856 {
8857 fclose(fd1);
8858 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname2);
8859 }
8860 else
8861 {
8862 int c1, c2;
8863 long count = 0;
8864
8865 for (;;)
8866 {
8867 c1 = fgetc(fd1);
8868 c2 = fgetc(fd2);
8869 if (c1 == EOF)
8870 {
8871 if (c2 != EOF)
8872 STRCPY(IObuff, "first file is shorter");
8873 break;
8874 }
8875 else if (c2 == EOF)
8876 {
8877 STRCPY(IObuff, "second file is shorter");
8878 break;
8879 }
8880 else if (c1 != c2)
8881 {
8882 vim_snprintf((char *)IObuff, IOSIZE,
8883 "difference at byte %ld", count);
8884 break;
8885 }
8886 ++count;
8887 }
Bram Moolenaar30494182018-02-20 21:46:05 +01008888 fclose(fd1);
8889 fclose(fd2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01008890 }
8891 }
8892 if (IObuff[0] != NUL)
8893 {
8894 prepare_assert_error(&ga);
8895 ga_concat(&ga, IObuff);
8896 assert_error(&ga);
8897 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02008898 return 1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01008899 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02008900 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01008901}
8902
Bram Moolenaar65a54642018-04-28 16:56:53 +02008903 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008904assert_match_common(typval_T *argvars, assert_type_T atype)
8905{
8906 garray_T ga;
8907 char_u buf1[NUMBUFLEN];
8908 char_u buf2[NUMBUFLEN];
8909 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
8910 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
8911
8912 if (pat == NULL || text == NULL)
8913 EMSG(_(e_invarg));
8914 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
8915 {
8916 prepare_assert_error(&ga);
8917 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
8918 atype);
8919 assert_error(&ga);
8920 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02008921 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008922 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02008923 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008924}
8925
Bram Moolenaar65a54642018-04-28 16:56:53 +02008926 int
Bram Moolenaar61c04492016-07-23 15:35:35 +02008927assert_inrange(typval_T *argvars)
8928{
8929 garray_T ga;
8930 int error = FALSE;
8931 varnumber_T lower = get_tv_number_chk(&argvars[0], &error);
8932 varnumber_T upper = get_tv_number_chk(&argvars[1], &error);
8933 varnumber_T actual = get_tv_number_chk(&argvars[2], &error);
8934 char_u *tofree;
8935 char msg[200];
8936 char_u numbuf[NUMBUFLEN];
8937
8938 if (error)
Bram Moolenaar65a54642018-04-28 16:56:53 +02008939 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02008940 if (actual < lower || actual > upper)
8941 {
8942 prepare_assert_error(&ga);
8943 if (argvars[3].v_type != VAR_UNKNOWN)
8944 {
8945 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
8946 vim_free(tofree);
8947 }
8948 else
8949 {
8950 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
8951 (long)lower, (long)upper, (long)actual);
8952 ga_concat(&ga, (char_u *)msg);
8953 }
8954 assert_error(&ga);
8955 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02008956 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02008957 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02008958 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02008959}
8960
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008961/*
8962 * Common for assert_true() and assert_false().
Bram Moolenaar65a54642018-04-28 16:56:53 +02008963 * Return non-zero for failure.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008964 */
Bram Moolenaar65a54642018-04-28 16:56:53 +02008965 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008966assert_bool(typval_T *argvars, int isTrue)
8967{
8968 int error = FALSE;
8969 garray_T ga;
8970
8971 if (argvars[0].v_type == VAR_SPECIAL
8972 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar65a54642018-04-28 16:56:53 +02008973 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008974 if (argvars[0].v_type != VAR_NUMBER
8975 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
8976 || error)
8977 {
8978 prepare_assert_error(&ga);
8979 fill_assert_error(&ga, &argvars[1],
8980 (char_u *)(isTrue ? "True" : "False"),
8981 NULL, &argvars[0], ASSERT_OTHER);
8982 assert_error(&ga);
8983 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02008984 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008985 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02008986 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008987}
8988
Bram Moolenaar65a54642018-04-28 16:56:53 +02008989 int
Bram Moolenaar42205552017-03-18 19:42:22 +01008990assert_report(typval_T *argvars)
8991{
8992 garray_T ga;
8993
8994 prepare_assert_error(&ga);
8995 ga_concat(&ga, get_tv_string(&argvars[0]));
8996 assert_error(&ga);
8997 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02008998 return 1;
Bram Moolenaar42205552017-03-18 19:42:22 +01008999}
9000
Bram Moolenaar65a54642018-04-28 16:56:53 +02009001 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009002assert_exception(typval_T *argvars)
9003{
9004 garray_T ga;
9005 char_u *error = get_tv_string_chk(&argvars[0]);
9006
9007 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9008 {
9009 prepare_assert_error(&ga);
9010 ga_concat(&ga, (char_u *)"v:exception is not set");
9011 assert_error(&ga);
9012 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009013 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009014 }
9015 else if (error != NULL
9016 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9017 {
9018 prepare_assert_error(&ga);
9019 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9020 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9021 assert_error(&ga);
9022 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009023 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009024 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009025 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009026}
9027
Bram Moolenaar65a54642018-04-28 16:56:53 +02009028 int
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009029assert_beeps(typval_T *argvars)
9030{
9031 char_u *cmd = get_tv_string_chk(&argvars[0]);
9032 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009033 int ret = 0;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009034
9035 called_vim_beep = FALSE;
9036 suppress_errthrow = TRUE;
9037 emsg_silent = FALSE;
9038 do_cmdline_cmd(cmd);
9039 if (!called_vim_beep)
9040 {
9041 prepare_assert_error(&ga);
9042 ga_concat(&ga, (char_u *)"command did not beep: ");
9043 ga_concat(&ga, cmd);
9044 assert_error(&ga);
9045 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009046 ret = 1;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009047 }
9048
9049 suppress_errthrow = FALSE;
9050 emsg_on_display = FALSE;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009051 return ret;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009052}
9053
Bram Moolenaar65a54642018-04-28 16:56:53 +02009054 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009055assert_fails(typval_T *argvars)
9056{
9057 char_u *cmd = get_tv_string_chk(&argvars[0]);
9058 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009059 int ret = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009060
9061 called_emsg = FALSE;
9062 suppress_errthrow = TRUE;
9063 emsg_silent = TRUE;
9064 do_cmdline_cmd(cmd);
9065 if (!called_emsg)
9066 {
9067 prepare_assert_error(&ga);
9068 ga_concat(&ga, (char_u *)"command did not fail: ");
9069 ga_concat(&ga, cmd);
9070 assert_error(&ga);
9071 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009072 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009073 }
9074 else if (argvars[1].v_type != VAR_UNKNOWN)
9075 {
9076 char_u buf[NUMBUFLEN];
9077 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9078
9079 if (error == NULL
9080 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9081 {
9082 prepare_assert_error(&ga);
9083 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9084 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
9085 assert_error(&ga);
9086 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009087 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009088 }
9089 }
9090
9091 called_emsg = FALSE;
9092 suppress_errthrow = FALSE;
9093 emsg_silent = FALSE;
9094 emsg_on_display = FALSE;
9095 set_vim_var_string(VV_ERRMSG, NULL, 0);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009096 return ret;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009097}
9098
9099/*
9100 * Append "str" to "gap", escaping unprintable characters.
9101 * Changes NL to \n, CR to \r, etc.
9102 */
9103 static void
9104ga_concat_esc(garray_T *gap, char_u *str)
9105{
9106 char_u *p;
9107 char_u buf[NUMBUFLEN];
9108
9109 if (str == NULL)
9110 {
9111 ga_concat(gap, (char_u *)"NULL");
9112 return;
9113 }
9114
9115 for (p = str; *p != NUL; ++p)
9116 switch (*p)
9117 {
9118 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9119 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9120 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9121 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9122 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9123 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9124 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9125 default:
9126 if (*p < ' ')
9127 {
9128 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9129 ga_concat(gap, buf);
9130 }
9131 else
9132 ga_append(gap, *p);
9133 break;
9134 }
9135}
9136
9137/*
9138 * Fill "gap" with information about an assert error.
9139 */
9140 void
9141fill_assert_error(
9142 garray_T *gap,
9143 typval_T *opt_msg_tv,
9144 char_u *exp_str,
9145 typval_T *exp_tv,
9146 typval_T *got_tv,
9147 assert_type_T atype)
9148{
9149 char_u numbuf[NUMBUFLEN];
9150 char_u *tofree;
9151
9152 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9153 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009154 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9155 vim_free(tofree);
9156 ga_concat(gap, (char_u *)": ");
9157 }
9158
9159 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9160 ga_concat(gap, (char_u *)"Pattern ");
9161 else if (atype == ASSERT_NOTEQUAL)
9162 ga_concat(gap, (char_u *)"Expected not equal to ");
9163 else
9164 ga_concat(gap, (char_u *)"Expected ");
9165 if (exp_str == NULL)
9166 {
9167 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009168 vim_free(tofree);
9169 }
9170 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009171 ga_concat_esc(gap, exp_str);
9172 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009173 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009174 if (atype == ASSERT_MATCH)
9175 ga_concat(gap, (char_u *)" does not match ");
9176 else if (atype == ASSERT_NOTMATCH)
9177 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009178 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009179 ga_concat(gap, (char_u *)" but got ");
9180 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
9181 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009182 }
9183}
9184
Bram Moolenaar31988702018-02-13 12:57:42 +01009185/*
9186 * Compare "typ1" and "typ2". Put the result in "typ1".
9187 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009188 int
9189typval_compare(
9190 typval_T *typ1, /* first operand */
9191 typval_T *typ2, /* second operand */
9192 exptype_T type, /* operator */
9193 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +01009194 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009195{
9196 int i;
9197 varnumber_T n1, n2;
9198 char_u *s1, *s2;
9199 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
9200
Bram Moolenaar31988702018-02-13 12:57:42 +01009201 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009202 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009203 /* For "is" a different type always means FALSE, for "notis"
9204 * it means TRUE. */
9205 n1 = (type == TYPE_NEQUAL);
9206 }
9207 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
9208 {
9209 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009210 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009211 n1 = (typ1->v_type == typ2->v_type
9212 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009213 if (type == TYPE_NEQUAL)
9214 n1 = !n1;
9215 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009216 else if (typ1->v_type != typ2->v_type
9217 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009218 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009219 if (typ1->v_type != typ2->v_type)
9220 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009221 else
Bram Moolenaar31988702018-02-13 12:57:42 +01009222 EMSG(_("E692: Invalid operation for List"));
9223 clear_tv(typ1);
9224 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009225 }
9226 else
9227 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009228 /* Compare two Lists for being equal or unequal. */
9229 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
9230 ic, FALSE);
9231 if (type == TYPE_NEQUAL)
9232 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009233 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009234 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009235
9236 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
9237 {
9238 if (type_is)
9239 {
9240 n1 = (typ1->v_type == typ2->v_type
9241 && typ1->vval.v_dict == typ2->vval.v_dict);
9242 if (type == TYPE_NEQUAL)
9243 n1 = !n1;
9244 }
9245 else if (typ1->v_type != typ2->v_type
9246 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9247 {
9248 if (typ1->v_type != typ2->v_type)
9249 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
9250 else
9251 EMSG(_("E736: Invalid operation for Dictionary"));
9252 clear_tv(typ1);
9253 return FAIL;
9254 }
9255 else
9256 {
9257 /* Compare two Dictionaries for being equal or unequal. */
9258 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
9259 ic, FALSE);
9260 if (type == TYPE_NEQUAL)
9261 n1 = !n1;
9262 }
9263 }
9264
9265 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
9266 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
9267 {
9268 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
9269 {
9270 EMSG(_("E694: Invalid operation for Funcrefs"));
9271 clear_tv(typ1);
9272 return FAIL;
9273 }
9274 if ((typ1->v_type == VAR_PARTIAL
9275 && typ1->vval.v_partial == NULL)
9276 || (typ2->v_type == VAR_PARTIAL
9277 && typ2->vval.v_partial == NULL))
9278 /* when a partial is NULL assume not equal */
9279 n1 = FALSE;
9280 else if (type_is)
9281 {
9282 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
9283 /* strings are considered the same if their value is
9284 * the same */
9285 n1 = tv_equal(typ1, typ2, ic, FALSE);
9286 else if (typ1->v_type == VAR_PARTIAL
9287 && typ2->v_type == VAR_PARTIAL)
9288 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
9289 else
9290 n1 = FALSE;
9291 }
9292 else
9293 n1 = tv_equal(typ1, typ2, ic, FALSE);
9294 if (type == TYPE_NEQUAL)
9295 n1 = !n1;
9296 }
9297
9298#ifdef FEAT_FLOAT
9299 /*
9300 * If one of the two variables is a float, compare as a float.
9301 * When using "=~" or "!~", always compare as string.
9302 */
9303 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
9304 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9305 {
9306 float_T f1, f2;
9307
9308 if (typ1->v_type == VAR_FLOAT)
9309 f1 = typ1->vval.v_float;
9310 else
9311 f1 = get_tv_number(typ1);
9312 if (typ2->v_type == VAR_FLOAT)
9313 f2 = typ2->vval.v_float;
9314 else
9315 f2 = get_tv_number(typ2);
9316 n1 = FALSE;
9317 switch (type)
9318 {
9319 case TYPE_EQUAL: n1 = (f1 == f2); break;
9320 case TYPE_NEQUAL: n1 = (f1 != f2); break;
9321 case TYPE_GREATER: n1 = (f1 > f2); break;
9322 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
9323 case TYPE_SMALLER: n1 = (f1 < f2); break;
9324 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
9325 case TYPE_UNKNOWN:
9326 case TYPE_MATCH:
9327 case TYPE_NOMATCH: break; /* avoid gcc warning */
9328 }
9329 }
9330#endif
9331
9332 /*
9333 * If one of the two variables is a number, compare as a number.
9334 * When using "=~" or "!~", always compare as string.
9335 */
9336 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
9337 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9338 {
9339 n1 = get_tv_number(typ1);
9340 n2 = get_tv_number(typ2);
9341 switch (type)
9342 {
9343 case TYPE_EQUAL: n1 = (n1 == n2); break;
9344 case TYPE_NEQUAL: n1 = (n1 != n2); break;
9345 case TYPE_GREATER: n1 = (n1 > n2); break;
9346 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
9347 case TYPE_SMALLER: n1 = (n1 < n2); break;
9348 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
9349 case TYPE_UNKNOWN:
9350 case TYPE_MATCH:
9351 case TYPE_NOMATCH: break; /* avoid gcc warning */
9352 }
9353 }
9354 else
9355 {
9356 s1 = get_tv_string_buf(typ1, buf1);
9357 s2 = get_tv_string_buf(typ2, buf2);
9358 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
9359 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
9360 else
9361 i = 0;
9362 n1 = FALSE;
9363 switch (type)
9364 {
9365 case TYPE_EQUAL: n1 = (i == 0); break;
9366 case TYPE_NEQUAL: n1 = (i != 0); break;
9367 case TYPE_GREATER: n1 = (i > 0); break;
9368 case TYPE_GEQUAL: n1 = (i >= 0); break;
9369 case TYPE_SMALLER: n1 = (i < 0); break;
9370 case TYPE_SEQUAL: n1 = (i <= 0); break;
9371
9372 case TYPE_MATCH:
9373 case TYPE_NOMATCH:
9374 n1 = pattern_match(s2, s1, ic);
9375 if (type == TYPE_NOMATCH)
9376 n1 = !n1;
9377 break;
9378
9379 case TYPE_UNKNOWN: break; /* avoid gcc warning */
9380 }
9381 }
9382 clear_tv(typ1);
9383 typ1->v_type = VAR_NUMBER;
9384 typ1->vval.v_number = n1;
9385
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009386 return OK;
9387}
9388
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009389 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02009390typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009391{
9392 char_u *tofree;
9393 char_u numbuf[NUMBUFLEN];
9394 char_u *ret = NULL;
9395
9396 if (arg == NULL)
9397 return vim_strsave((char_u *)"(does not exist)");
9398 ret = tv2string(arg, &tofree, numbuf, 0);
9399 /* Make a copy if we have a value but it's not in allocated memory. */
9400 if (ret != NULL && tofree == NULL)
9401 ret = vim_strsave(ret);
9402 return ret;
9403}
9404
9405 int
9406var_exists(char_u *var)
9407{
9408 char_u *name;
9409 char_u *tofree;
9410 typval_T tv;
9411 int len = 0;
9412 int n = FALSE;
9413
9414 /* get_name_len() takes care of expanding curly braces */
9415 name = var;
9416 len = get_name_len(&var, &tofree, TRUE, FALSE);
9417 if (len > 0)
9418 {
9419 if (tofree != NULL)
9420 name = tofree;
9421 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
9422 if (n)
9423 {
9424 /* handle d.key, l[idx], f(expr) */
9425 n = (handle_subscript(&var, &tv, TRUE, FALSE) == OK);
9426 if (n)
9427 clear_tv(&tv);
9428 }
9429 }
9430 if (*var != NUL)
9431 n = FALSE;
9432
9433 vim_free(tofree);
9434 return n;
9435}
9436
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437#endif /* FEAT_EVAL */
9438
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009440#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441
9442#ifdef WIN3264
9443/*
9444 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9445 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009446static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
9447static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
9448static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449
9450/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009451 * Get the short path (8.3) for the filename in "fnamep".
9452 * Only works for a valid file name.
9453 * When the path gets longer "fnamep" is changed and the allocated buffer
9454 * is put in "bufp".
9455 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9456 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457 */
9458 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009459get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009461 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 char_u *newbuf;
9463
9464 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009465 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466 if (l > len - 1)
9467 {
9468 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009469 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470 newbuf = vim_strnsave(*fnamep, l);
9471 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009472 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473
9474 vim_free(*bufp);
9475 *fnamep = *bufp = newbuf;
9476
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009477 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009478 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479 }
9480
9481 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009482 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483}
9484
9485/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009486 * Get the short path (8.3) for the filename in "fname". The converted
9487 * path is returned in "bufp".
9488 *
9489 * Some of the directories specified in "fname" may not exist. This function
9490 * will shorten the existing directories at the beginning of the path and then
9491 * append the remaining non-existing path.
9492 *
9493 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009494 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009495 * bufp - Pointer to an allocated buffer for the filename.
9496 * fnamelen - Length of the filename pointed to by fname
9497 *
9498 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 */
9500 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009501shortpath_for_invalid_fname(
9502 char_u **fname,
9503 char_u **bufp,
9504 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009506 char_u *short_fname, *save_fname, *pbuf_unused;
9507 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009509 int old_len, len;
9510 int new_len, sfx_len;
9511 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512
9513 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009514 old_len = *fnamelen;
9515 save_fname = vim_strnsave(*fname, old_len);
9516 pbuf_unused = NULL;
9517 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009519 endp = save_fname + old_len - 1; /* Find the end of the copy */
9520 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009522 /*
9523 * Try shortening the supplied path till it succeeds by removing one
9524 * directory at a time from the tail of the path.
9525 */
9526 len = 0;
9527 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009529 /* go back one path-separator */
9530 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9531 --endp;
9532 if (endp <= save_fname)
9533 break; /* processed the complete path */
9534
9535 /*
9536 * Replace the path separator with a NUL and try to shorten the
9537 * resulting path.
9538 */
9539 ch = *endp;
9540 *endp = 0;
9541 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009542 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009543 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9544 {
9545 retval = FAIL;
9546 goto theend;
9547 }
9548 *endp = ch; /* preserve the string */
9549
9550 if (len > 0)
9551 break; /* successfully shortened the path */
9552
9553 /* failed to shorten the path. Skip the path separator */
9554 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555 }
9556
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009557 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009559 /*
9560 * Succeeded in shortening the path. Now concatenate the shortened
9561 * path with the remaining path at the tail.
9562 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009564 /* Compute the length of the new path. */
9565 sfx_len = (int)(save_endp - endp) + 1;
9566 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009568 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009570 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009572 /* There is not enough space in the currently allocated string,
9573 * copy it to a buffer big enough. */
9574 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009576 {
9577 retval = FAIL;
9578 goto theend;
9579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580 }
9581 else
9582 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009583 /* Transfer short_fname to the main buffer (it's big enough),
9584 * unless get_short_pathname() did its work in-place. */
9585 *fname = *bufp = save_fname;
9586 if (short_fname != save_fname)
9587 vim_strncpy(save_fname, short_fname, len);
9588 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009590
9591 /* concat the not-shortened part of the path */
9592 vim_strncpy(*fname + len, endp, sfx_len);
9593 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009595
9596theend:
9597 vim_free(pbuf_unused);
9598 vim_free(save_fname);
9599
9600 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601}
9602
9603/*
9604 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009605 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 */
9607 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009608shortpath_for_partial(
9609 char_u **fnamep,
9610 char_u **bufp,
9611 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612{
9613 int sepcount, len, tflen;
9614 char_u *p;
9615 char_u *pbuf, *tfname;
9616 int hasTilde;
9617
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009618 /* Count up the path separators from the RHS.. so we know which part
9619 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009621 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622 if (vim_ispathsep(*p))
9623 ++sepcount;
9624
9625 /* Need full path first (use expand_env() to remove a "~/") */
9626 hasTilde = (**fnamep == '~');
9627 if (hasTilde)
9628 pbuf = tfname = expand_env_save(*fnamep);
9629 else
9630 pbuf = tfname = FullName_save(*fnamep, FALSE);
9631
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009632 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009634 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
9635 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636
9637 if (len == 0)
9638 {
9639 /* Don't have a valid filename, so shorten the rest of the
9640 * path if we can. This CAN give us invalid 8.3 filenames, but
9641 * there's not a lot of point in guessing what it might be.
9642 */
9643 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009644 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
9645 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 }
9647
9648 /* Count the paths backward to find the beginning of the desired string. */
9649 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009650 {
9651#ifdef FEAT_MBYTE
9652 if (has_mbyte)
9653 p -= mb_head_off(tfname, p);
9654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 if (vim_ispathsep(*p))
9656 {
9657 if (sepcount == 0 || (hasTilde && sepcount == 1))
9658 break;
9659 else
9660 sepcount --;
9661 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663 if (hasTilde)
9664 {
9665 --p;
9666 if (p >= tfname)
9667 *p = '~';
9668 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009669 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 }
9671 else
9672 ++p;
9673
9674 /* Copy in the string - p indexes into tfname - allocated at pbuf */
9675 vim_free(*bufp);
9676 *fnamelen = (int)STRLEN(p);
9677 *bufp = pbuf;
9678 *fnamep = p;
9679
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009680 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681}
9682#endif /* WIN3264 */
9683
9684/*
9685 * Adjust a filename, according to a string of modifiers.
9686 * *fnamep must be NUL terminated when called. When returning, the length is
9687 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009688 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689 * When there is an error, *fnamep is set to NULL.
9690 */
9691 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009692modify_fname(
9693 char_u *src, /* string with modifiers */
9694 int *usedlen, /* characters after src that are used */
9695 char_u **fnamep, /* file name so far */
9696 char_u **bufp, /* buffer for allocated file name or NULL */
9697 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698{
9699 int valid = 0;
9700 char_u *tail;
9701 char_u *s, *p, *pbuf;
9702 char_u dirname[MAXPATHL];
9703 int c;
9704 int has_fullname = 0;
9705#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009706 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707 int has_shortname = 0;
9708#endif
9709
9710repeat:
9711 /* ":p" - full path/file_name */
9712 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
9713 {
9714 has_fullname = 1;
9715
9716 valid |= VALID_PATH;
9717 *usedlen += 2;
9718
9719 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
9720 if ((*fnamep)[0] == '~'
9721#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
9722 && ((*fnamep)[1] == '/'
9723# ifdef BACKSLASH_IN_FILENAME
9724 || (*fnamep)[1] == '\\'
9725# endif
9726 || (*fnamep)[1] == NUL)
9727
9728#endif
9729 )
9730 {
9731 *fnamep = expand_env_save(*fnamep);
9732 vim_free(*bufp); /* free any allocated file name */
9733 *bufp = *fnamep;
9734 if (*fnamep == NULL)
9735 return -1;
9736 }
9737
9738 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009739 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 {
9741 if (vim_ispathsep(*p)
9742 && p[1] == '.'
9743 && (p[2] == NUL
9744 || vim_ispathsep(p[2])
9745 || (p[2] == '.'
9746 && (p[3] == NUL || vim_ispathsep(p[3])))))
9747 break;
9748 }
9749
9750 /* FullName_save() is slow, don't use it when not needed. */
9751 if (*p != NUL || !vim_isAbsName(*fnamep))
9752 {
9753 *fnamep = FullName_save(*fnamep, *p != NUL);
9754 vim_free(*bufp); /* free any allocated file name */
9755 *bufp = *fnamep;
9756 if (*fnamep == NULL)
9757 return -1;
9758 }
9759
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009760#ifdef WIN3264
9761# if _WIN32_WINNT >= 0x0500
9762 if (vim_strchr(*fnamep, '~') != NULL)
9763 {
9764 /* Expand 8.3 filename to full path. Needed to make sure the same
9765 * file does not have two different names.
9766 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
9767 p = alloc(_MAX_PATH + 1);
9768 if (p != NULL)
9769 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009770 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009771 {
9772 vim_free(*bufp);
9773 *bufp = *fnamep = p;
9774 }
9775 else
9776 vim_free(p);
9777 }
9778 }
9779# endif
9780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781 /* Append a path separator to a directory. */
9782 if (mch_isdir(*fnamep))
9783 {
9784 /* Make room for one or two extra characters. */
9785 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
9786 vim_free(*bufp); /* free any allocated file name */
9787 *bufp = *fnamep;
9788 if (*fnamep == NULL)
9789 return -1;
9790 add_pathsep(*fnamep);
9791 }
9792 }
9793
9794 /* ":." - path relative to the current directory */
9795 /* ":~" - path relative to the home directory */
9796 /* ":8" - shortname path - postponed till after */
9797 while (src[*usedlen] == ':'
9798 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
9799 {
9800 *usedlen += 2;
9801 if (c == '8')
9802 {
9803#ifdef WIN3264
9804 has_shortname = 1; /* Postpone this. */
9805#endif
9806 continue;
9807 }
9808 pbuf = NULL;
9809 /* Need full path first (use expand_env() to remove a "~/") */
9810 if (!has_fullname)
9811 {
9812 if (c == '.' && **fnamep == '~')
9813 p = pbuf = expand_env_save(*fnamep);
9814 else
9815 p = pbuf = FullName_save(*fnamep, FALSE);
9816 }
9817 else
9818 p = *fnamep;
9819
9820 has_fullname = 0;
9821
9822 if (p != NULL)
9823 {
9824 if (c == '.')
9825 {
9826 mch_dirname(dirname, MAXPATHL);
9827 s = shorten_fname(p, dirname);
9828 if (s != NULL)
9829 {
9830 *fnamep = s;
9831 if (pbuf != NULL)
9832 {
9833 vim_free(*bufp); /* free any allocated file name */
9834 *bufp = pbuf;
9835 pbuf = NULL;
9836 }
9837 }
9838 }
9839 else
9840 {
9841 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
9842 /* Only replace it when it starts with '~' */
9843 if (*dirname == '~')
9844 {
9845 s = vim_strsave(dirname);
9846 if (s != NULL)
9847 {
9848 *fnamep = s;
9849 vim_free(*bufp);
9850 *bufp = s;
9851 }
9852 }
9853 }
9854 vim_free(pbuf);
9855 }
9856 }
9857
9858 tail = gettail(*fnamep);
9859 *fnamelen = (int)STRLEN(*fnamep);
9860
9861 /* ":h" - head, remove "/file_name", can be repeated */
9862 /* Don't remove the first "/" or "c:\" */
9863 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
9864 {
9865 valid |= VALID_HEAD;
9866 *usedlen += 2;
9867 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009868 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009869 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870 *fnamelen = (int)(tail - *fnamep);
9871#ifdef VMS
9872 if (*fnamelen > 0)
9873 *fnamelen += 1; /* the path separator is part of the path */
9874#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009875 if (*fnamelen == 0)
9876 {
9877 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
9878 p = vim_strsave((char_u *)".");
9879 if (p == NULL)
9880 return -1;
9881 vim_free(*bufp);
9882 *bufp = *fnamep = tail = p;
9883 *fnamelen = 1;
9884 }
9885 else
9886 {
9887 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009888 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890 }
9891
9892 /* ":8" - shortname */
9893 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
9894 {
9895 *usedlen += 2;
9896#ifdef WIN3264
9897 has_shortname = 1;
9898#endif
9899 }
9900
9901#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009902 /*
9903 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 */
9905 if (has_shortname)
9906 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009907 /* Copy the string if it is shortened by :h and when it wasn't copied
9908 * yet, because we are going to change it in place. Avoids changing
9909 * the buffer name for "%:8". */
9910 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911 {
9912 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +02009913 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914 return -1;
9915 vim_free(*bufp);
9916 *bufp = *fnamep = p;
9917 }
9918
9919 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +02009920 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 if (!has_fullname && !vim_isAbsName(*fnamep))
9922 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009923 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 return -1;
9925 }
9926 else
9927 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009928 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929
Bram Moolenaardc935552011-08-17 15:23:23 +02009930 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009932 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933 return -1;
9934
9935 if (l == 0)
9936 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009937 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009939 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940 return -1;
9941 }
9942 *fnamelen = l;
9943 }
9944 }
9945#endif /* WIN3264 */
9946
9947 /* ":t" - tail, just the basename */
9948 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
9949 {
9950 *usedlen += 2;
9951 *fnamelen -= (int)(tail - *fnamep);
9952 *fnamep = tail;
9953 }
9954
9955 /* ":e" - extension, can be repeated */
9956 /* ":r" - root, without extension, can be repeated */
9957 while (src[*usedlen] == ':'
9958 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
9959 {
9960 /* find a '.' in the tail:
9961 * - for second :e: before the current fname
9962 * - otherwise: The last '.'
9963 */
9964 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
9965 s = *fnamep - 2;
9966 else
9967 s = *fnamep + *fnamelen - 1;
9968 for ( ; s > tail; --s)
9969 if (s[0] == '.')
9970 break;
9971 if (src[*usedlen + 1] == 'e') /* :e */
9972 {
9973 if (s > tail)
9974 {
9975 *fnamelen += (int)(*fnamep - (s + 1));
9976 *fnamep = s + 1;
9977#ifdef VMS
9978 /* cut version from the extension */
9979 s = *fnamep + *fnamelen - 1;
9980 for ( ; s > *fnamep; --s)
9981 if (s[0] == ';')
9982 break;
9983 if (s > *fnamep)
9984 *fnamelen = s - *fnamep;
9985#endif
9986 }
9987 else if (*fnamep <= tail)
9988 *fnamelen = 0;
9989 }
9990 else /* :r */
9991 {
9992 if (s > tail) /* remove one extension */
9993 *fnamelen = (int)(s - *fnamep);
9994 }
9995 *usedlen += 2;
9996 }
9997
9998 /* ":s?pat?foo?" - substitute */
9999 /* ":gs?pat?foo?" - global substitute */
10000 if (src[*usedlen] == ':'
10001 && (src[*usedlen + 1] == 's'
10002 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
10003 {
10004 char_u *str;
10005 char_u *pat;
10006 char_u *sub;
10007 int sep;
10008 char_u *flags;
10009 int didit = FALSE;
10010
10011 flags = (char_u *)"";
10012 s = src + *usedlen + 2;
10013 if (src[*usedlen + 1] == 'g')
10014 {
10015 flags = (char_u *)"g";
10016 ++s;
10017 }
10018
10019 sep = *s++;
10020 if (sep)
10021 {
10022 /* find end of pattern */
10023 p = vim_strchr(s, sep);
10024 if (p != NULL)
10025 {
10026 pat = vim_strnsave(s, (int)(p - s));
10027 if (pat != NULL)
10028 {
10029 s = p + 1;
10030 /* find end of substitution */
10031 p = vim_strchr(s, sep);
10032 if (p != NULL)
10033 {
10034 sub = vim_strnsave(s, (int)(p - s));
10035 str = vim_strnsave(*fnamep, *fnamelen);
10036 if (sub != NULL && str != NULL)
10037 {
10038 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010039 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 if (s != NULL)
10041 {
10042 *fnamep = s;
10043 *fnamelen = (int)STRLEN(s);
10044 vim_free(*bufp);
10045 *bufp = s;
10046 didit = TRUE;
10047 }
10048 }
10049 vim_free(sub);
10050 vim_free(str);
10051 }
10052 vim_free(pat);
10053 }
10054 }
10055 /* after using ":s", repeat all the modifiers */
10056 if (didit)
10057 goto repeat;
10058 }
10059 }
10060
Bram Moolenaar26df0922014-02-23 23:39:13 +010010061 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10062 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010063 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010064 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010065 if (c != NUL)
10066 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010067 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010068 if (c != NUL)
10069 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010070 if (p == NULL)
10071 return -1;
10072 vim_free(*bufp);
10073 *bufp = *fnamep = p;
10074 *fnamelen = (int)STRLEN(p);
10075 *usedlen += 2;
10076 }
10077
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078 return valid;
10079}
10080
10081/*
10082 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010083 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084 * "flags" can be "g" to do a global substitute.
10085 * Returns an allocated string, NULL for error.
10086 */
10087 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010088do_string_sub(
10089 char_u *str,
10090 char_u *pat,
10091 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010092 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010093 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094{
10095 int sublen;
10096 regmatch_T regmatch;
10097 int i;
10098 int do_all;
10099 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010100 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 garray_T ga;
10102 char_u *ret;
10103 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010104 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105
10106 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
10107 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010108 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109
10110 ga_init2(&ga, 1, 200);
10111
10112 do_all = (flags[0] == 'g');
10113
10114 regmatch.rm_ic = p_ic;
10115 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10116 if (regmatch.regprog != NULL)
10117 {
10118 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010119 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
10121 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010010122 /* Skip empty match except for first match. */
10123 if (regmatch.startp[0] == regmatch.endp[0])
10124 {
10125 if (zero_width == regmatch.startp[0])
10126 {
10127 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020010128 i = MB_PTR2LEN(tail);
10129 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
10130 (size_t)i);
10131 ga.ga_len += i;
10132 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010133 continue;
10134 }
10135 zero_width = regmatch.startp[0];
10136 }
10137
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138 /*
10139 * Get some space for a temporary buffer to do the substitution
10140 * into. It will contain:
10141 * - The text up to where the match is.
10142 * - The substituted text.
10143 * - The text after the match.
10144 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010145 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010010146 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
10148 {
10149 ga_clear(&ga);
10150 break;
10151 }
10152
10153 /* copy the text up to where the match is */
10154 i = (int)(regmatch.startp[0] - tail);
10155 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10156 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010157 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 + ga.ga_len + i, TRUE, TRUE, FALSE);
10159 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010160 tail = regmatch.endp[0];
10161 if (*tail == NUL)
10162 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163 if (!do_all)
10164 break;
10165 }
10166
10167 if (ga.ga_data != NULL)
10168 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10169
Bram Moolenaar473de612013-06-08 18:19:48 +020010170 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171 }
10172
10173 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10174 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010175 if (p_cpo == empty_option)
10176 p_cpo = save_cpo;
10177 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010178 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010179 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180
10181 return ret;
10182}
10183
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010184 static int
10185filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10186{
10187 typval_T rettv;
10188 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010189 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010190
10191 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10192 argv[0] = vimvars[VV_KEY].vv_tv;
10193 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010194 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10195 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010196 if (map)
10197 {
10198 /* map(): replace the list item value */
10199 clear_tv(tv);
10200 rettv.v_lock = 0;
10201 *tv = rettv;
10202 }
10203 else
10204 {
10205 int error = FALSE;
10206
10207 /* filter(): when expr is zero remove the item */
10208 *remp = (get_tv_number_chk(&rettv, &error) == 0);
10209 clear_tv(&rettv);
10210 /* On type error, nothing has been removed; return FAIL to stop the
10211 * loop. The error message was given by get_tv_number_chk(). */
10212 if (error)
10213 goto theend;
10214 }
10215 retval = OK;
10216theend:
10217 clear_tv(&vimvars[VV_VAL].vv_tv);
10218 return retval;
10219}
10220
10221
10222/*
10223 * Implementation of map() and filter().
10224 */
10225 void
10226filter_map(typval_T *argvars, typval_T *rettv, int map)
10227{
10228 typval_T *expr;
10229 listitem_T *li, *nli;
10230 list_T *l = NULL;
10231 dictitem_T *di;
10232 hashtab_T *ht;
10233 hashitem_T *hi;
10234 dict_T *d = NULL;
10235 typval_T save_val;
10236 typval_T save_key;
10237 int rem;
10238 int todo;
10239 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10240 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10241 : N_("filter() argument"));
10242 int save_did_emsg;
10243 int idx = 0;
10244
10245 if (argvars[0].v_type == VAR_LIST)
10246 {
10247 if ((l = argvars[0].vval.v_list) == NULL
10248 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
10249 return;
10250 }
10251 else if (argvars[0].v_type == VAR_DICT)
10252 {
10253 if ((d = argvars[0].vval.v_dict) == NULL
10254 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
10255 return;
10256 }
10257 else
10258 {
10259 EMSG2(_(e_listdictarg), ermsg);
10260 return;
10261 }
10262
10263 expr = &argvars[1];
10264 /* On type errors, the preceding call has already displayed an error
10265 * message. Avoid a misleading error message for an empty string that
10266 * was not passed as argument. */
10267 if (expr->v_type != VAR_UNKNOWN)
10268 {
10269 prepare_vimvar(VV_VAL, &save_val);
10270
10271 /* We reset "did_emsg" to be able to detect whether an error
10272 * occurred during evaluation of the expression. */
10273 save_did_emsg = did_emsg;
10274 did_emsg = FALSE;
10275
10276 prepare_vimvar(VV_KEY, &save_key);
10277 if (argvars[0].v_type == VAR_DICT)
10278 {
10279 vimvars[VV_KEY].vv_type = VAR_STRING;
10280
10281 ht = &d->dv_hashtab;
10282 hash_lock(ht);
10283 todo = (int)ht->ht_used;
10284 for (hi = ht->ht_array; todo > 0; ++hi)
10285 {
10286 if (!HASHITEM_EMPTY(hi))
10287 {
10288 int r;
10289
10290 --todo;
10291 di = HI2DI(hi);
10292 if (map &&
10293 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10294 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
10295 break;
10296 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10297 r = filter_map_one(&di->di_tv, expr, map, &rem);
10298 clear_tv(&vimvars[VV_KEY].vv_tv);
10299 if (r == FAIL || did_emsg)
10300 break;
10301 if (!map && rem)
10302 {
10303 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10304 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10305 break;
10306 dictitem_remove(d, di);
10307 }
10308 }
10309 }
10310 hash_unlock(ht);
10311 }
10312 else
10313 {
10314 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10315
10316 for (li = l->lv_first; li != NULL; li = nli)
10317 {
10318 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
10319 break;
10320 nli = li->li_next;
10321 vimvars[VV_KEY].vv_nr = idx;
10322 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10323 || did_emsg)
10324 break;
10325 if (!map && rem)
10326 listitem_remove(l, li);
10327 ++idx;
10328 }
10329 }
10330
10331 restore_vimvar(VV_KEY, &save_key);
10332 restore_vimvar(VV_VAL, &save_val);
10333
10334 did_emsg |= save_did_emsg;
10335 }
10336
10337 copy_tv(&argvars[0], rettv);
10338}
10339
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */