blob: 7dfb9812547b037a2bbe74fb846924ab13e2e9b1 [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 get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
236static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100237static int free_unref_items(int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100238static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100239static int get_env_len(char_u **arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100240static 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 +0200241static void check_vars(char_u *name, int len);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100242static typval_T *alloc_string_tv(char_u *string);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100243static void delete_var(hashtab_T *ht, hashitem_T *hi);
244static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
245static 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 +0100246static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200247
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200248/* for VIM_VERSION_ defines */
249#include "version.h"
250
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100251
252#if defined(EBCDIC) || defined(PROTO)
253/*
254 * Compare struct fst by function name.
255 */
256 static int
257compare_func_name(const void *s1, const void *s2)
258{
259 struct fst *p1 = (struct fst *)s1;
260 struct fst *p2 = (struct fst *)s2;
261
262 return STRCMP(p1->f_name, p2->f_name);
263}
264
265/*
266 * Sort the function table by function name.
267 * The sorting of the table above is ASCII dependant.
268 * On machines using EBCDIC we have to sort it.
269 */
270 static void
271sortFunctions(void)
272{
273 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
274
275 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
276}
277#endif
278
279
Bram Moolenaar33570922005-01-25 22:26:29 +0000280/*
281 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000282 */
283 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100284eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000285{
Bram Moolenaar33570922005-01-25 22:26:29 +0000286 int i;
287 struct vimvar *p;
288
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200289 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
290 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200291 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000292 hash_init(&compat_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200293 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
295 for (i = 0; i < VV_LEN; ++i)
296 {
297 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200298 if (STRLEN(p->vv_name) > 16)
299 {
Bram Moolenaarde330112017-01-08 20:50:52 +0100300 IEMSG("INTERNAL: name too long, increase size of dictitem16_T");
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200301 getout(1);
302 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000303 STRCPY(p->vv_di.di_key, p->vv_name);
304 if (p->vv_flags & VV_RO)
305 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
306 else if (p->vv_flags & VV_RO_SBX)
307 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
308 else
309 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000310
311 /* add to v: scope dict, unless the value is not always available */
312 if (p->vv_type != VAR_UNKNOWN)
313 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000314 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000315 /* add to compat scope dict */
316 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000317 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100318 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
319
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000320 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100321 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100322 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100323 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100324 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100325
326 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
327 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
328 set_vim_var_nr(VV_NONE, VVAL_NONE);
329 set_vim_var_nr(VV_NULL, VVAL_NULL);
330
Bram Moolenaarf562e722016-07-19 17:25:25 +0200331 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
332 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
333 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
334 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
335 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
336 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
337 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
338 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
339 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
340 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
341
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200342 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200343
344#ifdef EBCDIC
345 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100346 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200347 */
348 sortFunctions();
349#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000350}
351
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000352#if defined(EXITFREE) || defined(PROTO)
353 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100354eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000355{
356 int i;
357 struct vimvar *p;
358
359 for (i = 0; i < VV_LEN; ++i)
360 {
361 p = &vimvars[i];
362 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100363 VIM_CLEAR(p->vv_str);
Bram Moolenaard812df62008-11-09 12:46:09 +0000364 else if (p->vv_di.di_tv.v_type == VAR_LIST)
365 {
366 list_unref(p->vv_list);
367 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000368 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000369 }
370 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000371 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000372 hash_clear(&compat_hashtab);
373
Bram Moolenaard9fba312005-06-26 22:34:35 +0000374 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100375# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200376 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100377# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000378
379 /* global variables */
380 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000381
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000382 /* autoloaded script names */
383 ga_clear_strings(&ga_loaded);
384
Bram Moolenaarcca74132013-09-25 21:00:28 +0200385 /* Script-local variables. First clear all the variables and in a second
386 * loop free the scriptvar_T, because a variable in one script might hold
387 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200388 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200389 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200390 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200391 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200392 ga_clear(&ga_scripts);
393
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000394 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200395 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000396
397 /* functions */
398 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000399}
400#endif
401
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402
403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 * Set an internal variable to a string value. Creates the variable if it does
405 * not already exist.
406 */
407 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100408set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000410 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000411 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412
413 val = vim_strsave(value);
414 if (val != NULL)
415 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000416 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000417 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000419 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000420 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 }
422 }
423}
424
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000425static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200426#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000427static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000428static char_u *redir_endp = NULL;
429static char_u *redir_varname = NULL;
430
431/*
432 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100433 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000434 * Returns OK if successfully completed the setup. FAIL otherwise.
435 */
436 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100437var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000438{
439 int save_emsg;
440 int err;
441 typval_T tv;
442
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000443 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000444 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000445 {
446 EMSG(_(e_invarg));
447 return FAIL;
448 }
449
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000450 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000451 redir_varname = vim_strsave(name);
452 if (redir_varname == NULL)
453 return FAIL;
454
455 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
456 if (redir_lval == NULL)
457 {
458 var_redir_stop();
459 return FAIL;
460 }
461
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000462 /* The output is stored in growarray "redir_ga" until redirection ends. */
463 ga_init2(&redir_ga, (int)sizeof(char), 500);
464
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000465 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100466 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000467 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000468 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
469 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200470 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000471 if (redir_endp != NULL && *redir_endp != NUL)
472 /* Trailing characters are present after the variable name */
473 EMSG(_(e_trailing));
474 else
475 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000476 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000477 var_redir_stop();
478 return FAIL;
479 }
480
481 /* check if we can write to the variable: set it to or append an empty
482 * string */
483 save_emsg = did_emsg;
484 did_emsg = FALSE;
485 tv.v_type = VAR_STRING;
486 tv.vval.v_string = (char_u *)"";
487 if (append)
488 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
489 else
490 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200491 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000492 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000493 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000494 if (err)
495 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000496 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000497 var_redir_stop();
498 return FAIL;
499 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000500
501 return OK;
502}
503
504/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000505 * Append "value[value_len]" to the variable set by var_redir_start().
506 * The actual appending is postponed until redirection ends, because the value
507 * appended may in fact be the string we write to, changing it may cause freed
508 * memory to be used:
509 * :redir => foo
510 * :let foo
511 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000512 */
513 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100514var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000515{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000516 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000517
518 if (redir_lval == NULL)
519 return;
520
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000521 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000522 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000523 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000524 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000525
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000526 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000527 {
528 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000529 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000530 }
531 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000532 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000533}
534
535/*
536 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000537 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000538 */
539 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100540var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000541{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000542 typval_T tv;
543
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200544 if (EVALCMD_BUSY)
545 {
546 redir_lval = NULL;
547 return;
548 }
549
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000550 if (redir_lval != NULL)
551 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000552 /* If there was no error: assign the text to the variable. */
553 if (redir_endp != NULL)
554 {
555 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
556 tv.v_type = VAR_STRING;
557 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200558 /* Call get_lval() again, if it's inside a Dict or List it may
559 * have changed. */
560 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100561 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200562 if (redir_endp != NULL && redir_lval->ll_name != NULL)
563 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
564 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000565 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000566
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000567 /* free the collected output */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100568 VIM_CLEAR(redir_ga.ga_data);
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000569
Bram Moolenaard23a8232018-02-10 18:45:26 +0100570 VIM_CLEAR(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000571 }
Bram Moolenaard23a8232018-02-10 18:45:26 +0100572 VIM_CLEAR(redir_varname);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000573}
574
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575# if defined(FEAT_MBYTE) || defined(PROTO)
576 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100577eval_charconvert(
578 char_u *enc_from,
579 char_u *enc_to,
580 char_u *fname_from,
581 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582{
583 int err = FALSE;
584
585 set_vim_var_string(VV_CC_FROM, enc_from, -1);
586 set_vim_var_string(VV_CC_TO, enc_to, -1);
587 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
588 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
589 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
590 err = TRUE;
591 set_vim_var_string(VV_CC_FROM, NULL, -1);
592 set_vim_var_string(VV_CC_TO, NULL, -1);
593 set_vim_var_string(VV_FNAME_IN, NULL, -1);
594 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
595
596 if (err)
597 return FAIL;
598 return OK;
599}
600# endif
601
602# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
603 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100604eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605{
606 int err = FALSE;
607
608 set_vim_var_string(VV_FNAME_IN, fname, -1);
609 set_vim_var_string(VV_CMDARG, args, -1);
610 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
611 err = TRUE;
612 set_vim_var_string(VV_FNAME_IN, NULL, -1);
613 set_vim_var_string(VV_CMDARG, NULL, -1);
614
615 if (err)
616 {
617 mch_remove(fname);
618 return FAIL;
619 }
620 return OK;
621}
622# endif
623
624# if defined(FEAT_DIFF) || defined(PROTO)
625 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100626eval_diff(
627 char_u *origfile,
628 char_u *newfile,
629 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630{
631 int err = FALSE;
632
633 set_vim_var_string(VV_FNAME_IN, origfile, -1);
634 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
635 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
636 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
637 set_vim_var_string(VV_FNAME_IN, NULL, -1);
638 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
639 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
640}
641
642 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100643eval_patch(
644 char_u *origfile,
645 char_u *difffile,
646 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647{
648 int err;
649
650 set_vim_var_string(VV_FNAME_IN, origfile, -1);
651 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
652 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
653 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
654 set_vim_var_string(VV_FNAME_IN, NULL, -1);
655 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
656 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
657}
658# endif
659
660/*
661 * Top level evaluation function, returning a boolean.
662 * Sets "error" to TRUE if there was an error.
663 * Return TRUE or FALSE.
664 */
665 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100666eval_to_bool(
667 char_u *arg,
668 int *error,
669 char_u **nextcmd,
670 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671{
Bram Moolenaar33570922005-01-25 22:26:29 +0000672 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200673 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674
675 if (skip)
676 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000677 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 else
680 {
681 *error = FALSE;
682 if (!skip)
683 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000684 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000685 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 }
687 }
688 if (skip)
689 --emsg_skip;
690
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200691 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692}
693
Bram Moolenaar48570482017-10-30 21:48:41 +0100694 static int
695eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
696{
697 char_u *s;
698 int dummy;
699 char_u buf[NUMBUFLEN];
700
701 if (expr->v_type == VAR_FUNC)
702 {
703 s = expr->vval.v_string;
704 if (s == NULL || *s == NUL)
705 return FAIL;
706 if (call_func(s, (int)STRLEN(s), rettv, argc, argv, NULL,
707 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
708 return FAIL;
709 }
710 else if (expr->v_type == VAR_PARTIAL)
711 {
712 partial_T *partial = expr->vval.v_partial;
713
714 s = partial_name(partial);
715 if (s == NULL || *s == NUL)
716 return FAIL;
717 if (call_func(s, (int)STRLEN(s), rettv, argc, argv, NULL,
718 0L, 0L, &dummy, TRUE, partial, NULL) == FAIL)
719 return FAIL;
720 }
721 else
722 {
723 s = get_tv_string_buf_chk(expr, buf);
724 if (s == NULL)
725 return FAIL;
726 s = skipwhite(s);
727 if (eval1(&s, rettv, TRUE) == FAIL)
728 return FAIL;
729 if (*s != NUL) /* check for trailing chars after expr */
730 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200731 clear_tv(rettv);
Bram Moolenaar48570482017-10-30 21:48:41 +0100732 EMSG2(_(e_invexpr2), s);
733 return FAIL;
734 }
735 }
736 return OK;
737}
738
739/*
740 * Like eval_to_bool() but using a typval_T instead of a string.
741 * Works for string, funcref and partial.
742 */
743 int
744eval_expr_to_bool(typval_T *expr, int *error)
745{
746 typval_T rettv;
747 int res;
748
749 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
750 {
751 *error = TRUE;
752 return FALSE;
753 }
754 res = (get_tv_number_chk(&rettv, error) != 0);
755 clear_tv(&rettv);
756 return res;
757}
758
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759/*
760 * Top level evaluation function, returning a string. If "skip" is TRUE,
761 * only parsing to "nextcmd" is done, without reporting errors. Return
762 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
763 */
764 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100765eval_to_string_skip(
766 char_u *arg,
767 char_u **nextcmd,
768 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769{
Bram Moolenaar33570922005-01-25 22:26:29 +0000770 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 char_u *retval;
772
773 if (skip)
774 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000775 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 retval = NULL;
777 else
778 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000779 retval = vim_strsave(get_tv_string(&tv));
780 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 }
782 if (skip)
783 --emsg_skip;
784
785 return retval;
786}
787
788/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000789 * Skip over an expression at "*pp".
790 * Return FAIL for an error, OK otherwise.
791 */
792 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100793skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000794{
Bram Moolenaar33570922005-01-25 22:26:29 +0000795 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000796
797 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000798 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000799}
800
801/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000803 * When "convert" is TRUE convert a List into a sequence of lines and convert
804 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 * Return pointer to allocated memory, or NULL for failure.
806 */
807 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100808eval_to_string(
809 char_u *arg,
810 char_u **nextcmd,
811 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812{
Bram Moolenaar33570922005-01-25 22:26:29 +0000813 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000815 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000816#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000817 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000820 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 retval = NULL;
822 else
823 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000824 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000825 {
826 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000827 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200828 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200829 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200830 if (tv.vval.v_list->lv_len > 0)
831 ga_append(&ga, NL);
832 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000833 ga_append(&ga, NUL);
834 retval = (char_u *)ga.ga_data;
835 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000836#ifdef FEAT_FLOAT
837 else if (convert && tv.v_type == VAR_FLOAT)
838 {
839 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
840 retval = vim_strsave(numbuf);
841 }
842#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000843 else
844 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000845 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 }
847
848 return retval;
849}
850
851/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000852 * Call eval_to_string() without using current local variables and using
853 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 */
855 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100856eval_to_string_safe(
857 char_u *arg,
858 char_u **nextcmd,
859 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860{
861 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200862 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200864 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000865 if (use_sandbox)
866 ++sandbox;
867 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000868 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000869 if (use_sandbox)
870 --sandbox;
871 --textlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200872 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 return retval;
874}
875
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876/*
877 * Top level evaluation function, returning a number.
878 * Evaluates "expr" silently.
879 * Returns -1 for an error.
880 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200881 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100882eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883{
Bram Moolenaar33570922005-01-25 22:26:29 +0000884 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200885 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000886 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887
888 ++emsg_off;
889
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000890 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 retval = -1;
892 else
893 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000894 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000895 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 }
897 --emsg_off;
898
899 return retval;
900}
901
Bram Moolenaara40058a2005-07-11 22:42:07 +0000902/*
903 * Prepare v: variable "idx" to be used.
904 * Save the current typeval in "save_tv".
905 * When not used yet add the variable to the v: hashtable.
906 */
907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100908prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000909{
910 *save_tv = vimvars[idx].vv_tv;
911 if (vimvars[idx].vv_type == VAR_UNKNOWN)
912 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
913}
914
915/*
916 * Restore v: variable "idx" to typeval "save_tv".
917 * When no longer defined, remove the variable from the v: hashtable.
918 */
919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100920restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000921{
922 hashitem_T *hi;
923
Bram Moolenaara40058a2005-07-11 22:42:07 +0000924 vimvars[idx].vv_tv = *save_tv;
925 if (vimvars[idx].vv_type == VAR_UNKNOWN)
926 {
927 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
928 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100929 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +0000930 else
931 hash_remove(&vimvarht, hi);
932 }
933}
934
Bram Moolenaar3c56a962006-03-12 22:19:04 +0000935#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000936/*
937 * Evaluate an expression to a list with suggestions.
938 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000939 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000940 */
941 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100942eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000943{
944 typval_T save_val;
945 typval_T rettv;
946 list_T *list = NULL;
947 char_u *p = skipwhite(expr);
948
949 /* Set "v:val" to the bad word. */
950 prepare_vimvar(VV_VAL, &save_val);
951 vimvars[VV_VAL].vv_type = VAR_STRING;
952 vimvars[VV_VAL].vv_str = badword;
953 if (p_verbose == 0)
954 ++emsg_off;
955
956 if (eval1(&p, &rettv, TRUE) == OK)
957 {
958 if (rettv.v_type != VAR_LIST)
959 clear_tv(&rettv);
960 else
961 list = rettv.vval.v_list;
962 }
963
964 if (p_verbose == 0)
965 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000966 restore_vimvar(VV_VAL, &save_val);
967
968 return list;
969}
970
971/*
972 * "list" is supposed to contain two items: a word and a number. Return the
973 * word in "pp" and the number as the return value.
974 * Return -1 if anything isn't right.
975 * Used to get the good word and score from the eval_spell_expr() result.
976 */
977 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100978get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000979{
980 listitem_T *li;
981
982 li = list->lv_first;
983 if (li == NULL)
984 return -1;
985 *pp = get_tv_string(&li->li_tv);
986
987 li = li->li_next;
988 if (li == NULL)
989 return -1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200990 return (int)get_tv_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000991}
992#endif
993
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000994/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000995 * Top level evaluation function.
996 * Returns an allocated typval_T with the result.
997 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000998 */
999 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001000eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001001{
1002 typval_T *tv;
1003
1004 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001005 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001006 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001007
1008 return tv;
1009}
1010
1011
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001013 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +02001014 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
1015 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001016 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001018 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001019call_vim_function(
1020 char_u *func,
1021 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +02001022 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001023 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 int doesrange;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001026 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001028 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaarffa96842018-06-12 22:05:14 +02001029 ret = call_func(func, (int)STRLEN(func), rettv, argc, argv, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001031 &doesrange, TRUE, NULL, NULL);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001032 if (ret == FAIL)
1033 clear_tv(rettv);
1034
1035 return ret;
1036}
1037
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001038/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001039 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001040 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001041 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1042 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001043 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001044 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001045call_func_retnr(
1046 char_u *func,
1047 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001048 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001049{
1050 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001051 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001052
Bram Moolenaarded27a12018-08-01 19:06:03 +02001053 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001054 return -1;
1055
1056 retval = get_tv_number_chk(&rettv, NULL);
1057 clear_tv(&rettv);
1058 return retval;
1059}
1060
1061#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1062 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1063
Bram Moolenaar4f688582007-07-24 12:34:30 +00001064# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001065/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001066 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001067 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001068 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1069 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001070 */
1071 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001072call_func_retstr(
1073 char_u *func,
1074 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001075 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001076{
1077 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001078 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001079
Bram Moolenaarded27a12018-08-01 19:06:03 +02001080 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001081 return NULL;
1082
1083 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001084 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 return retval;
1086}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001087# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001088
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001089/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001090 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001091 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1092 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001093 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001094 */
1095 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001096call_func_retlist(
1097 char_u *func,
1098 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001099 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001100{
1101 typval_T rettv;
1102
Bram Moolenaarded27a12018-08-01 19:06:03 +02001103 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001104 return NULL;
1105
1106 if (rettv.v_type != VAR_LIST)
1107 {
1108 clear_tv(&rettv);
1109 return NULL;
1110 }
1111
1112 return rettv.vval.v_list;
1113}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114#endif
1115
Bram Moolenaar05159a02005-02-26 23:04:13 +00001116
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117#ifdef FEAT_FOLDING
1118/*
1119 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1120 * it in "*cp". Doesn't give error messages.
1121 */
1122 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001123eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124{
Bram Moolenaar33570922005-01-25 22:26:29 +00001125 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001126 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001128 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1129 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130
1131 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001132 if (use_sandbox)
1133 ++sandbox;
1134 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001136 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 retval = 0;
1138 else
1139 {
1140 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001141 if (tv.v_type == VAR_NUMBER)
1142 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001143 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 retval = 0;
1145 else
1146 {
1147 /* If the result is a string, check if there is a non-digit before
1148 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001149 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 if (!VIM_ISDIGIT(*s) && *s != '-')
1151 *cp = *s++;
1152 retval = atol((char *)s);
1153 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001154 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 }
1156 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001157 if (use_sandbox)
1158 --sandbox;
1159 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001161 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162}
1163#endif
1164
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001166 * ":let" list all variable values
1167 * ":let var1 var2" list variable values
1168 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001169 * ":let var += expr" assignment command.
1170 * ":let var -= expr" assignment command.
1171 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001172 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 */
1174 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001175ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176{
1177 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001178 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001179 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001181 int var_count = 0;
1182 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001183 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001184 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001185 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186
Bram Moolenaardb552d602006-03-23 22:59:57 +00001187 argend = skip_var_list(arg, &var_count, &semicolon);
1188 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001189 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001190 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1191 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001192 expr = skipwhite(argend);
1193 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1194 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001196 /*
1197 * ":let" without "=": list variables
1198 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001199 if (*arg == '[')
1200 EMSG(_(e_invarg));
1201 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001202 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001203 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001204 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001205 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001206 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001207 list_glob_vars(&first);
1208 list_buf_vars(&first);
1209 list_win_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001210 list_tab_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001211 list_script_vars(&first);
1212 list_func_vars(&first);
1213 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 eap->nextcmd = check_nextcmd(arg);
1216 }
1217 else
1218 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001219 op[0] = '=';
1220 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001221 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001222 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001223 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1224 op[0] = *expr; /* +=, -= or .= */
1225 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001226 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001227 else
1228 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001229
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 if (eap->skip)
1231 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001232 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 if (eap->skip)
1234 {
1235 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001236 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 --emsg_skip;
1238 }
1239 else if (i != FAIL)
1240 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001241 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001242 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001243 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 }
1245 }
1246}
1247
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001248/*
1249 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1250 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001251 * When "nextchars" is not NULL it points to a string with characters that
1252 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1253 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001254 * Returns OK or FAIL;
1255 */
1256 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001257ex_let_vars(
1258 char_u *arg_start,
1259 typval_T *tv,
1260 int copy, /* copy values from "tv", don't move */
1261 int semicolon, /* from skip_var_list() */
1262 int var_count, /* from skip_var_list() */
1263 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001264{
1265 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001266 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001267 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001268 listitem_T *item;
1269 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001270
1271 if (*arg != '[')
1272 {
1273 /*
1274 * ":let var = expr" or ":for var in list"
1275 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001276 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001277 return FAIL;
1278 return OK;
1279 }
1280
1281 /*
1282 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1283 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001284 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001285 {
1286 EMSG(_(e_listreq));
1287 return FAIL;
1288 }
1289
1290 i = list_len(l);
1291 if (semicolon == 0 && var_count < i)
1292 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001293 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001294 return FAIL;
1295 }
1296 if (var_count - semicolon > i)
1297 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001298 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001299 return FAIL;
1300 }
1301
1302 item = l->lv_first;
1303 while (*arg != ']')
1304 {
1305 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001306 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001307 item = item->li_next;
1308 if (arg == NULL)
1309 return FAIL;
1310
1311 arg = skipwhite(arg);
1312 if (*arg == ';')
1313 {
1314 /* Put the rest of the list (may be empty) in the var after ';'.
1315 * Create a new list for this. */
1316 l = list_alloc();
1317 if (l == NULL)
1318 return FAIL;
1319 while (item != NULL)
1320 {
1321 list_append_tv(l, &item->li_tv);
1322 item = item->li_next;
1323 }
1324
1325 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001326 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001327 ltv.vval.v_list = l;
1328 l->lv_refcount = 1;
1329
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001330 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1331 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001332 clear_tv(&ltv);
1333 if (arg == NULL)
1334 return FAIL;
1335 break;
1336 }
1337 else if (*arg != ',' && *arg != ']')
1338 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001339 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001340 return FAIL;
1341 }
1342 }
1343
1344 return OK;
1345}
1346
1347/*
1348 * Skip over assignable variable "var" or list of variables "[var, var]".
1349 * Used for ":let varvar = expr" and ":for varvar in expr".
1350 * For "[var, var]" increment "*var_count" for each variable.
1351 * for "[var, var; var]" set "semicolon".
1352 * Return NULL for an error.
1353 */
1354 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001355skip_var_list(
1356 char_u *arg,
1357 int *var_count,
1358 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001359{
1360 char_u *p, *s;
1361
1362 if (*arg == '[')
1363 {
1364 /* "[var, var]": find the matching ']'. */
1365 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001366 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001367 {
1368 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1369 s = skip_var_one(p);
1370 if (s == p)
1371 {
1372 EMSG2(_(e_invarg2), p);
1373 return NULL;
1374 }
1375 ++*var_count;
1376
1377 p = skipwhite(s);
1378 if (*p == ']')
1379 break;
1380 else if (*p == ';')
1381 {
1382 if (*semicolon == 1)
1383 {
1384 EMSG(_("Double ; in list of variables"));
1385 return NULL;
1386 }
1387 *semicolon = 1;
1388 }
1389 else if (*p != ',')
1390 {
1391 EMSG2(_(e_invarg2), p);
1392 return NULL;
1393 }
1394 }
1395 return p + 1;
1396 }
1397 else
1398 return skip_var_one(arg);
1399}
1400
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001401/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001402 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001403 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001404 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001405 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001406skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001407{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001408 if (*arg == '@' && arg[1] != NUL)
1409 return arg + 2;
1410 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1411 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001412}
1413
Bram Moolenaara7043832005-01-21 11:56:39 +00001414/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001415 * List variables for hashtab "ht" with prefix "prefix".
1416 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001417 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001418 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001419list_hashtable_vars(
1420 hashtab_T *ht,
1421 char_u *prefix,
1422 int empty,
1423 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001424{
Bram Moolenaar33570922005-01-25 22:26:29 +00001425 hashitem_T *hi;
1426 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001427 int todo;
Bram Moolenaarf86db782018-10-25 13:31:37 +02001428 char_u buf[IOSIZE];
Bram Moolenaara7043832005-01-21 11:56:39 +00001429
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001430 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001431 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1432 {
1433 if (!HASHITEM_EMPTY(hi))
1434 {
1435 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001436 di = HI2DI(hi);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001437
1438 // apply :filter /pat/ to variable name
1439 vim_strncpy((char_u *) buf, prefix, IOSIZE - 1);
1440 vim_strcat((char_u *) buf, di->di_key, IOSIZE);
1441 if (message_filtered(buf))
1442 continue;
1443
Bram Moolenaar33570922005-01-25 22:26:29 +00001444 if (empty || di->di_tv.v_type != VAR_STRING
1445 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001446 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001447 }
1448 }
1449}
1450
1451/*
1452 * List global variables.
1453 */
1454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001455list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001456{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001457 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001458}
1459
1460/*
1461 * List buffer variables.
1462 */
1463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001464list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001465{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001466 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001467 TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001468}
1469
1470/*
1471 * List window variables.
1472 */
1473 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001474list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001475{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001476 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001477 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001478}
1479
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001480/*
1481 * List tab page variables.
1482 */
1483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001484list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001485{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001486 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001487 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001488}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001489
Bram Moolenaara7043832005-01-21 11:56:39 +00001490/*
1491 * List Vim variables.
1492 */
1493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001494list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001495{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001496 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001497}
1498
1499/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001500 * List script-local variables, if there is a script.
1501 */
1502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001503list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001504{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001505 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
1506 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001507 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001508}
1509
1510/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001511 * List variables in "arg".
1512 */
1513 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001514list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001515{
1516 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001517 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001518 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001519 char_u *name_start;
1520 char_u *arg_subsc;
1521 char_u *tofree;
1522 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001523
1524 while (!ends_excmd(*arg) && !got_int)
1525 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001526 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001527 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001528 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001529 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001530 {
1531 emsg_severe = TRUE;
1532 EMSG(_(e_trailing));
1533 break;
1534 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001535 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001536 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001537 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001538 /* get_name_len() takes care of expanding curly braces */
1539 name_start = name = arg;
1540 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1541 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001542 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001543 /* This is mainly to keep test 49 working: when expanding
1544 * curly braces fails overrule the exception error message. */
1545 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001546 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001547 emsg_severe = TRUE;
1548 EMSG2(_(e_invarg2), arg);
1549 break;
1550 }
1551 error = TRUE;
1552 }
1553 else
1554 {
1555 if (tofree != NULL)
1556 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001557 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001558 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001559 else
1560 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001561 /* handle d.key, l[idx], f(expr) */
1562 arg_subsc = arg;
1563 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001564 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001565 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001566 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001567 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001568 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001569 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001570 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001571 case 'g': list_glob_vars(first); break;
1572 case 'b': list_buf_vars(first); break;
1573 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001574 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001575 case 'v': list_vim_vars(first); break;
1576 case 's': list_script_vars(first); break;
1577 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001578 default:
1579 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001580 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001581 }
1582 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001583 {
1584 char_u numbuf[NUMBUFLEN];
1585 char_u *tf;
1586 int c;
1587 char_u *s;
1588
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001589 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001590 c = *arg;
1591 *arg = NUL;
1592 list_one_var_a((char_u *)"",
1593 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001594 tv.v_type,
1595 s == NULL ? (char_u *)"" : s,
1596 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001597 *arg = c;
1598 vim_free(tf);
1599 }
1600 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001601 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001602 }
1603 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001604
1605 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001606 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001607
1608 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001609 }
1610
1611 return arg;
1612}
1613
1614/*
1615 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1616 * Returns a pointer to the char just after the var name.
1617 * Returns NULL if there is an error.
1618 */
1619 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001620ex_let_one(
1621 char_u *arg, /* points to variable name */
1622 typval_T *tv, /* value to assign to variable */
1623 int copy, /* copy value from "tv" */
1624 char_u *endchars, /* valid chars after variable name or NULL */
1625 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001626{
1627 int c1;
1628 char_u *name;
1629 char_u *p;
1630 char_u *arg_end = NULL;
1631 int len;
1632 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001633 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001634
1635 /*
1636 * ":let $VAR = expr": Set environment variable.
1637 */
1638 if (*arg == '$')
1639 {
1640 /* Find the end of the name. */
1641 ++arg;
1642 name = arg;
1643 len = get_env_len(&arg);
1644 if (len == 0)
1645 EMSG2(_(e_invarg2), name - 1);
1646 else
1647 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001648 if (op != NULL && (*op == '+' || *op == '-'))
1649 EMSG2(_(e_letwrong), op);
1650 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001651 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001652 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001653 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001654 {
1655 c1 = name[len];
1656 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001657 p = get_tv_string_chk(tv);
1658 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001659 {
1660 int mustfree = FALSE;
1661 char_u *s = vim_getenv(name, &mustfree);
1662
1663 if (s != NULL)
1664 {
1665 p = tofree = concat_str(s, p);
1666 if (mustfree)
1667 vim_free(s);
1668 }
1669 }
1670 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001671 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001672 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001673 if (STRICMP(name, "HOME") == 0)
1674 init_homedir();
1675 else if (didset_vim && STRICMP(name, "VIM") == 0)
1676 didset_vim = FALSE;
1677 else if (didset_vimruntime
1678 && STRICMP(name, "VIMRUNTIME") == 0)
1679 didset_vimruntime = FALSE;
1680 arg_end = arg;
1681 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001682 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001683 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001684 }
1685 }
1686 }
1687
1688 /*
1689 * ":let &option = expr": Set option value.
1690 * ":let &l:option = expr": Set local option value.
1691 * ":let &g:option = expr": Set global option value.
1692 */
1693 else if (*arg == '&')
1694 {
1695 /* Find the end of the name. */
1696 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001697 if (p == NULL || (endchars != NULL
1698 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001699 EMSG(_(e_letunexp));
1700 else
1701 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001702 long n;
1703 int opt_type;
1704 long numval;
1705 char_u *stringval = NULL;
1706 char_u *s;
1707
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001708 c1 = *p;
1709 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001710
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001711 n = (long)get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001712 s = get_tv_string_chk(tv); /* != NULL if number or string */
1713 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001714 {
1715 opt_type = get_option_value(arg, &numval,
1716 &stringval, opt_flags);
1717 if ((opt_type == 1 && *op == '.')
1718 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001719 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001720 EMSG2(_(e_letwrong), op);
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001721 s = NULL; /* don't set the value */
1722 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001723 else
1724 {
1725 if (opt_type == 1) /* number */
1726 {
1727 if (*op == '+')
1728 n = numval + n;
1729 else
1730 n = numval - n;
1731 }
1732 else if (opt_type == 0 && stringval != NULL) /* string */
1733 {
1734 s = concat_str(stringval, s);
1735 vim_free(stringval);
1736 stringval = s;
1737 }
1738 }
1739 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001740 if (s != NULL)
1741 {
1742 set_option_value(arg, n, s, opt_flags);
1743 arg_end = p;
1744 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001745 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001746 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001747 }
1748 }
1749
1750 /*
1751 * ":let @r = expr": Set register contents.
1752 */
1753 else if (*arg == '@')
1754 {
1755 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001756 if (op != NULL && (*op == '+' || *op == '-'))
1757 EMSG2(_(e_letwrong), op);
1758 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001759 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001760 EMSG(_(e_letunexp));
1761 else
1762 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001763 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001764 char_u *s;
1765
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001766 p = get_tv_string_chk(tv);
1767 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001768 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02001769 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001770 if (s != NULL)
1771 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001772 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001773 vim_free(s);
1774 }
1775 }
1776 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001777 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001778 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001779 arg_end = arg + 1;
1780 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001781 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001782 }
1783 }
1784
1785 /*
1786 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001787 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001788 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001789 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001790 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001791 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001792
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001793 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001794 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001795 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001796 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1797 EMSG(_(e_letunexp));
1798 else
1799 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001800 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001801 arg_end = p;
1802 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001803 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001804 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001805 }
1806
1807 else
1808 EMSG2(_(e_invarg2), arg);
1809
1810 return arg_end;
1811}
1812
1813/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001814 * Get an lval: variable, Dict item or List item that can be assigned a value
1815 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1816 * "name.key", "name.key[expr]" etc.
1817 * Indexing only works if "name" is an existing List or Dictionary.
1818 * "name" points to the start of the name.
1819 * If "rettv" is not NULL it points to the value to be assigned.
1820 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1821 * wrong; must end in space or cmd separator.
1822 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001823 * flags:
1824 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001825 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001826 * GLV_NO_AUTOLOAD: do not use script autoloading
1827 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001828 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001829 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001830 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001831 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001832 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001833get_lval(
1834 char_u *name,
1835 typval_T *rettv,
1836 lval_T *lp,
1837 int unlet,
1838 int skip,
1839 int flags, /* GLV_ values */
1840 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001841{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001842 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001843 char_u *expr_start, *expr_end;
1844 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001845 dictitem_T *v;
1846 typval_T var1;
1847 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001848 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001849 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001850 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001851 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001852 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001853 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001854
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001855 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001856 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001857
1858 if (skip)
1859 {
1860 /* When skipping just find the end of the name. */
1861 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001862 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001863 }
1864
1865 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001866 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001867 if (expr_start != NULL)
1868 {
1869 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001870 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001871 && *p != '[' && *p != '.')
1872 {
1873 EMSG(_(e_trailing));
1874 return NULL;
1875 }
1876
1877 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1878 if (lp->ll_exp_name == NULL)
1879 {
1880 /* Report an invalid expression in braces, unless the
1881 * expression evaluation has been cancelled due to an
1882 * aborting error, an interrupt, or an exception. */
1883 if (!aborting() && !quiet)
1884 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001885 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001886 EMSG2(_(e_invarg2), name);
1887 return NULL;
1888 }
1889 }
1890 lp->ll_name = lp->ll_exp_name;
1891 }
1892 else
1893 lp->ll_name = name;
1894
1895 /* Without [idx] or .key we are done. */
1896 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1897 return p;
1898
1899 cc = *p;
1900 *p = NUL;
Bram Moolenaar6e65d592017-12-07 22:11:27 +01001901 /* Only pass &ht when we would write to the variable, it prevents autoload
1902 * as well. */
1903 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
1904 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001905 if (v == NULL && !quiet)
1906 EMSG2(_(e_undefvar), lp->ll_name);
1907 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001908 if (v == NULL)
1909 return NULL;
1910
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001911 /*
1912 * Loop until no more [idx] or .key is following.
1913 */
Bram Moolenaar33570922005-01-25 22:26:29 +00001914 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001915 var1.v_type = VAR_UNKNOWN;
1916 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001917 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001918 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001919 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1920 && !(lp->ll_tv->v_type == VAR_DICT
1921 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001922 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001923 if (!quiet)
1924 EMSG(_("E689: Can only index a List or Dictionary"));
1925 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001926 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001927 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001928 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001929 if (!quiet)
1930 EMSG(_("E708: [:] must come last"));
1931 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001932 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001933
Bram Moolenaar8c711452005-01-14 21:53:12 +00001934 len = -1;
1935 if (*p == '.')
1936 {
1937 key = p + 1;
1938 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1939 ;
1940 if (len == 0)
1941 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001942 if (!quiet)
1943 EMSG(_(e_emptykey));
1944 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001945 }
1946 p = key + len;
1947 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001948 else
1949 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001950 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001951 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001952 if (*p == ':')
1953 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001954 else
1955 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001956 empty1 = FALSE;
1957 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001958 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001959 if (get_tv_string_chk(&var1) == NULL)
1960 {
1961 /* not a number or string */
1962 clear_tv(&var1);
1963 return NULL;
1964 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001965 }
1966
1967 /* Optionally get the second index [ :expr]. */
1968 if (*p == ':')
1969 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001970 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001971 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001972 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001973 EMSG(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001974 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001975 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001976 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001977 if (rettv != NULL && (rettv->v_type != VAR_LIST
1978 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001979 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001980 if (!quiet)
1981 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001982 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001983 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001984 }
1985 p = skipwhite(p + 1);
1986 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001987 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001988 else
1989 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001990 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001991 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
1992 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001993 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001994 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001995 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001996 if (get_tv_string_chk(&var2) == NULL)
1997 {
1998 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001999 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002000 clear_tv(&var2);
2001 return NULL;
2002 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002003 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002004 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002005 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002006 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002007 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002008
Bram Moolenaar8c711452005-01-14 21:53:12 +00002009 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002010 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002011 if (!quiet)
2012 EMSG(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002013 clear_tv(&var1);
2014 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002015 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002016 }
2017
2018 /* Skip to past ']'. */
2019 ++p;
2020 }
2021
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002022 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002023 {
2024 if (len == -1)
2025 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002026 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002027 key = get_tv_string_chk(&var1); /* is number or string */
2028 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002029 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002030 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002031 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002032 }
2033 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002034 lp->ll_list = NULL;
2035 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002036 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002037
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002038 /* When assigning to a scope dictionary check that a function and
2039 * variable name is valid (only variable name unless it is l: or
2040 * g: dictionary). Disallow overwriting a builtin function. */
2041 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002042 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002043 int prevval;
2044 int wrong;
2045
2046 if (len != -1)
2047 {
2048 prevval = key[len];
2049 key[len] = NUL;
2050 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002051 else
2052 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002053 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2054 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002055 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002056 || !valid_varname(key);
2057 if (len != -1)
2058 key[len] = prevval;
2059 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002060 return NULL;
2061 }
2062
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002063 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002064 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002065 /* Can't add "v:" variable. */
2066 if (lp->ll_dict == &vimvardict)
2067 {
2068 EMSG2(_(e_illvar), name);
2069 return NULL;
2070 }
2071
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002072 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002073 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002074 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002075 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002076 EMSG2(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002077 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002078 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002079 }
2080 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002081 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002082 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002083 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002084 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002085 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002086 p = NULL;
2087 break;
2088 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002089 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002090 else if ((flags & GLV_READ_ONLY) == 0
2091 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002092 {
2093 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002094 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002095 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002096
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002097 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002098 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002099 }
2100 else
2101 {
2102 /*
2103 * Get the number and item for the only or first index of the List.
2104 */
2105 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002106 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002107 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002108 /* is number or string */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002109 lp->ll_n1 = (long)get_tv_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002110 clear_tv(&var1);
2111
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002112 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002113 lp->ll_list = lp->ll_tv->vval.v_list;
2114 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2115 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002116 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002117 if (lp->ll_n1 < 0)
2118 {
2119 lp->ll_n1 = 0;
2120 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2121 }
2122 }
2123 if (lp->ll_li == NULL)
2124 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002125 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002126 if (!quiet)
2127 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002128 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002129 }
2130
2131 /*
2132 * May need to find the item or absolute index for the second
2133 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002134 * When no index given: "lp->ll_empty2" is TRUE.
2135 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002136 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002137 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002138 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002139 lp->ll_n2 = (long)get_tv_number(&var2);
2140 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002141 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002142 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002143 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002144 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002145 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002146 {
2147 if (!quiet)
2148 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002149 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002150 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002151 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002152 }
2153
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002154 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2155 if (lp->ll_n1 < 0)
2156 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2157 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002158 {
2159 if (!quiet)
2160 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002161 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002162 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002163 }
2164
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002165 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002166 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002167 }
2168
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002169 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002170 return p;
2171}
2172
2173/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002174 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002175 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002176 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002177clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002178{
2179 vim_free(lp->ll_exp_name);
2180 vim_free(lp->ll_newkey);
2181}
2182
2183/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002184 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002185 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002186 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002187 */
2188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002189set_var_lval(
2190 lval_T *lp,
2191 char_u *endp,
2192 typval_T *rettv,
2193 int copy,
2194 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002195{
2196 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002197 listitem_T *ri;
2198 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002199
2200 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002201 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002202 cc = *endp;
2203 *endp = NUL;
2204 if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002205 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002206 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002207
Bram Moolenaar79518e22017-02-17 16:31:35 +01002208 /* handle +=, -= and .= */
2209 di = NULL;
2210 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2211 &tv, &di, TRUE, FALSE) == OK)
2212 {
2213 if ((di == NULL
2214 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2215 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2216 FALSE)))
2217 && tv_op(&tv, rettv, op) == OK)
2218 set_var(lp->ll_name, &tv, FALSE);
2219 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002220 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002222 else
2223 set_var(lp->ll_name, rettv, copy);
2224 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002225 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002226 else if (tv_check_lock(lp->ll_newkey == NULL
2227 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002228 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002229 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002230 else if (lp->ll_range)
2231 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002232 listitem_T *ll_li = lp->ll_li;
2233 int ll_n1 = lp->ll_n1;
2234
2235 /*
2236 * Check whether any of the list items is locked
2237 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002238 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002239 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002240 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002241 return;
2242 ri = ri->li_next;
2243 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2244 break;
2245 ll_li = ll_li->li_next;
2246 ++ll_n1;
2247 }
2248
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002249 /*
2250 * Assign the List values to the list items.
2251 */
2252 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002253 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002254 if (op != NULL && *op != '=')
2255 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2256 else
2257 {
2258 clear_tv(&lp->ll_li->li_tv);
2259 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2260 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002261 ri = ri->li_next;
2262 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2263 break;
2264 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002265 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002266 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002267 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002268 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002269 ri = NULL;
2270 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002271 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002272 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002273 lp->ll_li = lp->ll_li->li_next;
2274 ++lp->ll_n1;
2275 }
2276 if (ri != NULL)
2277 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002278 else if (lp->ll_empty2
2279 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002280 : lp->ll_n1 != lp->ll_n2)
2281 EMSG(_("E711: List value has not enough items"));
2282 }
2283 else
2284 {
2285 /*
2286 * Assign to a List or Dictionary item.
2287 */
2288 if (lp->ll_newkey != NULL)
2289 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002290 if (op != NULL && *op != '=')
2291 {
2292 EMSG2(_(e_letwrong), op);
2293 return;
2294 }
2295
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002296 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002297 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002298 if (di == NULL)
2299 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002300 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2301 {
2302 vim_free(di);
2303 return;
2304 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002305 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002306 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002307 else if (op != NULL && *op != '=')
2308 {
2309 tv_op(lp->ll_tv, rettv, op);
2310 return;
2311 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002312 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002313 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002314
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002315 /*
2316 * Assign the value to the variable or list item.
2317 */
2318 if (copy)
2319 copy_tv(rettv, lp->ll_tv);
2320 else
2321 {
2322 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002323 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002324 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002325 }
2326 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327}
2328
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002329/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002330 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2331 * Returns OK or FAIL.
2332 */
2333 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002334tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002335{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002336 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002337 char_u numbuf[NUMBUFLEN];
2338 char_u *s;
2339
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002340 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2341 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2342 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002343 {
2344 switch (tv1->v_type)
2345 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002346 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002347 case VAR_DICT:
2348 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002349 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002350 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002351 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002352 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002353 break;
2354
2355 case VAR_LIST:
2356 if (*op != '+' || tv2->v_type != VAR_LIST)
2357 break;
2358 /* List += List */
2359 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2360 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2361 return OK;
2362
2363 case VAR_NUMBER:
2364 case VAR_STRING:
2365 if (tv2->v_type == VAR_LIST)
2366 break;
2367 if (*op == '+' || *op == '-')
2368 {
2369 /* nr += nr or nr -= nr*/
2370 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002371#ifdef FEAT_FLOAT
2372 if (tv2->v_type == VAR_FLOAT)
2373 {
2374 float_T f = n;
2375
2376 if (*op == '+')
2377 f += tv2->vval.v_float;
2378 else
2379 f -= tv2->vval.v_float;
2380 clear_tv(tv1);
2381 tv1->v_type = VAR_FLOAT;
2382 tv1->vval.v_float = f;
2383 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002384 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002385#endif
2386 {
2387 if (*op == '+')
2388 n += get_tv_number(tv2);
2389 else
2390 n -= get_tv_number(tv2);
2391 clear_tv(tv1);
2392 tv1->v_type = VAR_NUMBER;
2393 tv1->vval.v_number = n;
2394 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002395 }
2396 else
2397 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002398 if (tv2->v_type == VAR_FLOAT)
2399 break;
2400
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002401 /* str .= str */
2402 s = get_tv_string(tv1);
2403 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2404 clear_tv(tv1);
2405 tv1->v_type = VAR_STRING;
2406 tv1->vval.v_string = s;
2407 }
2408 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002409
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002410 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002411#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002412 {
2413 float_T f;
2414
2415 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2416 && tv2->v_type != VAR_NUMBER
2417 && tv2->v_type != VAR_STRING))
2418 break;
2419 if (tv2->v_type == VAR_FLOAT)
2420 f = tv2->vval.v_float;
2421 else
2422 f = get_tv_number(tv2);
2423 if (*op == '+')
2424 tv1->vval.v_float += f;
2425 else
2426 tv1->vval.v_float -= f;
2427 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002428#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002429 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002430 }
2431 }
2432
2433 EMSG2(_(e_letwrong), op);
2434 return FAIL;
2435}
2436
2437/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002438 * Evaluate the expression used in a ":for var in expr" command.
2439 * "arg" points to "var".
2440 * Set "*errp" to TRUE for an error, FALSE otherwise;
2441 * Return a pointer that holds the info. Null when there is an error.
2442 */
2443 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002444eval_for_line(
2445 char_u *arg,
2446 int *errp,
2447 char_u **nextcmdp,
2448 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002449{
Bram Moolenaar33570922005-01-25 22:26:29 +00002450 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002451 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002452 typval_T tv;
2453 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002454
2455 *errp = TRUE; /* default: there is an error */
2456
Bram Moolenaar33570922005-01-25 22:26:29 +00002457 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002458 if (fi == NULL)
2459 return NULL;
2460
2461 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2462 if (expr == NULL)
2463 return fi;
2464
2465 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002466 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002467 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002468 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002469 return fi;
2470 }
2471
2472 if (skip)
2473 ++emsg_skip;
2474 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2475 {
2476 *errp = FALSE;
2477 if (!skip)
2478 {
2479 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002480 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002481 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002482 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002483 clear_tv(&tv);
2484 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002485 else if (l == NULL)
2486 {
2487 /* a null list is like an empty list: do nothing */
2488 clear_tv(&tv);
2489 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002490 else
2491 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002492 /* No need to increment the refcount, it's already set for the
2493 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002494 fi->fi_list = l;
2495 list_add_watch(l, &fi->fi_lw);
2496 fi->fi_lw.lw_item = l->lv_first;
2497 }
2498 }
2499 }
2500 if (skip)
2501 --emsg_skip;
2502
2503 return fi;
2504}
2505
2506/*
2507 * Use the first item in a ":for" list. Advance to the next.
2508 * Assign the values to the variable (list). "arg" points to the first one.
2509 * Return TRUE when a valid item was found, FALSE when at end of list or
2510 * something wrong.
2511 */
2512 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002513next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002514{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002515 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002516 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002517 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002518
2519 item = fi->fi_lw.lw_item;
2520 if (item == NULL)
2521 result = FALSE;
2522 else
2523 {
2524 fi->fi_lw.lw_item = item->li_next;
2525 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2526 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2527 }
2528 return result;
2529}
2530
2531/*
2532 * Free the structure used to store info used by ":for".
2533 */
2534 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002535free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002536{
Bram Moolenaar33570922005-01-25 22:26:29 +00002537 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002538
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002539 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002540 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002541 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002542 list_unref(fi->fi_list);
2543 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002544 vim_free(fi);
2545}
2546
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2548
2549 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002550set_context_for_expression(
2551 expand_T *xp,
2552 char_u *arg,
2553 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554{
2555 int got_eq = FALSE;
2556 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002557 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002559 if (cmdidx == CMD_let)
2560 {
2561 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002562 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002563 {
2564 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002565 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002566 {
2567 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002568 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002569 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002570 break;
2571 }
2572 return;
2573 }
2574 }
2575 else
2576 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2577 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 while ((xp->xp_pattern = vim_strpbrk(arg,
2579 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2580 {
2581 c = *xp->xp_pattern;
2582 if (c == '&')
2583 {
2584 c = xp->xp_pattern[1];
2585 if (c == '&')
2586 {
2587 ++xp->xp_pattern;
2588 xp->xp_context = cmdidx != CMD_let || got_eq
2589 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2590 }
2591 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002592 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002594 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2595 xp->xp_pattern += 2;
2596
2597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 }
2599 else if (c == '$')
2600 {
2601 /* environment variable */
2602 xp->xp_context = EXPAND_ENV_VARS;
2603 }
2604 else if (c == '=')
2605 {
2606 got_eq = TRUE;
2607 xp->xp_context = EXPAND_EXPRESSION;
2608 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002609 else if (c == '#'
2610 && xp->xp_context == EXPAND_EXPRESSION)
2611 {
2612 /* Autoload function/variable contains '#'. */
2613 break;
2614 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002615 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 && xp->xp_context == EXPAND_FUNCTIONS
2617 && vim_strchr(xp->xp_pattern, '(') == NULL)
2618 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002619 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 break;
2621 }
2622 else if (cmdidx != CMD_let || got_eq)
2623 {
2624 if (c == '"') /* string */
2625 {
2626 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2627 if (c == '\\' && xp->xp_pattern[1] != NUL)
2628 ++xp->xp_pattern;
2629 xp->xp_context = EXPAND_NOTHING;
2630 }
2631 else if (c == '\'') /* literal string */
2632 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002633 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2635 /* skip */ ;
2636 xp->xp_context = EXPAND_NOTHING;
2637 }
2638 else if (c == '|')
2639 {
2640 if (xp->xp_pattern[1] == '|')
2641 {
2642 ++xp->xp_pattern;
2643 xp->xp_context = EXPAND_EXPRESSION;
2644 }
2645 else
2646 xp->xp_context = EXPAND_COMMANDS;
2647 }
2648 else
2649 xp->xp_context = EXPAND_EXPRESSION;
2650 }
2651 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002652 /* Doesn't look like something valid, expand as an expression
2653 * anyway. */
2654 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 arg = xp->xp_pattern;
2656 if (*arg != NUL)
2657 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2658 /* skip */ ;
2659 }
2660 xp->xp_pattern = arg;
2661}
2662
2663#endif /* FEAT_CMDL_COMPL */
2664
2665/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 * ":unlet[!] var1 ... " command.
2667 */
2668 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002669ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002671 ex_unletlock(eap, eap->arg, 0);
2672}
2673
2674/*
2675 * ":lockvar" and ":unlockvar" commands
2676 */
2677 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002678ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002679{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002681 int deep = 2;
2682
2683 if (eap->forceit)
2684 deep = -1;
2685 else if (vim_isdigit(*arg))
2686 {
2687 deep = getdigits(&arg);
2688 arg = skipwhite(arg);
2689 }
2690
2691 ex_unletlock(eap, arg, deep);
2692}
2693
2694/*
2695 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2696 */
2697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002698ex_unletlock(
2699 exarg_T *eap,
2700 char_u *argstart,
2701 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002702{
2703 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002706 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707
2708 do
2709 {
Bram Moolenaar137374f2018-05-13 15:59:50 +02002710 if (*arg == '$')
2711 {
2712 char_u *name = ++arg;
2713
2714 if (get_env_len(&arg) == 0)
2715 {
2716 EMSG2(_(e_invarg2), name - 1);
2717 return;
2718 }
2719 vim_unsetenv(name);
2720 arg = skipwhite(arg);
2721 continue;
2722 }
2723
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002725 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002726 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 if (lv.ll_name == NULL)
2728 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002729 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 if (name_end != NULL)
2733 {
2734 emsg_severe = TRUE;
2735 EMSG(_(e_trailing));
2736 }
2737 if (!(eap->skip || error))
2738 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 break;
2740 }
2741
2742 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002743 {
2744 if (eap->cmdidx == CMD_unlet)
2745 {
2746 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2747 error = TRUE;
2748 }
2749 else
2750 {
2751 if (do_lock_var(&lv, name_end, deep,
2752 eap->cmdidx == CMD_lockvar) == FAIL)
2753 error = TRUE;
2754 }
2755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002757 if (!eap->skip)
2758 clear_lval(&lv);
2759
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 arg = skipwhite(name_end);
2761 } while (!ends_excmd(*arg));
2762
2763 eap->nextcmd = check_nextcmd(arg);
2764}
2765
Bram Moolenaar8c711452005-01-14 21:53:12 +00002766 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002767do_unlet_var(
2768 lval_T *lp,
2769 char_u *name_end,
2770 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002771{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002772 int ret = OK;
2773 int cc;
2774
2775 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 cc = *name_end;
2778 *name_end = NUL;
2779
2780 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002781 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002783 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002784 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002785 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002786 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002787 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002788 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002789 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002790 else if (lp->ll_range)
2791 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002792 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002793 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01002794 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002795
2796 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
2797 {
2798 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02002799 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002800 return FAIL;
2801 ll_li = li;
2802 ++ll_n1;
2803 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002804
2805 /* Delete a range of List items. */
2806 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2807 {
2808 li = lp->ll_li->li_next;
2809 listitem_remove(lp->ll_list, lp->ll_li);
2810 lp->ll_li = li;
2811 ++lp->ll_n1;
2812 }
2813 }
2814 else
2815 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002816 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 /* unlet a List item. */
2818 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002819 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002820 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002821 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 }
2823
2824 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825}
2826
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827/*
2828 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002829 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 */
2831 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002832do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833{
Bram Moolenaar33570922005-01-25 22:26:29 +00002834 hashtab_T *ht;
2835 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002836 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002837 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002838 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839
Bram Moolenaar33570922005-01-25 22:26:29 +00002840 ht = find_var_ht(name, &varname);
2841 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002843 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002844 if (d == NULL)
2845 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002846 if (ht == &globvarht)
2847 d = &globvardict;
2848 else if (ht == &compat_hashtab)
2849 d = &vimvardict;
2850 else
2851 {
2852 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2853 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2854 }
2855 if (d == NULL)
2856 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01002857 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002858 return FAIL;
2859 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002860 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002861 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002862 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02002863 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002864 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00002865 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002866 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02002867 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002868 || var_check_ro(di->di_flags, name, FALSE)
2869 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01002870 return FAIL;
2871
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002872 delete_var(ht, hi);
2873 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00002874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002876 if (forceit)
2877 return OK;
2878 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 return FAIL;
2880}
2881
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002882/*
2883 * Lock or unlock variable indicated by "lp".
2884 * "deep" is the levels to go (-1 for unlimited);
2885 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2886 */
2887 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002888do_lock_var(
2889 lval_T *lp,
2890 char_u *name_end,
2891 int deep,
2892 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002893{
2894 int ret = OK;
2895 int cc;
2896 dictitem_T *di;
2897
2898 if (deep == 0) /* nothing to do */
2899 return OK;
2900
2901 if (lp->ll_tv == NULL)
2902 {
2903 cc = *name_end;
2904 *name_end = NUL;
2905
2906 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002907 di = find_var(lp->ll_name, NULL, TRUE);
2908 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002909 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01002910 else if ((di->di_flags & DI_FLAGS_FIX)
2911 && di->di_tv.v_type != VAR_DICT
2912 && di->di_tv.v_type != VAR_LIST)
2913 /* For historic reasons this error is not given for a list or dict.
2914 * E.g., the b: dict could be locked/unlocked. */
2915 EMSG2(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002916 else
2917 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002918 if (lock)
2919 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002920 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01002921 di->di_flags &= ~DI_FLAGS_LOCK;
2922 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002923 }
2924 *name_end = cc;
2925 }
2926 else if (lp->ll_range)
2927 {
2928 listitem_T *li = lp->ll_li;
2929
2930 /* (un)lock a range of List items. */
2931 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2932 {
2933 item_lock(&li->li_tv, deep, lock);
2934 li = li->li_next;
2935 ++lp->ll_n1;
2936 }
2937 }
2938 else if (lp->ll_list != NULL)
2939 /* (un)lock a List item. */
2940 item_lock(&lp->ll_li->li_tv, deep, lock);
2941 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02002942 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002943 item_lock(&lp->ll_di->di_tv, deep, lock);
2944
2945 return ret;
2946}
2947
2948/*
2949 * Lock or unlock an item. "deep" is nr of levels to go.
2950 */
2951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002952item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002953{
2954 static int recurse = 0;
2955 list_T *l;
2956 listitem_T *li;
2957 dict_T *d;
2958 hashitem_T *hi;
2959 int todo;
2960
2961 if (recurse >= DICT_MAXNEST)
2962 {
2963 EMSG(_("E743: variable nested too deep for (un)lock"));
2964 return;
2965 }
2966 if (deep == 0)
2967 return;
2968 ++recurse;
2969
2970 /* lock/unlock the item itself */
2971 if (lock)
2972 tv->v_lock |= VAR_LOCKED;
2973 else
2974 tv->v_lock &= ~VAR_LOCKED;
2975
2976 switch (tv->v_type)
2977 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01002978 case VAR_UNKNOWN:
2979 case VAR_NUMBER:
2980 case VAR_STRING:
2981 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002982 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002983 case VAR_FLOAT:
2984 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002985 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002986 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002987 break;
2988
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002989 case VAR_LIST:
2990 if ((l = tv->vval.v_list) != NULL)
2991 {
2992 if (lock)
2993 l->lv_lock |= VAR_LOCKED;
2994 else
2995 l->lv_lock &= ~VAR_LOCKED;
2996 if (deep < 0 || deep > 1)
2997 /* recursive: lock/unlock the items the List contains */
2998 for (li = l->lv_first; li != NULL; li = li->li_next)
2999 item_lock(&li->li_tv, deep - 1, lock);
3000 }
3001 break;
3002 case VAR_DICT:
3003 if ((d = tv->vval.v_dict) != NULL)
3004 {
3005 if (lock)
3006 d->dv_lock |= VAR_LOCKED;
3007 else
3008 d->dv_lock &= ~VAR_LOCKED;
3009 if (deep < 0 || deep > 1)
3010 {
3011 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003012 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003013 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3014 {
3015 if (!HASHITEM_EMPTY(hi))
3016 {
3017 --todo;
3018 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3019 }
3020 }
3021 }
3022 }
3023 }
3024 --recurse;
3025}
3026
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3028/*
3029 * Delete all "menutrans_" variables.
3030 */
3031 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003032del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033{
Bram Moolenaar33570922005-01-25 22:26:29 +00003034 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003035 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036
Bram Moolenaar33570922005-01-25 22:26:29 +00003037 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003038 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003039 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003040 {
3041 if (!HASHITEM_EMPTY(hi))
3042 {
3043 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003044 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3045 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003046 }
3047 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003048 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049}
3050#endif
3051
3052#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3053
3054/*
3055 * Local string buffer for the next two functions to store a variable name
3056 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3057 * get_user_var_name().
3058 */
3059
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060static char_u *varnamebuf = NULL;
3061static int varnamebuflen = 0;
3062
3063/*
3064 * Function to concatenate a prefix and a variable name.
3065 */
3066 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003067cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068{
3069 int len;
3070
3071 len = (int)STRLEN(name) + 3;
3072 if (len > varnamebuflen)
3073 {
3074 vim_free(varnamebuf);
3075 len += 10; /* some additional space */
3076 varnamebuf = alloc(len);
3077 if (varnamebuf == NULL)
3078 {
3079 varnamebuflen = 0;
3080 return NULL;
3081 }
3082 varnamebuflen = len;
3083 }
3084 *varnamebuf = prefix;
3085 varnamebuf[1] = ':';
3086 STRCPY(varnamebuf + 2, name);
3087 return varnamebuf;
3088}
3089
3090/*
3091 * Function given to ExpandGeneric() to obtain the list of user defined
3092 * (global/buffer/window/built-in) variable names.
3093 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003095get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003097 static long_u gdone;
3098 static long_u bdone;
3099 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003100 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003101 static int vidx;
3102 static hashitem_T *hi;
3103 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104
3105 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003106 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003107 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003108 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003109 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003110
3111 /* Global variables */
3112 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003114 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003115 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003116 else
3117 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003118 while (HASHITEM_EMPTY(hi))
3119 ++hi;
3120 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3121 return cat_prefix_varname('g', hi->hi_key);
3122 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003124
3125 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003126 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003127 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003129 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003130 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003131 else
3132 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003133 while (HASHITEM_EMPTY(hi))
3134 ++hi;
3135 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003137
3138 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003139 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003140 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003142 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003143 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003144 else
3145 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003146 while (HASHITEM_EMPTY(hi))
3147 ++hi;
3148 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003150
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003151 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003152 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003153 if (tdone < ht->ht_used)
3154 {
3155 if (tdone++ == 0)
3156 hi = ht->ht_array;
3157 else
3158 ++hi;
3159 while (HASHITEM_EMPTY(hi))
3160 ++hi;
3161 return cat_prefix_varname('t', hi->hi_key);
3162 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003163
Bram Moolenaar33570922005-01-25 22:26:29 +00003164 /* v: variables */
3165 if (vidx < VV_LEN)
3166 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167
Bram Moolenaard23a8232018-02-10 18:45:26 +01003168 VIM_CLEAR(varnamebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 varnamebuflen = 0;
3170 return NULL;
3171}
3172
3173#endif /* FEAT_CMDL_COMPL */
3174
3175/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003176 * Return TRUE if "pat" matches "text".
3177 * Does not use 'cpo' and always uses 'magic'.
3178 */
3179 static int
3180pattern_match(char_u *pat, char_u *text, int ic)
3181{
3182 int matches = FALSE;
3183 char_u *save_cpo;
3184 regmatch_T regmatch;
3185
3186 /* avoid 'l' flag in 'cpoptions' */
3187 save_cpo = p_cpo;
3188 p_cpo = (char_u *)"";
3189 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3190 if (regmatch.regprog != NULL)
3191 {
3192 regmatch.rm_ic = ic;
3193 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3194 vim_regfree(regmatch.regprog);
3195 }
3196 p_cpo = save_cpo;
3197 return matches;
3198}
3199
3200/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003202 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3204 */
3205
3206/*
3207 * Handle zero level expression.
3208 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003209 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003210 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 * Return OK or FAIL.
3212 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003213 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003214eval0(
3215 char_u *arg,
3216 typval_T *rettv,
3217 char_u **nextcmd,
3218 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219{
3220 int ret;
3221 char_u *p;
3222
3223 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003224 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 if (ret == FAIL || !ends_excmd(*p))
3226 {
3227 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003228 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 /*
3230 * Report the invalid expression unless the expression evaluation has
3231 * been cancelled due to an aborting error, an interrupt, or an
3232 * exception.
3233 */
3234 if (!aborting())
3235 EMSG2(_(e_invexpr2), arg);
3236 ret = FAIL;
3237 }
3238 if (nextcmd != NULL)
3239 *nextcmd = check_nextcmd(p);
3240
3241 return ret;
3242}
3243
3244/*
3245 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003246 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 *
3248 * "arg" must point to the first non-white of the expression.
3249 * "arg" is advanced to the next non-white after the recognized expression.
3250 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003251 * Note: "rettv.v_lock" is not set.
3252 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 * Return OK or FAIL.
3254 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003255 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003256eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257{
3258 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003259 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260
3261 /*
3262 * Get the first variable.
3263 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003264 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 return FAIL;
3266
3267 if ((*arg)[0] == '?')
3268 {
3269 result = FALSE;
3270 if (evaluate)
3271 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003272 int error = FALSE;
3273
3274 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003276 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003277 if (error)
3278 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 }
3280
3281 /*
3282 * Get the second variable.
3283 */
3284 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003285 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 return FAIL;
3287
3288 /*
3289 * Check for the ":".
3290 */
3291 if ((*arg)[0] != ':')
3292 {
3293 EMSG(_("E109: Missing ':' after '?'"));
3294 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003295 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 return FAIL;
3297 }
3298
3299 /*
3300 * Get the third variable.
3301 */
3302 *arg = skipwhite(*arg + 1);
3303 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3304 {
3305 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003306 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 return FAIL;
3308 }
3309 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003310 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 }
3312
3313 return OK;
3314}
3315
3316/*
3317 * Handle first level expression:
3318 * expr2 || expr2 || expr2 logical OR
3319 *
3320 * "arg" must point to the first non-white of the expression.
3321 * "arg" is advanced to the next non-white after the recognized expression.
3322 *
3323 * Return OK or FAIL.
3324 */
3325 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003326eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327{
Bram Moolenaar33570922005-01-25 22:26:29 +00003328 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 long result;
3330 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003331 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332
3333 /*
3334 * Get the first variable.
3335 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003336 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 return FAIL;
3338
3339 /*
3340 * Repeat until there is no following "||".
3341 */
3342 first = TRUE;
3343 result = FALSE;
3344 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3345 {
3346 if (evaluate && first)
3347 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003348 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003350 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003351 if (error)
3352 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 first = FALSE;
3354 }
3355
3356 /*
3357 * Get the second variable.
3358 */
3359 *arg = skipwhite(*arg + 2);
3360 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3361 return FAIL;
3362
3363 /*
3364 * Compute the result.
3365 */
3366 if (evaluate && !result)
3367 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003368 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003370 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003371 if (error)
3372 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 }
3374 if (evaluate)
3375 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003376 rettv->v_type = VAR_NUMBER;
3377 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 }
3379 }
3380
3381 return OK;
3382}
3383
3384/*
3385 * Handle second level expression:
3386 * expr3 && expr3 && expr3 logical AND
3387 *
3388 * "arg" must point to the first non-white of the expression.
3389 * "arg" is advanced to the next non-white after the recognized expression.
3390 *
3391 * Return OK or FAIL.
3392 */
3393 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003394eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395{
Bram Moolenaar33570922005-01-25 22:26:29 +00003396 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 long result;
3398 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003399 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400
3401 /*
3402 * Get the first variable.
3403 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003404 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 return FAIL;
3406
3407 /*
3408 * Repeat until there is no following "&&".
3409 */
3410 first = TRUE;
3411 result = TRUE;
3412 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3413 {
3414 if (evaluate && first)
3415 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003416 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003418 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003419 if (error)
3420 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 first = FALSE;
3422 }
3423
3424 /*
3425 * Get the second variable.
3426 */
3427 *arg = skipwhite(*arg + 2);
3428 if (eval4(arg, &var2, evaluate && result) == FAIL)
3429 return FAIL;
3430
3431 /*
3432 * Compute the result.
3433 */
3434 if (evaluate && result)
3435 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003436 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003438 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003439 if (error)
3440 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 }
3442 if (evaluate)
3443 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003444 rettv->v_type = VAR_NUMBER;
3445 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 }
3447 }
3448
3449 return OK;
3450}
3451
3452/*
3453 * Handle third level expression:
3454 * var1 == var2
3455 * var1 =~ var2
3456 * var1 != var2
3457 * var1 !~ var2
3458 * var1 > var2
3459 * var1 >= var2
3460 * var1 < var2
3461 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003462 * var1 is var2
3463 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 *
3465 * "arg" must point to the first non-white of the expression.
3466 * "arg" is advanced to the next non-white after the recognized expression.
3467 *
3468 * Return OK or FAIL.
3469 */
3470 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003471eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472{
Bram Moolenaar33570922005-01-25 22:26:29 +00003473 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 char_u *p;
3475 int i;
3476 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003477 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480
3481 /*
3482 * Get the first variable.
3483 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003484 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 return FAIL;
3486
3487 p = *arg;
3488 switch (p[0])
3489 {
3490 case '=': if (p[1] == '=')
3491 type = TYPE_EQUAL;
3492 else if (p[1] == '~')
3493 type = TYPE_MATCH;
3494 break;
3495 case '!': if (p[1] == '=')
3496 type = TYPE_NEQUAL;
3497 else if (p[1] == '~')
3498 type = TYPE_NOMATCH;
3499 break;
3500 case '>': if (p[1] != '=')
3501 {
3502 type = TYPE_GREATER;
3503 len = 1;
3504 }
3505 else
3506 type = TYPE_GEQUAL;
3507 break;
3508 case '<': if (p[1] != '=')
3509 {
3510 type = TYPE_SMALLER;
3511 len = 1;
3512 }
3513 else
3514 type = TYPE_SEQUAL;
3515 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003516 case 'i': if (p[1] == 's')
3517 {
3518 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3519 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003520 i = p[len];
3521 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003522 {
3523 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3524 type_is = TRUE;
3525 }
3526 }
3527 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 }
3529
3530 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003531 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 */
3533 if (type != TYPE_UNKNOWN)
3534 {
3535 /* extra question mark appended: ignore case */
3536 if (p[len] == '?')
3537 {
3538 ic = TRUE;
3539 ++len;
3540 }
3541 /* extra '#' appended: match case */
3542 else if (p[len] == '#')
3543 {
3544 ic = FALSE;
3545 ++len;
3546 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003547 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 else
3549 ic = p_ic;
3550
3551 /*
3552 * Get the second variable.
3553 */
3554 *arg = skipwhite(p + len);
3555 if (eval5(arg, &var2, evaluate) == FAIL)
3556 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003557 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 return FAIL;
3559 }
Bram Moolenaar31988702018-02-13 12:57:42 +01003560 if (evaluate)
3561 {
3562 int ret = typval_compare(rettv, &var2, type, type_is, ic);
3563
3564 clear_tv(&var2);
3565 return ret;
3566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 }
3568
3569 return OK;
3570}
3571
3572/*
3573 * Handle fourth level expression:
3574 * + number addition
3575 * - number subtraction
3576 * . string concatenation
3577 *
3578 * "arg" must point to the first non-white of the expression.
3579 * "arg" is advanced to the next non-white after the recognized expression.
3580 *
3581 * Return OK or FAIL.
3582 */
3583 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003584eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585{
Bram Moolenaar33570922005-01-25 22:26:29 +00003586 typval_T var2;
3587 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003589 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003590#ifdef FEAT_FLOAT
3591 float_T f1 = 0, f2 = 0;
3592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 char_u *s1, *s2;
3594 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3595 char_u *p;
3596
3597 /*
3598 * Get the first variable.
3599 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003600 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 return FAIL;
3602
3603 /*
3604 * Repeat computing, until no '+', '-' or '.' is following.
3605 */
3606 for (;;)
3607 {
3608 op = **arg;
3609 if (op != '+' && op != '-' && op != '.')
3610 break;
3611
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003612 if ((op != '+' || rettv->v_type != VAR_LIST)
3613#ifdef FEAT_FLOAT
3614 && (op == '.' || rettv->v_type != VAR_FLOAT)
3615#endif
3616 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003617 {
3618 /* For "list + ...", an illegal use of the first operand as
3619 * a number cannot be determined before evaluating the 2nd
3620 * operand: if this is also a list, all is ok.
3621 * For "something . ...", "something - ..." or "non-list + ...",
3622 * we know that the first operand needs to be a string or number
3623 * without evaluating the 2nd operand. So check before to avoid
3624 * side effects after an error. */
3625 if (evaluate && get_tv_string_chk(rettv) == NULL)
3626 {
3627 clear_tv(rettv);
3628 return FAIL;
3629 }
3630 }
3631
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 /*
3633 * Get the second variable.
3634 */
3635 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003636 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003638 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 return FAIL;
3640 }
3641
3642 if (evaluate)
3643 {
3644 /*
3645 * Compute the result.
3646 */
3647 if (op == '.')
3648 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003649 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
3650 s2 = get_tv_string_buf_chk(&var2, buf2);
3651 if (s2 == NULL) /* type error ? */
3652 {
3653 clear_tv(rettv);
3654 clear_tv(&var2);
3655 return FAIL;
3656 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003657 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003658 clear_tv(rettv);
3659 rettv->v_type = VAR_STRING;
3660 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003662 else if (op == '+' && rettv->v_type == VAR_LIST
3663 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003664 {
3665 /* concatenate Lists */
3666 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3667 &var3) == FAIL)
3668 {
3669 clear_tv(rettv);
3670 clear_tv(&var2);
3671 return FAIL;
3672 }
3673 clear_tv(rettv);
3674 *rettv = var3;
3675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 else
3677 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003678 int error = FALSE;
3679
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003680#ifdef FEAT_FLOAT
3681 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003682 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003683 f1 = rettv->vval.v_float;
3684 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003685 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003686 else
3687#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003688 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003689 n1 = get_tv_number_chk(rettv, &error);
3690 if (error)
3691 {
3692 /* This can only happen for "list + non-list". For
3693 * "non-list + ..." or "something - ...", we returned
3694 * before evaluating the 2nd operand. */
3695 clear_tv(rettv);
3696 return FAIL;
3697 }
3698#ifdef FEAT_FLOAT
3699 if (var2.v_type == VAR_FLOAT)
3700 f1 = n1;
3701#endif
3702 }
3703#ifdef FEAT_FLOAT
3704 if (var2.v_type == VAR_FLOAT)
3705 {
3706 f2 = var2.vval.v_float;
3707 n2 = 0;
3708 }
3709 else
3710#endif
3711 {
3712 n2 = get_tv_number_chk(&var2, &error);
3713 if (error)
3714 {
3715 clear_tv(rettv);
3716 clear_tv(&var2);
3717 return FAIL;
3718 }
3719#ifdef FEAT_FLOAT
3720 if (rettv->v_type == VAR_FLOAT)
3721 f2 = n2;
3722#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003723 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003724 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003725
3726#ifdef FEAT_FLOAT
3727 /* If there is a float on either side the result is a float. */
3728 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3729 {
3730 if (op == '+')
3731 f1 = f1 + f2;
3732 else
3733 f1 = f1 - f2;
3734 rettv->v_type = VAR_FLOAT;
3735 rettv->vval.v_float = f1;
3736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003738#endif
3739 {
3740 if (op == '+')
3741 n1 = n1 + n2;
3742 else
3743 n1 = n1 - n2;
3744 rettv->v_type = VAR_NUMBER;
3745 rettv->vval.v_number = n1;
3746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003748 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 }
3750 }
3751 return OK;
3752}
3753
3754/*
3755 * Handle fifth level expression:
3756 * * number multiplication
3757 * / number division
3758 * % number modulo
3759 *
3760 * "arg" must point to the first non-white of the expression.
3761 * "arg" is advanced to the next non-white after the recognized expression.
3762 *
3763 * Return OK or FAIL.
3764 */
3765 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003766eval6(
3767 char_u **arg,
3768 typval_T *rettv,
3769 int evaluate,
3770 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771{
Bram Moolenaar33570922005-01-25 22:26:29 +00003772 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003774 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003775#ifdef FEAT_FLOAT
3776 int use_float = FALSE;
3777 float_T f1 = 0, f2;
3778#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003779 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780
3781 /*
3782 * Get the first variable.
3783 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003784 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 return FAIL;
3786
3787 /*
3788 * Repeat computing, until no '*', '/' or '%' is following.
3789 */
3790 for (;;)
3791 {
3792 op = **arg;
3793 if (op != '*' && op != '/' && op != '%')
3794 break;
3795
3796 if (evaluate)
3797 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003798#ifdef FEAT_FLOAT
3799 if (rettv->v_type == VAR_FLOAT)
3800 {
3801 f1 = rettv->vval.v_float;
3802 use_float = TRUE;
3803 n1 = 0;
3804 }
3805 else
3806#endif
3807 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003808 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003809 if (error)
3810 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 }
3812 else
3813 n1 = 0;
3814
3815 /*
3816 * Get the second variable.
3817 */
3818 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003819 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 return FAIL;
3821
3822 if (evaluate)
3823 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003824#ifdef FEAT_FLOAT
3825 if (var2.v_type == VAR_FLOAT)
3826 {
3827 if (!use_float)
3828 {
3829 f1 = n1;
3830 use_float = TRUE;
3831 }
3832 f2 = var2.vval.v_float;
3833 n2 = 0;
3834 }
3835 else
3836#endif
3837 {
3838 n2 = get_tv_number_chk(&var2, &error);
3839 clear_tv(&var2);
3840 if (error)
3841 return FAIL;
3842#ifdef FEAT_FLOAT
3843 if (use_float)
3844 f2 = n2;
3845#endif
3846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847
3848 /*
3849 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003850 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003852#ifdef FEAT_FLOAT
3853 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003855 if (op == '*')
3856 f1 = f1 * f2;
3857 else if (op == '/')
3858 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003859# ifdef VMS
3860 /* VMS crashes on divide by zero, work around it */
3861 if (f2 == 0.0)
3862 {
3863 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003864 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003865 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003866 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003867 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003868 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003869 }
3870 else
3871 f1 = f1 / f2;
3872# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003873 /* We rely on the floating point library to handle divide
3874 * by zero to result in "inf" and not a crash. */
3875 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003876# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003879 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00003880 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003881 return FAIL;
3882 }
3883 rettv->v_type = VAR_FLOAT;
3884 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 }
3886 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003889 if (op == '*')
3890 n1 = n1 * n2;
3891 else if (op == '/')
3892 {
3893 if (n2 == 0) /* give an error message? */
3894 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003895 if (n1 == 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003896 n1 = VARNUM_MIN; /* similar to NaN */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003897 else if (n1 < 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003898 n1 = -VARNUM_MAX;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003899 else
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003900 n1 = VARNUM_MAX;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003901 }
3902 else
3903 n1 = n1 / n2;
3904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003906 {
3907 if (n2 == 0) /* give an error message? */
3908 n1 = 0;
3909 else
3910 n1 = n1 % n2;
3911 }
3912 rettv->v_type = VAR_NUMBER;
3913 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 }
3916 }
3917
3918 return OK;
3919}
3920
3921/*
3922 * Handle sixth level expression:
3923 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003924 * "string" string constant
3925 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 * &option-name option value
3927 * @r register contents
3928 * identifier variable value
3929 * function() function call
3930 * $VAR environment variable
3931 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003932 * [expr, expr] List
3933 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 *
3935 * Also handle:
3936 * ! in front logical NOT
3937 * - in front unary minus
3938 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003939 * trailing [] subscript in String or List
3940 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 *
3942 * "arg" must point to the first non-white of the expression.
3943 * "arg" is advanced to the next non-white after the recognized expression.
3944 *
3945 * Return OK or FAIL.
3946 */
3947 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003948eval7(
3949 char_u **arg,
3950 typval_T *rettv,
3951 int evaluate,
3952 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003954 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 int len;
3956 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 char_u *start_leader, *end_leader;
3958 int ret = OK;
3959 char_u *alias;
3960
3961 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003962 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003963 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003965 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966
3967 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003968 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 */
3970 start_leader = *arg;
3971 while (**arg == '!' || **arg == '-' || **arg == '+')
3972 *arg = skipwhite(*arg + 1);
3973 end_leader = *arg;
3974
3975 switch (**arg)
3976 {
3977 /*
3978 * Number constant.
3979 */
3980 case '0':
3981 case '1':
3982 case '2':
3983 case '3':
3984 case '4':
3985 case '5':
3986 case '6':
3987 case '7':
3988 case '8':
3989 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003990 {
3991#ifdef FEAT_FLOAT
3992 char_u *p = skipdigits(*arg + 1);
3993 int get_float = FALSE;
3994
3995 /* We accept a float when the format matches
3996 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003997 * strict to avoid backwards compatibility problems.
3998 * Don't look for a float after the "." operator, so that
3999 * ":let vers = 1.2.3" doesn't fail. */
4000 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004002 get_float = TRUE;
4003 p = skipdigits(p + 2);
4004 if (*p == 'e' || *p == 'E')
4005 {
4006 ++p;
4007 if (*p == '-' || *p == '+')
4008 ++p;
4009 if (!vim_isdigit(*p))
4010 get_float = FALSE;
4011 else
4012 p = skipdigits(p + 1);
4013 }
4014 if (ASCII_ISALPHA(*p) || *p == '.')
4015 get_float = FALSE;
4016 }
4017 if (get_float)
4018 {
4019 float_T f;
4020
4021 *arg += string2float(*arg, &f);
4022 if (evaluate)
4023 {
4024 rettv->v_type = VAR_FLOAT;
4025 rettv->vval.v_float = f;
4026 }
4027 }
4028 else
4029#endif
4030 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01004031 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004032 *arg += len;
4033 if (evaluate)
4034 {
4035 rettv->v_type = VAR_NUMBER;
4036 rettv->vval.v_number = n;
4037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 }
4039 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041
4042 /*
4043 * String constant: "string".
4044 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004045 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 break;
4047
4048 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004049 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004051 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004052 break;
4053
4054 /*
4055 * List: [expr, expr]
4056 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004057 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 break;
4059
4060 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004061 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004062 * Dictionary: {key: val, key: val}
4063 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004064 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4065 if (ret == NOTDONE)
4066 ret = get_dict_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004067 break;
4068
4069 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004070 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004072 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 break;
4074
4075 /*
4076 * Environment variable: $VAR.
4077 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004078 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 break;
4080
4081 /*
4082 * Register contents: @r.
4083 */
4084 case '@': ++*arg;
4085 if (evaluate)
4086 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004087 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004088 rettv->vval.v_string = get_reg_contents(**arg,
4089 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 }
4091 if (**arg != NUL)
4092 ++*arg;
4093 break;
4094
4095 /*
4096 * nested expression: (expression).
4097 */
4098 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004099 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 if (**arg == ')')
4101 ++*arg;
4102 else if (ret == OK)
4103 {
4104 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004105 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 ret = FAIL;
4107 }
4108 break;
4109
Bram Moolenaar8c711452005-01-14 21:53:12 +00004110 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 break;
4112 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004113
4114 if (ret == NOTDONE)
4115 {
4116 /*
4117 * Must be a variable or function name.
4118 * Can also be a curly-braces kind of name: {expr}.
4119 */
4120 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004121 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004122 if (alias != NULL)
4123 s = alias;
4124
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004125 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004126 ret = FAIL;
4127 else
4128 {
4129 if (**arg == '(') /* recursive! */
4130 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004131 partial_T *partial;
4132
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004133 if (!evaluate)
4134 check_vars(s, len);
4135
Bram Moolenaar8c711452005-01-14 21:53:12 +00004136 /* If "s" is the name of a variable of type VAR_FUNC
4137 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004138 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004139
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004140 /* Need to make a copy, in case evaluating the arguments makes
4141 * the name invalid. */
4142 s = vim_strsave(s);
4143 if (s == NULL)
4144 ret = FAIL;
4145 else
4146 /* Invoke the function. */
4147 ret = get_func_tv(s, len, rettv, arg,
4148 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4149 &len, evaluate, partial, NULL);
4150 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004151
4152 /* If evaluate is FALSE rettv->v_type was not set in
4153 * get_func_tv, but it's needed in handle_subscript() to parse
4154 * what follows. So set it here. */
4155 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4156 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004157 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004158 rettv->v_type = VAR_FUNC;
4159 }
4160
Bram Moolenaar8c711452005-01-14 21:53:12 +00004161 /* Stop the expression evaluation when immediately
4162 * aborting on error, or when an interrupt occurred or
4163 * an exception was thrown but not caught. */
4164 if (aborting())
4165 {
4166 if (ret == OK)
4167 clear_tv(rettv);
4168 ret = FAIL;
4169 }
4170 }
4171 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004172 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004173 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004174 {
4175 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004176 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004177 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004178 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004179 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004180 }
4181
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 *arg = skipwhite(*arg);
4183
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004184 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4185 * expr(expr). */
4186 if (ret == OK)
4187 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188
4189 /*
4190 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4191 */
4192 if (ret == OK && evaluate && end_leader > start_leader)
4193 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004194 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004195 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004196#ifdef FEAT_FLOAT
4197 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004198
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004199 if (rettv->v_type == VAR_FLOAT)
4200 f = rettv->vval.v_float;
4201 else
4202#endif
4203 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004204 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004206 clear_tv(rettv);
4207 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004209 else
4210 {
4211 while (end_leader > start_leader)
4212 {
4213 --end_leader;
4214 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004215 {
4216#ifdef FEAT_FLOAT
4217 if (rettv->v_type == VAR_FLOAT)
4218 f = !f;
4219 else
4220#endif
4221 val = !val;
4222 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004223 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004224 {
4225#ifdef FEAT_FLOAT
4226 if (rettv->v_type == VAR_FLOAT)
4227 f = -f;
4228 else
4229#endif
4230 val = -val;
4231 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004232 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004233#ifdef FEAT_FLOAT
4234 if (rettv->v_type == VAR_FLOAT)
4235 {
4236 clear_tv(rettv);
4237 rettv->vval.v_float = f;
4238 }
4239 else
4240#endif
4241 {
4242 clear_tv(rettv);
4243 rettv->v_type = VAR_NUMBER;
4244 rettv->vval.v_number = val;
4245 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 }
4248
4249 return ret;
4250}
4251
4252/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004253 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4254 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004255 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4256 */
4257 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004258eval_index(
4259 char_u **arg,
4260 typval_T *rettv,
4261 int evaluate,
4262 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004263{
4264 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004265 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004266 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004267 long len = -1;
4268 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004269 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004270 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004271
Bram Moolenaara03f2332016-02-06 18:09:59 +01004272 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004273 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004274 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004275 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004276 if (verbose)
4277 EMSG(_("E695: Cannot index a Funcref"));
4278 return FAIL;
4279 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004280#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004281 if (verbose)
4282 EMSG(_(e_float_as_string));
4283 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004284#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004285 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004286 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004287 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004288 if (verbose)
4289 EMSG(_("E909: Cannot index a special variable"));
4290 return FAIL;
4291 case VAR_UNKNOWN:
4292 if (evaluate)
4293 return FAIL;
4294 /* FALLTHROUGH */
4295
4296 case VAR_STRING:
4297 case VAR_NUMBER:
4298 case VAR_LIST:
4299 case VAR_DICT:
4300 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004301 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004302
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004303 init_tv(&var1);
4304 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004305 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004306 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004307 /*
4308 * dict.name
4309 */
4310 key = *arg + 1;
4311 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4312 ;
4313 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004314 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004315 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004316 }
4317 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004318 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004319 /*
4320 * something[idx]
4321 *
4322 * Get the (first) variable from inside the [].
4323 */
4324 *arg = skipwhite(*arg + 1);
4325 if (**arg == ':')
4326 empty1 = TRUE;
4327 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4328 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004329 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4330 {
4331 /* not a number or string */
4332 clear_tv(&var1);
4333 return FAIL;
4334 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004335
4336 /*
4337 * Get the second variable from inside the [:].
4338 */
4339 if (**arg == ':')
4340 {
4341 range = TRUE;
4342 *arg = skipwhite(*arg + 1);
4343 if (**arg == ']')
4344 empty2 = TRUE;
4345 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4346 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004347 if (!empty1)
4348 clear_tv(&var1);
4349 return FAIL;
4350 }
4351 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4352 {
4353 /* not a number or string */
4354 if (!empty1)
4355 clear_tv(&var1);
4356 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004357 return FAIL;
4358 }
4359 }
4360
4361 /* Check for the ']'. */
4362 if (**arg != ']')
4363 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004364 if (verbose)
4365 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004366 clear_tv(&var1);
4367 if (range)
4368 clear_tv(&var2);
4369 return FAIL;
4370 }
4371 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004372 }
4373
4374 if (evaluate)
4375 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004376 n1 = 0;
4377 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004378 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004379 n1 = get_tv_number(&var1);
4380 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004381 }
4382 if (range)
4383 {
4384 if (empty2)
4385 n2 = -1;
4386 else
4387 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004388 n2 = get_tv_number(&var2);
4389 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004390 }
4391 }
4392
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004393 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004394 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004395 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004396 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004397 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004398 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004399 case VAR_SPECIAL:
4400 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004401 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004402 break; /* not evaluating, skipping over subscript */
4403
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004404 case VAR_NUMBER:
4405 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004406 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004407 len = (long)STRLEN(s);
4408 if (range)
4409 {
4410 /* The resulting variable is a substring. If the indexes
4411 * are out of range the result is empty. */
4412 if (n1 < 0)
4413 {
4414 n1 = len + n1;
4415 if (n1 < 0)
4416 n1 = 0;
4417 }
4418 if (n2 < 0)
4419 n2 = len + n2;
4420 else if (n2 >= len)
4421 n2 = len;
4422 if (n1 >= len || n2 < 0 || n1 > n2)
4423 s = NULL;
4424 else
4425 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4426 }
4427 else
4428 {
4429 /* The resulting variable is a string of a single
4430 * character. If the index is too big or negative the
4431 * result is empty. */
4432 if (n1 >= len || n1 < 0)
4433 s = NULL;
4434 else
4435 s = vim_strnsave(s + n1, 1);
4436 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004437 clear_tv(rettv);
4438 rettv->v_type = VAR_STRING;
4439 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004440 break;
4441
4442 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004443 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004444 if (n1 < 0)
4445 n1 = len + n1;
4446 if (!empty1 && (n1 < 0 || n1 >= len))
4447 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004448 /* For a range we allow invalid values and return an empty
4449 * list. A list index out of range is an error. */
4450 if (!range)
4451 {
4452 if (verbose)
4453 EMSGN(_(e_listidx), n1);
4454 return FAIL;
4455 }
4456 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004457 }
4458 if (range)
4459 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004460 list_T *l;
4461 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004462
4463 if (n2 < 0)
4464 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004465 else if (n2 >= len)
4466 n2 = len - 1;
4467 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004468 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004469 l = list_alloc();
4470 if (l == NULL)
4471 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004472 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004473 n1 <= n2; ++n1)
4474 {
4475 if (list_append_tv(l, &item->li_tv) == FAIL)
4476 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004477 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004478 return FAIL;
4479 }
4480 item = item->li_next;
4481 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004482 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004483 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004484 }
4485 else
4486 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004487 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004488 clear_tv(rettv);
4489 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004490 }
4491 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004492
4493 case VAR_DICT:
4494 if (range)
4495 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004496 if (verbose)
4497 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004498 if (len == -1)
4499 clear_tv(&var1);
4500 return FAIL;
4501 }
4502 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004503 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004504
4505 if (len == -1)
4506 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02004507 key = get_tv_string_chk(&var1);
4508 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004509 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004510 clear_tv(&var1);
4511 return FAIL;
4512 }
4513 }
4514
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004515 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004516
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004517 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004518 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004519 if (len == -1)
4520 clear_tv(&var1);
4521 if (item == NULL)
4522 return FAIL;
4523
4524 copy_tv(&item->di_tv, &var1);
4525 clear_tv(rettv);
4526 *rettv = var1;
4527 }
4528 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004529 }
4530 }
4531
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004532 return OK;
4533}
4534
4535/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 * Get an option value.
4537 * "arg" points to the '&' or '+' before the option name.
4538 * "arg" is advanced to character after the option name.
4539 * Return OK or FAIL.
4540 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004541 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004542get_option_tv(
4543 char_u **arg,
4544 typval_T *rettv, /* when NULL, only check if option exists */
4545 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546{
4547 char_u *option_end;
4548 long numval;
4549 char_u *stringval;
4550 int opt_type;
4551 int c;
4552 int working = (**arg == '+'); /* has("+option") */
4553 int ret = OK;
4554 int opt_flags;
4555
4556 /*
4557 * Isolate the option name and find its value.
4558 */
4559 option_end = find_option_end(arg, &opt_flags);
4560 if (option_end == NULL)
4561 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004562 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 EMSG2(_("E112: Option name missing: %s"), *arg);
4564 return FAIL;
4565 }
4566
4567 if (!evaluate)
4568 {
4569 *arg = option_end;
4570 return OK;
4571 }
4572
4573 c = *option_end;
4574 *option_end = NUL;
4575 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004576 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577
4578 if (opt_type == -3) /* invalid name */
4579 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004580 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 EMSG2(_("E113: Unknown option: %s"), *arg);
4582 ret = FAIL;
4583 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004584 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 {
4586 if (opt_type == -2) /* hidden string option */
4587 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004588 rettv->v_type = VAR_STRING;
4589 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 }
4591 else if (opt_type == -1) /* hidden number option */
4592 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004593 rettv->v_type = VAR_NUMBER;
4594 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 }
4596 else if (opt_type == 1) /* number option */
4597 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004598 rettv->v_type = VAR_NUMBER;
4599 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 }
4601 else /* string option */
4602 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004603 rettv->v_type = VAR_STRING;
4604 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 }
4606 }
4607 else if (working && (opt_type == -2 || opt_type == -1))
4608 ret = FAIL;
4609
4610 *option_end = c; /* put back for error messages */
4611 *arg = option_end;
4612
4613 return ret;
4614}
4615
4616/*
4617 * Allocate a variable for a string constant.
4618 * Return OK or FAIL.
4619 */
4620 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004621get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622{
4623 char_u *p;
4624 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 int extra = 0;
4626
4627 /*
4628 * Find the end of the string, skipping backslashed characters.
4629 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004630 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 {
4632 if (*p == '\\' && p[1] != NUL)
4633 {
4634 ++p;
4635 /* A "\<x>" form occupies at least 4 characters, and produces up
4636 * to 6 characters: reserve space for 2 extra */
4637 if (*p == '<')
4638 extra += 2;
4639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 }
4641
4642 if (*p != '"')
4643 {
4644 EMSG2(_("E114: Missing quote: %s"), *arg);
4645 return FAIL;
4646 }
4647
4648 /* If only parsing, set *arg and return here */
4649 if (!evaluate)
4650 {
4651 *arg = p + 1;
4652 return OK;
4653 }
4654
4655 /*
4656 * Copy the string into allocated memory, handling backslashed
4657 * characters.
4658 */
4659 name = alloc((unsigned)(p - *arg + extra));
4660 if (name == NULL)
4661 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004662 rettv->v_type = VAR_STRING;
4663 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664
Bram Moolenaar8c711452005-01-14 21:53:12 +00004665 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 {
4667 if (*p == '\\')
4668 {
4669 switch (*++p)
4670 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004671 case 'b': *name++ = BS; ++p; break;
4672 case 'e': *name++ = ESC; ++p; break;
4673 case 'f': *name++ = FF; ++p; break;
4674 case 'n': *name++ = NL; ++p; break;
4675 case 'r': *name++ = CAR; ++p; break;
4676 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677
4678 case 'X': /* hex: "\x1", "\x12" */
4679 case 'x':
4680 case 'u': /* Unicode: "\u0023" */
4681 case 'U':
4682 if (vim_isxdigit(p[1]))
4683 {
4684 int n, nr;
4685 int c = toupper(*p);
4686
4687 if (c == 'X')
4688 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004689 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004691 else
4692 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 nr = 0;
4694 while (--n >= 0 && vim_isxdigit(p[1]))
4695 {
4696 ++p;
4697 nr = (nr << 4) + hex2nr(*p);
4698 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004699 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700#ifdef FEAT_MBYTE
4701 /* For "\u" store the number according to
4702 * 'encoding'. */
4703 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004704 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 else
4706#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004707 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 break;
4710
4711 /* octal: "\1", "\12", "\123" */
4712 case '0':
4713 case '1':
4714 case '2':
4715 case '3':
4716 case '4':
4717 case '5':
4718 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004719 case '7': *name = *p++ - '0';
4720 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004722 *name = (*name << 3) + *p++ - '0';
4723 if (*p >= '0' && *p <= '7')
4724 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004726 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 break;
4728
4729 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02004730 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 if (extra != 0)
4732 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004733 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 break;
4735 }
4736 /* FALLTHROUGH */
4737
Bram Moolenaar8c711452005-01-14 21:53:12 +00004738 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 break;
4740 }
4741 }
4742 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004743 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004746 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02004747 if (*p != NUL) /* just in case */
4748 ++p;
4749 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 return OK;
4752}
4753
4754/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004755 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 * Return OK or FAIL.
4757 */
4758 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004759get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760{
4761 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004762 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004763 int reduce = 0;
4764
4765 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004766 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004767 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004768 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004769 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004770 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004771 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004772 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004773 break;
4774 ++reduce;
4775 ++p;
4776 }
4777 }
4778
Bram Moolenaar8c711452005-01-14 21:53:12 +00004779 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004780 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004781 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004782 return FAIL;
4783 }
4784
Bram Moolenaar8c711452005-01-14 21:53:12 +00004785 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004786 if (!evaluate)
4787 {
4788 *arg = p + 1;
4789 return OK;
4790 }
4791
4792 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004793 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004794 */
4795 str = alloc((unsigned)((p - *arg) - reduce));
4796 if (str == NULL)
4797 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004798 rettv->v_type = VAR_STRING;
4799 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004800
Bram Moolenaar8c711452005-01-14 21:53:12 +00004801 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004802 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004803 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004804 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004805 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004806 break;
4807 ++p;
4808 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004809 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004810 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004811 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004812 *arg = p + 1;
4813
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004814 return OK;
4815}
4816
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004817/*
4818 * Return the function name of the partial.
4819 */
4820 char_u *
4821partial_name(partial_T *pt)
4822{
4823 if (pt->pt_name != NULL)
4824 return pt->pt_name;
4825 return pt->pt_func->uf_name;
4826}
4827
Bram Moolenaarddecc252016-04-06 22:59:37 +02004828 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004829partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004830{
4831 int i;
4832
4833 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004834 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004835 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004836 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004837 if (pt->pt_name != NULL)
4838 {
4839 func_unref(pt->pt_name);
4840 vim_free(pt->pt_name);
4841 }
4842 else
4843 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004844 vim_free(pt);
4845}
4846
4847/*
4848 * Unreference a closure: decrement the reference count and free it when it
4849 * becomes zero.
4850 */
4851 void
4852partial_unref(partial_T *pt)
4853{
4854 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004855 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004856}
4857
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004858static int tv_equal_recurse_limit;
4859
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004860 static int
4861func_equal(
4862 typval_T *tv1,
4863 typval_T *tv2,
4864 int ic) /* ignore case */
4865{
4866 char_u *s1, *s2;
4867 dict_T *d1, *d2;
4868 int a1, a2;
4869 int i;
4870
4871 /* empty and NULL function name considered the same */
4872 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004873 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004874 if (s1 != NULL && *s1 == NUL)
4875 s1 = NULL;
4876 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004877 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004878 if (s2 != NULL && *s2 == NUL)
4879 s2 = NULL;
4880 if (s1 == NULL || s2 == NULL)
4881 {
4882 if (s1 != s2)
4883 return FALSE;
4884 }
4885 else if (STRCMP(s1, s2) != 0)
4886 return FALSE;
4887
4888 /* empty dict and NULL dict is different */
4889 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
4890 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
4891 if (d1 == NULL || d2 == NULL)
4892 {
4893 if (d1 != d2)
4894 return FALSE;
4895 }
4896 else if (!dict_equal(d1, d2, ic, TRUE))
4897 return FALSE;
4898
4899 /* empty list and no list considered the same */
4900 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
4901 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
4902 if (a1 != a2)
4903 return FALSE;
4904 for (i = 0; i < a1; ++i)
4905 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
4906 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
4907 return FALSE;
4908
4909 return TRUE;
4910}
4911
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004912/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004913 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004914 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004915 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004916 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004917 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004918tv_equal(
4919 typval_T *tv1,
4920 typval_T *tv2,
4921 int ic, /* ignore case */
4922 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004923{
4924 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004925 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004926 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00004927 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004928
Bram Moolenaar8b402a02006-10-17 13:16:39 +00004929 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004930 * recursiveness to a limit. We guess they are equal then.
4931 * A fixed limit has the problem of still taking an awful long time.
4932 * Reduce the limit every time running into it. That should work fine for
4933 * deeply linked structures that are not recursively linked and catch
4934 * recursiveness quickly. */
4935 if (!recursive)
4936 tv_equal_recurse_limit = 1000;
4937 if (recursive_cnt >= tv_equal_recurse_limit)
4938 {
4939 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00004940 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004941 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004942
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02004943 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
4944 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004945 if ((tv1->v_type == VAR_FUNC
4946 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
4947 && (tv2->v_type == VAR_FUNC
4948 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
4949 {
4950 ++recursive_cnt;
4951 r = func_equal(tv1, tv2, ic);
4952 --recursive_cnt;
4953 return r;
4954 }
4955
4956 if (tv1->v_type != tv2->v_type)
4957 return FALSE;
4958
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004959 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004960 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004961 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004962 ++recursive_cnt;
4963 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
4964 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00004965 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004966
4967 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004968 ++recursive_cnt;
4969 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
4970 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00004971 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004972
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004973 case VAR_NUMBER:
4974 return tv1->vval.v_number == tv2->vval.v_number;
4975
4976 case VAR_STRING:
4977 s1 = get_tv_string_buf(tv1, buf1);
4978 s2 = get_tv_string_buf(tv2, buf2);
4979 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004980
4981 case VAR_SPECIAL:
4982 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01004983
4984 case VAR_FLOAT:
4985#ifdef FEAT_FLOAT
4986 return tv1->vval.v_float == tv2->vval.v_float;
4987#endif
4988 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004989#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01004990 return tv1->vval.v_job == tv2->vval.v_job;
4991#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01004992 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004993#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01004994 return tv1->vval.v_channel == tv2->vval.v_channel;
4995#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004996 case VAR_FUNC:
4997 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004998 case VAR_UNKNOWN:
4999 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005000 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005001
Bram Moolenaara03f2332016-02-06 18:09:59 +01005002 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5003 * does not equal anything, not even itself. */
5004 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005005}
5006
5007/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005008 * Return the next (unique) copy ID.
5009 * Used for serializing nested structures.
5010 */
5011 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005012get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005013{
5014 current_copyID += COPYID_INC;
5015 return current_copyID;
5016}
5017
5018/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005019 * Garbage collection for lists and dictionaries.
5020 *
5021 * We use reference counts to be able to free most items right away when they
5022 * are no longer used. But for composite items it's possible that it becomes
5023 * unused while the reference count is > 0: When there is a recursive
5024 * reference. Example:
5025 * :let l = [1, 2, 3]
5026 * :let d = {9: l}
5027 * :let l[1] = d
5028 *
5029 * Since this is quite unusual we handle this with garbage collection: every
5030 * once in a while find out which lists and dicts are not referenced from any
5031 * variable.
5032 *
5033 * Here is a good reference text about garbage collection (refers to Python
5034 * but it applies to all reference-counting mechanisms):
5035 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005036 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005037
5038/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005039 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005040 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005041 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005042 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005043 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005044garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005045{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005046 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005047 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005048 buf_T *buf;
5049 win_T *wp;
5050 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005051 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005052 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005053
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005054 if (!testing)
5055 {
5056 /* Only do this once. */
5057 want_garbage_collect = FALSE;
5058 may_garbage_collect = FALSE;
5059 garbage_collect_at_exit = FALSE;
5060 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005061
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005062 /* We advance by two because we add one for items referenced through
5063 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005064 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005065
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005066 /*
5067 * 1. Go through all accessible variables and mark all lists and dicts
5068 * with copyID.
5069 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005070
5071 /* Don't free variables in the previous_funccal list unless they are only
5072 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005073 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005074 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005075
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005076 /* script-local variables */
5077 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005078 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005079
5080 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005081 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005082 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5083 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005084
5085 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005086 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005087 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5088 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005089 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005090 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5091 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005092
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005093 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005094 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005095 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5096 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005097 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005098 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005099
5100 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005101 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005102
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005103 /* named functions (matters for closures) */
5104 abort = abort || set_ref_in_functions(copyID);
5105
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005106 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005107 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005108
Bram Moolenaard812df62008-11-09 12:46:09 +00005109 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005110 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005111
Bram Moolenaar1dced572012-04-05 16:54:08 +02005112#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005113 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005114#endif
5115
Bram Moolenaardb913952012-06-29 12:54:53 +02005116#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005117 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005118#endif
5119
5120#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005121 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005122#endif
5123
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005124#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005125 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005126 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005127#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005128#ifdef FEAT_NETBEANS_INTG
5129 abort = abort || set_ref_in_nb_channel(copyID);
5130#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005131
Bram Moolenaare3188e22016-05-31 21:13:04 +02005132#ifdef FEAT_TIMERS
5133 abort = abort || set_ref_in_timer(copyID);
5134#endif
5135
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005136#ifdef FEAT_QUICKFIX
5137 abort = abort || set_ref_in_quickfix(copyID);
5138#endif
5139
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005140#ifdef FEAT_TERMINAL
5141 abort = abort || set_ref_in_term(copyID);
5142#endif
5143
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005144 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005145 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005146 /*
5147 * 2. Free lists and dictionaries that are not referenced.
5148 */
5149 did_free = free_unref_items(copyID);
5150
5151 /*
5152 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005153 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005154 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005155 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005156 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005157 else if (p_verbose > 0)
5158 {
5159 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
5160 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005161
5162 return did_free;
5163}
5164
5165/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005166 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005167 */
5168 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005169free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005170{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005171 int did_free = FALSE;
5172
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005173 /* Let all "free" functions know that we are here. This means no
5174 * dictionaries, lists, channels or jobs are to be freed, because we will
5175 * do that here. */
5176 in_free_unref_items = TRUE;
5177
5178 /*
5179 * PASS 1: free the contents of the items. We don't free the items
5180 * themselves yet, so that it is possible to decrement refcount counters
5181 */
5182
Bram Moolenaarcd524592016-07-17 14:57:05 +02005183 /* Go through the list of dicts and free items without the copyID. */
5184 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005185
Bram Moolenaarda861d62016-07-17 15:46:27 +02005186 /* Go through the list of lists and free items without the copyID. */
5187 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005188
5189#ifdef FEAT_JOB_CHANNEL
5190 /* Go through the list of jobs and free items without the copyID. This
5191 * must happen before doing channels, because jobs refer to channels, but
5192 * the reference from the channel to the job isn't tracked. */
5193 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5194
5195 /* Go through the list of channels and free items without the copyID. */
5196 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5197#endif
5198
5199 /*
5200 * PASS 2: free the items themselves.
5201 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005202 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005203 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005204
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005205#ifdef FEAT_JOB_CHANNEL
5206 /* Go through the list of jobs and free items without the copyID. This
5207 * must happen before doing channels, because jobs refer to channels, but
5208 * the reference from the channel to the job isn't tracked. */
5209 free_unused_jobs(copyID, COPYID_MASK);
5210
5211 /* Go through the list of channels and free items without the copyID. */
5212 free_unused_channels(copyID, COPYID_MASK);
5213#endif
5214
5215 in_free_unref_items = FALSE;
5216
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005217 return did_free;
5218}
5219
5220/*
5221 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005222 * "list_stack" is used to add lists to be marked. Can be NULL.
5223 *
5224 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005225 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005226 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005227set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005228{
5229 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005230 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005231 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005232 hashtab_T *cur_ht;
5233 ht_stack_T *ht_stack = NULL;
5234 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005235
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005236 cur_ht = ht;
5237 for (;;)
5238 {
5239 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005240 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005241 /* Mark each item in the hashtab. If the item contains a hashtab
5242 * it is added to ht_stack, if it contains a list it is added to
5243 * list_stack. */
5244 todo = (int)cur_ht->ht_used;
5245 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5246 if (!HASHITEM_EMPTY(hi))
5247 {
5248 --todo;
5249 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5250 &ht_stack, list_stack);
5251 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005252 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005253
5254 if (ht_stack == NULL)
5255 break;
5256
5257 /* take an item from the stack */
5258 cur_ht = ht_stack->ht;
5259 tempitem = ht_stack;
5260 ht_stack = ht_stack->prev;
5261 free(tempitem);
5262 }
5263
5264 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005265}
5266
5267/*
5268 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005269 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5270 *
5271 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005272 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005273 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005274set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005275{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005276 listitem_T *li;
5277 int abort = FALSE;
5278 list_T *cur_l;
5279 list_stack_T *list_stack = NULL;
5280 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005281
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005282 cur_l = l;
5283 for (;;)
5284 {
5285 if (!abort)
5286 /* Mark each item in the list. If the item contains a hashtab
5287 * it is added to ht_stack, if it contains a list it is added to
5288 * list_stack. */
5289 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5290 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5291 ht_stack, &list_stack);
5292 if (list_stack == NULL)
5293 break;
5294
5295 /* take an item from the stack */
5296 cur_l = list_stack->list;
5297 tempitem = list_stack;
5298 list_stack = list_stack->prev;
5299 free(tempitem);
5300 }
5301
5302 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005303}
5304
5305/*
5306 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005307 * "list_stack" is used to add lists to be marked. Can be NULL.
5308 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5309 *
5310 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005311 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005312 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005313set_ref_in_item(
5314 typval_T *tv,
5315 int copyID,
5316 ht_stack_T **ht_stack,
5317 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005318{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005319 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005320
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005321 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005322 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005323 dict_T *dd = tv->vval.v_dict;
5324
Bram Moolenaara03f2332016-02-06 18:09:59 +01005325 if (dd != NULL && dd->dv_copyID != copyID)
5326 {
5327 /* Didn't see this dict yet. */
5328 dd->dv_copyID = copyID;
5329 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005330 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005331 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5332 }
5333 else
5334 {
5335 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5336 if (newitem == NULL)
5337 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005338 else
5339 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005340 newitem->ht = &dd->dv_hashtab;
5341 newitem->prev = *ht_stack;
5342 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005343 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005344 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005345 }
5346 }
5347 else if (tv->v_type == VAR_LIST)
5348 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005349 list_T *ll = tv->vval.v_list;
5350
Bram Moolenaara03f2332016-02-06 18:09:59 +01005351 if (ll != NULL && ll->lv_copyID != copyID)
5352 {
5353 /* Didn't see this list yet. */
5354 ll->lv_copyID = copyID;
5355 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005356 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005357 abort = set_ref_in_list(ll, copyID, ht_stack);
5358 }
5359 else
5360 {
5361 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005362 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005363 if (newitem == NULL)
5364 abort = TRUE;
5365 else
5366 {
5367 newitem->list = ll;
5368 newitem->prev = *list_stack;
5369 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005370 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005371 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005372 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005373 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005374 else if (tv->v_type == VAR_FUNC)
5375 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005376 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005377 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005378 else if (tv->v_type == VAR_PARTIAL)
5379 {
5380 partial_T *pt = tv->vval.v_partial;
5381 int i;
5382
5383 /* A partial does not have a copyID, because it cannot contain itself.
5384 */
5385 if (pt != NULL)
5386 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005387 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005388
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005389 if (pt->pt_dict != NULL)
5390 {
5391 typval_T dtv;
5392
5393 dtv.v_type = VAR_DICT;
5394 dtv.vval.v_dict = pt->pt_dict;
5395 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5396 }
5397
5398 for (i = 0; i < pt->pt_argc; ++i)
5399 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5400 ht_stack, list_stack);
5401 }
5402 }
5403#ifdef FEAT_JOB_CHANNEL
5404 else if (tv->v_type == VAR_JOB)
5405 {
5406 job_T *job = tv->vval.v_job;
5407 typval_T dtv;
5408
5409 if (job != NULL && job->jv_copyID != copyID)
5410 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005411 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005412 if (job->jv_channel != NULL)
5413 {
5414 dtv.v_type = VAR_CHANNEL;
5415 dtv.vval.v_channel = job->jv_channel;
5416 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5417 }
5418 if (job->jv_exit_partial != NULL)
5419 {
5420 dtv.v_type = VAR_PARTIAL;
5421 dtv.vval.v_partial = job->jv_exit_partial;
5422 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5423 }
5424 }
5425 }
5426 else if (tv->v_type == VAR_CHANNEL)
5427 {
5428 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005429 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005430 typval_T dtv;
5431 jsonq_T *jq;
5432 cbq_T *cq;
5433
5434 if (ch != NULL && ch->ch_copyID != copyID)
5435 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005436 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005437 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005438 {
5439 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5440 jq = jq->jq_next)
5441 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5442 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5443 cq = cq->cq_next)
5444 if (cq->cq_partial != NULL)
5445 {
5446 dtv.v_type = VAR_PARTIAL;
5447 dtv.vval.v_partial = cq->cq_partial;
5448 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5449 }
5450 if (ch->ch_part[part].ch_partial != NULL)
5451 {
5452 dtv.v_type = VAR_PARTIAL;
5453 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5454 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5455 }
5456 }
5457 if (ch->ch_partial != NULL)
5458 {
5459 dtv.v_type = VAR_PARTIAL;
5460 dtv.vval.v_partial = ch->ch_partial;
5461 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5462 }
5463 if (ch->ch_close_partial != NULL)
5464 {
5465 dtv.v_type = VAR_PARTIAL;
5466 dtv.vval.v_partial = ch->ch_close_partial;
5467 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5468 }
5469 }
5470 }
5471#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005472 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005473}
5474
Bram Moolenaar17a13432016-01-24 14:22:10 +01005475 static char *
5476get_var_special_name(int nr)
5477{
5478 switch (nr)
5479 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005480 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005481 case VVAL_TRUE: return "v:true";
5482 case VVAL_NONE: return "v:none";
5483 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005484 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01005485 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01005486 return "42";
5487}
5488
Bram Moolenaar8c711452005-01-14 21:53:12 +00005489/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005490 * Return a string with the string representation of a variable.
5491 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005492 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005493 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005494 * When both "echo_style" and "composite_val" are FALSE, put quotes around
5495 * stings as "string()", otherwise does not put quotes around strings, as
5496 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005497 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5498 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005499 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005500 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005501 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005502echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005503 typval_T *tv,
5504 char_u **tofree,
5505 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005506 int copyID,
5507 int echo_style,
5508 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005509 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005510{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005511 static int recurse = 0;
5512 char_u *r = NULL;
5513
Bram Moolenaar33570922005-01-25 22:26:29 +00005514 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005515 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005516 if (!did_echo_string_emsg)
5517 {
5518 /* Only give this message once for a recursive call to avoid
5519 * flooding the user with errors. And stop iterating over lists
5520 * and dicts. */
5521 did_echo_string_emsg = TRUE;
5522 EMSG(_("E724: variable nested too deep for displaying"));
5523 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005524 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005525 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005526 }
5527 ++recurse;
5528
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005529 switch (tv->v_type)
5530 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005531 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005532 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005533 {
5534 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005535 r = tv->vval.v_string;
5536 if (r == NULL)
5537 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005538 }
5539 else
5540 {
5541 *tofree = string_quote(tv->vval.v_string, FALSE);
5542 r = *tofree;
5543 }
5544 break;
5545
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005546 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005547 if (echo_style)
5548 {
5549 *tofree = NULL;
5550 r = tv->vval.v_string;
5551 }
5552 else
5553 {
5554 *tofree = string_quote(tv->vval.v_string, TRUE);
5555 r = *tofree;
5556 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005557 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005558
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005559 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005560 {
5561 partial_T *pt = tv->vval.v_partial;
5562 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005563 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005564 garray_T ga;
5565 int i;
5566 char_u *tf;
5567
5568 ga_init2(&ga, 1, 100);
5569 ga_concat(&ga, (char_u *)"function(");
5570 if (fname != NULL)
5571 {
5572 ga_concat(&ga, fname);
5573 vim_free(fname);
5574 }
5575 if (pt != NULL && pt->pt_argc > 0)
5576 {
5577 ga_concat(&ga, (char_u *)", [");
5578 for (i = 0; i < pt->pt_argc; ++i)
5579 {
5580 if (i > 0)
5581 ga_concat(&ga, (char_u *)", ");
5582 ga_concat(&ga,
5583 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5584 vim_free(tf);
5585 }
5586 ga_concat(&ga, (char_u *)"]");
5587 }
5588 if (pt != NULL && pt->pt_dict != NULL)
5589 {
5590 typval_T dtv;
5591
5592 ga_concat(&ga, (char_u *)", ");
5593 dtv.v_type = VAR_DICT;
5594 dtv.vval.v_dict = pt->pt_dict;
5595 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5596 vim_free(tf);
5597 }
5598 ga_concat(&ga, (char_u *)")");
5599
5600 *tofree = ga.ga_data;
5601 r = *tofree;
5602 break;
5603 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005604
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005605 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005606 if (tv->vval.v_list == NULL)
5607 {
5608 *tofree = NULL;
5609 r = NULL;
5610 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005611 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5612 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005613 {
5614 *tofree = NULL;
5615 r = (char_u *)"[...]";
5616 }
5617 else
5618 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005619 int old_copyID = tv->vval.v_list->lv_copyID;
5620
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005621 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005622 *tofree = list2string(tv, copyID, restore_copyID);
5623 if (restore_copyID)
5624 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005625 r = *tofree;
5626 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005627 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005628
Bram Moolenaar8c711452005-01-14 21:53:12 +00005629 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005630 if (tv->vval.v_dict == NULL)
5631 {
5632 *tofree = NULL;
5633 r = NULL;
5634 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005635 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5636 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005637 {
5638 *tofree = NULL;
5639 r = (char_u *)"{...}";
5640 }
5641 else
5642 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005643 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005644 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005645 *tofree = dict2string(tv, copyID, restore_copyID);
5646 if (restore_copyID)
5647 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005648 r = *tofree;
5649 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005650 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005651
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005652 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005653 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005654 *tofree = NULL;
5655 r = get_tv_string_buf(tv, numbuf);
5656 break;
5657
Bram Moolenaar835dc632016-02-07 14:27:38 +01005658 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005659 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005660 *tofree = NULL;
5661 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005662 if (composite_val)
5663 {
5664 *tofree = string_quote(r, FALSE);
5665 r = *tofree;
5666 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005667 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005668
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005669 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005670#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005671 *tofree = NULL;
5672 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5673 r = numbuf;
5674 break;
5675#endif
5676
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005677 case VAR_SPECIAL:
5678 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005679 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005680 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005681 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005682
Bram Moolenaar8502c702014-06-17 12:51:16 +02005683 if (--recurse == 0)
5684 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005685 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005686}
5687
5688/*
5689 * Return a string with the string representation of a variable.
5690 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5691 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005692 * Does not put quotes around strings, as ":echo" displays values.
5693 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5694 * May return NULL.
5695 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005696 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005697echo_string(
5698 typval_T *tv,
5699 char_u **tofree,
5700 char_u *numbuf,
5701 int copyID)
5702{
5703 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5704}
5705
5706/*
5707 * Return a string with the string representation of a variable.
5708 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5709 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005710 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005711 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005712 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02005713 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005714tv2string(
5715 typval_T *tv,
5716 char_u **tofree,
5717 char_u *numbuf,
5718 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005719{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005720 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005721}
5722
5723/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005724 * Return string "str" in ' quotes, doubling ' characters.
5725 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005726 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005727 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005728 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005729string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005730{
Bram Moolenaar33570922005-01-25 22:26:29 +00005731 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005732 char_u *p, *r, *s;
5733
Bram Moolenaar33570922005-01-25 22:26:29 +00005734 len = (function ? 13 : 3);
5735 if (str != NULL)
5736 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005737 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005738 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005739 if (*p == '\'')
5740 ++len;
5741 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005742 s = r = alloc(len);
5743 if (r != NULL)
5744 {
5745 if (function)
5746 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005747 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005748 r += 10;
5749 }
5750 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005751 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005752 if (str != NULL)
5753 for (p = str; *p != NUL; )
5754 {
5755 if (*p == '\'')
5756 *r++ = '\'';
5757 MB_COPY_CHAR(p, r);
5758 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005759 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005760 if (function)
5761 *r++ = ')';
5762 *r++ = NUL;
5763 }
5764 return s;
5765}
5766
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005767#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005768/*
5769 * Convert the string "text" to a floating point number.
5770 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5771 * this always uses a decimal point.
5772 * Returns the length of the text that was consumed.
5773 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005774 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005775string2float(
5776 char_u *text,
5777 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005778{
5779 char *s = (char *)text;
5780 float_T f;
5781
Bram Moolenaar62473612017-01-08 19:25:40 +01005782 /* MS-Windows does not deal with "inf" and "nan" properly. */
5783 if (STRNICMP(text, "inf", 3) == 0)
5784 {
5785 *value = INFINITY;
5786 return 3;
5787 }
5788 if (STRNICMP(text, "-inf", 3) == 0)
5789 {
5790 *value = -INFINITY;
5791 return 4;
5792 }
5793 if (STRNICMP(text, "nan", 3) == 0)
5794 {
5795 *value = NAN;
5796 return 3;
5797 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005798 f = strtod(s, &s);
5799 *value = f;
5800 return (int)((char_u *)s - text);
5801}
5802#endif
5803
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005804/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 * Get the value of an environment variable.
5806 * "arg" is pointing to the '$'. It is advanced to after the name.
5807 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005808 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 */
5810 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005811get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812{
5813 char_u *string = NULL;
5814 int len;
5815 int cc;
5816 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005817 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818
5819 ++*arg;
5820 name = *arg;
5821 len = get_env_len(arg);
5822 if (evaluate)
5823 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005824 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01005825 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005826
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005827 cc = name[len];
5828 name[len] = NUL;
5829 /* first try vim_getenv(), fast for normal environment vars */
5830 string = vim_getenv(name, &mustfree);
5831 if (string != NULL && *string != NUL)
5832 {
5833 if (!mustfree)
5834 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005836 else
5837 {
5838 if (mustfree)
5839 vim_free(string);
5840
5841 /* next try expanding things like $VIM and ${HOME} */
5842 string = expand_env_save(name - 1);
5843 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01005844 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005845 }
5846 name[len] = cc;
5847
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005848 rettv->v_type = VAR_STRING;
5849 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 }
5851
5852 return OK;
5853}
5854
Bram Moolenaard6e256c2011-12-14 15:32:50 +01005855
5856
5857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005859 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005861 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005862var2fpos(
5863 typval_T *varp,
5864 int dollar_lnum, /* TRUE when $ is last line */
5865 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005867 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005869 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870
Bram Moolenaara5525202006-03-02 22:52:09 +00005871 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005872 if (varp->v_type == VAR_LIST)
5873 {
5874 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005875 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005876 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005877 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005878
5879 l = varp->vval.v_list;
5880 if (l == NULL)
5881 return NULL;
5882
5883 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00005884 pos.lnum = list_find_nr(l, 0L, &error);
5885 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005886 return NULL; /* invalid line number */
5887
5888 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00005889 pos.col = list_find_nr(l, 1L, &error);
5890 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005891 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005892 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005893
5894 /* We accept "$" for the column number: last column. */
5895 li = list_find(l, 1L);
5896 if (li != NULL && li->li_tv.v_type == VAR_STRING
5897 && li->li_tv.vval.v_string != NULL
5898 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
5899 pos.col = len + 1;
5900
Bram Moolenaara5525202006-03-02 22:52:09 +00005901 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005902 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005903 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00005904 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005905
Bram Moolenaara5525202006-03-02 22:52:09 +00005906#ifdef FEAT_VIRTUALEDIT
5907 /* Get the virtual offset. Defaults to zero. */
5908 pos.coladd = list_find_nr(l, 2L, &error);
5909 if (error)
5910 pos.coladd = 0;
5911#endif
5912
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005913 return &pos;
5914 }
5915
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005916 name = get_tv_string_chk(varp);
5917 if (name == NULL)
5918 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005919 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005921 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
5922 {
5923 if (VIsual_active)
5924 return &VIsual;
5925 return &curwin->w_cursor;
5926 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005927 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005929 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5931 return NULL;
5932 return pp;
5933 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005934
5935#ifdef FEAT_VIRTUALEDIT
5936 pos.coladd = 0;
5937#endif
5938
Bram Moolenaar477933c2007-07-17 14:32:23 +00005939 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005940 {
5941 pos.col = 0;
5942 if (name[1] == '0') /* "w0": first visible line */
5943 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005944 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005945 /* In silent Ex mode topline is zero, but that's not a valid line
5946 * number; use one instead. */
5947 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005948 return &pos;
5949 }
5950 else if (name[1] == '$') /* "w$": last visible line */
5951 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005952 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005953 /* In silent Ex mode botline is zero, return zero then. */
5954 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005955 return &pos;
5956 }
5957 }
5958 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005960 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 {
5962 pos.lnum = curbuf->b_ml.ml_line_count;
5963 pos.col = 0;
5964 }
5965 else
5966 {
5967 pos.lnum = curwin->w_cursor.lnum;
5968 pos.col = (colnr_T)STRLEN(ml_get_curline());
5969 }
5970 return &pos;
5971 }
5972 return NULL;
5973}
5974
5975/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005976 * Convert list in "arg" into a position and optional file number.
5977 * When "fnump" is NULL there is no file number, only 3 items.
5978 * Note that the column is passed on as-is, the caller may want to decrement
5979 * it to use 1 for the first column.
5980 * Return FAIL when conversion is not possible, doesn't check the position for
5981 * validity.
5982 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005983 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005984list2fpos(
5985 typval_T *arg,
5986 pos_T *posp,
5987 int *fnump,
5988 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005989{
5990 list_T *l = arg->vval.v_list;
5991 long i = 0;
5992 long n;
5993
Bram Moolenaar493c1782014-05-28 14:34:46 +02005994 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5995 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00005996 if (arg->v_type != VAR_LIST
5997 || l == NULL
5998 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005999 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006000 return FAIL;
6001
6002 if (fnump != NULL)
6003 {
6004 n = list_find_nr(l, i++, NULL); /* fnum */
6005 if (n < 0)
6006 return FAIL;
6007 if (n == 0)
6008 n = curbuf->b_fnum; /* current buffer */
6009 *fnump = n;
6010 }
6011
6012 n = list_find_nr(l, i++, NULL); /* lnum */
6013 if (n < 0)
6014 return FAIL;
6015 posp->lnum = n;
6016
6017 n = list_find_nr(l, i++, NULL); /* col */
6018 if (n < 0)
6019 return FAIL;
6020 posp->col = n;
6021
6022#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +02006023 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006024 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006025 posp->coladd = 0;
6026 else
6027 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006028#endif
6029
Bram Moolenaar493c1782014-05-28 14:34:46 +02006030 if (curswantp != NULL)
6031 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6032
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006033 return OK;
6034}
6035
6036/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 * Get the length of an environment variable name.
6038 * Advance "arg" to the first character after the name.
6039 * Return 0 for error.
6040 */
6041 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006042get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043{
6044 char_u *p;
6045 int len;
6046
6047 for (p = *arg; vim_isIDc(*p); ++p)
6048 ;
6049 if (p == *arg) /* no name found */
6050 return 0;
6051
6052 len = (int)(p - *arg);
6053 *arg = p;
6054 return len;
6055}
6056
6057/*
6058 * Get the length of the name of a function or internal variable.
6059 * "arg" is advanced to the first non-white character after the name.
6060 * Return 0 if something is wrong.
6061 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006062 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006063get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064{
6065 char_u *p;
6066 int len;
6067
6068 /* Find the end of the name. */
6069 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006070 {
6071 if (*p == ':')
6072 {
6073 /* "s:" is start of "s:var", but "n:" is not and can be used in
6074 * slice "[n:]". Also "xx:" is not a namespace. */
6075 len = (int)(p - *arg);
6076 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6077 || len > 1)
6078 break;
6079 }
6080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 if (p == *arg) /* no name found */
6082 return 0;
6083
6084 len = (int)(p - *arg);
6085 *arg = skipwhite(p);
6086
6087 return len;
6088}
6089
6090/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006091 * Get the length of the name of a variable or function.
6092 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006094 * Return -1 if curly braces expansion failed.
6095 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 * If the name contains 'magic' {}'s, expand them and return the
6097 * expanded name in an allocated string via 'alias' - caller must free.
6098 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006099 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006100get_name_len(
6101 char_u **arg,
6102 char_u **alias,
6103 int evaluate,
6104 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105{
6106 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 char_u *p;
6108 char_u *expr_start;
6109 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110
6111 *alias = NULL; /* default to no alias */
6112
6113 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6114 && (*arg)[2] == (int)KE_SNR)
6115 {
6116 /* hard coded <SNR>, already translated */
6117 *arg += 3;
6118 return get_id_len(arg) + 3;
6119 }
6120 len = eval_fname_script(*arg);
6121 if (len > 0)
6122 {
6123 /* literal "<SID>", "s:" or "<SNR>" */
6124 *arg += len;
6125 }
6126
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006128 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006130 p = find_name_end(*arg, &expr_start, &expr_end,
6131 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 if (expr_start != NULL)
6133 {
6134 char_u *temp_string;
6135
6136 if (!evaluate)
6137 {
6138 len += (int)(p - *arg);
6139 *arg = skipwhite(p);
6140 return len;
6141 }
6142
6143 /*
6144 * Include any <SID> etc in the expanded string:
6145 * Thus the -len here.
6146 */
6147 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6148 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006149 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 *alias = temp_string;
6151 *arg = skipwhite(p);
6152 return (int)STRLEN(temp_string);
6153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154
6155 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006156 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 EMSG2(_(e_invexpr2), *arg);
6158
6159 return len;
6160}
6161
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006162/*
6163 * Find the end of a variable or function name, taking care of magic braces.
6164 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6165 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006166 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006167 * Return a pointer to just after the name. Equal to "arg" if there is no
6168 * valid name.
6169 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006170 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006171find_name_end(
6172 char_u *arg,
6173 char_u **expr_start,
6174 char_u **expr_end,
6175 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006177 int mb_nest = 0;
6178 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006180 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006182 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006184 *expr_start = NULL;
6185 *expr_end = NULL;
6186 }
6187
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006188 /* Quick check for valid starting character. */
6189 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6190 return arg;
6191
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006192 for (p = arg; *p != NUL
6193 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006194 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006195 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006196 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006197 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006198 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006199 if (*p == '\'')
6200 {
6201 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006202 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006203 ;
6204 if (*p == NUL)
6205 break;
6206 }
6207 else if (*p == '"')
6208 {
6209 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006210 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006211 if (*p == '\\' && p[1] != NUL)
6212 ++p;
6213 if (*p == NUL)
6214 break;
6215 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006216 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6217 {
6218 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006219 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006220 len = (int)(p - arg);
6221 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006222 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006223 break;
6224 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006225
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006226 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006228 if (*p == '[')
6229 ++br_nest;
6230 else if (*p == ']')
6231 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006233
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006234 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006236 if (*p == '{')
6237 {
6238 mb_nest++;
6239 if (expr_start != NULL && *expr_start == NULL)
6240 *expr_start = p;
6241 }
6242 else if (*p == '}')
6243 {
6244 mb_nest--;
6245 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6246 *expr_end = p;
6247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 }
6250
6251 return p;
6252}
6253
6254/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006255 * Expands out the 'magic' {}'s in a variable/function name.
6256 * Note that this can call itself recursively, to deal with
6257 * constructs like foo{bar}{baz}{bam}
6258 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6259 * "in_start" ^
6260 * "expr_start" ^
6261 * "expr_end" ^
6262 * "in_end" ^
6263 *
6264 * Returns a new allocated string, which the caller must free.
6265 * Returns NULL for failure.
6266 */
6267 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006268make_expanded_name(
6269 char_u *in_start,
6270 char_u *expr_start,
6271 char_u *expr_end,
6272 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006273{
6274 char_u c1;
6275 char_u *retval = NULL;
6276 char_u *temp_result;
6277 char_u *nextcmd = NULL;
6278
6279 if (expr_end == NULL || in_end == NULL)
6280 return NULL;
6281 *expr_start = NUL;
6282 *expr_end = NUL;
6283 c1 = *in_end;
6284 *in_end = NUL;
6285
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006286 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006287 if (temp_result != NULL && nextcmd == NULL)
6288 {
6289 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6290 + (in_end - expr_end) + 1));
6291 if (retval != NULL)
6292 {
6293 STRCPY(retval, in_start);
6294 STRCAT(retval, temp_result);
6295 STRCAT(retval, expr_end + 1);
6296 }
6297 }
6298 vim_free(temp_result);
6299
6300 *in_end = c1; /* put char back for error messages */
6301 *expr_start = '{';
6302 *expr_end = '}';
6303
6304 if (retval != NULL)
6305 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006306 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006307 if (expr_start != NULL)
6308 {
6309 /* Further expansion! */
6310 temp_result = make_expanded_name(retval, expr_start,
6311 expr_end, temp_result);
6312 vim_free(retval);
6313 retval = temp_result;
6314 }
6315 }
6316
6317 return retval;
6318}
6319
6320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006322 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006324 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006325eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006327 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6328}
6329
6330/*
6331 * Return TRUE if character "c" can be used as the first character in a
6332 * variable or function name (excluding '{' and '}').
6333 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006334 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006335eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006336{
6337 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338}
6339
6340/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 * Set number v: variable to "val".
6342 */
6343 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006344set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006346 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347}
6348
6349/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006350 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006352 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006353get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006355 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356}
6357
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006358/*
6359 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01006360 * If the String variable has never been set, return an empty string.
6361 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006362 */
6363 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006364get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006365{
6366 return get_tv_string(&vimvars[idx].vv_tv);
6367}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006368
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006370 * Get List v: variable value. Caller must take care of reference count when
6371 * needed.
6372 */
6373 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006374get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006375{
6376 return vimvars[idx].vv_list;
6377}
6378
6379/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006380 * Get Dict v: variable value. Caller must take care of reference count when
6381 * needed.
6382 */
6383 dict_T *
6384get_vim_var_dict(int idx)
6385{
6386 return vimvars[idx].vv_dict;
6387}
6388
6389/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006390 * Set v:char to character "c".
6391 */
6392 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006393set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006394{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006395 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006396
6397#ifdef FEAT_MBYTE
6398 if (has_mbyte)
6399 buf[(*mb_char2bytes)(c, buf)] = NUL;
6400 else
6401#endif
6402 {
6403 buf[0] = c;
6404 buf[1] = NUL;
6405 }
6406 set_vim_var_string(VV_CHAR, buf, -1);
6407}
6408
6409/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006410 * Set v:count to "count" and v:count1 to "count1".
6411 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 */
6413 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006414set_vcount(
6415 long count,
6416 long count1,
6417 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006419 if (set_prevcount)
6420 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006421 vimvars[VV_COUNT].vv_nr = count;
6422 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423}
6424
6425/*
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02006426 * Save variables that might be changed as a side effect. Used when executing
6427 * a timer callback.
6428 */
6429 void
6430save_vimvars(vimvars_save_T *vvsave)
6431{
6432 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
6433 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
6434 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
6435}
6436
6437/*
6438 * Restore variables saved by save_vimvars().
6439 */
6440 void
6441restore_vimvars(vimvars_save_T *vvsave)
6442{
6443 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
6444 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
6445 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
6446}
6447
6448/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 * Set string v: variable to a copy of "val".
6450 */
6451 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006452set_vim_var_string(
6453 int idx,
6454 char_u *val,
6455 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456{
Bram Moolenaara542c682016-01-31 16:28:04 +01006457 clear_tv(&vimvars[idx].vv_di.di_tv);
6458 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006460 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006462 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006464 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465}
6466
6467/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006468 * Set List v: variable to "val".
6469 */
6470 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006471set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006472{
Bram Moolenaara542c682016-01-31 16:28:04 +01006473 clear_tv(&vimvars[idx].vv_di.di_tv);
6474 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006475 vimvars[idx].vv_list = val;
6476 if (val != NULL)
6477 ++val->lv_refcount;
6478}
6479
6480/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006481 * Set Dictionary v: variable to "val".
6482 */
6483 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006484set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006485{
Bram Moolenaara542c682016-01-31 16:28:04 +01006486 clear_tv(&vimvars[idx].vv_di.di_tv);
6487 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006488 vimvars[idx].vv_dict = val;
6489 if (val != NULL)
6490 {
6491 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006492 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02006493 }
6494}
6495
6496/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 * Set v:register if needed.
6498 */
6499 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006500set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501{
6502 char_u regname;
6503
6504 if (c == 0 || c == ' ')
6505 regname = '"';
6506 else
6507 regname = c;
6508 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006509 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 set_vim_var_string(VV_REG, &regname, 1);
6511}
6512
6513/*
6514 * Get or set v:exception. If "oldval" == NULL, return the current value.
6515 * Otherwise, restore the value to "oldval" and return NULL.
6516 * Must always be called in pairs to save and restore v:exception! Does not
6517 * take care of memory allocations.
6518 */
6519 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006520v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521{
6522 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006523 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524
Bram Moolenaare9a41262005-01-15 22:18:47 +00006525 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 return NULL;
6527}
6528
6529/*
6530 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
6531 * Otherwise, restore the value to "oldval" and return NULL.
6532 * Must always be called in pairs to save and restore v:throwpoint! Does not
6533 * take care of memory allocations.
6534 */
6535 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006536v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537{
6538 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006539 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540
Bram Moolenaare9a41262005-01-15 22:18:47 +00006541 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 return NULL;
6543}
6544
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545/*
6546 * Set v:cmdarg.
6547 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6548 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6549 * Must always be called in pairs!
6550 */
6551 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006552set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553{
6554 char_u *oldval;
6555 char_u *newval;
6556 unsigned len;
6557
Bram Moolenaare9a41262005-01-15 22:18:47 +00006558 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006559 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006561 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006562 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006563 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 }
6565
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006566 if (eap->force_bin == FORCE_BIN)
6567 len = 6;
6568 else if (eap->force_bin == FORCE_NOBIN)
6569 len = 8;
6570 else
6571 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006572
6573 if (eap->read_edit)
6574 len += 7;
6575
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006576 if (eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006577 len += 10; /* " ++ff=unix" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006578# ifdef FEAT_MBYTE
6579 if (eap->force_enc != 0)
6580 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006581 if (eap->bad_char != 0)
6582 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006583# endif
6584
6585 newval = alloc(len + 1);
6586 if (newval == NULL)
6587 return NULL;
6588
6589 if (eap->force_bin == FORCE_BIN)
6590 sprintf((char *)newval, " ++bin");
6591 else if (eap->force_bin == FORCE_NOBIN)
6592 sprintf((char *)newval, " ++nobin");
6593 else
6594 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006595
6596 if (eap->read_edit)
6597 STRCAT(newval, " ++edit");
6598
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006599 if (eap->force_ff != 0)
6600 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006601 eap->force_ff == 'u' ? "unix"
6602 : eap->force_ff == 'd' ? "dos"
6603 : "mac");
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006604#ifdef FEAT_MBYTE
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006605 if (eap->force_enc != 0)
6606 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6607 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006608 if (eap->bad_char == BAD_KEEP)
6609 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6610 else if (eap->bad_char == BAD_DROP)
6611 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6612 else if (eap->bad_char != 0)
6613 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006614#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00006615 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006616 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618
6619/*
6620 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006621 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006623 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006624get_var_tv(
6625 char_u *name,
6626 int len, /* length of "name" */
6627 typval_T *rettv, /* NULL when only checking existence */
6628 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
6629 int verbose, /* may give error message */
6630 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631{
6632 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006633 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006634 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636
6637 /* truncate the name, so that we can use strcmp() */
6638 cc = name[len];
6639 name[len] = NUL;
6640
6641 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 * Check for user-defined variables.
6643 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01006644 v = find_var(name, NULL, no_autoload);
6645 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006647 tv = &v->di_tv;
6648 if (dip != NULL)
6649 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 }
6651
Bram Moolenaare9a41262005-01-15 22:18:47 +00006652 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006654 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006655 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 ret = FAIL;
6657 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006658 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006659 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660
6661 name[len] = cc;
6662
6663 return ret;
6664}
6665
6666/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006667 * Check if variable "name[len]" is a local variable or an argument.
6668 * If so, "*eval_lavars_used" is set to TRUE.
6669 */
6670 static void
6671check_vars(char_u *name, int len)
6672{
6673 int cc;
6674 char_u *varname;
6675 hashtab_T *ht;
6676
6677 if (eval_lavars_used == NULL)
6678 return;
6679
6680 /* truncate the name, so that we can use strcmp() */
6681 cc = name[len];
6682 name[len] = NUL;
6683
6684 ht = find_var_ht(name, &varname);
6685 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
6686 {
6687 if (find_var(name, NULL, TRUE) != NULL)
6688 *eval_lavars_used = TRUE;
6689 }
6690
6691 name[len] = cc;
6692}
6693
6694/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006695 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
6696 * Also handle function call with Funcref variable: func(expr)
6697 * Can all be combined: dict.func(expr)[idx]['func'](expr)
6698 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006699 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006700handle_subscript(
6701 char_u **arg,
6702 typval_T *rettv,
6703 int evaluate, /* do more than finding the end */
6704 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006705{
6706 int ret = OK;
6707 dict_T *selfdict = NULL;
6708 char_u *s;
6709 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006710 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006711
6712 while (ret == OK
6713 && (**arg == '['
6714 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006715 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6716 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01006717 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006718 {
6719 if (**arg == '(')
6720 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006721 partial_T *pt = NULL;
6722
Bram Moolenaard9fba312005-06-26 22:34:35 +00006723 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006724 if (evaluate)
6725 {
6726 functv = *rettv;
6727 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006728
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006729 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006730 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006731 {
6732 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006733 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006734 }
6735 else
6736 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006737 }
6738 else
6739 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006740 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00006741 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006742 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006743
6744 /* Clear the funcref afterwards, so that deleting it while
6745 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006746 if (evaluate)
6747 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006748
6749 /* Stop the expression evaluation when immediately aborting on
6750 * error, or when an interrupt occurred or an exception was thrown
6751 * but not caught. */
6752 if (aborting())
6753 {
6754 if (ret == OK)
6755 clear_tv(rettv);
6756 ret = FAIL;
6757 }
6758 dict_unref(selfdict);
6759 selfdict = NULL;
6760 }
6761 else /* **arg == '[' || **arg == '.' */
6762 {
6763 dict_unref(selfdict);
6764 if (rettv->v_type == VAR_DICT)
6765 {
6766 selfdict = rettv->vval.v_dict;
6767 if (selfdict != NULL)
6768 ++selfdict->dv_refcount;
6769 }
6770 else
6771 selfdict = NULL;
6772 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
6773 {
6774 clear_tv(rettv);
6775 ret = FAIL;
6776 }
6777 }
6778 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006779
Bram Moolenaar1d429612016-05-24 15:44:17 +02006780 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
6781 * Don't do this when "Func" is already a partial that was bound
6782 * explicitly (pt_auto is FALSE). */
6783 if (selfdict != NULL
6784 && (rettv->v_type == VAR_FUNC
6785 || (rettv->v_type == VAR_PARTIAL
6786 && (rettv->vval.v_partial->pt_auto
6787 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006788 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006789
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006790 dict_unref(selfdict);
6791 return ret;
6792}
6793
6794/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006795 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006796 * value).
6797 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01006798 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006799alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006800{
Bram Moolenaar33570922005-01-25 22:26:29 +00006801 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006802}
6803
6804/*
6805 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 * The string "s" must have been allocated, it is consumed.
6807 * Return NULL for out of memory, the variable otherwise.
6808 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006809 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006810alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811{
Bram Moolenaar33570922005-01-25 22:26:29 +00006812 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006814 rettv = alloc_tv();
6815 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006817 rettv->v_type = VAR_STRING;
6818 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 }
6820 else
6821 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006822 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823}
6824
6825/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006826 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006828 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006829free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830{
6831 if (varp != NULL)
6832 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006833 switch (varp->v_type)
6834 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006835 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006836 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02006837 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006838 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006839 vim_free(varp->vval.v_string);
6840 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006841 case VAR_PARTIAL:
6842 partial_unref(varp->vval.v_partial);
6843 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006844 case VAR_LIST:
6845 list_unref(varp->vval.v_list);
6846 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006847 case VAR_DICT:
6848 dict_unref(varp->vval.v_dict);
6849 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006850 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006851#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006852 job_unref(varp->vval.v_job);
6853 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006854#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006855 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006856#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006857 channel_unref(varp->vval.v_channel);
6858 break;
6859#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006860 case VAR_NUMBER:
6861 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00006862 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01006863 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00006864 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 vim_free(varp);
6867 }
6868}
6869
6870/*
6871 * Free the memory for a variable value and set the value to NULL or 0.
6872 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006873 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006874clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875{
6876 if (varp != NULL)
6877 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006878 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006880 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006881 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02006882 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006883 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01006884 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006885 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006886 case VAR_PARTIAL:
6887 partial_unref(varp->vval.v_partial);
6888 varp->vval.v_partial = NULL;
6889 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006890 case VAR_LIST:
6891 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006892 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006893 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006894 case VAR_DICT:
6895 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006896 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006897 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006898 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006899 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006900 varp->vval.v_number = 0;
6901 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006902 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006903#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006904 varp->vval.v_float = 0.0;
6905 break;
6906#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006907 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006908#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006909 job_unref(varp->vval.v_job);
6910 varp->vval.v_job = NULL;
6911#endif
6912 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01006913 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006914#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006915 channel_unref(varp->vval.v_channel);
6916 varp->vval.v_channel = NULL;
6917#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006918 case VAR_UNKNOWN:
6919 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006921 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 }
6923}
6924
6925/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006926 * Set the value of a variable to NULL without freeing items.
6927 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006928 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006929init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006930{
6931 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00006932 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006933}
6934
6935/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 * Get the number value of a variable.
6937 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006938 * For incompatible types, return 0.
6939 * get_tv_number_chk() is similar to get_tv_number(), but informs the
6940 * caller of incompatible types: it sets *denote to TRUE if "denote"
6941 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006943 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006944get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006946 int error = FALSE;
6947
6948 return get_tv_number_chk(varp, &error); /* return 0L on error */
6949}
6950
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006951 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006952get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006953{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006954 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006956 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006958 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006959 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006960 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006961#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +00006962 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006963 break;
6964#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006965 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006966 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +00006967 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006968 break;
6969 case VAR_STRING:
6970 if (varp->vval.v_string != NULL)
6971 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006972 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006973 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006974 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +00006975 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006976 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006977 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +00006978 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006979 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006980 case VAR_SPECIAL:
6981 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
6982 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006983 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006984#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006985 EMSG(_("E910: Using a Job as a Number"));
6986 break;
6987#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006988 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006989#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006990 EMSG(_("E913: Using a Channel as a Number"));
6991 break;
6992#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01006993 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01006994 internal_error("get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006995 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006997 if (denote == NULL) /* useful for values that must be unsigned */
6998 n = -1;
6999 else
7000 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007001 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002}
7003
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007004#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007005 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007006get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007007{
7008 switch (varp->v_type)
7009 {
7010 case VAR_NUMBER:
7011 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007012 case VAR_FLOAT:
7013 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007014 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007015 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007016 EMSG(_("E891: Using a Funcref as a Float"));
7017 break;
7018 case VAR_STRING:
7019 EMSG(_("E892: Using a String as a Float"));
7020 break;
7021 case VAR_LIST:
7022 EMSG(_("E893: Using a List as a Float"));
7023 break;
7024 case VAR_DICT:
7025 EMSG(_("E894: Using a Dictionary as a Float"));
7026 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007027 case VAR_SPECIAL:
7028 EMSG(_("E907: Using a special value as a Float"));
7029 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007030 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007031# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007032 EMSG(_("E911: Using a Job as a Float"));
7033 break;
7034# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007035 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007036# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007037 EMSG(_("E914: Using a Channel as a Float"));
7038 break;
7039# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007040 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007041 internal_error("get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007042 break;
7043 }
7044 return 0;
7045}
7046#endif
7047
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 * Get the string value of a variable.
7050 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +00007051 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7052 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 * If the String variable has never been set, return an empty string.
7054 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007055 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
7056 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007058 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007059get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060{
7061 static char_u mybuf[NUMBUFLEN];
7062
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007063 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064}
7065
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007066 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007067get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007069 char_u *res = get_tv_string_buf_chk(varp, buf);
7070
7071 return res != NULL ? res : (char_u *)"";
7072}
7073
Bram Moolenaar7d647822014-04-05 21:28:56 +02007074/*
7075 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7076 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007077 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007078get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007079{
7080 static char_u mybuf[NUMBUFLEN];
7081
7082 return get_tv_string_buf_chk(varp, mybuf);
7083}
7084
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007085 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007086get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007087{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007088 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007090 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007091 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaarea391762018-04-08 13:07:22 +02007092 (long long)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007093 return buf;
7094 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007095 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007096 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007097 break;
7098 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007099 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007100 break;
7101 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007102 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007103 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007104 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007105#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +02007106 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007107 break;
7108#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007109 case VAR_STRING:
7110 if (varp->vval.v_string != NULL)
7111 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007112 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007113 case VAR_SPECIAL:
7114 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7115 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007116 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007117#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007118 {
7119 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007120 char *status;
7121
7122 if (job == NULL)
7123 return (char_u *)"no process";
7124 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007125 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007126 : "run";
7127# ifdef UNIX
7128 vim_snprintf((char *)buf, NUMBUFLEN,
7129 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007130# elif defined(WIN32)
7131 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007132 "process %ld %s",
7133 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007134 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007135# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007136 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007137 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7138# endif
7139 return buf;
7140 }
7141#endif
7142 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007143 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007144#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007145 {
7146 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007147 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007148
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007149 if (channel == NULL)
7150 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7151 else
7152 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007153 "channel %d %s", channel->ch_id, status);
7154 return buf;
7155 }
7156#endif
7157 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007158 case VAR_UNKNOWN:
7159 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007160 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007162 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163}
7164
7165/*
7166 * Find variable "name" in the list of variables.
7167 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007168 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007169 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007170 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007172 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007173find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007176 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007177 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178
Bram Moolenaara7043832005-01-21 11:56:39 +00007179 ht = find_var_ht(name, &varname);
7180 if (htp != NULL)
7181 *htp = ht;
7182 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007184 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7185 if (ret != NULL)
7186 return ret;
7187
7188 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007189 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190}
7191
7192/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007193 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007194 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007196 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007197find_var_in_ht(
7198 hashtab_T *ht,
7199 int htname,
7200 char_u *varname,
7201 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007202{
Bram Moolenaar33570922005-01-25 22:26:29 +00007203 hashitem_T *hi;
7204
7205 if (*varname == NUL)
7206 {
7207 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007208 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007209 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007210 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007211 case 'g': return &globvars_var;
7212 case 'v': return &vimvars_var;
7213 case 'b': return &curbuf->b_bufvar;
7214 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007215 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007216 case 'l': return get_funccal_local_var();
7217 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007218 }
7219 return NULL;
7220 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007221
7222 hi = hash_find(ht, varname);
7223 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007224 {
7225 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007226 * worked find the variable again. Don't auto-load a script if it was
7227 * loaded already, otherwise it would be loaded every time when
7228 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007229 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007230 {
7231 /* Note: script_autoload() may make "hi" invalid. It must either
7232 * be obtained again or not used. */
7233 if (!script_autoload(varname, FALSE) || aborting())
7234 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007235 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007236 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007237 if (HASHITEM_EMPTY(hi))
7238 return NULL;
7239 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007240 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007241}
7242
7243/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007244 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007245 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007246 * Set "varname" to the start of name without ':'.
7247 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007248 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007249find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007251 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007252 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007253
Bram Moolenaar73627d02015-08-11 15:46:09 +02007254 if (name[0] == NUL)
7255 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 if (name[1] != ':')
7257 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007258 /* The name must not start with a colon or #. */
7259 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 return NULL;
7261 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007262
7263 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007264 hi = hash_find(&compat_hashtab, name);
7265 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +00007266 return &compat_hashtab;
7267
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007268 ht = get_funccal_local_ht();
7269 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007270 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007271 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 }
7273 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007274 if (*name == 'g') /* global variable */
7275 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007276 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7277 */
7278 if (vim_strchr(name + 2, ':') != NULL
7279 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007280 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007282 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007284 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007285 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007286 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007287 if (*name == 'v') /* v: variable */
7288 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007289 if (*name == 'a') /* a: function argument */
7290 return get_funccal_args_ht();
7291 if (*name == 'l') /* l: local function variable */
7292 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 if (*name == 's' /* script variable */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007294 && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
7295 return &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 return NULL;
7297}
7298
7299/*
7300 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +02007301 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 * Returns NULL when it doesn't exist.
7303 */
7304 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007305get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306{
Bram Moolenaar33570922005-01-25 22:26:29 +00007307 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007309 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 if (v == NULL)
7311 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007312 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313}
7314
7315/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007316 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 * sourcing this script and when executing functions defined in the script.
7318 */
7319 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007320new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321{
Bram Moolenaara7043832005-01-21 11:56:39 +00007322 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007323 hashtab_T *ht;
7324 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007325
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7327 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007328 /* Re-allocating ga_data means that an ht_array pointing to
7329 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007330 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007331 for (i = 1; i <= ga_scripts.ga_len; ++i)
7332 {
7333 ht = &SCRIPT_VARS(i);
7334 if (ht->ht_mask == HT_INIT_SIZE - 1)
7335 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007336 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007337 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007338 }
7339
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 while (ga_scripts.ga_len < id)
7341 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007342 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007343 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007344 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 }
7347 }
7348}
7349
7350/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007351 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7352 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 */
7354 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007355init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356{
Bram Moolenaar33570922005-01-25 22:26:29 +00007357 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007358 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007359 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007360 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007361 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007362 dict_var->di_tv.vval.v_dict = dict;
7363 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007364 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007365 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7366 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367}
7368
7369/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007370 * Unreference a dictionary initialized by init_var_dict().
7371 */
7372 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007373unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007374{
7375 /* Now the dict needs to be freed if no one else is using it, go back to
7376 * normal reference counting. */
7377 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7378 dict_unref(dict);
7379}
7380
7381/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007383 * Frees all allocated variables and the value they contain.
7384 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 */
7386 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007387vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007388{
7389 vars_clear_ext(ht, TRUE);
7390}
7391
7392/*
7393 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7394 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007395 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007396vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397{
Bram Moolenaara7043832005-01-21 11:56:39 +00007398 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007399 hashitem_T *hi;
7400 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401
Bram Moolenaar33570922005-01-25 22:26:29 +00007402 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007403 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007404 for (hi = ht->ht_array; todo > 0; ++hi)
7405 {
7406 if (!HASHITEM_EMPTY(hi))
7407 {
7408 --todo;
7409
Bram Moolenaar33570922005-01-25 22:26:29 +00007410 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007411 * ht_array might change then. hash_clear() takes care of it
7412 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007413 v = HI2DI(hi);
7414 if (free_val)
7415 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007416 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007417 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007418 }
7419 }
7420 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007421 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422}
7423
Bram Moolenaara7043832005-01-21 11:56:39 +00007424/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007425 * Delete a variable from hashtab "ht" at item "hi".
7426 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007427 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007429delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430{
Bram Moolenaar33570922005-01-25 22:26:29 +00007431 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007432
7433 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007434 clear_tv(&di->di_tv);
7435 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436}
7437
7438/*
7439 * List the value of one internal variable.
7440 */
7441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007442list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007444 char_u *tofree;
7445 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007446 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007447
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007448 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007449 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007450 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007451 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452}
7453
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007455list_one_var_a(
7456 char_u *prefix,
7457 char_u *name,
7458 int type,
7459 char_u *string,
7460 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461{
Bram Moolenaar31859182007-08-14 20:41:13 +00007462 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7463 msg_start();
7464 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 if (name != NULL) /* "a:" vars don't have a name stored */
7466 msg_puts(name);
7467 msg_putchar(' ');
7468 msg_advance(22);
7469 if (type == VAR_NUMBER)
7470 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007471 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007472 msg_putchar('*');
7473 else if (type == VAR_LIST)
7474 {
7475 msg_putchar('[');
7476 if (*string == '[')
7477 ++string;
7478 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007479 else if (type == VAR_DICT)
7480 {
7481 msg_putchar('{');
7482 if (*string == '{')
7483 ++string;
7484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 else
7486 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007487
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007489
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007490 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007491 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007492 if (*first)
7493 {
7494 msg_clr_eos();
7495 *first = FALSE;
7496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497}
7498
7499/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007500 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 * If the variable already exists, the value is updated.
7502 * Otherwise the variable is created.
7503 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007504 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007505set_var(
7506 char_u *name,
7507 typval_T *tv,
7508 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509{
Bram Moolenaar33570922005-01-25 22:26:29 +00007510 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007512 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007514 ht = find_var_ht(name, &varname);
7515 if (ht == NULL || *varname == NUL)
7516 {
7517 EMSG2(_(e_illvar), name);
7518 return;
7519 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007520 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007521
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007522 /* Search in parent scope which is possible to reference from lambda */
7523 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007524 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007525
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007526 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7527 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007528 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007529
Bram Moolenaar33570922005-01-25 22:26:29 +00007530 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007532 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007533 if (var_check_ro(v->di_flags, name, FALSE)
7534 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007535 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007536
7537 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007538 * Handle setting internal v: variables separately where needed to
7539 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007540 */
7541 if (ht == &vimvarht)
7542 {
7543 if (v->di_tv.v_type == VAR_STRING)
7544 {
7545 vim_free(v->di_tv.vval.v_string);
7546 if (copy || tv->v_type != VAR_STRING)
7547 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
7548 else
7549 {
7550 /* Take over the string to avoid an extra alloc/free. */
7551 v->di_tv.vval.v_string = tv->vval.v_string;
7552 tv->vval.v_string = NULL;
7553 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007554 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007555 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007556 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007557 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007558 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007559 if (STRCMP(varname, "searchforward") == 0)
7560 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007561#ifdef FEAT_SEARCH_EXTRA
7562 else if (STRCMP(varname, "hlsearch") == 0)
7563 {
7564 no_hlsearch = !v->di_tv.vval.v_number;
7565 redraw_all_later(SOME_VALID);
7566 }
7567#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007568 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007569 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007570 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar95f09602016-11-10 20:01:45 +01007571 internal_error("set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +00007572 }
7573
7574 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 }
7576 else /* add a new variable */
7577 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007578 /* Can't add "v:" variable. */
7579 if (ht == &vimvarht)
7580 {
7581 EMSG2(_(e_illvar), name);
7582 return;
7583 }
7584
Bram Moolenaar92124a32005-06-17 22:03:40 +00007585 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007586 if (!valid_varname(varname))
7587 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00007588
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007589 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7590 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00007591 if (v == NULL)
7592 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007593 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00007594 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007596 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 return;
7598 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007599 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007601
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007602 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00007603 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007604 else
7605 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007606 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007607 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007608 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610}
7611
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007612/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007613 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00007614 * Also give an error message.
7615 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007616 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007617var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00007618{
7619 if (flags & DI_FLAGS_RO)
7620 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007621 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007622 return TRUE;
7623 }
7624 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
7625 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007626 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007627 return TRUE;
7628 }
7629 return FALSE;
7630}
7631
7632/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007633 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
7634 * Also give an error message.
7635 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007636 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007637var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007638{
7639 if (flags & DI_FLAGS_FIX)
7640 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007641 EMSG2(_("E795: Cannot delete variable %s"),
7642 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007643 return TRUE;
7644 }
7645 return FALSE;
7646}
7647
7648/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007649 * Check if a funcref is assigned to a valid variable name.
7650 * Return TRUE and give an error if not.
7651 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007652 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007653var_check_func_name(
7654 char_u *name, /* points to start of variable name */
7655 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007656{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02007657 /* Allow for w: b: s: and t:. */
7658 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007659 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
7660 ? name[2] : name[0]))
7661 {
7662 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
7663 name);
7664 return TRUE;
7665 }
7666 /* Don't allow hiding a function. When "v" is not NULL we might be
7667 * assigning another function to the same var, the type is checked
7668 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02007669 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007670 {
7671 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
7672 name);
7673 return TRUE;
7674 }
7675 return FALSE;
7676}
7677
7678/*
7679 * Check if a variable name is valid.
7680 * Return FALSE and give an error if not.
7681 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007682 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007683valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007684{
7685 char_u *p;
7686
7687 for (p = varname; *p != NUL; ++p)
7688 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
7689 && *p != AUTOLOAD_CHAR)
7690 {
7691 EMSG2(_(e_illvar), varname);
7692 return FALSE;
7693 }
7694 return TRUE;
7695}
7696
7697/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007698 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02007699 * Also give an error message, using "name" or _("name") when use_gettext is
7700 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007701 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007702 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007703tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007704{
7705 if (lock & VAR_LOCKED)
7706 {
7707 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007708 name == NULL ? (char_u *)_("Unknown")
7709 : use_gettext ? (char_u *)_(name)
7710 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007711 return TRUE;
7712 }
7713 if (lock & VAR_FIXED)
7714 {
7715 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007716 name == NULL ? (char_u *)_("Unknown")
7717 : use_gettext ? (char_u *)_(name)
7718 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007719 return TRUE;
7720 }
7721 return FALSE;
7722}
7723
7724/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007725 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007726 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007727 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007728 * It is OK for "from" and "to" to point to the same item. This is used to
7729 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007730 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007731 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007732copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007734 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007735 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007736 switch (from->v_type)
7737 {
7738 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007739 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007740 to->vval.v_number = from->vval.v_number;
7741 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007742 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007743#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007744 to->vval.v_float = from->vval.v_float;
7745 break;
7746#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007747 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007748#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007749 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007750 if (to->vval.v_job != NULL)
7751 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007752 break;
7753#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007754 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007755#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007756 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007757 if (to->vval.v_channel != NULL)
7758 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01007759 break;
7760#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007761 case VAR_STRING:
7762 case VAR_FUNC:
7763 if (from->vval.v_string == NULL)
7764 to->vval.v_string = NULL;
7765 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007766 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007767 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007768 if (from->v_type == VAR_FUNC)
7769 func_ref(to->vval.v_string);
7770 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007771 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007772 case VAR_PARTIAL:
7773 if (from->vval.v_partial == NULL)
7774 to->vval.v_partial = NULL;
7775 else
7776 {
7777 to->vval.v_partial = from->vval.v_partial;
7778 ++to->vval.v_partial->pt_refcount;
7779 }
7780 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007781 case VAR_LIST:
7782 if (from->vval.v_list == NULL)
7783 to->vval.v_list = NULL;
7784 else
7785 {
7786 to->vval.v_list = from->vval.v_list;
7787 ++to->vval.v_list->lv_refcount;
7788 }
7789 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007790 case VAR_DICT:
7791 if (from->vval.v_dict == NULL)
7792 to->vval.v_dict = NULL;
7793 else
7794 {
7795 to->vval.v_dict = from->vval.v_dict;
7796 ++to->vval.v_dict->dv_refcount;
7797 }
7798 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007799 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007800 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007801 break;
7802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803}
7804
7805/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007806 * Make a copy of an item.
7807 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007808 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7809 * reference to an already copied list/dict can be used.
7810 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007811 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007812 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007813item_copy(
7814 typval_T *from,
7815 typval_T *to,
7816 int deep,
7817 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007818{
7819 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007820 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007821
Bram Moolenaar33570922005-01-25 22:26:29 +00007822 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007823 {
7824 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007825 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007826 }
7827 ++recurse;
7828
7829 switch (from->v_type)
7830 {
7831 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007832 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007833 case VAR_STRING:
7834 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007835 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007836 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007837 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007838 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007839 copy_tv(from, to);
7840 break;
7841 case VAR_LIST:
7842 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007843 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007844 if (from->vval.v_list == NULL)
7845 to->vval.v_list = NULL;
7846 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
7847 {
7848 /* use the copy made earlier */
7849 to->vval.v_list = from->vval.v_list->lv_copylist;
7850 ++to->vval.v_list->lv_refcount;
7851 }
7852 else
7853 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
7854 if (to->vval.v_list == NULL)
7855 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007856 break;
7857 case VAR_DICT:
7858 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007859 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007860 if (from->vval.v_dict == NULL)
7861 to->vval.v_dict = NULL;
7862 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7863 {
7864 /* use the copy made earlier */
7865 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7866 ++to->vval.v_dict->dv_refcount;
7867 }
7868 else
7869 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
7870 if (to->vval.v_dict == NULL)
7871 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007872 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007873 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007874 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007875 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007876 }
7877 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007878 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007879}
7880
7881/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007882 * This function is used by f_input() and f_inputdialog() functions. The third
7883 * argument to f_input() specifies the type of completion to use at the
7884 * prompt. The third argument to f_inputdialog() specifies the value to return
7885 * when the user cancels the prompt.
7886 */
7887 void
7888get_user_input(
7889 typval_T *argvars,
7890 typval_T *rettv,
7891 int inputdialog,
7892 int secret)
7893{
7894 char_u *prompt = get_tv_string_chk(&argvars[0]);
7895 char_u *p = NULL;
7896 int c;
7897 char_u buf[NUMBUFLEN];
7898 int cmd_silent_save = cmd_silent;
7899 char_u *defstr = (char_u *)"";
7900 int xp_type = EXPAND_NOTHING;
7901 char_u *xp_arg = NULL;
7902
7903 rettv->v_type = VAR_STRING;
7904 rettv->vval.v_string = NULL;
7905
7906#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007907 /* While starting up, there is no place to enter text. When running tests
7908 * with --not-a-term we assume feedkeys() will be used. */
7909 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007910 return;
7911#endif
7912
7913 cmd_silent = FALSE; /* Want to see the prompt. */
7914 if (prompt != NULL)
7915 {
7916 /* Only the part of the message after the last NL is considered as
7917 * prompt for the command line */
7918 p = vim_strrchr(prompt, '\n');
7919 if (p == NULL)
7920 p = prompt;
7921 else
7922 {
7923 ++p;
7924 c = *p;
7925 *p = NUL;
7926 msg_start();
7927 msg_clr_eos();
7928 msg_puts_attr(prompt, echo_attr);
7929 msg_didout = FALSE;
7930 msg_starthere();
7931 *p = c;
7932 }
7933 cmdline_row = msg_row;
7934
7935 if (argvars[1].v_type != VAR_UNKNOWN)
7936 {
7937 defstr = get_tv_string_buf_chk(&argvars[1], buf);
7938 if (defstr != NULL)
7939 stuffReadbuffSpec(defstr);
7940
7941 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
7942 {
7943 char_u *xp_name;
7944 int xp_namelen;
7945 long argt;
7946
7947 /* input() with a third argument: completion */
7948 rettv->vval.v_string = NULL;
7949
7950 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
7951 if (xp_name == NULL)
7952 return;
7953
7954 xp_namelen = (int)STRLEN(xp_name);
7955
7956 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
7957 &xp_arg) == FAIL)
7958 return;
7959 }
7960 }
7961
7962 if (defstr != NULL)
7963 {
7964 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02007965
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007966 ex_normal_busy = 0;
7967 rettv->vval.v_string =
7968 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
7969 xp_type, xp_arg);
7970 ex_normal_busy = save_ex_normal_busy;
7971 }
7972 if (inputdialog && rettv->vval.v_string == NULL
7973 && argvars[1].v_type != VAR_UNKNOWN
7974 && argvars[2].v_type != VAR_UNKNOWN)
7975 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
7976 &argvars[2], buf));
7977
7978 vim_free(xp_arg);
7979
7980 /* since the user typed this, no need to wait for return */
7981 need_wait_return = FALSE;
7982 msg_didout = FALSE;
7983 }
7984 cmd_silent = cmd_silent_save;
7985}
7986
7987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 * ":echo expr1 ..." print each argument separated with a space, add a
7989 * newline at the end.
7990 * ":echon expr1 ..." print each argument plain.
7991 */
7992 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007993ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994{
7995 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007996 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007997 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 char_u *p;
7999 int needclr = TRUE;
8000 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008001 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01008002 int did_emsg_before = did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003
8004 if (eap->skip)
8005 ++emsg_skip;
8006 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8007 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008008 /* If eval1() causes an error message the text from the command may
8009 * still need to be cleared. E.g., "echo 22,44". */
8010 need_clr_eos = needclr;
8011
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008013 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 {
8015 /*
8016 * Report the invalid expression unless the expression evaluation
8017 * has been cancelled due to an aborting error, an interrupt, or an
8018 * exception.
8019 */
Bram Moolenaar76a63452018-11-28 20:38:37 +01008020 if (!aborting() && did_emsg == did_emsg_before)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008022 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 break;
8024 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008025 need_clr_eos = FALSE;
8026
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 if (!eap->skip)
8028 {
8029 if (atstart)
8030 {
8031 atstart = FALSE;
8032 /* Call msg_start() after eval1(), evaluating the expression
8033 * may cause a message to appear. */
8034 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008035 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008036 /* Mark the saved text as finishing the line, so that what
8037 * follows is displayed on a new line when scrolling back
8038 * at the more prompt. */
8039 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 }
8043 else if (eap->cmdidx == CMD_echo)
8044 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008045 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008046 if (p != NULL)
8047 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008049 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008051 if (*p != TAB && needclr)
8052 {
8053 /* remove any text still there from the command */
8054 msg_clr_eos();
8055 needclr = FALSE;
8056 }
8057 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 }
8059 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008060 {
8061#ifdef FEAT_MBYTE
8062 if (has_mbyte)
8063 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008064 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008065
8066 (void)msg_outtrans_len_attr(p, i, echo_attr);
8067 p += i - 1;
8068 }
8069 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008071 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008074 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008076 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 arg = skipwhite(arg);
8078 }
8079 eap->nextcmd = check_nextcmd(arg);
8080
8081 if (eap->skip)
8082 --emsg_skip;
8083 else
8084 {
8085 /* remove text that may still be there from the command */
8086 if (needclr)
8087 msg_clr_eos();
8088 if (eap->cmdidx == CMD_echo)
8089 msg_end();
8090 }
8091}
8092
8093/*
8094 * ":echohl {name}".
8095 */
8096 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008097ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008099 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100}
8101
8102/*
8103 * ":execute expr1 ..." execute the result of an expression.
8104 * ":echomsg expr1 ..." Print a message
8105 * ":echoerr expr1 ..." Print an error
8106 * Each gets spaces around each argument and a newline at the end for
8107 * echo commands
8108 */
8109 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008110ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111{
8112 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008113 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 int ret = OK;
8115 char_u *p;
8116 garray_T ga;
8117 int len;
Bram Moolenaar8ff5af92018-11-28 21:20:38 +01008118 int save_did_emsg = did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119
8120 ga_init2(&ga, 1, 80);
8121
8122 if (eap->skip)
8123 ++emsg_skip;
8124 while (*arg != NUL && *arg != '|' && *arg != '\n')
8125 {
8126 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008127 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 {
8129 /*
8130 * Report the invalid expression unless the expression evaluation
8131 * has been cancelled due to an aborting error, an interrupt, or an
8132 * exception.
8133 */
Bram Moolenaar8ff5af92018-11-28 21:20:38 +01008134 if (!aborting() && did_emsg == save_did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 EMSG2(_(e_invexpr2), p);
8136 ret = FAIL;
8137 break;
8138 }
8139
8140 if (!eap->skip)
8141 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008142 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 len = (int)STRLEN(p);
8144 if (ga_grow(&ga, len + 2) == FAIL)
8145 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008146 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 ret = FAIL;
8148 break;
8149 }
8150 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 ga.ga_len += len;
8154 }
8155
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008156 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 arg = skipwhite(arg);
8158 }
8159
8160 if (ret != FAIL && ga.ga_data != NULL)
8161 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008162 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8163 {
8164 /* Mark the already saved text as finishing the line, so that what
8165 * follows is displayed on a new line when scrolling back at the
8166 * more prompt. */
8167 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008168 }
8169
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008171 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008173 out_flush();
8174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 else if (eap->cmdidx == CMD_echoerr)
8176 {
8177 /* We don't want to abort following commands, restore did_emsg. */
8178 save_did_emsg = did_emsg;
8179 EMSG((char_u *)ga.ga_data);
8180 if (!force_abort)
8181 did_emsg = save_did_emsg;
8182 }
8183 else if (eap->cmdidx == CMD_execute)
8184 do_cmdline((char_u *)ga.ga_data,
8185 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8186 }
8187
8188 ga_clear(&ga);
8189
8190 if (eap->skip)
8191 --emsg_skip;
8192
8193 eap->nextcmd = check_nextcmd(arg);
8194}
8195
8196/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008197 * Find window specified by "vp" in tabpage "tp".
8198 */
8199 win_T *
8200find_win_by_nr(
8201 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01008202 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008203{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008204 win_T *wp;
Bram Moolenaare6e39892018-10-25 12:32:11 +02008205 int nr = (int)get_tv_number_chk(vp, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008206
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008207 if (nr < 0)
8208 return NULL;
8209 if (nr == 0)
8210 return curwin;
8211
Bram Moolenaar29323592016-07-24 22:04:11 +02008212 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8213 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008214 if (nr >= LOWEST_WIN_ID)
8215 {
8216 if (wp->w_id == nr)
8217 return wp;
8218 }
8219 else if (--nr <= 0)
8220 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008221 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008222 if (nr >= LOWEST_WIN_ID)
8223 return NULL;
8224 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008225}
8226
8227/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02008228 * Find a window: When using a Window ID in any tab page, when using a number
8229 * in the current tab page.
8230 */
8231 win_T *
8232find_win_by_nr_or_id(typval_T *vp)
8233{
8234 int nr = (int)get_tv_number_chk(vp, NULL);
8235
8236 if (nr >= LOWEST_WIN_ID)
8237 return win_id2wp(vp);
8238 return find_win_by_nr(vp, NULL);
8239}
8240
8241/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008242 * Find window specified by "wvp" in tabpage "tvp".
8243 */
8244 win_T *
8245find_tabwin(
8246 typval_T *wvp, /* VAR_UNKNOWN for current window */
8247 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
8248{
8249 win_T *wp = NULL;
8250 tabpage_T *tp = NULL;
8251 long n;
8252
8253 if (wvp->v_type != VAR_UNKNOWN)
8254 {
8255 if (tvp->v_type != VAR_UNKNOWN)
8256 {
8257 n = (long)get_tv_number(tvp);
8258 if (n >= 0)
8259 tp = find_tabpage(n);
8260 }
8261 else
8262 tp = curtab;
8263
8264 if (tp != NULL)
8265 wp = find_win_by_nr(wvp, tp);
8266 }
8267 else
8268 wp = curwin;
8269
8270 return wp;
8271}
8272
8273/*
8274 * getwinvar() and gettabwinvar()
8275 */
8276 void
8277getwinvar(
8278 typval_T *argvars,
8279 typval_T *rettv,
8280 int off) /* 1 for gettabwinvar() */
8281{
8282 win_T *win;
8283 char_u *varname;
8284 dictitem_T *v;
8285 tabpage_T *tp = NULL;
8286 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008287 win_T *oldcurwin;
8288 tabpage_T *oldtabpage;
8289 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008290
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008291 if (off == 1)
8292 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8293 else
8294 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008295 win = find_win_by_nr(&argvars[off], tp);
8296 varname = get_tv_string_chk(&argvars[off + 1]);
8297 ++emsg_off;
8298
8299 rettv->v_type = VAR_STRING;
8300 rettv->vval.v_string = NULL;
8301
8302 if (win != NULL && varname != NULL)
8303 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008304 /* Set curwin to be our win, temporarily. Also set the tabpage,
8305 * otherwise the window is not valid. Only do this when needed,
8306 * autocommands get blocked. */
8307 need_switch_win = !(tp == curtab && win == curwin);
8308 if (!need_switch_win
8309 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008310 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008311 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008312 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008313 if (varname[1] == NUL)
8314 {
8315 /* get all window-local options in a dict */
8316 dict_T *opts = get_winbuf_options(FALSE);
8317
8318 if (opts != NULL)
8319 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008320 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02008321 done = TRUE;
8322 }
8323 }
8324 else if (get_option_tv(&varname, rettv, 1) == OK)
8325 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008326 done = TRUE;
8327 }
8328 else
8329 {
8330 /* Look up the variable. */
8331 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8332 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8333 varname, FALSE);
8334 if (v != NULL)
8335 {
8336 copy_tv(&v->di_tv, rettv);
8337 done = TRUE;
8338 }
8339 }
8340 }
8341
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008342 if (need_switch_win)
8343 /* restore previous notion of curwin */
8344 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008345 }
8346
8347 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8348 /* use the default return value */
8349 copy_tv(&argvars[off + 2], rettv);
8350
8351 --emsg_off;
8352}
8353
8354/*
8355 * "setwinvar()" and "settabwinvar()" functions
8356 */
8357 void
8358setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8359{
8360 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008361 win_T *save_curwin;
8362 tabpage_T *save_curtab;
8363 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008364 char_u *varname, *winvarname;
8365 typval_T *varp;
8366 char_u nbuf[NUMBUFLEN];
8367 tabpage_T *tp = NULL;
8368
8369 if (check_restricted() || check_secure())
8370 return;
8371
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008372 if (off == 1)
8373 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8374 else
8375 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008376 win = find_win_by_nr(&argvars[off], tp);
8377 varname = get_tv_string_chk(&argvars[off + 1]);
8378 varp = &argvars[off + 2];
8379
8380 if (win != NULL && varname != NULL && varp != NULL)
8381 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008382 need_switch_win = !(tp == curtab && win == curwin);
8383 if (!need_switch_win
8384 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008385 {
8386 if (*varname == '&')
8387 {
8388 long numval;
8389 char_u *strval;
8390 int error = FALSE;
8391
8392 ++varname;
8393 numval = (long)get_tv_number_chk(varp, &error);
8394 strval = get_tv_string_buf_chk(varp, nbuf);
8395 if (!error && strval != NULL)
8396 set_option_value(varname, numval, strval, OPT_LOCAL);
8397 }
8398 else
8399 {
8400 winvarname = alloc((unsigned)STRLEN(varname) + 3);
8401 if (winvarname != NULL)
8402 {
8403 STRCPY(winvarname, "w:");
8404 STRCPY(winvarname + 2, varname);
8405 set_var(winvarname, varp, TRUE);
8406 vim_free(winvarname);
8407 }
8408 }
8409 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008410 if (need_switch_win)
8411 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008412 }
8413}
8414
8415/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8417 * "arg" points to the "&" or '+' when called, to "option" when returning.
8418 * Returns NULL when no option name found. Otherwise pointer to the char
8419 * after the option name.
8420 */
8421 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008422find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423{
8424 char_u *p = *arg;
8425
8426 ++p;
8427 if (*p == 'g' && p[1] == ':')
8428 {
8429 *opt_flags = OPT_GLOBAL;
8430 p += 2;
8431 }
8432 else if (*p == 'l' && p[1] == ':')
8433 {
8434 *opt_flags = OPT_LOCAL;
8435 p += 2;
8436 }
8437 else
8438 *opt_flags = 0;
8439
8440 if (!ASCII_ISALPHA(*p))
8441 return NULL;
8442 *arg = p;
8443
8444 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
8445 p += 4; /* termcap option */
8446 else
8447 while (ASCII_ISALPHA(*p))
8448 ++p;
8449 return p;
8450}
8451
8452/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008453 * Return the autoload script name for a function or variable name.
8454 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02008456 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008457autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02008458{
Bram Moolenaara1544c02013-05-30 12:35:52 +02008459 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008460 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02008461
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008462 /* Get the script file name: replace '#' with '/', append ".vim". */
8463 scriptname = alloc((unsigned)(STRLEN(name) + 14));
8464 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008465 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008466 STRCPY(scriptname, "autoload/");
8467 STRCAT(scriptname, name);
8468 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
8469 STRCAT(scriptname, ".vim");
8470 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
8471 *p = '/';
8472 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008473}
8474
8475/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008476 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008477 * Return TRUE if a package was loaded.
8478 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008479 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008480script_autoload(
8481 char_u *name,
8482 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008483{
8484 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008485 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008486 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008487 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008488
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008489 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008490 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008491 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008492 return FALSE;
8493
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008494 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008495
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008496 /* Find the name in the list of previously loaded package names. Skip
8497 * "autoload/", it's always the same. */
8498 for (i = 0; i < ga_loaded.ga_len; ++i)
8499 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
8500 break;
8501 if (!reload && i < ga_loaded.ga_len)
8502 ret = FALSE; /* was loaded already */
8503 else
8504 {
8505 /* Remember the name if it wasn't loaded already. */
8506 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
8507 {
8508 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
8509 tofree = NULL;
8510 }
8511
8512 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008513 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008514 ret = TRUE;
8515 }
8516
8517 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008518 return ret;
8519}
8520
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
8522typedef enum
8523{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008524 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
8525 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
8526 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527} var_flavour_T;
8528
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 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 Moolenaar27e80c82018-10-14 21:41:01 +02008556 funccal_entry_T funccal_entry;
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 Moolenaar27e80c82018-10-14 21:41:01 +02008605 save_funccal(&funccal_entry);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008606 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02008607 restore_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 Moolenaarf29c1c62018-09-10 21:05:02 +02008749last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00008750{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008751 char_u *p;
8752
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008753 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00008754 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008755 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008756 if (p != NULL)
8757 {
8758 verbose_enter();
8759 MSG_PUTS(_("\n\tLast set from "));
8760 MSG_PUTS(p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008761 if (script_ctx.sc_lnum > 0)
8762 {
8763 MSG_PUTS(_(" line "));
8764 msg_outnum((long)script_ctx.sc_lnum);
8765 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008766 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008767 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008768 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00008769 }
8770}
8771
Bram Moolenaar53744302015-07-17 17:38:22 +02008772/* reset v:option_new, v:option_old and v:option_type */
8773 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008774reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02008775{
8776 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
8777 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
8778 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
8779}
8780
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008781/*
8782 * Prepare "gap" for an assert error and add the sourcing position.
8783 */
8784 void
8785prepare_assert_error(garray_T *gap)
8786{
8787 char buf[NUMBUFLEN];
8788
8789 ga_init2(gap, 1, 100);
8790 if (sourcing_name != NULL)
8791 {
8792 ga_concat(gap, sourcing_name);
8793 if (sourcing_lnum > 0)
8794 ga_concat(gap, (char_u *)" ");
8795 }
8796 if (sourcing_lnum > 0)
8797 {
8798 sprintf(buf, "line %ld", (long)sourcing_lnum);
8799 ga_concat(gap, (char_u *)buf);
8800 }
8801 if (sourcing_name != NULL || sourcing_lnum > 0)
8802 ga_concat(gap, (char_u *)": ");
8803}
8804
8805/*
8806 * Add an assert error to v:errors.
8807 */
8808 void
8809assert_error(garray_T *gap)
8810{
8811 struct vimvar *vp = &vimvars[VV_ERRORS];
8812
8813 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
8814 /* Make sure v:errors is a list. */
8815 set_vim_var_list(VV_ERRORS, list_alloc());
8816 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
8817}
8818
Bram Moolenaar65a54642018-04-28 16:56:53 +02008819 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008820assert_equal_common(typval_T *argvars, assert_type_T atype)
8821{
8822 garray_T ga;
8823
8824 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
8825 != (atype == ASSERT_EQUAL))
8826 {
8827 prepare_assert_error(&ga);
8828 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
8829 atype);
8830 assert_error(&ga);
8831 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02008832 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008833 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02008834 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008835}
8836
Bram Moolenaar65a54642018-04-28 16:56:53 +02008837 int
Bram Moolenaard96ff162018-02-18 22:13:29 +01008838assert_equalfile(typval_T *argvars)
8839{
8840 char_u buf1[NUMBUFLEN];
8841 char_u buf2[NUMBUFLEN];
8842 char_u *fname1 = get_tv_string_buf_chk(&argvars[0], buf1);
8843 char_u *fname2 = get_tv_string_buf_chk(&argvars[1], buf2);
8844 garray_T ga;
8845 FILE *fd1;
8846 FILE *fd2;
8847
8848 if (fname1 == NULL || fname2 == NULL)
Bram Moolenaar65a54642018-04-28 16:56:53 +02008849 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01008850
8851 IObuff[0] = NUL;
8852 fd1 = mch_fopen((char *)fname1, READBIN);
8853 if (fd1 == NULL)
8854 {
8855 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname1);
8856 }
8857 else
8858 {
8859 fd2 = mch_fopen((char *)fname2, READBIN);
8860 if (fd2 == NULL)
8861 {
8862 fclose(fd1);
8863 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname2);
8864 }
8865 else
8866 {
8867 int c1, c2;
8868 long count = 0;
8869
8870 for (;;)
8871 {
8872 c1 = fgetc(fd1);
8873 c2 = fgetc(fd2);
8874 if (c1 == EOF)
8875 {
8876 if (c2 != EOF)
8877 STRCPY(IObuff, "first file is shorter");
8878 break;
8879 }
8880 else if (c2 == EOF)
8881 {
8882 STRCPY(IObuff, "second file is shorter");
8883 break;
8884 }
8885 else if (c1 != c2)
8886 {
8887 vim_snprintf((char *)IObuff, IOSIZE,
8888 "difference at byte %ld", count);
8889 break;
8890 }
8891 ++count;
8892 }
Bram Moolenaar30494182018-02-20 21:46:05 +01008893 fclose(fd1);
8894 fclose(fd2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01008895 }
8896 }
8897 if (IObuff[0] != NUL)
8898 {
8899 prepare_assert_error(&ga);
8900 ga_concat(&ga, IObuff);
8901 assert_error(&ga);
8902 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02008903 return 1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01008904 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02008905 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01008906}
8907
Bram Moolenaar65a54642018-04-28 16:56:53 +02008908 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008909assert_match_common(typval_T *argvars, assert_type_T atype)
8910{
8911 garray_T ga;
8912 char_u buf1[NUMBUFLEN];
8913 char_u buf2[NUMBUFLEN];
8914 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
8915 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
8916
8917 if (pat == NULL || text == NULL)
8918 EMSG(_(e_invarg));
8919 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
8920 {
8921 prepare_assert_error(&ga);
8922 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
8923 atype);
8924 assert_error(&ga);
8925 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02008926 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008927 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02008928 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008929}
8930
Bram Moolenaar65a54642018-04-28 16:56:53 +02008931 int
Bram Moolenaar61c04492016-07-23 15:35:35 +02008932assert_inrange(typval_T *argvars)
8933{
8934 garray_T ga;
8935 int error = FALSE;
8936 varnumber_T lower = get_tv_number_chk(&argvars[0], &error);
8937 varnumber_T upper = get_tv_number_chk(&argvars[1], &error);
8938 varnumber_T actual = get_tv_number_chk(&argvars[2], &error);
8939 char_u *tofree;
8940 char msg[200];
8941 char_u numbuf[NUMBUFLEN];
8942
8943 if (error)
Bram Moolenaar65a54642018-04-28 16:56:53 +02008944 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02008945 if (actual < lower || actual > upper)
8946 {
8947 prepare_assert_error(&ga);
8948 if (argvars[3].v_type != VAR_UNKNOWN)
8949 {
8950 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
8951 vim_free(tofree);
8952 }
8953 else
8954 {
8955 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
8956 (long)lower, (long)upper, (long)actual);
8957 ga_concat(&ga, (char_u *)msg);
8958 }
8959 assert_error(&ga);
8960 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02008961 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02008962 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02008963 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02008964}
8965
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008966/*
8967 * Common for assert_true() and assert_false().
Bram Moolenaar65a54642018-04-28 16:56:53 +02008968 * Return non-zero for failure.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008969 */
Bram Moolenaar65a54642018-04-28 16:56:53 +02008970 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008971assert_bool(typval_T *argvars, int isTrue)
8972{
8973 int error = FALSE;
8974 garray_T ga;
8975
8976 if (argvars[0].v_type == VAR_SPECIAL
8977 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar65a54642018-04-28 16:56:53 +02008978 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008979 if (argvars[0].v_type != VAR_NUMBER
8980 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
8981 || error)
8982 {
8983 prepare_assert_error(&ga);
8984 fill_assert_error(&ga, &argvars[1],
8985 (char_u *)(isTrue ? "True" : "False"),
8986 NULL, &argvars[0], ASSERT_OTHER);
8987 assert_error(&ga);
8988 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02008989 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008990 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02008991 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008992}
8993
Bram Moolenaar65a54642018-04-28 16:56:53 +02008994 int
Bram Moolenaar42205552017-03-18 19:42:22 +01008995assert_report(typval_T *argvars)
8996{
8997 garray_T ga;
8998
8999 prepare_assert_error(&ga);
9000 ga_concat(&ga, get_tv_string(&argvars[0]));
9001 assert_error(&ga);
9002 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009003 return 1;
Bram Moolenaar42205552017-03-18 19:42:22 +01009004}
9005
Bram Moolenaar65a54642018-04-28 16:56:53 +02009006 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009007assert_exception(typval_T *argvars)
9008{
9009 garray_T ga;
9010 char_u *error = get_tv_string_chk(&argvars[0]);
9011
9012 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9013 {
9014 prepare_assert_error(&ga);
9015 ga_concat(&ga, (char_u *)"v:exception is not set");
9016 assert_error(&ga);
9017 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009018 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009019 }
9020 else if (error != NULL
9021 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9022 {
9023 prepare_assert_error(&ga);
9024 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9025 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9026 assert_error(&ga);
9027 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009028 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009029 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009030 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009031}
9032
Bram Moolenaar65a54642018-04-28 16:56:53 +02009033 int
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009034assert_beeps(typval_T *argvars)
9035{
9036 char_u *cmd = get_tv_string_chk(&argvars[0]);
9037 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009038 int ret = 0;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009039
9040 called_vim_beep = FALSE;
9041 suppress_errthrow = TRUE;
9042 emsg_silent = FALSE;
9043 do_cmdline_cmd(cmd);
9044 if (!called_vim_beep)
9045 {
9046 prepare_assert_error(&ga);
9047 ga_concat(&ga, (char_u *)"command did not beep: ");
9048 ga_concat(&ga, cmd);
9049 assert_error(&ga);
9050 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009051 ret = 1;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009052 }
9053
9054 suppress_errthrow = FALSE;
9055 emsg_on_display = FALSE;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009056 return ret;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009057}
9058
Bram Moolenaar65a54642018-04-28 16:56:53 +02009059 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009060assert_fails(typval_T *argvars)
9061{
9062 char_u *cmd = get_tv_string_chk(&argvars[0]);
9063 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009064 int ret = 0;
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02009065 char_u numbuf[NUMBUFLEN];
9066 char_u *tofree;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009067
9068 called_emsg = FALSE;
9069 suppress_errthrow = TRUE;
9070 emsg_silent = TRUE;
9071 do_cmdline_cmd(cmd);
9072 if (!called_emsg)
9073 {
9074 prepare_assert_error(&ga);
9075 ga_concat(&ga, (char_u *)"command did not fail: ");
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02009076 if (argvars[1].v_type != VAR_UNKNOWN
9077 && argvars[2].v_type != VAR_UNKNOWN)
9078 {
9079 ga_concat(&ga, echo_string(&argvars[2], &tofree, numbuf, 0));
9080 vim_free(tofree);
9081 }
9082 else
9083 ga_concat(&ga, cmd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009084 assert_error(&ga);
9085 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009086 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009087 }
9088 else if (argvars[1].v_type != VAR_UNKNOWN)
9089 {
9090 char_u buf[NUMBUFLEN];
9091 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9092
9093 if (error == NULL
9094 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9095 {
9096 prepare_assert_error(&ga);
9097 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9098 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
9099 assert_error(&ga);
9100 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009101 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009102 }
9103 }
9104
9105 called_emsg = FALSE;
9106 suppress_errthrow = FALSE;
9107 emsg_silent = FALSE;
9108 emsg_on_display = FALSE;
9109 set_vim_var_string(VV_ERRMSG, NULL, 0);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009110 return ret;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009111}
9112
9113/*
9114 * Append "str" to "gap", escaping unprintable characters.
9115 * Changes NL to \n, CR to \r, etc.
9116 */
9117 static void
9118ga_concat_esc(garray_T *gap, char_u *str)
9119{
9120 char_u *p;
9121 char_u buf[NUMBUFLEN];
9122
9123 if (str == NULL)
9124 {
9125 ga_concat(gap, (char_u *)"NULL");
9126 return;
9127 }
9128
9129 for (p = str; *p != NUL; ++p)
9130 switch (*p)
9131 {
9132 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9133 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9134 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9135 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9136 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9137 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9138 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9139 default:
9140 if (*p < ' ')
9141 {
9142 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9143 ga_concat(gap, buf);
9144 }
9145 else
9146 ga_append(gap, *p);
9147 break;
9148 }
9149}
9150
9151/*
9152 * Fill "gap" with information about an assert error.
9153 */
9154 void
9155fill_assert_error(
9156 garray_T *gap,
9157 typval_T *opt_msg_tv,
9158 char_u *exp_str,
9159 typval_T *exp_tv,
9160 typval_T *got_tv,
9161 assert_type_T atype)
9162{
9163 char_u numbuf[NUMBUFLEN];
9164 char_u *tofree;
9165
9166 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9167 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009168 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9169 vim_free(tofree);
9170 ga_concat(gap, (char_u *)": ");
9171 }
9172
9173 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9174 ga_concat(gap, (char_u *)"Pattern ");
9175 else if (atype == ASSERT_NOTEQUAL)
9176 ga_concat(gap, (char_u *)"Expected not equal to ");
9177 else
9178 ga_concat(gap, (char_u *)"Expected ");
9179 if (exp_str == NULL)
9180 {
9181 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009182 vim_free(tofree);
9183 }
9184 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009185 ga_concat_esc(gap, exp_str);
9186 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009187 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009188 if (atype == ASSERT_MATCH)
9189 ga_concat(gap, (char_u *)" does not match ");
9190 else if (atype == ASSERT_NOTMATCH)
9191 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009192 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009193 ga_concat(gap, (char_u *)" but got ");
9194 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
9195 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009196 }
9197}
9198
Bram Moolenaar31988702018-02-13 12:57:42 +01009199/*
9200 * Compare "typ1" and "typ2". Put the result in "typ1".
9201 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009202 int
9203typval_compare(
9204 typval_T *typ1, /* first operand */
9205 typval_T *typ2, /* second operand */
9206 exptype_T type, /* operator */
9207 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +01009208 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009209{
9210 int i;
9211 varnumber_T n1, n2;
9212 char_u *s1, *s2;
9213 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
9214
Bram Moolenaar31988702018-02-13 12:57:42 +01009215 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009216 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009217 /* For "is" a different type always means FALSE, for "notis"
9218 * it means TRUE. */
9219 n1 = (type == TYPE_NEQUAL);
9220 }
9221 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
9222 {
9223 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009224 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009225 n1 = (typ1->v_type == typ2->v_type
9226 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009227 if (type == TYPE_NEQUAL)
9228 n1 = !n1;
9229 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009230 else if (typ1->v_type != typ2->v_type
9231 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009232 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009233 if (typ1->v_type != typ2->v_type)
9234 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009235 else
Bram Moolenaar31988702018-02-13 12:57:42 +01009236 EMSG(_("E692: Invalid operation for List"));
9237 clear_tv(typ1);
9238 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009239 }
9240 else
9241 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009242 /* Compare two Lists for being equal or unequal. */
9243 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
9244 ic, FALSE);
9245 if (type == TYPE_NEQUAL)
9246 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009247 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009248 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009249
9250 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
9251 {
9252 if (type_is)
9253 {
9254 n1 = (typ1->v_type == typ2->v_type
9255 && typ1->vval.v_dict == typ2->vval.v_dict);
9256 if (type == TYPE_NEQUAL)
9257 n1 = !n1;
9258 }
9259 else if (typ1->v_type != typ2->v_type
9260 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9261 {
9262 if (typ1->v_type != typ2->v_type)
9263 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
9264 else
9265 EMSG(_("E736: Invalid operation for Dictionary"));
9266 clear_tv(typ1);
9267 return FAIL;
9268 }
9269 else
9270 {
9271 /* Compare two Dictionaries for being equal or unequal. */
9272 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
9273 ic, FALSE);
9274 if (type == TYPE_NEQUAL)
9275 n1 = !n1;
9276 }
9277 }
9278
9279 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
9280 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
9281 {
9282 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
9283 {
9284 EMSG(_("E694: Invalid operation for Funcrefs"));
9285 clear_tv(typ1);
9286 return FAIL;
9287 }
9288 if ((typ1->v_type == VAR_PARTIAL
9289 && typ1->vval.v_partial == NULL)
9290 || (typ2->v_type == VAR_PARTIAL
9291 && typ2->vval.v_partial == NULL))
9292 /* when a partial is NULL assume not equal */
9293 n1 = FALSE;
9294 else if (type_is)
9295 {
9296 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
9297 /* strings are considered the same if their value is
9298 * the same */
9299 n1 = tv_equal(typ1, typ2, ic, FALSE);
9300 else if (typ1->v_type == VAR_PARTIAL
9301 && typ2->v_type == VAR_PARTIAL)
9302 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
9303 else
9304 n1 = FALSE;
9305 }
9306 else
9307 n1 = tv_equal(typ1, typ2, ic, FALSE);
9308 if (type == TYPE_NEQUAL)
9309 n1 = !n1;
9310 }
9311
9312#ifdef FEAT_FLOAT
9313 /*
9314 * If one of the two variables is a float, compare as a float.
9315 * When using "=~" or "!~", always compare as string.
9316 */
9317 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
9318 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9319 {
9320 float_T f1, f2;
9321
9322 if (typ1->v_type == VAR_FLOAT)
9323 f1 = typ1->vval.v_float;
9324 else
9325 f1 = get_tv_number(typ1);
9326 if (typ2->v_type == VAR_FLOAT)
9327 f2 = typ2->vval.v_float;
9328 else
9329 f2 = get_tv_number(typ2);
9330 n1 = FALSE;
9331 switch (type)
9332 {
9333 case TYPE_EQUAL: n1 = (f1 == f2); break;
9334 case TYPE_NEQUAL: n1 = (f1 != f2); break;
9335 case TYPE_GREATER: n1 = (f1 > f2); break;
9336 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
9337 case TYPE_SMALLER: n1 = (f1 < f2); break;
9338 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
9339 case TYPE_UNKNOWN:
9340 case TYPE_MATCH:
9341 case TYPE_NOMATCH: break; /* avoid gcc warning */
9342 }
9343 }
9344#endif
9345
9346 /*
9347 * If one of the two variables is a number, compare as a number.
9348 * When using "=~" or "!~", always compare as string.
9349 */
9350 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
9351 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9352 {
9353 n1 = get_tv_number(typ1);
9354 n2 = get_tv_number(typ2);
9355 switch (type)
9356 {
9357 case TYPE_EQUAL: n1 = (n1 == n2); break;
9358 case TYPE_NEQUAL: n1 = (n1 != n2); break;
9359 case TYPE_GREATER: n1 = (n1 > n2); break;
9360 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
9361 case TYPE_SMALLER: n1 = (n1 < n2); break;
9362 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
9363 case TYPE_UNKNOWN:
9364 case TYPE_MATCH:
9365 case TYPE_NOMATCH: break; /* avoid gcc warning */
9366 }
9367 }
9368 else
9369 {
9370 s1 = get_tv_string_buf(typ1, buf1);
9371 s2 = get_tv_string_buf(typ2, buf2);
9372 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
9373 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
9374 else
9375 i = 0;
9376 n1 = FALSE;
9377 switch (type)
9378 {
9379 case TYPE_EQUAL: n1 = (i == 0); break;
9380 case TYPE_NEQUAL: n1 = (i != 0); break;
9381 case TYPE_GREATER: n1 = (i > 0); break;
9382 case TYPE_GEQUAL: n1 = (i >= 0); break;
9383 case TYPE_SMALLER: n1 = (i < 0); break;
9384 case TYPE_SEQUAL: n1 = (i <= 0); break;
9385
9386 case TYPE_MATCH:
9387 case TYPE_NOMATCH:
9388 n1 = pattern_match(s2, s1, ic);
9389 if (type == TYPE_NOMATCH)
9390 n1 = !n1;
9391 break;
9392
9393 case TYPE_UNKNOWN: break; /* avoid gcc warning */
9394 }
9395 }
9396 clear_tv(typ1);
9397 typ1->v_type = VAR_NUMBER;
9398 typ1->vval.v_number = n1;
9399
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009400 return OK;
9401}
9402
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009403 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02009404typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009405{
9406 char_u *tofree;
9407 char_u numbuf[NUMBUFLEN];
9408 char_u *ret = NULL;
9409
9410 if (arg == NULL)
9411 return vim_strsave((char_u *)"(does not exist)");
9412 ret = tv2string(arg, &tofree, numbuf, 0);
9413 /* Make a copy if we have a value but it's not in allocated memory. */
9414 if (ret != NULL && tofree == NULL)
9415 ret = vim_strsave(ret);
9416 return ret;
9417}
9418
9419 int
9420var_exists(char_u *var)
9421{
9422 char_u *name;
9423 char_u *tofree;
9424 typval_T tv;
9425 int len = 0;
9426 int n = FALSE;
9427
9428 /* get_name_len() takes care of expanding curly braces */
9429 name = var;
9430 len = get_name_len(&var, &tofree, TRUE, FALSE);
9431 if (len > 0)
9432 {
9433 if (tofree != NULL)
9434 name = tofree;
9435 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
9436 if (n)
9437 {
9438 /* handle d.key, l[idx], f(expr) */
9439 n = (handle_subscript(&var, &tv, TRUE, FALSE) == OK);
9440 if (n)
9441 clear_tv(&tv);
9442 }
9443 }
9444 if (*var != NUL)
9445 n = FALSE;
9446
9447 vim_free(tofree);
9448 return n;
9449}
9450
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451#endif /* FEAT_EVAL */
9452
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009454#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455
9456#ifdef WIN3264
9457/*
9458 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9459 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460
9461/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009462 * Get the short path (8.3) for the filename in "fnamep".
9463 * Only works for a valid file name.
9464 * When the path gets longer "fnamep" is changed and the allocated buffer
9465 * is put in "bufp".
9466 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9467 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468 */
9469 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009470get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009472 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473 char_u *newbuf;
9474
9475 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009476 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477 if (l > len - 1)
9478 {
9479 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009480 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 newbuf = vim_strnsave(*fnamep, l);
9482 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009483 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484
9485 vim_free(*bufp);
9486 *fnamep = *bufp = newbuf;
9487
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009488 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009489 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 }
9491
9492 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009493 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494}
9495
9496/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009497 * Get the short path (8.3) for the filename in "fname". The converted
9498 * path is returned in "bufp".
9499 *
9500 * Some of the directories specified in "fname" may not exist. This function
9501 * will shorten the existing directories at the beginning of the path and then
9502 * append the remaining non-existing path.
9503 *
9504 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009505 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009506 * bufp - Pointer to an allocated buffer for the filename.
9507 * fnamelen - Length of the filename pointed to by fname
9508 *
9509 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510 */
9511 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009512shortpath_for_invalid_fname(
9513 char_u **fname,
9514 char_u **bufp,
9515 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009517 char_u *short_fname, *save_fname, *pbuf_unused;
9518 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009520 int old_len, len;
9521 int new_len, sfx_len;
9522 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523
9524 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009525 old_len = *fnamelen;
9526 save_fname = vim_strnsave(*fname, old_len);
9527 pbuf_unused = NULL;
9528 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009530 endp = save_fname + old_len - 1; /* Find the end of the copy */
9531 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009533 /*
9534 * Try shortening the supplied path till it succeeds by removing one
9535 * directory at a time from the tail of the path.
9536 */
9537 len = 0;
9538 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009540 /* go back one path-separator */
9541 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9542 --endp;
9543 if (endp <= save_fname)
9544 break; /* processed the complete path */
9545
9546 /*
9547 * Replace the path separator with a NUL and try to shorten the
9548 * resulting path.
9549 */
9550 ch = *endp;
9551 *endp = 0;
9552 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009553 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009554 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9555 {
9556 retval = FAIL;
9557 goto theend;
9558 }
9559 *endp = ch; /* preserve the string */
9560
9561 if (len > 0)
9562 break; /* successfully shortened the path */
9563
9564 /* failed to shorten the path. Skip the path separator */
9565 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566 }
9567
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009568 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009570 /*
9571 * Succeeded in shortening the path. Now concatenate the shortened
9572 * path with the remaining path at the tail.
9573 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009575 /* Compute the length of the new path. */
9576 sfx_len = (int)(save_endp - endp) + 1;
9577 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009579 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009581 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009583 /* There is not enough space in the currently allocated string,
9584 * copy it to a buffer big enough. */
9585 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009587 {
9588 retval = FAIL;
9589 goto theend;
9590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 }
9592 else
9593 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009594 /* Transfer short_fname to the main buffer (it's big enough),
9595 * unless get_short_pathname() did its work in-place. */
9596 *fname = *bufp = save_fname;
9597 if (short_fname != save_fname)
9598 vim_strncpy(save_fname, short_fname, len);
9599 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009601
9602 /* concat the not-shortened part of the path */
9603 vim_strncpy(*fname + len, endp, sfx_len);
9604 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009606
9607theend:
9608 vim_free(pbuf_unused);
9609 vim_free(save_fname);
9610
9611 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612}
9613
9614/*
9615 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009616 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 */
9618 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009619shortpath_for_partial(
9620 char_u **fnamep,
9621 char_u **bufp,
9622 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623{
9624 int sepcount, len, tflen;
9625 char_u *p;
9626 char_u *pbuf, *tfname;
9627 int hasTilde;
9628
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009629 /* Count up the path separators from the RHS.. so we know which part
9630 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009632 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633 if (vim_ispathsep(*p))
9634 ++sepcount;
9635
9636 /* Need full path first (use expand_env() to remove a "~/") */
9637 hasTilde = (**fnamep == '~');
9638 if (hasTilde)
9639 pbuf = tfname = expand_env_save(*fnamep);
9640 else
9641 pbuf = tfname = FullName_save(*fnamep, FALSE);
9642
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009643 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009645 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
9646 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647
9648 if (len == 0)
9649 {
9650 /* Don't have a valid filename, so shorten the rest of the
9651 * path if we can. This CAN give us invalid 8.3 filenames, but
9652 * there's not a lot of point in guessing what it might be.
9653 */
9654 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009655 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
9656 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657 }
9658
9659 /* Count the paths backward to find the beginning of the desired string. */
9660 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009661 {
9662#ifdef FEAT_MBYTE
9663 if (has_mbyte)
9664 p -= mb_head_off(tfname, p);
9665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666 if (vim_ispathsep(*p))
9667 {
9668 if (sepcount == 0 || (hasTilde && sepcount == 1))
9669 break;
9670 else
9671 sepcount --;
9672 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674 if (hasTilde)
9675 {
9676 --p;
9677 if (p >= tfname)
9678 *p = '~';
9679 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009680 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681 }
9682 else
9683 ++p;
9684
9685 /* Copy in the string - p indexes into tfname - allocated at pbuf */
9686 vim_free(*bufp);
9687 *fnamelen = (int)STRLEN(p);
9688 *bufp = pbuf;
9689 *fnamep = p;
9690
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009691 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692}
9693#endif /* WIN3264 */
9694
9695/*
9696 * Adjust a filename, according to a string of modifiers.
9697 * *fnamep must be NUL terminated when called. When returning, the length is
9698 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009699 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 * When there is an error, *fnamep is set to NULL.
9701 */
9702 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009703modify_fname(
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009704 char_u *src, // string with modifiers
9705 int tilde_file, // "~" is a file name, not $HOME
9706 int *usedlen, // characters after src that are used
9707 char_u **fnamep, // file name so far
9708 char_u **bufp, // buffer for allocated file name or NULL
9709 int *fnamelen) // length of fnamep
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710{
9711 int valid = 0;
9712 char_u *tail;
9713 char_u *s, *p, *pbuf;
9714 char_u dirname[MAXPATHL];
9715 int c;
9716 int has_fullname = 0;
9717#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009718 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719 int has_shortname = 0;
9720#endif
9721
9722repeat:
9723 /* ":p" - full path/file_name */
9724 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
9725 {
9726 has_fullname = 1;
9727
9728 valid |= VALID_PATH;
9729 *usedlen += 2;
9730
9731 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
9732 if ((*fnamep)[0] == '~'
9733#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
9734 && ((*fnamep)[1] == '/'
9735# ifdef BACKSLASH_IN_FILENAME
9736 || (*fnamep)[1] == '\\'
9737# endif
9738 || (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009740 && !(tilde_file && (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 )
9742 {
9743 *fnamep = expand_env_save(*fnamep);
9744 vim_free(*bufp); /* free any allocated file name */
9745 *bufp = *fnamep;
9746 if (*fnamep == NULL)
9747 return -1;
9748 }
9749
9750 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009751 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 {
9753 if (vim_ispathsep(*p)
9754 && p[1] == '.'
9755 && (p[2] == NUL
9756 || vim_ispathsep(p[2])
9757 || (p[2] == '.'
9758 && (p[3] == NUL || vim_ispathsep(p[3])))))
9759 break;
9760 }
9761
9762 /* FullName_save() is slow, don't use it when not needed. */
9763 if (*p != NUL || !vim_isAbsName(*fnamep))
9764 {
9765 *fnamep = FullName_save(*fnamep, *p != NUL);
9766 vim_free(*bufp); /* free any allocated file name */
9767 *bufp = *fnamep;
9768 if (*fnamep == NULL)
9769 return -1;
9770 }
9771
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009772#ifdef WIN3264
9773# if _WIN32_WINNT >= 0x0500
9774 if (vim_strchr(*fnamep, '~') != NULL)
9775 {
9776 /* Expand 8.3 filename to full path. Needed to make sure the same
9777 * file does not have two different names.
9778 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
9779 p = alloc(_MAX_PATH + 1);
9780 if (p != NULL)
9781 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009782 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009783 {
9784 vim_free(*bufp);
9785 *bufp = *fnamep = p;
9786 }
9787 else
9788 vim_free(p);
9789 }
9790 }
9791# endif
9792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793 /* Append a path separator to a directory. */
9794 if (mch_isdir(*fnamep))
9795 {
9796 /* Make room for one or two extra characters. */
9797 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
9798 vim_free(*bufp); /* free any allocated file name */
9799 *bufp = *fnamep;
9800 if (*fnamep == NULL)
9801 return -1;
9802 add_pathsep(*fnamep);
9803 }
9804 }
9805
9806 /* ":." - path relative to the current directory */
9807 /* ":~" - path relative to the home directory */
9808 /* ":8" - shortname path - postponed till after */
9809 while (src[*usedlen] == ':'
9810 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
9811 {
9812 *usedlen += 2;
9813 if (c == '8')
9814 {
9815#ifdef WIN3264
9816 has_shortname = 1; /* Postpone this. */
9817#endif
9818 continue;
9819 }
9820 pbuf = NULL;
9821 /* Need full path first (use expand_env() to remove a "~/") */
9822 if (!has_fullname)
9823 {
9824 if (c == '.' && **fnamep == '~')
9825 p = pbuf = expand_env_save(*fnamep);
9826 else
9827 p = pbuf = FullName_save(*fnamep, FALSE);
9828 }
9829 else
9830 p = *fnamep;
9831
9832 has_fullname = 0;
9833
9834 if (p != NULL)
9835 {
9836 if (c == '.')
9837 {
9838 mch_dirname(dirname, MAXPATHL);
9839 s = shorten_fname(p, dirname);
9840 if (s != NULL)
9841 {
9842 *fnamep = s;
9843 if (pbuf != NULL)
9844 {
9845 vim_free(*bufp); /* free any allocated file name */
9846 *bufp = pbuf;
9847 pbuf = NULL;
9848 }
9849 }
9850 }
9851 else
9852 {
9853 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
9854 /* Only replace it when it starts with '~' */
9855 if (*dirname == '~')
9856 {
9857 s = vim_strsave(dirname);
9858 if (s != NULL)
9859 {
9860 *fnamep = s;
9861 vim_free(*bufp);
9862 *bufp = s;
9863 }
9864 }
9865 }
9866 vim_free(pbuf);
9867 }
9868 }
9869
9870 tail = gettail(*fnamep);
9871 *fnamelen = (int)STRLEN(*fnamep);
9872
9873 /* ":h" - head, remove "/file_name", can be repeated */
9874 /* Don't remove the first "/" or "c:\" */
9875 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
9876 {
9877 valid |= VALID_HEAD;
9878 *usedlen += 2;
9879 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009880 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009881 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882 *fnamelen = (int)(tail - *fnamep);
9883#ifdef VMS
9884 if (*fnamelen > 0)
9885 *fnamelen += 1; /* the path separator is part of the path */
9886#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009887 if (*fnamelen == 0)
9888 {
9889 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
9890 p = vim_strsave((char_u *)".");
9891 if (p == NULL)
9892 return -1;
9893 vim_free(*bufp);
9894 *bufp = *fnamep = tail = p;
9895 *fnamelen = 1;
9896 }
9897 else
9898 {
9899 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009900 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902 }
9903
9904 /* ":8" - shortname */
9905 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
9906 {
9907 *usedlen += 2;
9908#ifdef WIN3264
9909 has_shortname = 1;
9910#endif
9911 }
9912
9913#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009914 /*
9915 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916 */
9917 if (has_shortname)
9918 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009919 /* Copy the string if it is shortened by :h and when it wasn't copied
9920 * yet, because we are going to change it in place. Avoids changing
9921 * the buffer name for "%:8". */
9922 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923 {
9924 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +02009925 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926 return -1;
9927 vim_free(*bufp);
9928 *bufp = *fnamep = p;
9929 }
9930
9931 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +02009932 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933 if (!has_fullname && !vim_isAbsName(*fnamep))
9934 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009935 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 return -1;
9937 }
9938 else
9939 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009940 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941
Bram Moolenaardc935552011-08-17 15:23:23 +02009942 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009944 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945 return -1;
9946
9947 if (l == 0)
9948 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009949 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009951 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 return -1;
9953 }
9954 *fnamelen = l;
9955 }
9956 }
9957#endif /* WIN3264 */
9958
9959 /* ":t" - tail, just the basename */
9960 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
9961 {
9962 *usedlen += 2;
9963 *fnamelen -= (int)(tail - *fnamep);
9964 *fnamep = tail;
9965 }
9966
9967 /* ":e" - extension, can be repeated */
9968 /* ":r" - root, without extension, can be repeated */
9969 while (src[*usedlen] == ':'
9970 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
9971 {
9972 /* find a '.' in the tail:
9973 * - for second :e: before the current fname
9974 * - otherwise: The last '.'
9975 */
9976 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
9977 s = *fnamep - 2;
9978 else
9979 s = *fnamep + *fnamelen - 1;
9980 for ( ; s > tail; --s)
9981 if (s[0] == '.')
9982 break;
9983 if (src[*usedlen + 1] == 'e') /* :e */
9984 {
9985 if (s > tail)
9986 {
9987 *fnamelen += (int)(*fnamep - (s + 1));
9988 *fnamep = s + 1;
9989#ifdef VMS
9990 /* cut version from the extension */
9991 s = *fnamep + *fnamelen - 1;
9992 for ( ; s > *fnamep; --s)
9993 if (s[0] == ';')
9994 break;
9995 if (s > *fnamep)
9996 *fnamelen = s - *fnamep;
9997#endif
9998 }
9999 else if (*fnamep <= tail)
10000 *fnamelen = 0;
10001 }
10002 else /* :r */
10003 {
10004 if (s > tail) /* remove one extension */
10005 *fnamelen = (int)(s - *fnamep);
10006 }
10007 *usedlen += 2;
10008 }
10009
10010 /* ":s?pat?foo?" - substitute */
10011 /* ":gs?pat?foo?" - global substitute */
10012 if (src[*usedlen] == ':'
10013 && (src[*usedlen + 1] == 's'
10014 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
10015 {
10016 char_u *str;
10017 char_u *pat;
10018 char_u *sub;
10019 int sep;
10020 char_u *flags;
10021 int didit = FALSE;
10022
10023 flags = (char_u *)"";
10024 s = src + *usedlen + 2;
10025 if (src[*usedlen + 1] == 'g')
10026 {
10027 flags = (char_u *)"g";
10028 ++s;
10029 }
10030
10031 sep = *s++;
10032 if (sep)
10033 {
10034 /* find end of pattern */
10035 p = vim_strchr(s, sep);
10036 if (p != NULL)
10037 {
10038 pat = vim_strnsave(s, (int)(p - s));
10039 if (pat != NULL)
10040 {
10041 s = p + 1;
10042 /* find end of substitution */
10043 p = vim_strchr(s, sep);
10044 if (p != NULL)
10045 {
10046 sub = vim_strnsave(s, (int)(p - s));
10047 str = vim_strnsave(*fnamep, *fnamelen);
10048 if (sub != NULL && str != NULL)
10049 {
10050 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010051 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 if (s != NULL)
10053 {
10054 *fnamep = s;
10055 *fnamelen = (int)STRLEN(s);
10056 vim_free(*bufp);
10057 *bufp = s;
10058 didit = TRUE;
10059 }
10060 }
10061 vim_free(sub);
10062 vim_free(str);
10063 }
10064 vim_free(pat);
10065 }
10066 }
10067 /* after using ":s", repeat all the modifiers */
10068 if (didit)
10069 goto repeat;
10070 }
10071 }
10072
Bram Moolenaar26df0922014-02-23 23:39:13 +010010073 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10074 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010075 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010076 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010077 if (c != NUL)
10078 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010079 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010080 if (c != NUL)
10081 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010082 if (p == NULL)
10083 return -1;
10084 vim_free(*bufp);
10085 *bufp = *fnamep = p;
10086 *fnamelen = (int)STRLEN(p);
10087 *usedlen += 2;
10088 }
10089
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090 return valid;
10091}
10092
10093/*
10094 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010095 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096 * "flags" can be "g" to do a global substitute.
10097 * Returns an allocated string, NULL for error.
10098 */
10099 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010100do_string_sub(
10101 char_u *str,
10102 char_u *pat,
10103 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010104 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010105 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106{
10107 int sublen;
10108 regmatch_T regmatch;
10109 int i;
10110 int do_all;
10111 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010112 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113 garray_T ga;
10114 char_u *ret;
10115 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010116 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117
10118 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
10119 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010120 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121
10122 ga_init2(&ga, 1, 200);
10123
10124 do_all = (flags[0] == 'g');
10125
10126 regmatch.rm_ic = p_ic;
10127 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10128 if (regmatch.regprog != NULL)
10129 {
10130 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010131 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
10133 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010010134 /* Skip empty match except for first match. */
10135 if (regmatch.startp[0] == regmatch.endp[0])
10136 {
10137 if (zero_width == regmatch.startp[0])
10138 {
10139 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020010140 i = MB_PTR2LEN(tail);
10141 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
10142 (size_t)i);
10143 ga.ga_len += i;
10144 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010145 continue;
10146 }
10147 zero_width = regmatch.startp[0];
10148 }
10149
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150 /*
10151 * Get some space for a temporary buffer to do the substitution
10152 * into. It will contain:
10153 * - The text up to where the match is.
10154 * - The substituted text.
10155 * - The text after the match.
10156 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010157 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010010158 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
10160 {
10161 ga_clear(&ga);
10162 break;
10163 }
10164
10165 /* copy the text up to where the match is */
10166 i = (int)(regmatch.startp[0] - tail);
10167 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10168 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010169 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170 + ga.ga_len + i, TRUE, TRUE, FALSE);
10171 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010172 tail = regmatch.endp[0];
10173 if (*tail == NUL)
10174 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175 if (!do_all)
10176 break;
10177 }
10178
10179 if (ga.ga_data != NULL)
10180 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10181
Bram Moolenaar473de612013-06-08 18:19:48 +020010182 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183 }
10184
10185 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10186 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010187 if (p_cpo == empty_option)
10188 p_cpo = save_cpo;
10189 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010190 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010191 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192
10193 return ret;
10194}
10195
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010196 static int
10197filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10198{
10199 typval_T rettv;
10200 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010201 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010202
10203 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10204 argv[0] = vimvars[VV_KEY].vv_tv;
10205 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010206 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10207 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010208 if (map)
10209 {
10210 /* map(): replace the list item value */
10211 clear_tv(tv);
10212 rettv.v_lock = 0;
10213 *tv = rettv;
10214 }
10215 else
10216 {
10217 int error = FALSE;
10218
10219 /* filter(): when expr is zero remove the item */
10220 *remp = (get_tv_number_chk(&rettv, &error) == 0);
10221 clear_tv(&rettv);
10222 /* On type error, nothing has been removed; return FAIL to stop the
10223 * loop. The error message was given by get_tv_number_chk(). */
10224 if (error)
10225 goto theend;
10226 }
10227 retval = OK;
10228theend:
10229 clear_tv(&vimvars[VV_VAL].vv_tv);
10230 return retval;
10231}
10232
10233
10234/*
10235 * Implementation of map() and filter().
10236 */
10237 void
10238filter_map(typval_T *argvars, typval_T *rettv, int map)
10239{
10240 typval_T *expr;
10241 listitem_T *li, *nli;
10242 list_T *l = NULL;
10243 dictitem_T *di;
10244 hashtab_T *ht;
10245 hashitem_T *hi;
10246 dict_T *d = NULL;
10247 typval_T save_val;
10248 typval_T save_key;
10249 int rem;
10250 int todo;
10251 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10252 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10253 : N_("filter() argument"));
10254 int save_did_emsg;
10255 int idx = 0;
10256
10257 if (argvars[0].v_type == VAR_LIST)
10258 {
10259 if ((l = argvars[0].vval.v_list) == NULL
10260 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
10261 return;
10262 }
10263 else if (argvars[0].v_type == VAR_DICT)
10264 {
10265 if ((d = argvars[0].vval.v_dict) == NULL
10266 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
10267 return;
10268 }
10269 else
10270 {
10271 EMSG2(_(e_listdictarg), ermsg);
10272 return;
10273 }
10274
10275 expr = &argvars[1];
10276 /* On type errors, the preceding call has already displayed an error
10277 * message. Avoid a misleading error message for an empty string that
10278 * was not passed as argument. */
10279 if (expr->v_type != VAR_UNKNOWN)
10280 {
10281 prepare_vimvar(VV_VAL, &save_val);
10282
10283 /* We reset "did_emsg" to be able to detect whether an error
10284 * occurred during evaluation of the expression. */
10285 save_did_emsg = did_emsg;
10286 did_emsg = FALSE;
10287
10288 prepare_vimvar(VV_KEY, &save_key);
10289 if (argvars[0].v_type == VAR_DICT)
10290 {
10291 vimvars[VV_KEY].vv_type = VAR_STRING;
10292
10293 ht = &d->dv_hashtab;
10294 hash_lock(ht);
10295 todo = (int)ht->ht_used;
10296 for (hi = ht->ht_array; todo > 0; ++hi)
10297 {
10298 if (!HASHITEM_EMPTY(hi))
10299 {
10300 int r;
10301
10302 --todo;
10303 di = HI2DI(hi);
10304 if (map &&
10305 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10306 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
10307 break;
10308 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10309 r = filter_map_one(&di->di_tv, expr, map, &rem);
10310 clear_tv(&vimvars[VV_KEY].vv_tv);
10311 if (r == FAIL || did_emsg)
10312 break;
10313 if (!map && rem)
10314 {
10315 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10316 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10317 break;
10318 dictitem_remove(d, di);
10319 }
10320 }
10321 }
10322 hash_unlock(ht);
10323 }
10324 else
10325 {
10326 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10327
10328 for (li = l->lv_first; li != NULL; li = nli)
10329 {
10330 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
10331 break;
10332 nli = li->li_next;
10333 vimvars[VV_KEY].vv_nr = idx;
10334 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10335 || did_emsg)
10336 break;
10337 if (!map && rem)
10338 listitem_remove(l, li);
10339 ++idx;
10340 }
10341 }
10342
10343 restore_vimvar(VV_KEY, &save_key);
10344 restore_vimvar(VV_VAL, &save_val);
10345
10346 did_emsg |= save_did_emsg;
10347 }
10348
10349 copy_tv(&argvars[0], rettv);
10350}
10351
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */