blob: e70bfd8ab31511321cf34bbeafc8e70dccb63507 [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;
1428
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001429 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001430 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1431 {
1432 if (!HASHITEM_EMPTY(hi))
1433 {
1434 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001435 di = HI2DI(hi);
1436 if (empty || di->di_tv.v_type != VAR_STRING
1437 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001438 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001439 }
1440 }
1441}
1442
1443/*
1444 * List global variables.
1445 */
1446 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001447list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001448{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001449 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001450}
1451
1452/*
1453 * List buffer variables.
1454 */
1455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001456list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001457{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001458 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001459 TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001460}
1461
1462/*
1463 * List window variables.
1464 */
1465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001466list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001467{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001468 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001469 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001470}
1471
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001472/*
1473 * List tab page variables.
1474 */
1475 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001476list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001477{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001478 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001479 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001480}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001481
Bram Moolenaara7043832005-01-21 11:56:39 +00001482/*
1483 * List Vim variables.
1484 */
1485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001486list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001487{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001488 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001489}
1490
1491/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001492 * List script-local variables, if there is a script.
1493 */
1494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001495list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001496{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001497 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
1498 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001499 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001500}
1501
1502/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001503 * List variables in "arg".
1504 */
1505 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001506list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001507{
1508 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001509 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001510 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001511 char_u *name_start;
1512 char_u *arg_subsc;
1513 char_u *tofree;
1514 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001515
1516 while (!ends_excmd(*arg) && !got_int)
1517 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001518 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001519 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001520 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001521 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001522 {
1523 emsg_severe = TRUE;
1524 EMSG(_(e_trailing));
1525 break;
1526 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001527 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001528 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001529 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001530 /* get_name_len() takes care of expanding curly braces */
1531 name_start = name = arg;
1532 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1533 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001534 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001535 /* This is mainly to keep test 49 working: when expanding
1536 * curly braces fails overrule the exception error message. */
1537 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001538 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001539 emsg_severe = TRUE;
1540 EMSG2(_(e_invarg2), arg);
1541 break;
1542 }
1543 error = TRUE;
1544 }
1545 else
1546 {
1547 if (tofree != NULL)
1548 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001549 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001550 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001551 else
1552 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001553 /* handle d.key, l[idx], f(expr) */
1554 arg_subsc = arg;
1555 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001556 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001557 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001558 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001559 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001560 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001561 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001562 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001563 case 'g': list_glob_vars(first); break;
1564 case 'b': list_buf_vars(first); break;
1565 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001566 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001567 case 'v': list_vim_vars(first); break;
1568 case 's': list_script_vars(first); break;
1569 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001570 default:
1571 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001572 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001573 }
1574 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001575 {
1576 char_u numbuf[NUMBUFLEN];
1577 char_u *tf;
1578 int c;
1579 char_u *s;
1580
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001581 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001582 c = *arg;
1583 *arg = NUL;
1584 list_one_var_a((char_u *)"",
1585 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001586 tv.v_type,
1587 s == NULL ? (char_u *)"" : s,
1588 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001589 *arg = c;
1590 vim_free(tf);
1591 }
1592 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001593 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001594 }
1595 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001596
1597 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001598 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001599
1600 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001601 }
1602
1603 return arg;
1604}
1605
1606/*
1607 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1608 * Returns a pointer to the char just after the var name.
1609 * Returns NULL if there is an error.
1610 */
1611 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001612ex_let_one(
1613 char_u *arg, /* points to variable name */
1614 typval_T *tv, /* value to assign to variable */
1615 int copy, /* copy value from "tv" */
1616 char_u *endchars, /* valid chars after variable name or NULL */
1617 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001618{
1619 int c1;
1620 char_u *name;
1621 char_u *p;
1622 char_u *arg_end = NULL;
1623 int len;
1624 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001625 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001626
1627 /*
1628 * ":let $VAR = expr": Set environment variable.
1629 */
1630 if (*arg == '$')
1631 {
1632 /* Find the end of the name. */
1633 ++arg;
1634 name = arg;
1635 len = get_env_len(&arg);
1636 if (len == 0)
1637 EMSG2(_(e_invarg2), name - 1);
1638 else
1639 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001640 if (op != NULL && (*op == '+' || *op == '-'))
1641 EMSG2(_(e_letwrong), op);
1642 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001643 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001644 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001645 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001646 {
1647 c1 = name[len];
1648 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001649 p = get_tv_string_chk(tv);
1650 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001651 {
1652 int mustfree = FALSE;
1653 char_u *s = vim_getenv(name, &mustfree);
1654
1655 if (s != NULL)
1656 {
1657 p = tofree = concat_str(s, p);
1658 if (mustfree)
1659 vim_free(s);
1660 }
1661 }
1662 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001663 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001664 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001665 if (STRICMP(name, "HOME") == 0)
1666 init_homedir();
1667 else if (didset_vim && STRICMP(name, "VIM") == 0)
1668 didset_vim = FALSE;
1669 else if (didset_vimruntime
1670 && STRICMP(name, "VIMRUNTIME") == 0)
1671 didset_vimruntime = FALSE;
1672 arg_end = arg;
1673 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001674 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001675 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001676 }
1677 }
1678 }
1679
1680 /*
1681 * ":let &option = expr": Set option value.
1682 * ":let &l:option = expr": Set local option value.
1683 * ":let &g:option = expr": Set global option value.
1684 */
1685 else if (*arg == '&')
1686 {
1687 /* Find the end of the name. */
1688 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001689 if (p == NULL || (endchars != NULL
1690 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001691 EMSG(_(e_letunexp));
1692 else
1693 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001694 long n;
1695 int opt_type;
1696 long numval;
1697 char_u *stringval = NULL;
1698 char_u *s;
1699
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001700 c1 = *p;
1701 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001702
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001703 n = (long)get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001704 s = get_tv_string_chk(tv); /* != NULL if number or string */
1705 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001706 {
1707 opt_type = get_option_value(arg, &numval,
1708 &stringval, opt_flags);
1709 if ((opt_type == 1 && *op == '.')
1710 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001711 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001712 EMSG2(_(e_letwrong), op);
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001713 s = NULL; /* don't set the value */
1714 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001715 else
1716 {
1717 if (opt_type == 1) /* number */
1718 {
1719 if (*op == '+')
1720 n = numval + n;
1721 else
1722 n = numval - n;
1723 }
1724 else if (opt_type == 0 && stringval != NULL) /* string */
1725 {
1726 s = concat_str(stringval, s);
1727 vim_free(stringval);
1728 stringval = s;
1729 }
1730 }
1731 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001732 if (s != NULL)
1733 {
1734 set_option_value(arg, n, s, opt_flags);
1735 arg_end = p;
1736 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001737 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001738 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001739 }
1740 }
1741
1742 /*
1743 * ":let @r = expr": Set register contents.
1744 */
1745 else if (*arg == '@')
1746 {
1747 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001748 if (op != NULL && (*op == '+' || *op == '-'))
1749 EMSG2(_(e_letwrong), op);
1750 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001751 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001752 EMSG(_(e_letunexp));
1753 else
1754 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001755 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001756 char_u *s;
1757
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001758 p = get_tv_string_chk(tv);
1759 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001760 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02001761 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001762 if (s != NULL)
1763 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001764 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001765 vim_free(s);
1766 }
1767 }
1768 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001769 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001770 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001771 arg_end = arg + 1;
1772 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001773 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001774 }
1775 }
1776
1777 /*
1778 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001779 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001780 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001781 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001782 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001783 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001784
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001785 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001786 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001787 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001788 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1789 EMSG(_(e_letunexp));
1790 else
1791 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001792 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001793 arg_end = p;
1794 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001795 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001796 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001797 }
1798
1799 else
1800 EMSG2(_(e_invarg2), arg);
1801
1802 return arg_end;
1803}
1804
1805/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001806 * Get an lval: variable, Dict item or List item that can be assigned a value
1807 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1808 * "name.key", "name.key[expr]" etc.
1809 * Indexing only works if "name" is an existing List or Dictionary.
1810 * "name" points to the start of the name.
1811 * If "rettv" is not NULL it points to the value to be assigned.
1812 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1813 * wrong; must end in space or cmd separator.
1814 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001815 * flags:
1816 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001817 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001818 * GLV_NO_AUTOLOAD: do not use script autoloading
1819 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001820 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001821 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001822 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001823 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001824 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001825get_lval(
1826 char_u *name,
1827 typval_T *rettv,
1828 lval_T *lp,
1829 int unlet,
1830 int skip,
1831 int flags, /* GLV_ values */
1832 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001833{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001834 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001835 char_u *expr_start, *expr_end;
1836 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001837 dictitem_T *v;
1838 typval_T var1;
1839 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001840 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001841 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001842 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001843 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001844 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001845 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001846
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001847 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001848 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001849
1850 if (skip)
1851 {
1852 /* When skipping just find the end of the name. */
1853 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001854 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001855 }
1856
1857 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001858 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001859 if (expr_start != NULL)
1860 {
1861 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001862 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001863 && *p != '[' && *p != '.')
1864 {
1865 EMSG(_(e_trailing));
1866 return NULL;
1867 }
1868
1869 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1870 if (lp->ll_exp_name == NULL)
1871 {
1872 /* Report an invalid expression in braces, unless the
1873 * expression evaluation has been cancelled due to an
1874 * aborting error, an interrupt, or an exception. */
1875 if (!aborting() && !quiet)
1876 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001877 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001878 EMSG2(_(e_invarg2), name);
1879 return NULL;
1880 }
1881 }
1882 lp->ll_name = lp->ll_exp_name;
1883 }
1884 else
1885 lp->ll_name = name;
1886
1887 /* Without [idx] or .key we are done. */
1888 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1889 return p;
1890
1891 cc = *p;
1892 *p = NUL;
Bram Moolenaar6e65d592017-12-07 22:11:27 +01001893 /* Only pass &ht when we would write to the variable, it prevents autoload
1894 * as well. */
1895 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
1896 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001897 if (v == NULL && !quiet)
1898 EMSG2(_(e_undefvar), lp->ll_name);
1899 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001900 if (v == NULL)
1901 return NULL;
1902
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001903 /*
1904 * Loop until no more [idx] or .key is following.
1905 */
Bram Moolenaar33570922005-01-25 22:26:29 +00001906 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001907 var1.v_type = VAR_UNKNOWN;
1908 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001909 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001910 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001911 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1912 && !(lp->ll_tv->v_type == VAR_DICT
1913 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001914 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001915 if (!quiet)
1916 EMSG(_("E689: Can only index a List or Dictionary"));
1917 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001918 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001919 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001920 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001921 if (!quiet)
1922 EMSG(_("E708: [:] must come last"));
1923 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001925
Bram Moolenaar8c711452005-01-14 21:53:12 +00001926 len = -1;
1927 if (*p == '.')
1928 {
1929 key = p + 1;
1930 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1931 ;
1932 if (len == 0)
1933 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001934 if (!quiet)
1935 EMSG(_(e_emptykey));
1936 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001937 }
1938 p = key + len;
1939 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001940 else
1941 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001942 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001943 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001944 if (*p == ':')
1945 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001946 else
1947 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001948 empty1 = FALSE;
1949 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001950 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001951 if (get_tv_string_chk(&var1) == NULL)
1952 {
1953 /* not a number or string */
1954 clear_tv(&var1);
1955 return NULL;
1956 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001957 }
1958
1959 /* Optionally get the second index [ :expr]. */
1960 if (*p == ':')
1961 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001962 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001963 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001964 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001965 EMSG(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001966 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001967 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001968 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001969 if (rettv != NULL && (rettv->v_type != VAR_LIST
1970 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001971 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001972 if (!quiet)
1973 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001974 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001975 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001976 }
1977 p = skipwhite(p + 1);
1978 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001979 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001980 else
1981 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001982 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001983 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
1984 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001985 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001986 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001987 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001988 if (get_tv_string_chk(&var2) == NULL)
1989 {
1990 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001991 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001992 clear_tv(&var2);
1993 return NULL;
1994 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001995 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001996 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001997 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001998 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001999 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002000
Bram Moolenaar8c711452005-01-14 21:53:12 +00002001 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002002 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002003 if (!quiet)
2004 EMSG(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002005 clear_tv(&var1);
2006 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002007 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002008 }
2009
2010 /* Skip to past ']'. */
2011 ++p;
2012 }
2013
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002014 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002015 {
2016 if (len == -1)
2017 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002018 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002019 key = get_tv_string_chk(&var1); /* is number or string */
2020 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002021 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002022 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002023 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002024 }
2025 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002026 lp->ll_list = NULL;
2027 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002028 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002029
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002030 /* When assigning to a scope dictionary check that a function and
2031 * variable name is valid (only variable name unless it is l: or
2032 * g: dictionary). Disallow overwriting a builtin function. */
2033 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002034 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002035 int prevval;
2036 int wrong;
2037
2038 if (len != -1)
2039 {
2040 prevval = key[len];
2041 key[len] = NUL;
2042 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002043 else
2044 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002045 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2046 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002047 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002048 || !valid_varname(key);
2049 if (len != -1)
2050 key[len] = prevval;
2051 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002052 return NULL;
2053 }
2054
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002055 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002056 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002057 /* Can't add "v:" variable. */
2058 if (lp->ll_dict == &vimvardict)
2059 {
2060 EMSG2(_(e_illvar), name);
2061 return NULL;
2062 }
2063
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002064 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002065 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002066 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002067 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002068 EMSG2(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002069 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002070 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002071 }
2072 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002073 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002074 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002075 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002076 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002077 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002078 p = NULL;
2079 break;
2080 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002081 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002082 else if ((flags & GLV_READ_ONLY) == 0
2083 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002084 {
2085 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002086 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002087 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002088
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002089 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002090 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002091 }
2092 else
2093 {
2094 /*
2095 * Get the number and item for the only or first index of the List.
2096 */
2097 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002098 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002099 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002100 /* is number or string */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002101 lp->ll_n1 = (long)get_tv_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002102 clear_tv(&var1);
2103
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002104 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002105 lp->ll_list = lp->ll_tv->vval.v_list;
2106 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2107 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002108 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002109 if (lp->ll_n1 < 0)
2110 {
2111 lp->ll_n1 = 0;
2112 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2113 }
2114 }
2115 if (lp->ll_li == NULL)
2116 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002117 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002118 if (!quiet)
2119 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002120 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002121 }
2122
2123 /*
2124 * May need to find the item or absolute index for the second
2125 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002126 * When no index given: "lp->ll_empty2" is TRUE.
2127 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002128 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002129 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002130 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002131 lp->ll_n2 = (long)get_tv_number(&var2);
2132 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002133 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002134 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002135 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002136 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002137 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002138 {
2139 if (!quiet)
2140 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002141 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002142 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002143 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002144 }
2145
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002146 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2147 if (lp->ll_n1 < 0)
2148 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2149 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002150 {
2151 if (!quiet)
2152 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002153 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002154 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002155 }
2156
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002157 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002158 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002159 }
2160
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002161 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002162 return p;
2163}
2164
2165/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002166 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002167 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002168 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002169clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002170{
2171 vim_free(lp->ll_exp_name);
2172 vim_free(lp->ll_newkey);
2173}
2174
2175/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002176 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002177 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002178 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002179 */
2180 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002181set_var_lval(
2182 lval_T *lp,
2183 char_u *endp,
2184 typval_T *rettv,
2185 int copy,
2186 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002187{
2188 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002189 listitem_T *ri;
2190 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002191
2192 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002193 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002194 cc = *endp;
2195 *endp = NUL;
2196 if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002197 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002198 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002199
Bram Moolenaar79518e22017-02-17 16:31:35 +01002200 /* handle +=, -= and .= */
2201 di = NULL;
2202 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2203 &tv, &di, TRUE, FALSE) == OK)
2204 {
2205 if ((di == NULL
2206 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2207 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2208 FALSE)))
2209 && tv_op(&tv, rettv, op) == OK)
2210 set_var(lp->ll_name, &tv, FALSE);
2211 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002212 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002213 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002214 else
2215 set_var(lp->ll_name, rettv, copy);
2216 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002217 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002218 else if (tv_check_lock(lp->ll_newkey == NULL
2219 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002220 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002221 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002222 else if (lp->ll_range)
2223 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002224 listitem_T *ll_li = lp->ll_li;
2225 int ll_n1 = lp->ll_n1;
2226
2227 /*
2228 * Check whether any of the list items is locked
2229 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002230 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002231 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002232 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002233 return;
2234 ri = ri->li_next;
2235 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2236 break;
2237 ll_li = ll_li->li_next;
2238 ++ll_n1;
2239 }
2240
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002241 /*
2242 * Assign the List values to the list items.
2243 */
2244 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002245 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002246 if (op != NULL && *op != '=')
2247 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2248 else
2249 {
2250 clear_tv(&lp->ll_li->li_tv);
2251 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2252 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002253 ri = ri->li_next;
2254 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2255 break;
2256 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002257 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002258 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002259 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002260 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002261 ri = NULL;
2262 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002263 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002264 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002265 lp->ll_li = lp->ll_li->li_next;
2266 ++lp->ll_n1;
2267 }
2268 if (ri != NULL)
2269 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002270 else if (lp->ll_empty2
2271 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002272 : lp->ll_n1 != lp->ll_n2)
2273 EMSG(_("E711: List value has not enough items"));
2274 }
2275 else
2276 {
2277 /*
2278 * Assign to a List or Dictionary item.
2279 */
2280 if (lp->ll_newkey != NULL)
2281 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002282 if (op != NULL && *op != '=')
2283 {
2284 EMSG2(_(e_letwrong), op);
2285 return;
2286 }
2287
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002288 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002289 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002290 if (di == NULL)
2291 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002292 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2293 {
2294 vim_free(di);
2295 return;
2296 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002297 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002298 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002299 else if (op != NULL && *op != '=')
2300 {
2301 tv_op(lp->ll_tv, rettv, op);
2302 return;
2303 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002304 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002305 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002306
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002307 /*
2308 * Assign the value to the variable or list item.
2309 */
2310 if (copy)
2311 copy_tv(rettv, lp->ll_tv);
2312 else
2313 {
2314 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002315 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002316 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002317 }
2318 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002319}
2320
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002321/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002322 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2323 * Returns OK or FAIL.
2324 */
2325 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002326tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002327{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002328 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002329 char_u numbuf[NUMBUFLEN];
2330 char_u *s;
2331
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002332 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2333 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2334 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002335 {
2336 switch (tv1->v_type)
2337 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002338 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002339 case VAR_DICT:
2340 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002341 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002342 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002343 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002344 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002345 break;
2346
2347 case VAR_LIST:
2348 if (*op != '+' || tv2->v_type != VAR_LIST)
2349 break;
2350 /* List += List */
2351 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2352 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2353 return OK;
2354
2355 case VAR_NUMBER:
2356 case VAR_STRING:
2357 if (tv2->v_type == VAR_LIST)
2358 break;
2359 if (*op == '+' || *op == '-')
2360 {
2361 /* nr += nr or nr -= nr*/
2362 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002363#ifdef FEAT_FLOAT
2364 if (tv2->v_type == VAR_FLOAT)
2365 {
2366 float_T f = n;
2367
2368 if (*op == '+')
2369 f += tv2->vval.v_float;
2370 else
2371 f -= tv2->vval.v_float;
2372 clear_tv(tv1);
2373 tv1->v_type = VAR_FLOAT;
2374 tv1->vval.v_float = f;
2375 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002376 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002377#endif
2378 {
2379 if (*op == '+')
2380 n += get_tv_number(tv2);
2381 else
2382 n -= get_tv_number(tv2);
2383 clear_tv(tv1);
2384 tv1->v_type = VAR_NUMBER;
2385 tv1->vval.v_number = n;
2386 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002387 }
2388 else
2389 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002390 if (tv2->v_type == VAR_FLOAT)
2391 break;
2392
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002393 /* str .= str */
2394 s = get_tv_string(tv1);
2395 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2396 clear_tv(tv1);
2397 tv1->v_type = VAR_STRING;
2398 tv1->vval.v_string = s;
2399 }
2400 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002401
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002402 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002403#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002404 {
2405 float_T f;
2406
2407 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2408 && tv2->v_type != VAR_NUMBER
2409 && tv2->v_type != VAR_STRING))
2410 break;
2411 if (tv2->v_type == VAR_FLOAT)
2412 f = tv2->vval.v_float;
2413 else
2414 f = get_tv_number(tv2);
2415 if (*op == '+')
2416 tv1->vval.v_float += f;
2417 else
2418 tv1->vval.v_float -= f;
2419 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002420#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002421 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002422 }
2423 }
2424
2425 EMSG2(_(e_letwrong), op);
2426 return FAIL;
2427}
2428
2429/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002430 * Evaluate the expression used in a ":for var in expr" command.
2431 * "arg" points to "var".
2432 * Set "*errp" to TRUE for an error, FALSE otherwise;
2433 * Return a pointer that holds the info. Null when there is an error.
2434 */
2435 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002436eval_for_line(
2437 char_u *arg,
2438 int *errp,
2439 char_u **nextcmdp,
2440 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002441{
Bram Moolenaar33570922005-01-25 22:26:29 +00002442 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002443 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002444 typval_T tv;
2445 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002446
2447 *errp = TRUE; /* default: there is an error */
2448
Bram Moolenaar33570922005-01-25 22:26:29 +00002449 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002450 if (fi == NULL)
2451 return NULL;
2452
2453 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2454 if (expr == NULL)
2455 return fi;
2456
2457 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002458 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002459 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002460 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002461 return fi;
2462 }
2463
2464 if (skip)
2465 ++emsg_skip;
2466 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2467 {
2468 *errp = FALSE;
2469 if (!skip)
2470 {
2471 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002472 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002473 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002474 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002475 clear_tv(&tv);
2476 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002477 else if (l == NULL)
2478 {
2479 /* a null list is like an empty list: do nothing */
2480 clear_tv(&tv);
2481 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002482 else
2483 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002484 /* No need to increment the refcount, it's already set for the
2485 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002486 fi->fi_list = l;
2487 list_add_watch(l, &fi->fi_lw);
2488 fi->fi_lw.lw_item = l->lv_first;
2489 }
2490 }
2491 }
2492 if (skip)
2493 --emsg_skip;
2494
2495 return fi;
2496}
2497
2498/*
2499 * Use the first item in a ":for" list. Advance to the next.
2500 * Assign the values to the variable (list). "arg" points to the first one.
2501 * Return TRUE when a valid item was found, FALSE when at end of list or
2502 * something wrong.
2503 */
2504 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002505next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002506{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002507 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002508 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002509 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002510
2511 item = fi->fi_lw.lw_item;
2512 if (item == NULL)
2513 result = FALSE;
2514 else
2515 {
2516 fi->fi_lw.lw_item = item->li_next;
2517 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2518 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2519 }
2520 return result;
2521}
2522
2523/*
2524 * Free the structure used to store info used by ":for".
2525 */
2526 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002527free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002528{
Bram Moolenaar33570922005-01-25 22:26:29 +00002529 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002530
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002531 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002532 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002533 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002534 list_unref(fi->fi_list);
2535 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002536 vim_free(fi);
2537}
2538
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2540
2541 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002542set_context_for_expression(
2543 expand_T *xp,
2544 char_u *arg,
2545 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546{
2547 int got_eq = FALSE;
2548 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002549 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002551 if (cmdidx == CMD_let)
2552 {
2553 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002554 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002555 {
2556 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002557 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002558 {
2559 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002560 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002561 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002562 break;
2563 }
2564 return;
2565 }
2566 }
2567 else
2568 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2569 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 while ((xp->xp_pattern = vim_strpbrk(arg,
2571 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2572 {
2573 c = *xp->xp_pattern;
2574 if (c == '&')
2575 {
2576 c = xp->xp_pattern[1];
2577 if (c == '&')
2578 {
2579 ++xp->xp_pattern;
2580 xp->xp_context = cmdidx != CMD_let || got_eq
2581 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2582 }
2583 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002584 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002586 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2587 xp->xp_pattern += 2;
2588
2589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 }
2591 else if (c == '$')
2592 {
2593 /* environment variable */
2594 xp->xp_context = EXPAND_ENV_VARS;
2595 }
2596 else if (c == '=')
2597 {
2598 got_eq = TRUE;
2599 xp->xp_context = EXPAND_EXPRESSION;
2600 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002601 else if (c == '#'
2602 && xp->xp_context == EXPAND_EXPRESSION)
2603 {
2604 /* Autoload function/variable contains '#'. */
2605 break;
2606 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002607 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 && xp->xp_context == EXPAND_FUNCTIONS
2609 && vim_strchr(xp->xp_pattern, '(') == NULL)
2610 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002611 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 break;
2613 }
2614 else if (cmdidx != CMD_let || got_eq)
2615 {
2616 if (c == '"') /* string */
2617 {
2618 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2619 if (c == '\\' && xp->xp_pattern[1] != NUL)
2620 ++xp->xp_pattern;
2621 xp->xp_context = EXPAND_NOTHING;
2622 }
2623 else if (c == '\'') /* literal string */
2624 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002625 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2627 /* skip */ ;
2628 xp->xp_context = EXPAND_NOTHING;
2629 }
2630 else if (c == '|')
2631 {
2632 if (xp->xp_pattern[1] == '|')
2633 {
2634 ++xp->xp_pattern;
2635 xp->xp_context = EXPAND_EXPRESSION;
2636 }
2637 else
2638 xp->xp_context = EXPAND_COMMANDS;
2639 }
2640 else
2641 xp->xp_context = EXPAND_EXPRESSION;
2642 }
2643 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002644 /* Doesn't look like something valid, expand as an expression
2645 * anyway. */
2646 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 arg = xp->xp_pattern;
2648 if (*arg != NUL)
2649 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2650 /* skip */ ;
2651 }
2652 xp->xp_pattern = arg;
2653}
2654
2655#endif /* FEAT_CMDL_COMPL */
2656
2657/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 * ":unlet[!] var1 ... " command.
2659 */
2660 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002661ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002663 ex_unletlock(eap, eap->arg, 0);
2664}
2665
2666/*
2667 * ":lockvar" and ":unlockvar" commands
2668 */
2669 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002670ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002671{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002673 int deep = 2;
2674
2675 if (eap->forceit)
2676 deep = -1;
2677 else if (vim_isdigit(*arg))
2678 {
2679 deep = getdigits(&arg);
2680 arg = skipwhite(arg);
2681 }
2682
2683 ex_unletlock(eap, arg, deep);
2684}
2685
2686/*
2687 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2688 */
2689 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002690ex_unletlock(
2691 exarg_T *eap,
2692 char_u *argstart,
2693 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002694{
2695 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002698 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699
2700 do
2701 {
Bram Moolenaar137374f2018-05-13 15:59:50 +02002702 if (*arg == '$')
2703 {
2704 char_u *name = ++arg;
2705
2706 if (get_env_len(&arg) == 0)
2707 {
2708 EMSG2(_(e_invarg2), name - 1);
2709 return;
2710 }
2711 vim_unsetenv(name);
2712 arg = skipwhite(arg);
2713 continue;
2714 }
2715
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002717 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002718 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 if (lv.ll_name == NULL)
2720 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002721 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 if (name_end != NULL)
2725 {
2726 emsg_severe = TRUE;
2727 EMSG(_(e_trailing));
2728 }
2729 if (!(eap->skip || error))
2730 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 break;
2732 }
2733
2734 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002735 {
2736 if (eap->cmdidx == CMD_unlet)
2737 {
2738 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2739 error = TRUE;
2740 }
2741 else
2742 {
2743 if (do_lock_var(&lv, name_end, deep,
2744 eap->cmdidx == CMD_lockvar) == FAIL)
2745 error = TRUE;
2746 }
2747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 if (!eap->skip)
2750 clear_lval(&lv);
2751
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 arg = skipwhite(name_end);
2753 } while (!ends_excmd(*arg));
2754
2755 eap->nextcmd = check_nextcmd(arg);
2756}
2757
Bram Moolenaar8c711452005-01-14 21:53:12 +00002758 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002759do_unlet_var(
2760 lval_T *lp,
2761 char_u *name_end,
2762 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002763{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 int ret = OK;
2765 int cc;
2766
2767 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002768 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 cc = *name_end;
2770 *name_end = NUL;
2771
2772 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002773 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002775 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002777 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002778 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002779 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002780 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002781 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 else if (lp->ll_range)
2783 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002784 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002785 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01002786 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002787
2788 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
2789 {
2790 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02002791 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002792 return FAIL;
2793 ll_li = li;
2794 ++ll_n1;
2795 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796
2797 /* Delete a range of List items. */
2798 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2799 {
2800 li = lp->ll_li->li_next;
2801 listitem_remove(lp->ll_list, lp->ll_li);
2802 lp->ll_li = li;
2803 ++lp->ll_n1;
2804 }
2805 }
2806 else
2807 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809 /* unlet a List item. */
2810 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002811 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002812 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002813 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 }
2815
2816 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817}
2818
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819/*
2820 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002821 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 */
2823 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002824do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825{
Bram Moolenaar33570922005-01-25 22:26:29 +00002826 hashtab_T *ht;
2827 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002828 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002829 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002830 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831
Bram Moolenaar33570922005-01-25 22:26:29 +00002832 ht = find_var_ht(name, &varname);
2833 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002835 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002836 if (d == NULL)
2837 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002838 if (ht == &globvarht)
2839 d = &globvardict;
2840 else if (ht == &compat_hashtab)
2841 d = &vimvardict;
2842 else
2843 {
2844 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2845 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2846 }
2847 if (d == NULL)
2848 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01002849 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002850 return FAIL;
2851 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002852 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002853 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002854 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02002855 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002856 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00002857 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002858 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02002859 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002860 || var_check_ro(di->di_flags, name, FALSE)
2861 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01002862 return FAIL;
2863
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002864 delete_var(ht, hi);
2865 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00002866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002868 if (forceit)
2869 return OK;
2870 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 return FAIL;
2872}
2873
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002874/*
2875 * Lock or unlock variable indicated by "lp".
2876 * "deep" is the levels to go (-1 for unlimited);
2877 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2878 */
2879 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002880do_lock_var(
2881 lval_T *lp,
2882 char_u *name_end,
2883 int deep,
2884 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002885{
2886 int ret = OK;
2887 int cc;
2888 dictitem_T *di;
2889
2890 if (deep == 0) /* nothing to do */
2891 return OK;
2892
2893 if (lp->ll_tv == NULL)
2894 {
2895 cc = *name_end;
2896 *name_end = NUL;
2897
2898 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002899 di = find_var(lp->ll_name, NULL, TRUE);
2900 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002901 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01002902 else if ((di->di_flags & DI_FLAGS_FIX)
2903 && di->di_tv.v_type != VAR_DICT
2904 && di->di_tv.v_type != VAR_LIST)
2905 /* For historic reasons this error is not given for a list or dict.
2906 * E.g., the b: dict could be locked/unlocked. */
2907 EMSG2(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002908 else
2909 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002910 if (lock)
2911 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002912 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01002913 di->di_flags &= ~DI_FLAGS_LOCK;
2914 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002915 }
2916 *name_end = cc;
2917 }
2918 else if (lp->ll_range)
2919 {
2920 listitem_T *li = lp->ll_li;
2921
2922 /* (un)lock a range of List items. */
2923 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2924 {
2925 item_lock(&li->li_tv, deep, lock);
2926 li = li->li_next;
2927 ++lp->ll_n1;
2928 }
2929 }
2930 else if (lp->ll_list != NULL)
2931 /* (un)lock a List item. */
2932 item_lock(&lp->ll_li->li_tv, deep, lock);
2933 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02002934 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002935 item_lock(&lp->ll_di->di_tv, deep, lock);
2936
2937 return ret;
2938}
2939
2940/*
2941 * Lock or unlock an item. "deep" is nr of levels to go.
2942 */
2943 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002944item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002945{
2946 static int recurse = 0;
2947 list_T *l;
2948 listitem_T *li;
2949 dict_T *d;
2950 hashitem_T *hi;
2951 int todo;
2952
2953 if (recurse >= DICT_MAXNEST)
2954 {
2955 EMSG(_("E743: variable nested too deep for (un)lock"));
2956 return;
2957 }
2958 if (deep == 0)
2959 return;
2960 ++recurse;
2961
2962 /* lock/unlock the item itself */
2963 if (lock)
2964 tv->v_lock |= VAR_LOCKED;
2965 else
2966 tv->v_lock &= ~VAR_LOCKED;
2967
2968 switch (tv->v_type)
2969 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01002970 case VAR_UNKNOWN:
2971 case VAR_NUMBER:
2972 case VAR_STRING:
2973 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002974 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002975 case VAR_FLOAT:
2976 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002977 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002978 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002979 break;
2980
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002981 case VAR_LIST:
2982 if ((l = tv->vval.v_list) != NULL)
2983 {
2984 if (lock)
2985 l->lv_lock |= VAR_LOCKED;
2986 else
2987 l->lv_lock &= ~VAR_LOCKED;
2988 if (deep < 0 || deep > 1)
2989 /* recursive: lock/unlock the items the List contains */
2990 for (li = l->lv_first; li != NULL; li = li->li_next)
2991 item_lock(&li->li_tv, deep - 1, lock);
2992 }
2993 break;
2994 case VAR_DICT:
2995 if ((d = tv->vval.v_dict) != NULL)
2996 {
2997 if (lock)
2998 d->dv_lock |= VAR_LOCKED;
2999 else
3000 d->dv_lock &= ~VAR_LOCKED;
3001 if (deep < 0 || deep > 1)
3002 {
3003 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003004 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003005 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3006 {
3007 if (!HASHITEM_EMPTY(hi))
3008 {
3009 --todo;
3010 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3011 }
3012 }
3013 }
3014 }
3015 }
3016 --recurse;
3017}
3018
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3020/*
3021 * Delete all "menutrans_" variables.
3022 */
3023 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003024del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025{
Bram Moolenaar33570922005-01-25 22:26:29 +00003026 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003027 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028
Bram Moolenaar33570922005-01-25 22:26:29 +00003029 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003030 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003031 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003032 {
3033 if (!HASHITEM_EMPTY(hi))
3034 {
3035 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003036 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3037 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003038 }
3039 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003040 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041}
3042#endif
3043
3044#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3045
3046/*
3047 * Local string buffer for the next two functions to store a variable name
3048 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3049 * get_user_var_name().
3050 */
3051
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052static char_u *varnamebuf = NULL;
3053static int varnamebuflen = 0;
3054
3055/*
3056 * Function to concatenate a prefix and a variable name.
3057 */
3058 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003059cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060{
3061 int len;
3062
3063 len = (int)STRLEN(name) + 3;
3064 if (len > varnamebuflen)
3065 {
3066 vim_free(varnamebuf);
3067 len += 10; /* some additional space */
3068 varnamebuf = alloc(len);
3069 if (varnamebuf == NULL)
3070 {
3071 varnamebuflen = 0;
3072 return NULL;
3073 }
3074 varnamebuflen = len;
3075 }
3076 *varnamebuf = prefix;
3077 varnamebuf[1] = ':';
3078 STRCPY(varnamebuf + 2, name);
3079 return varnamebuf;
3080}
3081
3082/*
3083 * Function given to ExpandGeneric() to obtain the list of user defined
3084 * (global/buffer/window/built-in) variable names.
3085 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003087get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003089 static long_u gdone;
3090 static long_u bdone;
3091 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003092 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003093 static int vidx;
3094 static hashitem_T *hi;
3095 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096
3097 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003098 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003099 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003100 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003101 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003102
3103 /* Global variables */
3104 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003106 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003107 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003108 else
3109 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003110 while (HASHITEM_EMPTY(hi))
3111 ++hi;
3112 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3113 return cat_prefix_varname('g', hi->hi_key);
3114 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003116
3117 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003118 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003119 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003121 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003122 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003123 else
3124 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003125 while (HASHITEM_EMPTY(hi))
3126 ++hi;
3127 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003129
3130 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003131 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003132 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003134 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003135 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003136 else
3137 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003138 while (HASHITEM_EMPTY(hi))
3139 ++hi;
3140 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003142
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003143 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003144 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003145 if (tdone < ht->ht_used)
3146 {
3147 if (tdone++ == 0)
3148 hi = ht->ht_array;
3149 else
3150 ++hi;
3151 while (HASHITEM_EMPTY(hi))
3152 ++hi;
3153 return cat_prefix_varname('t', hi->hi_key);
3154 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003155
Bram Moolenaar33570922005-01-25 22:26:29 +00003156 /* v: variables */
3157 if (vidx < VV_LEN)
3158 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159
Bram Moolenaard23a8232018-02-10 18:45:26 +01003160 VIM_CLEAR(varnamebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 varnamebuflen = 0;
3162 return NULL;
3163}
3164
3165#endif /* FEAT_CMDL_COMPL */
3166
3167/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003168 * Return TRUE if "pat" matches "text".
3169 * Does not use 'cpo' and always uses 'magic'.
3170 */
3171 static int
3172pattern_match(char_u *pat, char_u *text, int ic)
3173{
3174 int matches = FALSE;
3175 char_u *save_cpo;
3176 regmatch_T regmatch;
3177
3178 /* avoid 'l' flag in 'cpoptions' */
3179 save_cpo = p_cpo;
3180 p_cpo = (char_u *)"";
3181 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3182 if (regmatch.regprog != NULL)
3183 {
3184 regmatch.rm_ic = ic;
3185 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3186 vim_regfree(regmatch.regprog);
3187 }
3188 p_cpo = save_cpo;
3189 return matches;
3190}
3191
3192/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003194 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3196 */
3197
3198/*
3199 * Handle zero level expression.
3200 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003201 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003202 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 * Return OK or FAIL.
3204 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003205 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003206eval0(
3207 char_u *arg,
3208 typval_T *rettv,
3209 char_u **nextcmd,
3210 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211{
3212 int ret;
3213 char_u *p;
3214
3215 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003216 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 if (ret == FAIL || !ends_excmd(*p))
3218 {
3219 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003220 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 /*
3222 * Report the invalid expression unless the expression evaluation has
3223 * been cancelled due to an aborting error, an interrupt, or an
3224 * exception.
3225 */
3226 if (!aborting())
3227 EMSG2(_(e_invexpr2), arg);
3228 ret = FAIL;
3229 }
3230 if (nextcmd != NULL)
3231 *nextcmd = check_nextcmd(p);
3232
3233 return ret;
3234}
3235
3236/*
3237 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003238 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 *
3240 * "arg" must point to the first non-white of the expression.
3241 * "arg" is advanced to the next non-white after the recognized expression.
3242 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003243 * Note: "rettv.v_lock" is not set.
3244 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 * Return OK or FAIL.
3246 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003247 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003248eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249{
3250 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003251 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252
3253 /*
3254 * Get the first variable.
3255 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003256 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 return FAIL;
3258
3259 if ((*arg)[0] == '?')
3260 {
3261 result = FALSE;
3262 if (evaluate)
3263 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003264 int error = FALSE;
3265
3266 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003268 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003269 if (error)
3270 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 }
3272
3273 /*
3274 * Get the second variable.
3275 */
3276 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003277 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 return FAIL;
3279
3280 /*
3281 * Check for the ":".
3282 */
3283 if ((*arg)[0] != ':')
3284 {
3285 EMSG(_("E109: Missing ':' after '?'"));
3286 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003287 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 return FAIL;
3289 }
3290
3291 /*
3292 * Get the third variable.
3293 */
3294 *arg = skipwhite(*arg + 1);
3295 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3296 {
3297 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003298 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 return FAIL;
3300 }
3301 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003302 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 }
3304
3305 return OK;
3306}
3307
3308/*
3309 * Handle first level expression:
3310 * expr2 || expr2 || expr2 logical OR
3311 *
3312 * "arg" must point to the first non-white of the expression.
3313 * "arg" is advanced to the next non-white after the recognized expression.
3314 *
3315 * Return OK or FAIL.
3316 */
3317 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003318eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319{
Bram Moolenaar33570922005-01-25 22:26:29 +00003320 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 long result;
3322 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003323 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324
3325 /*
3326 * Get the first variable.
3327 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003328 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 return FAIL;
3330
3331 /*
3332 * Repeat until there is no following "||".
3333 */
3334 first = TRUE;
3335 result = FALSE;
3336 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3337 {
3338 if (evaluate && first)
3339 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003340 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003342 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003343 if (error)
3344 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 first = FALSE;
3346 }
3347
3348 /*
3349 * Get the second variable.
3350 */
3351 *arg = skipwhite(*arg + 2);
3352 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3353 return FAIL;
3354
3355 /*
3356 * Compute the result.
3357 */
3358 if (evaluate && !result)
3359 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003360 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003362 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003363 if (error)
3364 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 }
3366 if (evaluate)
3367 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003368 rettv->v_type = VAR_NUMBER;
3369 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 }
3371 }
3372
3373 return OK;
3374}
3375
3376/*
3377 * Handle second level expression:
3378 * expr3 && expr3 && expr3 logical AND
3379 *
3380 * "arg" must point to the first non-white of the expression.
3381 * "arg" is advanced to the next non-white after the recognized expression.
3382 *
3383 * Return OK or FAIL.
3384 */
3385 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003386eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387{
Bram Moolenaar33570922005-01-25 22:26:29 +00003388 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 long result;
3390 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003391 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392
3393 /*
3394 * Get the first variable.
3395 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003396 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 return FAIL;
3398
3399 /*
3400 * Repeat until there is no following "&&".
3401 */
3402 first = TRUE;
3403 result = TRUE;
3404 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3405 {
3406 if (evaluate && first)
3407 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003408 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003410 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003411 if (error)
3412 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 first = FALSE;
3414 }
3415
3416 /*
3417 * Get the second variable.
3418 */
3419 *arg = skipwhite(*arg + 2);
3420 if (eval4(arg, &var2, evaluate && result) == FAIL)
3421 return FAIL;
3422
3423 /*
3424 * Compute the result.
3425 */
3426 if (evaluate && result)
3427 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003428 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003430 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003431 if (error)
3432 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 }
3434 if (evaluate)
3435 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003436 rettv->v_type = VAR_NUMBER;
3437 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 }
3439 }
3440
3441 return OK;
3442}
3443
3444/*
3445 * Handle third level expression:
3446 * var1 == var2
3447 * var1 =~ var2
3448 * var1 != var2
3449 * var1 !~ var2
3450 * var1 > var2
3451 * var1 >= var2
3452 * var1 < var2
3453 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003454 * var1 is var2
3455 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 *
3457 * "arg" must point to the first non-white of the expression.
3458 * "arg" is advanced to the next non-white after the recognized expression.
3459 *
3460 * Return OK or FAIL.
3461 */
3462 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003463eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464{
Bram Moolenaar33570922005-01-25 22:26:29 +00003465 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 char_u *p;
3467 int i;
3468 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003469 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472
3473 /*
3474 * Get the first variable.
3475 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003476 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 return FAIL;
3478
3479 p = *arg;
3480 switch (p[0])
3481 {
3482 case '=': if (p[1] == '=')
3483 type = TYPE_EQUAL;
3484 else if (p[1] == '~')
3485 type = TYPE_MATCH;
3486 break;
3487 case '!': if (p[1] == '=')
3488 type = TYPE_NEQUAL;
3489 else if (p[1] == '~')
3490 type = TYPE_NOMATCH;
3491 break;
3492 case '>': if (p[1] != '=')
3493 {
3494 type = TYPE_GREATER;
3495 len = 1;
3496 }
3497 else
3498 type = TYPE_GEQUAL;
3499 break;
3500 case '<': if (p[1] != '=')
3501 {
3502 type = TYPE_SMALLER;
3503 len = 1;
3504 }
3505 else
3506 type = TYPE_SEQUAL;
3507 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003508 case 'i': if (p[1] == 's')
3509 {
3510 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3511 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003512 i = p[len];
3513 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003514 {
3515 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3516 type_is = TRUE;
3517 }
3518 }
3519 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 }
3521
3522 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003523 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 */
3525 if (type != TYPE_UNKNOWN)
3526 {
3527 /* extra question mark appended: ignore case */
3528 if (p[len] == '?')
3529 {
3530 ic = TRUE;
3531 ++len;
3532 }
3533 /* extra '#' appended: match case */
3534 else if (p[len] == '#')
3535 {
3536 ic = FALSE;
3537 ++len;
3538 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003539 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 else
3541 ic = p_ic;
3542
3543 /*
3544 * Get the second variable.
3545 */
3546 *arg = skipwhite(p + len);
3547 if (eval5(arg, &var2, evaluate) == FAIL)
3548 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003549 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 return FAIL;
3551 }
Bram Moolenaar31988702018-02-13 12:57:42 +01003552 if (evaluate)
3553 {
3554 int ret = typval_compare(rettv, &var2, type, type_is, ic);
3555
3556 clear_tv(&var2);
3557 return ret;
3558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 }
3560
3561 return OK;
3562}
3563
3564/*
3565 * Handle fourth level expression:
3566 * + number addition
3567 * - number subtraction
3568 * . string concatenation
3569 *
3570 * "arg" must point to the first non-white of the expression.
3571 * "arg" is advanced to the next non-white after the recognized expression.
3572 *
3573 * Return OK or FAIL.
3574 */
3575 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003576eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577{
Bram Moolenaar33570922005-01-25 22:26:29 +00003578 typval_T var2;
3579 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003581 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003582#ifdef FEAT_FLOAT
3583 float_T f1 = 0, f2 = 0;
3584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 char_u *s1, *s2;
3586 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3587 char_u *p;
3588
3589 /*
3590 * Get the first variable.
3591 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003592 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 return FAIL;
3594
3595 /*
3596 * Repeat computing, until no '+', '-' or '.' is following.
3597 */
3598 for (;;)
3599 {
3600 op = **arg;
3601 if (op != '+' && op != '-' && op != '.')
3602 break;
3603
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003604 if ((op != '+' || rettv->v_type != VAR_LIST)
3605#ifdef FEAT_FLOAT
3606 && (op == '.' || rettv->v_type != VAR_FLOAT)
3607#endif
3608 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003609 {
3610 /* For "list + ...", an illegal use of the first operand as
3611 * a number cannot be determined before evaluating the 2nd
3612 * operand: if this is also a list, all is ok.
3613 * For "something . ...", "something - ..." or "non-list + ...",
3614 * we know that the first operand needs to be a string or number
3615 * without evaluating the 2nd operand. So check before to avoid
3616 * side effects after an error. */
3617 if (evaluate && get_tv_string_chk(rettv) == NULL)
3618 {
3619 clear_tv(rettv);
3620 return FAIL;
3621 }
3622 }
3623
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 /*
3625 * Get the second variable.
3626 */
3627 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003628 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003630 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 return FAIL;
3632 }
3633
3634 if (evaluate)
3635 {
3636 /*
3637 * Compute the result.
3638 */
3639 if (op == '.')
3640 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003641 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
3642 s2 = get_tv_string_buf_chk(&var2, buf2);
3643 if (s2 == NULL) /* type error ? */
3644 {
3645 clear_tv(rettv);
3646 clear_tv(&var2);
3647 return FAIL;
3648 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003649 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003650 clear_tv(rettv);
3651 rettv->v_type = VAR_STRING;
3652 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003654 else if (op == '+' && rettv->v_type == VAR_LIST
3655 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003656 {
3657 /* concatenate Lists */
3658 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3659 &var3) == FAIL)
3660 {
3661 clear_tv(rettv);
3662 clear_tv(&var2);
3663 return FAIL;
3664 }
3665 clear_tv(rettv);
3666 *rettv = var3;
3667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 else
3669 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003670 int error = FALSE;
3671
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003672#ifdef FEAT_FLOAT
3673 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003674 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003675 f1 = rettv->vval.v_float;
3676 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003677 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003678 else
3679#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003680 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003681 n1 = get_tv_number_chk(rettv, &error);
3682 if (error)
3683 {
3684 /* This can only happen for "list + non-list". For
3685 * "non-list + ..." or "something - ...", we returned
3686 * before evaluating the 2nd operand. */
3687 clear_tv(rettv);
3688 return FAIL;
3689 }
3690#ifdef FEAT_FLOAT
3691 if (var2.v_type == VAR_FLOAT)
3692 f1 = n1;
3693#endif
3694 }
3695#ifdef FEAT_FLOAT
3696 if (var2.v_type == VAR_FLOAT)
3697 {
3698 f2 = var2.vval.v_float;
3699 n2 = 0;
3700 }
3701 else
3702#endif
3703 {
3704 n2 = get_tv_number_chk(&var2, &error);
3705 if (error)
3706 {
3707 clear_tv(rettv);
3708 clear_tv(&var2);
3709 return FAIL;
3710 }
3711#ifdef FEAT_FLOAT
3712 if (rettv->v_type == VAR_FLOAT)
3713 f2 = n2;
3714#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003715 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003716 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003717
3718#ifdef FEAT_FLOAT
3719 /* If there is a float on either side the result is a float. */
3720 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3721 {
3722 if (op == '+')
3723 f1 = f1 + f2;
3724 else
3725 f1 = f1 - f2;
3726 rettv->v_type = VAR_FLOAT;
3727 rettv->vval.v_float = f1;
3728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003730#endif
3731 {
3732 if (op == '+')
3733 n1 = n1 + n2;
3734 else
3735 n1 = n1 - n2;
3736 rettv->v_type = VAR_NUMBER;
3737 rettv->vval.v_number = n1;
3738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003740 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 }
3742 }
3743 return OK;
3744}
3745
3746/*
3747 * Handle fifth level expression:
3748 * * number multiplication
3749 * / number division
3750 * % number modulo
3751 *
3752 * "arg" must point to the first non-white of the expression.
3753 * "arg" is advanced to the next non-white after the recognized expression.
3754 *
3755 * Return OK or FAIL.
3756 */
3757 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003758eval6(
3759 char_u **arg,
3760 typval_T *rettv,
3761 int evaluate,
3762 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763{
Bram Moolenaar33570922005-01-25 22:26:29 +00003764 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003766 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003767#ifdef FEAT_FLOAT
3768 int use_float = FALSE;
3769 float_T f1 = 0, f2;
3770#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003771 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772
3773 /*
3774 * Get the first variable.
3775 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003776 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 return FAIL;
3778
3779 /*
3780 * Repeat computing, until no '*', '/' or '%' is following.
3781 */
3782 for (;;)
3783 {
3784 op = **arg;
3785 if (op != '*' && op != '/' && op != '%')
3786 break;
3787
3788 if (evaluate)
3789 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003790#ifdef FEAT_FLOAT
3791 if (rettv->v_type == VAR_FLOAT)
3792 {
3793 f1 = rettv->vval.v_float;
3794 use_float = TRUE;
3795 n1 = 0;
3796 }
3797 else
3798#endif
3799 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003800 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003801 if (error)
3802 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 }
3804 else
3805 n1 = 0;
3806
3807 /*
3808 * Get the second variable.
3809 */
3810 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003811 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 return FAIL;
3813
3814 if (evaluate)
3815 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003816#ifdef FEAT_FLOAT
3817 if (var2.v_type == VAR_FLOAT)
3818 {
3819 if (!use_float)
3820 {
3821 f1 = n1;
3822 use_float = TRUE;
3823 }
3824 f2 = var2.vval.v_float;
3825 n2 = 0;
3826 }
3827 else
3828#endif
3829 {
3830 n2 = get_tv_number_chk(&var2, &error);
3831 clear_tv(&var2);
3832 if (error)
3833 return FAIL;
3834#ifdef FEAT_FLOAT
3835 if (use_float)
3836 f2 = n2;
3837#endif
3838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839
3840 /*
3841 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003842 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003844#ifdef FEAT_FLOAT
3845 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003847 if (op == '*')
3848 f1 = f1 * f2;
3849 else if (op == '/')
3850 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003851# ifdef VMS
3852 /* VMS crashes on divide by zero, work around it */
3853 if (f2 == 0.0)
3854 {
3855 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003856 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003857 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003858 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003859 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003860 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003861 }
3862 else
3863 f1 = f1 / f2;
3864# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003865 /* We rely on the floating point library to handle divide
3866 * by zero to result in "inf" and not a crash. */
3867 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003868# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003871 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00003872 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003873 return FAIL;
3874 }
3875 rettv->v_type = VAR_FLOAT;
3876 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 }
3878 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003881 if (op == '*')
3882 n1 = n1 * n2;
3883 else if (op == '/')
3884 {
3885 if (n2 == 0) /* give an error message? */
3886 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003887 if (n1 == 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003888 n1 = VARNUM_MIN; /* similar to NaN */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003889 else if (n1 < 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003890 n1 = -VARNUM_MAX;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003891 else
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003892 n1 = VARNUM_MAX;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003893 }
3894 else
3895 n1 = n1 / n2;
3896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003898 {
3899 if (n2 == 0) /* give an error message? */
3900 n1 = 0;
3901 else
3902 n1 = n1 % n2;
3903 }
3904 rettv->v_type = VAR_NUMBER;
3905 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 }
3908 }
3909
3910 return OK;
3911}
3912
3913/*
3914 * Handle sixth level expression:
3915 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003916 * "string" string constant
3917 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 * &option-name option value
3919 * @r register contents
3920 * identifier variable value
3921 * function() function call
3922 * $VAR environment variable
3923 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003924 * [expr, expr] List
3925 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 *
3927 * Also handle:
3928 * ! in front logical NOT
3929 * - in front unary minus
3930 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003931 * trailing [] subscript in String or List
3932 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 *
3934 * "arg" must point to the first non-white of the expression.
3935 * "arg" is advanced to the next non-white after the recognized expression.
3936 *
3937 * Return OK or FAIL.
3938 */
3939 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003940eval7(
3941 char_u **arg,
3942 typval_T *rettv,
3943 int evaluate,
3944 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003946 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 int len;
3948 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 char_u *start_leader, *end_leader;
3950 int ret = OK;
3951 char_u *alias;
3952
3953 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003954 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003955 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003957 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958
3959 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003960 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 */
3962 start_leader = *arg;
3963 while (**arg == '!' || **arg == '-' || **arg == '+')
3964 *arg = skipwhite(*arg + 1);
3965 end_leader = *arg;
3966
3967 switch (**arg)
3968 {
3969 /*
3970 * Number constant.
3971 */
3972 case '0':
3973 case '1':
3974 case '2':
3975 case '3':
3976 case '4':
3977 case '5':
3978 case '6':
3979 case '7':
3980 case '8':
3981 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003982 {
3983#ifdef FEAT_FLOAT
3984 char_u *p = skipdigits(*arg + 1);
3985 int get_float = FALSE;
3986
3987 /* We accept a float when the format matches
3988 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003989 * strict to avoid backwards compatibility problems.
3990 * Don't look for a float after the "." operator, so that
3991 * ":let vers = 1.2.3" doesn't fail. */
3992 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003994 get_float = TRUE;
3995 p = skipdigits(p + 2);
3996 if (*p == 'e' || *p == 'E')
3997 {
3998 ++p;
3999 if (*p == '-' || *p == '+')
4000 ++p;
4001 if (!vim_isdigit(*p))
4002 get_float = FALSE;
4003 else
4004 p = skipdigits(p + 1);
4005 }
4006 if (ASCII_ISALPHA(*p) || *p == '.')
4007 get_float = FALSE;
4008 }
4009 if (get_float)
4010 {
4011 float_T f;
4012
4013 *arg += string2float(*arg, &f);
4014 if (evaluate)
4015 {
4016 rettv->v_type = VAR_FLOAT;
4017 rettv->vval.v_float = f;
4018 }
4019 }
4020 else
4021#endif
4022 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01004023 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004024 *arg += len;
4025 if (evaluate)
4026 {
4027 rettv->v_type = VAR_NUMBER;
4028 rettv->vval.v_number = n;
4029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 }
4031 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033
4034 /*
4035 * String constant: "string".
4036 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004037 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 break;
4039
4040 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004041 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004043 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004044 break;
4045
4046 /*
4047 * List: [expr, expr]
4048 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004049 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 break;
4051
4052 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004053 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004054 * Dictionary: {key: val, key: val}
4055 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004056 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4057 if (ret == NOTDONE)
4058 ret = get_dict_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004059 break;
4060
4061 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004062 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004064 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 break;
4066
4067 /*
4068 * Environment variable: $VAR.
4069 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004070 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 break;
4072
4073 /*
4074 * Register contents: @r.
4075 */
4076 case '@': ++*arg;
4077 if (evaluate)
4078 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004079 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004080 rettv->vval.v_string = get_reg_contents(**arg,
4081 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 }
4083 if (**arg != NUL)
4084 ++*arg;
4085 break;
4086
4087 /*
4088 * nested expression: (expression).
4089 */
4090 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004091 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 if (**arg == ')')
4093 ++*arg;
4094 else if (ret == OK)
4095 {
4096 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004097 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 ret = FAIL;
4099 }
4100 break;
4101
Bram Moolenaar8c711452005-01-14 21:53:12 +00004102 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 break;
4104 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004105
4106 if (ret == NOTDONE)
4107 {
4108 /*
4109 * Must be a variable or function name.
4110 * Can also be a curly-braces kind of name: {expr}.
4111 */
4112 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004113 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004114 if (alias != NULL)
4115 s = alias;
4116
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004117 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004118 ret = FAIL;
4119 else
4120 {
4121 if (**arg == '(') /* recursive! */
4122 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004123 partial_T *partial;
4124
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004125 if (!evaluate)
4126 check_vars(s, len);
4127
Bram Moolenaar8c711452005-01-14 21:53:12 +00004128 /* If "s" is the name of a variable of type VAR_FUNC
4129 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004130 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004131
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004132 /* Need to make a copy, in case evaluating the arguments makes
4133 * the name invalid. */
4134 s = vim_strsave(s);
4135 if (s == NULL)
4136 ret = FAIL;
4137 else
4138 /* Invoke the function. */
4139 ret = get_func_tv(s, len, rettv, arg,
4140 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4141 &len, evaluate, partial, NULL);
4142 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004143
4144 /* If evaluate is FALSE rettv->v_type was not set in
4145 * get_func_tv, but it's needed in handle_subscript() to parse
4146 * what follows. So set it here. */
4147 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4148 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004149 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004150 rettv->v_type = VAR_FUNC;
4151 }
4152
Bram Moolenaar8c711452005-01-14 21:53:12 +00004153 /* Stop the expression evaluation when immediately
4154 * aborting on error, or when an interrupt occurred or
4155 * an exception was thrown but not caught. */
4156 if (aborting())
4157 {
4158 if (ret == OK)
4159 clear_tv(rettv);
4160 ret = FAIL;
4161 }
4162 }
4163 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004164 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004165 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004166 {
4167 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004168 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004169 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004170 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004171 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004172 }
4173
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 *arg = skipwhite(*arg);
4175
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004176 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4177 * expr(expr). */
4178 if (ret == OK)
4179 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180
4181 /*
4182 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4183 */
4184 if (ret == OK && evaluate && end_leader > start_leader)
4185 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004186 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004187 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004188#ifdef FEAT_FLOAT
4189 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004190
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004191 if (rettv->v_type == VAR_FLOAT)
4192 f = rettv->vval.v_float;
4193 else
4194#endif
4195 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004196 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004198 clear_tv(rettv);
4199 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004201 else
4202 {
4203 while (end_leader > start_leader)
4204 {
4205 --end_leader;
4206 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004207 {
4208#ifdef FEAT_FLOAT
4209 if (rettv->v_type == VAR_FLOAT)
4210 f = !f;
4211 else
4212#endif
4213 val = !val;
4214 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004215 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004216 {
4217#ifdef FEAT_FLOAT
4218 if (rettv->v_type == VAR_FLOAT)
4219 f = -f;
4220 else
4221#endif
4222 val = -val;
4223 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004224 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004225#ifdef FEAT_FLOAT
4226 if (rettv->v_type == VAR_FLOAT)
4227 {
4228 clear_tv(rettv);
4229 rettv->vval.v_float = f;
4230 }
4231 else
4232#endif
4233 {
4234 clear_tv(rettv);
4235 rettv->v_type = VAR_NUMBER;
4236 rettv->vval.v_number = val;
4237 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 }
4240
4241 return ret;
4242}
4243
4244/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004245 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4246 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004247 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4248 */
4249 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004250eval_index(
4251 char_u **arg,
4252 typval_T *rettv,
4253 int evaluate,
4254 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004255{
4256 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004257 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004258 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004259 long len = -1;
4260 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004261 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004262 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004263
Bram Moolenaara03f2332016-02-06 18:09:59 +01004264 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004265 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004266 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004267 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004268 if (verbose)
4269 EMSG(_("E695: Cannot index a Funcref"));
4270 return FAIL;
4271 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004272#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004273 if (verbose)
4274 EMSG(_(e_float_as_string));
4275 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004276#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004277 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004278 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004279 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004280 if (verbose)
4281 EMSG(_("E909: Cannot index a special variable"));
4282 return FAIL;
4283 case VAR_UNKNOWN:
4284 if (evaluate)
4285 return FAIL;
4286 /* FALLTHROUGH */
4287
4288 case VAR_STRING:
4289 case VAR_NUMBER:
4290 case VAR_LIST:
4291 case VAR_DICT:
4292 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004293 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004294
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004295 init_tv(&var1);
4296 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004297 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004298 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004299 /*
4300 * dict.name
4301 */
4302 key = *arg + 1;
4303 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4304 ;
4305 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004306 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004307 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004308 }
4309 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004310 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004311 /*
4312 * something[idx]
4313 *
4314 * Get the (first) variable from inside the [].
4315 */
4316 *arg = skipwhite(*arg + 1);
4317 if (**arg == ':')
4318 empty1 = TRUE;
4319 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4320 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004321 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4322 {
4323 /* not a number or string */
4324 clear_tv(&var1);
4325 return FAIL;
4326 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004327
4328 /*
4329 * Get the second variable from inside the [:].
4330 */
4331 if (**arg == ':')
4332 {
4333 range = TRUE;
4334 *arg = skipwhite(*arg + 1);
4335 if (**arg == ']')
4336 empty2 = TRUE;
4337 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4338 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004339 if (!empty1)
4340 clear_tv(&var1);
4341 return FAIL;
4342 }
4343 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4344 {
4345 /* not a number or string */
4346 if (!empty1)
4347 clear_tv(&var1);
4348 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004349 return FAIL;
4350 }
4351 }
4352
4353 /* Check for the ']'. */
4354 if (**arg != ']')
4355 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004356 if (verbose)
4357 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004358 clear_tv(&var1);
4359 if (range)
4360 clear_tv(&var2);
4361 return FAIL;
4362 }
4363 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004364 }
4365
4366 if (evaluate)
4367 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004368 n1 = 0;
4369 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004370 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004371 n1 = get_tv_number(&var1);
4372 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004373 }
4374 if (range)
4375 {
4376 if (empty2)
4377 n2 = -1;
4378 else
4379 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004380 n2 = get_tv_number(&var2);
4381 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004382 }
4383 }
4384
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004385 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004386 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004387 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004388 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004389 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004390 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004391 case VAR_SPECIAL:
4392 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004393 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004394 break; /* not evaluating, skipping over subscript */
4395
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004396 case VAR_NUMBER:
4397 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004398 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004399 len = (long)STRLEN(s);
4400 if (range)
4401 {
4402 /* The resulting variable is a substring. If the indexes
4403 * are out of range the result is empty. */
4404 if (n1 < 0)
4405 {
4406 n1 = len + n1;
4407 if (n1 < 0)
4408 n1 = 0;
4409 }
4410 if (n2 < 0)
4411 n2 = len + n2;
4412 else if (n2 >= len)
4413 n2 = len;
4414 if (n1 >= len || n2 < 0 || n1 > n2)
4415 s = NULL;
4416 else
4417 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4418 }
4419 else
4420 {
4421 /* The resulting variable is a string of a single
4422 * character. If the index is too big or negative the
4423 * result is empty. */
4424 if (n1 >= len || n1 < 0)
4425 s = NULL;
4426 else
4427 s = vim_strnsave(s + n1, 1);
4428 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004429 clear_tv(rettv);
4430 rettv->v_type = VAR_STRING;
4431 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004432 break;
4433
4434 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004435 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004436 if (n1 < 0)
4437 n1 = len + n1;
4438 if (!empty1 && (n1 < 0 || n1 >= len))
4439 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004440 /* For a range we allow invalid values and return an empty
4441 * list. A list index out of range is an error. */
4442 if (!range)
4443 {
4444 if (verbose)
4445 EMSGN(_(e_listidx), n1);
4446 return FAIL;
4447 }
4448 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004449 }
4450 if (range)
4451 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004452 list_T *l;
4453 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004454
4455 if (n2 < 0)
4456 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004457 else if (n2 >= len)
4458 n2 = len - 1;
4459 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004460 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004461 l = list_alloc();
4462 if (l == NULL)
4463 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004464 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004465 n1 <= n2; ++n1)
4466 {
4467 if (list_append_tv(l, &item->li_tv) == FAIL)
4468 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004469 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004470 return FAIL;
4471 }
4472 item = item->li_next;
4473 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004474 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004475 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004476 }
4477 else
4478 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004479 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004480 clear_tv(rettv);
4481 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004482 }
4483 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004484
4485 case VAR_DICT:
4486 if (range)
4487 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004488 if (verbose)
4489 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004490 if (len == -1)
4491 clear_tv(&var1);
4492 return FAIL;
4493 }
4494 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004495 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004496
4497 if (len == -1)
4498 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02004499 key = get_tv_string_chk(&var1);
4500 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004501 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004502 clear_tv(&var1);
4503 return FAIL;
4504 }
4505 }
4506
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004507 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004508
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004509 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004510 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004511 if (len == -1)
4512 clear_tv(&var1);
4513 if (item == NULL)
4514 return FAIL;
4515
4516 copy_tv(&item->di_tv, &var1);
4517 clear_tv(rettv);
4518 *rettv = var1;
4519 }
4520 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004521 }
4522 }
4523
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004524 return OK;
4525}
4526
4527/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 * Get an option value.
4529 * "arg" points to the '&' or '+' before the option name.
4530 * "arg" is advanced to character after the option name.
4531 * Return OK or FAIL.
4532 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004533 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004534get_option_tv(
4535 char_u **arg,
4536 typval_T *rettv, /* when NULL, only check if option exists */
4537 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538{
4539 char_u *option_end;
4540 long numval;
4541 char_u *stringval;
4542 int opt_type;
4543 int c;
4544 int working = (**arg == '+'); /* has("+option") */
4545 int ret = OK;
4546 int opt_flags;
4547
4548 /*
4549 * Isolate the option name and find its value.
4550 */
4551 option_end = find_option_end(arg, &opt_flags);
4552 if (option_end == NULL)
4553 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004554 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 EMSG2(_("E112: Option name missing: %s"), *arg);
4556 return FAIL;
4557 }
4558
4559 if (!evaluate)
4560 {
4561 *arg = option_end;
4562 return OK;
4563 }
4564
4565 c = *option_end;
4566 *option_end = NUL;
4567 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004568 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569
4570 if (opt_type == -3) /* invalid name */
4571 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004572 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 EMSG2(_("E113: Unknown option: %s"), *arg);
4574 ret = FAIL;
4575 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004576 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 {
4578 if (opt_type == -2) /* hidden string option */
4579 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004580 rettv->v_type = VAR_STRING;
4581 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 }
4583 else if (opt_type == -1) /* hidden number option */
4584 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004585 rettv->v_type = VAR_NUMBER;
4586 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 }
4588 else if (opt_type == 1) /* number option */
4589 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004590 rettv->v_type = VAR_NUMBER;
4591 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 }
4593 else /* string option */
4594 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004595 rettv->v_type = VAR_STRING;
4596 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 }
4598 }
4599 else if (working && (opt_type == -2 || opt_type == -1))
4600 ret = FAIL;
4601
4602 *option_end = c; /* put back for error messages */
4603 *arg = option_end;
4604
4605 return ret;
4606}
4607
4608/*
4609 * Allocate a variable for a string constant.
4610 * Return OK or FAIL.
4611 */
4612 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004613get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614{
4615 char_u *p;
4616 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 int extra = 0;
4618
4619 /*
4620 * Find the end of the string, skipping backslashed characters.
4621 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004622 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 {
4624 if (*p == '\\' && p[1] != NUL)
4625 {
4626 ++p;
4627 /* A "\<x>" form occupies at least 4 characters, and produces up
4628 * to 6 characters: reserve space for 2 extra */
4629 if (*p == '<')
4630 extra += 2;
4631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 }
4633
4634 if (*p != '"')
4635 {
4636 EMSG2(_("E114: Missing quote: %s"), *arg);
4637 return FAIL;
4638 }
4639
4640 /* If only parsing, set *arg and return here */
4641 if (!evaluate)
4642 {
4643 *arg = p + 1;
4644 return OK;
4645 }
4646
4647 /*
4648 * Copy the string into allocated memory, handling backslashed
4649 * characters.
4650 */
4651 name = alloc((unsigned)(p - *arg + extra));
4652 if (name == NULL)
4653 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004654 rettv->v_type = VAR_STRING;
4655 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656
Bram Moolenaar8c711452005-01-14 21:53:12 +00004657 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 {
4659 if (*p == '\\')
4660 {
4661 switch (*++p)
4662 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004663 case 'b': *name++ = BS; ++p; break;
4664 case 'e': *name++ = ESC; ++p; break;
4665 case 'f': *name++ = FF; ++p; break;
4666 case 'n': *name++ = NL; ++p; break;
4667 case 'r': *name++ = CAR; ++p; break;
4668 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669
4670 case 'X': /* hex: "\x1", "\x12" */
4671 case 'x':
4672 case 'u': /* Unicode: "\u0023" */
4673 case 'U':
4674 if (vim_isxdigit(p[1]))
4675 {
4676 int n, nr;
4677 int c = toupper(*p);
4678
4679 if (c == 'X')
4680 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004681 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004683 else
4684 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 nr = 0;
4686 while (--n >= 0 && vim_isxdigit(p[1]))
4687 {
4688 ++p;
4689 nr = (nr << 4) + hex2nr(*p);
4690 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004691 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692#ifdef FEAT_MBYTE
4693 /* For "\u" store the number according to
4694 * 'encoding'. */
4695 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004696 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 else
4698#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004699 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 break;
4702
4703 /* octal: "\1", "\12", "\123" */
4704 case '0':
4705 case '1':
4706 case '2':
4707 case '3':
4708 case '4':
4709 case '5':
4710 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004711 case '7': *name = *p++ - '0';
4712 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004714 *name = (*name << 3) + *p++ - '0';
4715 if (*p >= '0' && *p <= '7')
4716 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004718 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 break;
4720
4721 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02004722 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 if (extra != 0)
4724 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004725 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 break;
4727 }
4728 /* FALLTHROUGH */
4729
Bram Moolenaar8c711452005-01-14 21:53:12 +00004730 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 break;
4732 }
4733 }
4734 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004735 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004738 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02004739 if (*p != NUL) /* just in case */
4740 ++p;
4741 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 return OK;
4744}
4745
4746/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004747 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 * Return OK or FAIL.
4749 */
4750 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004751get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752{
4753 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004754 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004755 int reduce = 0;
4756
4757 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004758 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004759 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004760 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004761 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004762 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004763 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004764 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004765 break;
4766 ++reduce;
4767 ++p;
4768 }
4769 }
4770
Bram Moolenaar8c711452005-01-14 21:53:12 +00004771 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004772 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004773 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004774 return FAIL;
4775 }
4776
Bram Moolenaar8c711452005-01-14 21:53:12 +00004777 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004778 if (!evaluate)
4779 {
4780 *arg = p + 1;
4781 return OK;
4782 }
4783
4784 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004785 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004786 */
4787 str = alloc((unsigned)((p - *arg) - reduce));
4788 if (str == NULL)
4789 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004790 rettv->v_type = VAR_STRING;
4791 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004792
Bram Moolenaar8c711452005-01-14 21:53:12 +00004793 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004794 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004795 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004796 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004797 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004798 break;
4799 ++p;
4800 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004801 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004802 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004803 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004804 *arg = p + 1;
4805
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004806 return OK;
4807}
4808
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004809/*
4810 * Return the function name of the partial.
4811 */
4812 char_u *
4813partial_name(partial_T *pt)
4814{
4815 if (pt->pt_name != NULL)
4816 return pt->pt_name;
4817 return pt->pt_func->uf_name;
4818}
4819
Bram Moolenaarddecc252016-04-06 22:59:37 +02004820 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004821partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004822{
4823 int i;
4824
4825 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004826 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004827 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004828 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004829 if (pt->pt_name != NULL)
4830 {
4831 func_unref(pt->pt_name);
4832 vim_free(pt->pt_name);
4833 }
4834 else
4835 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004836 vim_free(pt);
4837}
4838
4839/*
4840 * Unreference a closure: decrement the reference count and free it when it
4841 * becomes zero.
4842 */
4843 void
4844partial_unref(partial_T *pt)
4845{
4846 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004847 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004848}
4849
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004850static int tv_equal_recurse_limit;
4851
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004852 static int
4853func_equal(
4854 typval_T *tv1,
4855 typval_T *tv2,
4856 int ic) /* ignore case */
4857{
4858 char_u *s1, *s2;
4859 dict_T *d1, *d2;
4860 int a1, a2;
4861 int i;
4862
4863 /* empty and NULL function name considered the same */
4864 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004865 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004866 if (s1 != NULL && *s1 == NUL)
4867 s1 = NULL;
4868 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004869 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004870 if (s2 != NULL && *s2 == NUL)
4871 s2 = NULL;
4872 if (s1 == NULL || s2 == NULL)
4873 {
4874 if (s1 != s2)
4875 return FALSE;
4876 }
4877 else if (STRCMP(s1, s2) != 0)
4878 return FALSE;
4879
4880 /* empty dict and NULL dict is different */
4881 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
4882 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
4883 if (d1 == NULL || d2 == NULL)
4884 {
4885 if (d1 != d2)
4886 return FALSE;
4887 }
4888 else if (!dict_equal(d1, d2, ic, TRUE))
4889 return FALSE;
4890
4891 /* empty list and no list considered the same */
4892 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
4893 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
4894 if (a1 != a2)
4895 return FALSE;
4896 for (i = 0; i < a1; ++i)
4897 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
4898 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
4899 return FALSE;
4900
4901 return TRUE;
4902}
4903
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004904/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004905 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004906 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004907 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004908 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004909 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004910tv_equal(
4911 typval_T *tv1,
4912 typval_T *tv2,
4913 int ic, /* ignore case */
4914 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004915{
4916 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004917 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004918 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00004919 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004920
Bram Moolenaar8b402a02006-10-17 13:16:39 +00004921 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004922 * recursiveness to a limit. We guess they are equal then.
4923 * A fixed limit has the problem of still taking an awful long time.
4924 * Reduce the limit every time running into it. That should work fine for
4925 * deeply linked structures that are not recursively linked and catch
4926 * recursiveness quickly. */
4927 if (!recursive)
4928 tv_equal_recurse_limit = 1000;
4929 if (recursive_cnt >= tv_equal_recurse_limit)
4930 {
4931 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00004932 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004933 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004934
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02004935 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
4936 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004937 if ((tv1->v_type == VAR_FUNC
4938 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
4939 && (tv2->v_type == VAR_FUNC
4940 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
4941 {
4942 ++recursive_cnt;
4943 r = func_equal(tv1, tv2, ic);
4944 --recursive_cnt;
4945 return r;
4946 }
4947
4948 if (tv1->v_type != tv2->v_type)
4949 return FALSE;
4950
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004951 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004952 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004953 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004954 ++recursive_cnt;
4955 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
4956 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00004957 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004958
4959 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004960 ++recursive_cnt;
4961 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
4962 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00004963 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004964
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004965 case VAR_NUMBER:
4966 return tv1->vval.v_number == tv2->vval.v_number;
4967
4968 case VAR_STRING:
4969 s1 = get_tv_string_buf(tv1, buf1);
4970 s2 = get_tv_string_buf(tv2, buf2);
4971 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004972
4973 case VAR_SPECIAL:
4974 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01004975
4976 case VAR_FLOAT:
4977#ifdef FEAT_FLOAT
4978 return tv1->vval.v_float == tv2->vval.v_float;
4979#endif
4980 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004981#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01004982 return tv1->vval.v_job == tv2->vval.v_job;
4983#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01004984 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004985#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01004986 return tv1->vval.v_channel == tv2->vval.v_channel;
4987#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004988 case VAR_FUNC:
4989 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004990 case VAR_UNKNOWN:
4991 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004992 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004993
Bram Moolenaara03f2332016-02-06 18:09:59 +01004994 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
4995 * does not equal anything, not even itself. */
4996 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004997}
4998
4999/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005000 * Return the next (unique) copy ID.
5001 * Used for serializing nested structures.
5002 */
5003 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005004get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005005{
5006 current_copyID += COPYID_INC;
5007 return current_copyID;
5008}
5009
5010/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005011 * Garbage collection for lists and dictionaries.
5012 *
5013 * We use reference counts to be able to free most items right away when they
5014 * are no longer used. But for composite items it's possible that it becomes
5015 * unused while the reference count is > 0: When there is a recursive
5016 * reference. Example:
5017 * :let l = [1, 2, 3]
5018 * :let d = {9: l}
5019 * :let l[1] = d
5020 *
5021 * Since this is quite unusual we handle this with garbage collection: every
5022 * once in a while find out which lists and dicts are not referenced from any
5023 * variable.
5024 *
5025 * Here is a good reference text about garbage collection (refers to Python
5026 * but it applies to all reference-counting mechanisms):
5027 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005028 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005029
5030/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005031 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005032 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005033 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005034 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005035 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005036garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005037{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005038 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005039 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005040 buf_T *buf;
5041 win_T *wp;
5042 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005043 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005044 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005045
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005046 if (!testing)
5047 {
5048 /* Only do this once. */
5049 want_garbage_collect = FALSE;
5050 may_garbage_collect = FALSE;
5051 garbage_collect_at_exit = FALSE;
5052 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005053
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005054 /* We advance by two because we add one for items referenced through
5055 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005056 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005057
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005058 /*
5059 * 1. Go through all accessible variables and mark all lists and dicts
5060 * with copyID.
5061 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005062
5063 /* Don't free variables in the previous_funccal list unless they are only
5064 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005065 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005066 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005067
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005068 /* script-local variables */
5069 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005070 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005071
5072 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005073 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005074 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5075 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005076
5077 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005078 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005079 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5080 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005081 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005082 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5083 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005084
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005085 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005086 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005087 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5088 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005089 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005090 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005091
5092 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005093 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005094
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005095 /* named functions (matters for closures) */
5096 abort = abort || set_ref_in_functions(copyID);
5097
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005098 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005099 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005100
Bram Moolenaard812df62008-11-09 12:46:09 +00005101 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005102 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005103
Bram Moolenaar1dced572012-04-05 16:54:08 +02005104#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005105 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005106#endif
5107
Bram Moolenaardb913952012-06-29 12:54:53 +02005108#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005109 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005110#endif
5111
5112#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005113 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005114#endif
5115
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005116#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005117 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005118 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005119#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005120#ifdef FEAT_NETBEANS_INTG
5121 abort = abort || set_ref_in_nb_channel(copyID);
5122#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005123
Bram Moolenaare3188e22016-05-31 21:13:04 +02005124#ifdef FEAT_TIMERS
5125 abort = abort || set_ref_in_timer(copyID);
5126#endif
5127
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005128#ifdef FEAT_QUICKFIX
5129 abort = abort || set_ref_in_quickfix(copyID);
5130#endif
5131
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005132#ifdef FEAT_TERMINAL
5133 abort = abort || set_ref_in_term(copyID);
5134#endif
5135
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005136 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005137 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005138 /*
5139 * 2. Free lists and dictionaries that are not referenced.
5140 */
5141 did_free = free_unref_items(copyID);
5142
5143 /*
5144 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005145 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005146 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005147 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005148 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005149 else if (p_verbose > 0)
5150 {
5151 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
5152 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005153
5154 return did_free;
5155}
5156
5157/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005158 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005159 */
5160 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005161free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005162{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005163 int did_free = FALSE;
5164
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005165 /* Let all "free" functions know that we are here. This means no
5166 * dictionaries, lists, channels or jobs are to be freed, because we will
5167 * do that here. */
5168 in_free_unref_items = TRUE;
5169
5170 /*
5171 * PASS 1: free the contents of the items. We don't free the items
5172 * themselves yet, so that it is possible to decrement refcount counters
5173 */
5174
Bram Moolenaarcd524592016-07-17 14:57:05 +02005175 /* Go through the list of dicts and free items without the copyID. */
5176 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005177
Bram Moolenaarda861d62016-07-17 15:46:27 +02005178 /* Go through the list of lists and free items without the copyID. */
5179 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005180
5181#ifdef FEAT_JOB_CHANNEL
5182 /* Go through the list of jobs and free items without the copyID. This
5183 * must happen before doing channels, because jobs refer to channels, but
5184 * the reference from the channel to the job isn't tracked. */
5185 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5186
5187 /* Go through the list of channels and free items without the copyID. */
5188 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5189#endif
5190
5191 /*
5192 * PASS 2: free the items themselves.
5193 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005194 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005195 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005196
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005197#ifdef FEAT_JOB_CHANNEL
5198 /* Go through the list of jobs and free items without the copyID. This
5199 * must happen before doing channels, because jobs refer to channels, but
5200 * the reference from the channel to the job isn't tracked. */
5201 free_unused_jobs(copyID, COPYID_MASK);
5202
5203 /* Go through the list of channels and free items without the copyID. */
5204 free_unused_channels(copyID, COPYID_MASK);
5205#endif
5206
5207 in_free_unref_items = FALSE;
5208
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005209 return did_free;
5210}
5211
5212/*
5213 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005214 * "list_stack" is used to add lists to be marked. Can be NULL.
5215 *
5216 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005217 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005218 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005219set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005220{
5221 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005222 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005223 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005224 hashtab_T *cur_ht;
5225 ht_stack_T *ht_stack = NULL;
5226 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005227
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005228 cur_ht = ht;
5229 for (;;)
5230 {
5231 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005232 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005233 /* Mark each item in the hashtab. If the item contains a hashtab
5234 * it is added to ht_stack, if it contains a list it is added to
5235 * list_stack. */
5236 todo = (int)cur_ht->ht_used;
5237 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5238 if (!HASHITEM_EMPTY(hi))
5239 {
5240 --todo;
5241 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5242 &ht_stack, list_stack);
5243 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005244 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005245
5246 if (ht_stack == NULL)
5247 break;
5248
5249 /* take an item from the stack */
5250 cur_ht = ht_stack->ht;
5251 tempitem = ht_stack;
5252 ht_stack = ht_stack->prev;
5253 free(tempitem);
5254 }
5255
5256 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005257}
5258
5259/*
5260 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005261 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5262 *
5263 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005264 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005265 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005266set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005267{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005268 listitem_T *li;
5269 int abort = FALSE;
5270 list_T *cur_l;
5271 list_stack_T *list_stack = NULL;
5272 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005273
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005274 cur_l = l;
5275 for (;;)
5276 {
5277 if (!abort)
5278 /* Mark each item in the list. If the item contains a hashtab
5279 * it is added to ht_stack, if it contains a list it is added to
5280 * list_stack. */
5281 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5282 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5283 ht_stack, &list_stack);
5284 if (list_stack == NULL)
5285 break;
5286
5287 /* take an item from the stack */
5288 cur_l = list_stack->list;
5289 tempitem = list_stack;
5290 list_stack = list_stack->prev;
5291 free(tempitem);
5292 }
5293
5294 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005295}
5296
5297/*
5298 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005299 * "list_stack" is used to add lists to be marked. Can be NULL.
5300 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5301 *
5302 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005303 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005304 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005305set_ref_in_item(
5306 typval_T *tv,
5307 int copyID,
5308 ht_stack_T **ht_stack,
5309 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005310{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005311 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005312
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005313 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005314 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005315 dict_T *dd = tv->vval.v_dict;
5316
Bram Moolenaara03f2332016-02-06 18:09:59 +01005317 if (dd != NULL && dd->dv_copyID != copyID)
5318 {
5319 /* Didn't see this dict yet. */
5320 dd->dv_copyID = copyID;
5321 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005322 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005323 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5324 }
5325 else
5326 {
5327 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5328 if (newitem == NULL)
5329 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005330 else
5331 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005332 newitem->ht = &dd->dv_hashtab;
5333 newitem->prev = *ht_stack;
5334 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005335 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005336 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005337 }
5338 }
5339 else if (tv->v_type == VAR_LIST)
5340 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005341 list_T *ll = tv->vval.v_list;
5342
Bram Moolenaara03f2332016-02-06 18:09:59 +01005343 if (ll != NULL && ll->lv_copyID != copyID)
5344 {
5345 /* Didn't see this list yet. */
5346 ll->lv_copyID = copyID;
5347 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005348 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005349 abort = set_ref_in_list(ll, copyID, ht_stack);
5350 }
5351 else
5352 {
5353 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005354 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005355 if (newitem == NULL)
5356 abort = TRUE;
5357 else
5358 {
5359 newitem->list = ll;
5360 newitem->prev = *list_stack;
5361 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005362 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005363 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005364 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005365 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005366 else if (tv->v_type == VAR_FUNC)
5367 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005368 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005369 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005370 else if (tv->v_type == VAR_PARTIAL)
5371 {
5372 partial_T *pt = tv->vval.v_partial;
5373 int i;
5374
5375 /* A partial does not have a copyID, because it cannot contain itself.
5376 */
5377 if (pt != NULL)
5378 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005379 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005380
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005381 if (pt->pt_dict != NULL)
5382 {
5383 typval_T dtv;
5384
5385 dtv.v_type = VAR_DICT;
5386 dtv.vval.v_dict = pt->pt_dict;
5387 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5388 }
5389
5390 for (i = 0; i < pt->pt_argc; ++i)
5391 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5392 ht_stack, list_stack);
5393 }
5394 }
5395#ifdef FEAT_JOB_CHANNEL
5396 else if (tv->v_type == VAR_JOB)
5397 {
5398 job_T *job = tv->vval.v_job;
5399 typval_T dtv;
5400
5401 if (job != NULL && job->jv_copyID != copyID)
5402 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005403 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005404 if (job->jv_channel != NULL)
5405 {
5406 dtv.v_type = VAR_CHANNEL;
5407 dtv.vval.v_channel = job->jv_channel;
5408 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5409 }
5410 if (job->jv_exit_partial != NULL)
5411 {
5412 dtv.v_type = VAR_PARTIAL;
5413 dtv.vval.v_partial = job->jv_exit_partial;
5414 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5415 }
5416 }
5417 }
5418 else if (tv->v_type == VAR_CHANNEL)
5419 {
5420 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005421 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005422 typval_T dtv;
5423 jsonq_T *jq;
5424 cbq_T *cq;
5425
5426 if (ch != NULL && ch->ch_copyID != copyID)
5427 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005428 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005429 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005430 {
5431 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5432 jq = jq->jq_next)
5433 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5434 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5435 cq = cq->cq_next)
5436 if (cq->cq_partial != NULL)
5437 {
5438 dtv.v_type = VAR_PARTIAL;
5439 dtv.vval.v_partial = cq->cq_partial;
5440 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5441 }
5442 if (ch->ch_part[part].ch_partial != NULL)
5443 {
5444 dtv.v_type = VAR_PARTIAL;
5445 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5446 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5447 }
5448 }
5449 if (ch->ch_partial != NULL)
5450 {
5451 dtv.v_type = VAR_PARTIAL;
5452 dtv.vval.v_partial = ch->ch_partial;
5453 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5454 }
5455 if (ch->ch_close_partial != NULL)
5456 {
5457 dtv.v_type = VAR_PARTIAL;
5458 dtv.vval.v_partial = ch->ch_close_partial;
5459 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5460 }
5461 }
5462 }
5463#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005464 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005465}
5466
Bram Moolenaar17a13432016-01-24 14:22:10 +01005467 static char *
5468get_var_special_name(int nr)
5469{
5470 switch (nr)
5471 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005472 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005473 case VVAL_TRUE: return "v:true";
5474 case VVAL_NONE: return "v:none";
5475 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005476 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01005477 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01005478 return "42";
5479}
5480
Bram Moolenaar8c711452005-01-14 21:53:12 +00005481/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005482 * Return a string with the string representation of a variable.
5483 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005484 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005485 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005486 * When both "echo_style" and "composite_val" are FALSE, put quotes around
5487 * stings as "string()", otherwise does not put quotes around strings, as
5488 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005489 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5490 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005491 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005492 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005493 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005494echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005495 typval_T *tv,
5496 char_u **tofree,
5497 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005498 int copyID,
5499 int echo_style,
5500 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005501 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005502{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005503 static int recurse = 0;
5504 char_u *r = NULL;
5505
Bram Moolenaar33570922005-01-25 22:26:29 +00005506 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005507 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005508 if (!did_echo_string_emsg)
5509 {
5510 /* Only give this message once for a recursive call to avoid
5511 * flooding the user with errors. And stop iterating over lists
5512 * and dicts. */
5513 did_echo_string_emsg = TRUE;
5514 EMSG(_("E724: variable nested too deep for displaying"));
5515 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005516 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005517 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005518 }
5519 ++recurse;
5520
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005521 switch (tv->v_type)
5522 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005523 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005524 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005525 {
5526 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005527 r = tv->vval.v_string;
5528 if (r == NULL)
5529 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005530 }
5531 else
5532 {
5533 *tofree = string_quote(tv->vval.v_string, FALSE);
5534 r = *tofree;
5535 }
5536 break;
5537
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005538 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005539 if (echo_style)
5540 {
5541 *tofree = NULL;
5542 r = tv->vval.v_string;
5543 }
5544 else
5545 {
5546 *tofree = string_quote(tv->vval.v_string, TRUE);
5547 r = *tofree;
5548 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005549 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005550
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005551 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005552 {
5553 partial_T *pt = tv->vval.v_partial;
5554 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005555 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005556 garray_T ga;
5557 int i;
5558 char_u *tf;
5559
5560 ga_init2(&ga, 1, 100);
5561 ga_concat(&ga, (char_u *)"function(");
5562 if (fname != NULL)
5563 {
5564 ga_concat(&ga, fname);
5565 vim_free(fname);
5566 }
5567 if (pt != NULL && pt->pt_argc > 0)
5568 {
5569 ga_concat(&ga, (char_u *)", [");
5570 for (i = 0; i < pt->pt_argc; ++i)
5571 {
5572 if (i > 0)
5573 ga_concat(&ga, (char_u *)", ");
5574 ga_concat(&ga,
5575 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5576 vim_free(tf);
5577 }
5578 ga_concat(&ga, (char_u *)"]");
5579 }
5580 if (pt != NULL && pt->pt_dict != NULL)
5581 {
5582 typval_T dtv;
5583
5584 ga_concat(&ga, (char_u *)", ");
5585 dtv.v_type = VAR_DICT;
5586 dtv.vval.v_dict = pt->pt_dict;
5587 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5588 vim_free(tf);
5589 }
5590 ga_concat(&ga, (char_u *)")");
5591
5592 *tofree = ga.ga_data;
5593 r = *tofree;
5594 break;
5595 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005596
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005597 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005598 if (tv->vval.v_list == NULL)
5599 {
5600 *tofree = NULL;
5601 r = NULL;
5602 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005603 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5604 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005605 {
5606 *tofree = NULL;
5607 r = (char_u *)"[...]";
5608 }
5609 else
5610 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005611 int old_copyID = tv->vval.v_list->lv_copyID;
5612
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005613 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005614 *tofree = list2string(tv, copyID, restore_copyID);
5615 if (restore_copyID)
5616 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005617 r = *tofree;
5618 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005619 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005620
Bram Moolenaar8c711452005-01-14 21:53:12 +00005621 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005622 if (tv->vval.v_dict == NULL)
5623 {
5624 *tofree = NULL;
5625 r = NULL;
5626 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005627 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5628 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005629 {
5630 *tofree = NULL;
5631 r = (char_u *)"{...}";
5632 }
5633 else
5634 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005635 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005636 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005637 *tofree = dict2string(tv, copyID, restore_copyID);
5638 if (restore_copyID)
5639 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005640 r = *tofree;
5641 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005642 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005643
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005644 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005645 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005646 *tofree = NULL;
5647 r = get_tv_string_buf(tv, numbuf);
5648 break;
5649
Bram Moolenaar835dc632016-02-07 14:27:38 +01005650 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005651 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005652 *tofree = NULL;
5653 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005654 if (composite_val)
5655 {
5656 *tofree = string_quote(r, FALSE);
5657 r = *tofree;
5658 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005659 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005660
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005661 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005662#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005663 *tofree = NULL;
5664 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5665 r = numbuf;
5666 break;
5667#endif
5668
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005669 case VAR_SPECIAL:
5670 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005671 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005672 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005673 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005674
Bram Moolenaar8502c702014-06-17 12:51:16 +02005675 if (--recurse == 0)
5676 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005677 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005678}
5679
5680/*
5681 * Return a string with the string representation of a variable.
5682 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5683 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005684 * Does not put quotes around strings, as ":echo" displays values.
5685 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5686 * May return NULL.
5687 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005688 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005689echo_string(
5690 typval_T *tv,
5691 char_u **tofree,
5692 char_u *numbuf,
5693 int copyID)
5694{
5695 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5696}
5697
5698/*
5699 * Return a string with the string representation of a variable.
5700 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5701 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005702 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005703 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005704 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02005705 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005706tv2string(
5707 typval_T *tv,
5708 char_u **tofree,
5709 char_u *numbuf,
5710 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005711{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005712 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005713}
5714
5715/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005716 * Return string "str" in ' quotes, doubling ' characters.
5717 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005718 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005719 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005720 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005721string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005722{
Bram Moolenaar33570922005-01-25 22:26:29 +00005723 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005724 char_u *p, *r, *s;
5725
Bram Moolenaar33570922005-01-25 22:26:29 +00005726 len = (function ? 13 : 3);
5727 if (str != NULL)
5728 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005729 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005730 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005731 if (*p == '\'')
5732 ++len;
5733 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005734 s = r = alloc(len);
5735 if (r != NULL)
5736 {
5737 if (function)
5738 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005739 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005740 r += 10;
5741 }
5742 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005743 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005744 if (str != NULL)
5745 for (p = str; *p != NUL; )
5746 {
5747 if (*p == '\'')
5748 *r++ = '\'';
5749 MB_COPY_CHAR(p, r);
5750 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005751 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005752 if (function)
5753 *r++ = ')';
5754 *r++ = NUL;
5755 }
5756 return s;
5757}
5758
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005759#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005760/*
5761 * Convert the string "text" to a floating point number.
5762 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5763 * this always uses a decimal point.
5764 * Returns the length of the text that was consumed.
5765 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005766 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005767string2float(
5768 char_u *text,
5769 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005770{
5771 char *s = (char *)text;
5772 float_T f;
5773
Bram Moolenaar62473612017-01-08 19:25:40 +01005774 /* MS-Windows does not deal with "inf" and "nan" properly. */
5775 if (STRNICMP(text, "inf", 3) == 0)
5776 {
5777 *value = INFINITY;
5778 return 3;
5779 }
5780 if (STRNICMP(text, "-inf", 3) == 0)
5781 {
5782 *value = -INFINITY;
5783 return 4;
5784 }
5785 if (STRNICMP(text, "nan", 3) == 0)
5786 {
5787 *value = NAN;
5788 return 3;
5789 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005790 f = strtod(s, &s);
5791 *value = f;
5792 return (int)((char_u *)s - text);
5793}
5794#endif
5795
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005796/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 * Get the value of an environment variable.
5798 * "arg" is pointing to the '$'. It is advanced to after the name.
5799 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005800 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 */
5802 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005803get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804{
5805 char_u *string = NULL;
5806 int len;
5807 int cc;
5808 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005809 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810
5811 ++*arg;
5812 name = *arg;
5813 len = get_env_len(arg);
5814 if (evaluate)
5815 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005816 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01005817 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005818
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005819 cc = name[len];
5820 name[len] = NUL;
5821 /* first try vim_getenv(), fast for normal environment vars */
5822 string = vim_getenv(name, &mustfree);
5823 if (string != NULL && *string != NUL)
5824 {
5825 if (!mustfree)
5826 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005828 else
5829 {
5830 if (mustfree)
5831 vim_free(string);
5832
5833 /* next try expanding things like $VIM and ${HOME} */
5834 string = expand_env_save(name - 1);
5835 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01005836 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005837 }
5838 name[len] = cc;
5839
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005840 rettv->v_type = VAR_STRING;
5841 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 }
5843
5844 return OK;
5845}
5846
Bram Moolenaard6e256c2011-12-14 15:32:50 +01005847
5848
5849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005851 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005853 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005854var2fpos(
5855 typval_T *varp,
5856 int dollar_lnum, /* TRUE when $ is last line */
5857 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005859 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005861 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862
Bram Moolenaara5525202006-03-02 22:52:09 +00005863 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005864 if (varp->v_type == VAR_LIST)
5865 {
5866 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005867 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005868 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005869 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005870
5871 l = varp->vval.v_list;
5872 if (l == NULL)
5873 return NULL;
5874
5875 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00005876 pos.lnum = list_find_nr(l, 0L, &error);
5877 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005878 return NULL; /* invalid line number */
5879
5880 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00005881 pos.col = list_find_nr(l, 1L, &error);
5882 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005883 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005884 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005885
5886 /* We accept "$" for the column number: last column. */
5887 li = list_find(l, 1L);
5888 if (li != NULL && li->li_tv.v_type == VAR_STRING
5889 && li->li_tv.vval.v_string != NULL
5890 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
5891 pos.col = len + 1;
5892
Bram Moolenaara5525202006-03-02 22:52:09 +00005893 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005894 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005895 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00005896 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005897
Bram Moolenaara5525202006-03-02 22:52:09 +00005898#ifdef FEAT_VIRTUALEDIT
5899 /* Get the virtual offset. Defaults to zero. */
5900 pos.coladd = list_find_nr(l, 2L, &error);
5901 if (error)
5902 pos.coladd = 0;
5903#endif
5904
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005905 return &pos;
5906 }
5907
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005908 name = get_tv_string_chk(varp);
5909 if (name == NULL)
5910 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005911 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005913 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
5914 {
5915 if (VIsual_active)
5916 return &VIsual;
5917 return &curwin->w_cursor;
5918 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005919 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005921 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5923 return NULL;
5924 return pp;
5925 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005926
5927#ifdef FEAT_VIRTUALEDIT
5928 pos.coladd = 0;
5929#endif
5930
Bram Moolenaar477933c2007-07-17 14:32:23 +00005931 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005932 {
5933 pos.col = 0;
5934 if (name[1] == '0') /* "w0": first visible line */
5935 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005936 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005937 /* In silent Ex mode topline is zero, but that's not a valid line
5938 * number; use one instead. */
5939 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005940 return &pos;
5941 }
5942 else if (name[1] == '$') /* "w$": last visible line */
5943 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005944 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005945 /* In silent Ex mode botline is zero, return zero then. */
5946 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005947 return &pos;
5948 }
5949 }
5950 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005952 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 {
5954 pos.lnum = curbuf->b_ml.ml_line_count;
5955 pos.col = 0;
5956 }
5957 else
5958 {
5959 pos.lnum = curwin->w_cursor.lnum;
5960 pos.col = (colnr_T)STRLEN(ml_get_curline());
5961 }
5962 return &pos;
5963 }
5964 return NULL;
5965}
5966
5967/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005968 * Convert list in "arg" into a position and optional file number.
5969 * When "fnump" is NULL there is no file number, only 3 items.
5970 * Note that the column is passed on as-is, the caller may want to decrement
5971 * it to use 1 for the first column.
5972 * Return FAIL when conversion is not possible, doesn't check the position for
5973 * validity.
5974 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005975 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005976list2fpos(
5977 typval_T *arg,
5978 pos_T *posp,
5979 int *fnump,
5980 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005981{
5982 list_T *l = arg->vval.v_list;
5983 long i = 0;
5984 long n;
5985
Bram Moolenaar493c1782014-05-28 14:34:46 +02005986 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5987 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00005988 if (arg->v_type != VAR_LIST
5989 || l == NULL
5990 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005991 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005992 return FAIL;
5993
5994 if (fnump != NULL)
5995 {
5996 n = list_find_nr(l, i++, NULL); /* fnum */
5997 if (n < 0)
5998 return FAIL;
5999 if (n == 0)
6000 n = curbuf->b_fnum; /* current buffer */
6001 *fnump = n;
6002 }
6003
6004 n = list_find_nr(l, i++, NULL); /* lnum */
6005 if (n < 0)
6006 return FAIL;
6007 posp->lnum = n;
6008
6009 n = list_find_nr(l, i++, NULL); /* col */
6010 if (n < 0)
6011 return FAIL;
6012 posp->col = n;
6013
6014#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +02006015 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006016 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006017 posp->coladd = 0;
6018 else
6019 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006020#endif
6021
Bram Moolenaar493c1782014-05-28 14:34:46 +02006022 if (curswantp != NULL)
6023 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6024
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006025 return OK;
6026}
6027
6028/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 * Get the length of an environment variable name.
6030 * Advance "arg" to the first character after the name.
6031 * Return 0 for error.
6032 */
6033 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006034get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035{
6036 char_u *p;
6037 int len;
6038
6039 for (p = *arg; vim_isIDc(*p); ++p)
6040 ;
6041 if (p == *arg) /* no name found */
6042 return 0;
6043
6044 len = (int)(p - *arg);
6045 *arg = p;
6046 return len;
6047}
6048
6049/*
6050 * Get the length of the name of a function or internal variable.
6051 * "arg" is advanced to the first non-white character after the name.
6052 * Return 0 if something is wrong.
6053 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006054 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006055get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056{
6057 char_u *p;
6058 int len;
6059
6060 /* Find the end of the name. */
6061 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006062 {
6063 if (*p == ':')
6064 {
6065 /* "s:" is start of "s:var", but "n:" is not and can be used in
6066 * slice "[n:]". Also "xx:" is not a namespace. */
6067 len = (int)(p - *arg);
6068 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6069 || len > 1)
6070 break;
6071 }
6072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 if (p == *arg) /* no name found */
6074 return 0;
6075
6076 len = (int)(p - *arg);
6077 *arg = skipwhite(p);
6078
6079 return len;
6080}
6081
6082/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006083 * Get the length of the name of a variable or function.
6084 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006086 * Return -1 if curly braces expansion failed.
6087 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 * If the name contains 'magic' {}'s, expand them and return the
6089 * expanded name in an allocated string via 'alias' - caller must free.
6090 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006091 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006092get_name_len(
6093 char_u **arg,
6094 char_u **alias,
6095 int evaluate,
6096 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097{
6098 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 char_u *p;
6100 char_u *expr_start;
6101 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102
6103 *alias = NULL; /* default to no alias */
6104
6105 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6106 && (*arg)[2] == (int)KE_SNR)
6107 {
6108 /* hard coded <SNR>, already translated */
6109 *arg += 3;
6110 return get_id_len(arg) + 3;
6111 }
6112 len = eval_fname_script(*arg);
6113 if (len > 0)
6114 {
6115 /* literal "<SID>", "s:" or "<SNR>" */
6116 *arg += len;
6117 }
6118
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006120 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006122 p = find_name_end(*arg, &expr_start, &expr_end,
6123 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 if (expr_start != NULL)
6125 {
6126 char_u *temp_string;
6127
6128 if (!evaluate)
6129 {
6130 len += (int)(p - *arg);
6131 *arg = skipwhite(p);
6132 return len;
6133 }
6134
6135 /*
6136 * Include any <SID> etc in the expanded string:
6137 * Thus the -len here.
6138 */
6139 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6140 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006141 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 *alias = temp_string;
6143 *arg = skipwhite(p);
6144 return (int)STRLEN(temp_string);
6145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146
6147 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006148 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 EMSG2(_(e_invexpr2), *arg);
6150
6151 return len;
6152}
6153
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006154/*
6155 * Find the end of a variable or function name, taking care of magic braces.
6156 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6157 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006158 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006159 * Return a pointer to just after the name. Equal to "arg" if there is no
6160 * valid name.
6161 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006162 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006163find_name_end(
6164 char_u *arg,
6165 char_u **expr_start,
6166 char_u **expr_end,
6167 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006169 int mb_nest = 0;
6170 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006172 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006174 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006176 *expr_start = NULL;
6177 *expr_end = NULL;
6178 }
6179
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006180 /* Quick check for valid starting character. */
6181 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6182 return arg;
6183
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006184 for (p = arg; *p != NUL
6185 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006186 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006187 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006188 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006189 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006190 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006191 if (*p == '\'')
6192 {
6193 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006194 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006195 ;
6196 if (*p == NUL)
6197 break;
6198 }
6199 else if (*p == '"')
6200 {
6201 /* skip over "str\"ing" 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 if (*p == '\\' && p[1] != NUL)
6204 ++p;
6205 if (*p == NUL)
6206 break;
6207 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006208 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6209 {
6210 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006211 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006212 len = (int)(p - arg);
6213 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006214 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006215 break;
6216 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006217
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006218 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006220 if (*p == '[')
6221 ++br_nest;
6222 else if (*p == ']')
6223 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006225
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006226 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006228 if (*p == '{')
6229 {
6230 mb_nest++;
6231 if (expr_start != NULL && *expr_start == NULL)
6232 *expr_start = p;
6233 }
6234 else if (*p == '}')
6235 {
6236 mb_nest--;
6237 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6238 *expr_end = p;
6239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 }
6242
6243 return p;
6244}
6245
6246/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006247 * Expands out the 'magic' {}'s in a variable/function name.
6248 * Note that this can call itself recursively, to deal with
6249 * constructs like foo{bar}{baz}{bam}
6250 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6251 * "in_start" ^
6252 * "expr_start" ^
6253 * "expr_end" ^
6254 * "in_end" ^
6255 *
6256 * Returns a new allocated string, which the caller must free.
6257 * Returns NULL for failure.
6258 */
6259 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006260make_expanded_name(
6261 char_u *in_start,
6262 char_u *expr_start,
6263 char_u *expr_end,
6264 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006265{
6266 char_u c1;
6267 char_u *retval = NULL;
6268 char_u *temp_result;
6269 char_u *nextcmd = NULL;
6270
6271 if (expr_end == NULL || in_end == NULL)
6272 return NULL;
6273 *expr_start = NUL;
6274 *expr_end = NUL;
6275 c1 = *in_end;
6276 *in_end = NUL;
6277
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006278 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006279 if (temp_result != NULL && nextcmd == NULL)
6280 {
6281 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6282 + (in_end - expr_end) + 1));
6283 if (retval != NULL)
6284 {
6285 STRCPY(retval, in_start);
6286 STRCAT(retval, temp_result);
6287 STRCAT(retval, expr_end + 1);
6288 }
6289 }
6290 vim_free(temp_result);
6291
6292 *in_end = c1; /* put char back for error messages */
6293 *expr_start = '{';
6294 *expr_end = '}';
6295
6296 if (retval != NULL)
6297 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006298 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006299 if (expr_start != NULL)
6300 {
6301 /* Further expansion! */
6302 temp_result = make_expanded_name(retval, expr_start,
6303 expr_end, temp_result);
6304 vim_free(retval);
6305 retval = temp_result;
6306 }
6307 }
6308
6309 return retval;
6310}
6311
6312/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006314 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006316 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006317eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006319 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6320}
6321
6322/*
6323 * Return TRUE if character "c" can be used as the first character in a
6324 * variable or function name (excluding '{' and '}').
6325 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006326 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006327eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006328{
6329 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330}
6331
6332/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 * Set number v: variable to "val".
6334 */
6335 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006336set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006338 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339}
6340
6341/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006342 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006344 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006345get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006347 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348}
6349
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006350/*
6351 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01006352 * If the String variable has never been set, return an empty string.
6353 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006354 */
6355 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006356get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006357{
6358 return get_tv_string(&vimvars[idx].vv_tv);
6359}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006360
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006362 * Get List v: variable value. Caller must take care of reference count when
6363 * needed.
6364 */
6365 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006366get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006367{
6368 return vimvars[idx].vv_list;
6369}
6370
6371/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006372 * Get Dict v: variable value. Caller must take care of reference count when
6373 * needed.
6374 */
6375 dict_T *
6376get_vim_var_dict(int idx)
6377{
6378 return vimvars[idx].vv_dict;
6379}
6380
6381/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006382 * Set v:char to character "c".
6383 */
6384 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006385set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006386{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006387 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006388
6389#ifdef FEAT_MBYTE
6390 if (has_mbyte)
6391 buf[(*mb_char2bytes)(c, buf)] = NUL;
6392 else
6393#endif
6394 {
6395 buf[0] = c;
6396 buf[1] = NUL;
6397 }
6398 set_vim_var_string(VV_CHAR, buf, -1);
6399}
6400
6401/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006402 * Set v:count to "count" and v:count1 to "count1".
6403 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 */
6405 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006406set_vcount(
6407 long count,
6408 long count1,
6409 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006411 if (set_prevcount)
6412 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006413 vimvars[VV_COUNT].vv_nr = count;
6414 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415}
6416
6417/*
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02006418 * Save variables that might be changed as a side effect. Used when executing
6419 * a timer callback.
6420 */
6421 void
6422save_vimvars(vimvars_save_T *vvsave)
6423{
6424 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
6425 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
6426 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
6427}
6428
6429/*
6430 * Restore variables saved by save_vimvars().
6431 */
6432 void
6433restore_vimvars(vimvars_save_T *vvsave)
6434{
6435 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
6436 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
6437 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
6438}
6439
6440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 * Set string v: variable to a copy of "val".
6442 */
6443 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006444set_vim_var_string(
6445 int idx,
6446 char_u *val,
6447 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448{
Bram Moolenaara542c682016-01-31 16:28:04 +01006449 clear_tv(&vimvars[idx].vv_di.di_tv);
6450 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006452 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006454 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006456 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457}
6458
6459/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006460 * Set List v: variable to "val".
6461 */
6462 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006463set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006464{
Bram Moolenaara542c682016-01-31 16:28:04 +01006465 clear_tv(&vimvars[idx].vv_di.di_tv);
6466 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006467 vimvars[idx].vv_list = val;
6468 if (val != NULL)
6469 ++val->lv_refcount;
6470}
6471
6472/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006473 * Set Dictionary v: variable to "val".
6474 */
6475 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006476set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006477{
Bram Moolenaara542c682016-01-31 16:28:04 +01006478 clear_tv(&vimvars[idx].vv_di.di_tv);
6479 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006480 vimvars[idx].vv_dict = val;
6481 if (val != NULL)
6482 {
6483 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006484 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02006485 }
6486}
6487
6488/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 * Set v:register if needed.
6490 */
6491 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006492set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493{
6494 char_u regname;
6495
6496 if (c == 0 || c == ' ')
6497 regname = '"';
6498 else
6499 regname = c;
6500 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006501 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 set_vim_var_string(VV_REG, &regname, 1);
6503}
6504
6505/*
6506 * Get or set v:exception. If "oldval" == NULL, return the current value.
6507 * Otherwise, restore the value to "oldval" and return NULL.
6508 * Must always be called in pairs to save and restore v:exception! Does not
6509 * take care of memory allocations.
6510 */
6511 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006512v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513{
6514 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006515 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516
Bram Moolenaare9a41262005-01-15 22:18:47 +00006517 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 return NULL;
6519}
6520
6521/*
6522 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
6523 * Otherwise, restore the value to "oldval" and return NULL.
6524 * Must always be called in pairs to save and restore v:throwpoint! Does not
6525 * take care of memory allocations.
6526 */
6527 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006528v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529{
6530 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006531 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532
Bram Moolenaare9a41262005-01-15 22:18:47 +00006533 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 return NULL;
6535}
6536
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537/*
6538 * Set v:cmdarg.
6539 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6540 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6541 * Must always be called in pairs!
6542 */
6543 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006544set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545{
6546 char_u *oldval;
6547 char_u *newval;
6548 unsigned len;
6549
Bram Moolenaare9a41262005-01-15 22:18:47 +00006550 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006551 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006553 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006554 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006555 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 }
6557
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006558 if (eap->force_bin == FORCE_BIN)
6559 len = 6;
6560 else if (eap->force_bin == FORCE_NOBIN)
6561 len = 8;
6562 else
6563 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006564
6565 if (eap->read_edit)
6566 len += 7;
6567
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006568 if (eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006569 len += 10; /* " ++ff=unix" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006570# ifdef FEAT_MBYTE
6571 if (eap->force_enc != 0)
6572 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006573 if (eap->bad_char != 0)
6574 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006575# endif
6576
6577 newval = alloc(len + 1);
6578 if (newval == NULL)
6579 return NULL;
6580
6581 if (eap->force_bin == FORCE_BIN)
6582 sprintf((char *)newval, " ++bin");
6583 else if (eap->force_bin == FORCE_NOBIN)
6584 sprintf((char *)newval, " ++nobin");
6585 else
6586 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006587
6588 if (eap->read_edit)
6589 STRCAT(newval, " ++edit");
6590
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006591 if (eap->force_ff != 0)
6592 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006593 eap->force_ff == 'u' ? "unix"
6594 : eap->force_ff == 'd' ? "dos"
6595 : "mac");
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006596#ifdef FEAT_MBYTE
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006597 if (eap->force_enc != 0)
6598 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6599 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006600 if (eap->bad_char == BAD_KEEP)
6601 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6602 else if (eap->bad_char == BAD_DROP)
6603 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6604 else if (eap->bad_char != 0)
6605 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006606#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00006607 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006608 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610
6611/*
6612 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006613 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006615 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006616get_var_tv(
6617 char_u *name,
6618 int len, /* length of "name" */
6619 typval_T *rettv, /* NULL when only checking existence */
6620 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
6621 int verbose, /* may give error message */
6622 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623{
6624 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006625 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006626 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628
6629 /* truncate the name, so that we can use strcmp() */
6630 cc = name[len];
6631 name[len] = NUL;
6632
6633 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 * Check for user-defined variables.
6635 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01006636 v = find_var(name, NULL, no_autoload);
6637 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006639 tv = &v->di_tv;
6640 if (dip != NULL)
6641 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 }
6643
Bram Moolenaare9a41262005-01-15 22:18:47 +00006644 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006646 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006647 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 ret = FAIL;
6649 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006650 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006651 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652
6653 name[len] = cc;
6654
6655 return ret;
6656}
6657
6658/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006659 * Check if variable "name[len]" is a local variable or an argument.
6660 * If so, "*eval_lavars_used" is set to TRUE.
6661 */
6662 static void
6663check_vars(char_u *name, int len)
6664{
6665 int cc;
6666 char_u *varname;
6667 hashtab_T *ht;
6668
6669 if (eval_lavars_used == NULL)
6670 return;
6671
6672 /* truncate the name, so that we can use strcmp() */
6673 cc = name[len];
6674 name[len] = NUL;
6675
6676 ht = find_var_ht(name, &varname);
6677 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
6678 {
6679 if (find_var(name, NULL, TRUE) != NULL)
6680 *eval_lavars_used = TRUE;
6681 }
6682
6683 name[len] = cc;
6684}
6685
6686/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006687 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
6688 * Also handle function call with Funcref variable: func(expr)
6689 * Can all be combined: dict.func(expr)[idx]['func'](expr)
6690 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006691 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006692handle_subscript(
6693 char_u **arg,
6694 typval_T *rettv,
6695 int evaluate, /* do more than finding the end */
6696 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006697{
6698 int ret = OK;
6699 dict_T *selfdict = NULL;
6700 char_u *s;
6701 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006702 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006703
6704 while (ret == OK
6705 && (**arg == '['
6706 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006707 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6708 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01006709 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006710 {
6711 if (**arg == '(')
6712 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006713 partial_T *pt = NULL;
6714
Bram Moolenaard9fba312005-06-26 22:34:35 +00006715 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006716 if (evaluate)
6717 {
6718 functv = *rettv;
6719 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006720
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006721 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006722 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006723 {
6724 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006725 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006726 }
6727 else
6728 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006729 }
6730 else
6731 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006732 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00006733 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006734 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006735
6736 /* Clear the funcref afterwards, so that deleting it while
6737 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006738 if (evaluate)
6739 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006740
6741 /* Stop the expression evaluation when immediately aborting on
6742 * error, or when an interrupt occurred or an exception was thrown
6743 * but not caught. */
6744 if (aborting())
6745 {
6746 if (ret == OK)
6747 clear_tv(rettv);
6748 ret = FAIL;
6749 }
6750 dict_unref(selfdict);
6751 selfdict = NULL;
6752 }
6753 else /* **arg == '[' || **arg == '.' */
6754 {
6755 dict_unref(selfdict);
6756 if (rettv->v_type == VAR_DICT)
6757 {
6758 selfdict = rettv->vval.v_dict;
6759 if (selfdict != NULL)
6760 ++selfdict->dv_refcount;
6761 }
6762 else
6763 selfdict = NULL;
6764 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
6765 {
6766 clear_tv(rettv);
6767 ret = FAIL;
6768 }
6769 }
6770 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006771
Bram Moolenaar1d429612016-05-24 15:44:17 +02006772 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
6773 * Don't do this when "Func" is already a partial that was bound
6774 * explicitly (pt_auto is FALSE). */
6775 if (selfdict != NULL
6776 && (rettv->v_type == VAR_FUNC
6777 || (rettv->v_type == VAR_PARTIAL
6778 && (rettv->vval.v_partial->pt_auto
6779 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006780 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006781
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006782 dict_unref(selfdict);
6783 return ret;
6784}
6785
6786/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006787 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006788 * value).
6789 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01006790 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006791alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006792{
Bram Moolenaar33570922005-01-25 22:26:29 +00006793 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006794}
6795
6796/*
6797 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 * The string "s" must have been allocated, it is consumed.
6799 * Return NULL for out of memory, the variable otherwise.
6800 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006801 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006802alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803{
Bram Moolenaar33570922005-01-25 22:26:29 +00006804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006806 rettv = alloc_tv();
6807 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006809 rettv->v_type = VAR_STRING;
6810 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 }
6812 else
6813 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006814 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815}
6816
6817/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006818 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006820 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006821free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822{
6823 if (varp != NULL)
6824 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006825 switch (varp->v_type)
6826 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006827 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006828 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02006829 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006830 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006831 vim_free(varp->vval.v_string);
6832 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006833 case VAR_PARTIAL:
6834 partial_unref(varp->vval.v_partial);
6835 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006836 case VAR_LIST:
6837 list_unref(varp->vval.v_list);
6838 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006839 case VAR_DICT:
6840 dict_unref(varp->vval.v_dict);
6841 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006842 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006843#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006844 job_unref(varp->vval.v_job);
6845 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006846#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006847 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006848#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006849 channel_unref(varp->vval.v_channel);
6850 break;
6851#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006852 case VAR_NUMBER:
6853 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00006854 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01006855 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00006856 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 vim_free(varp);
6859 }
6860}
6861
6862/*
6863 * Free the memory for a variable value and set the value to NULL or 0.
6864 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006865 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006866clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867{
6868 if (varp != NULL)
6869 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006870 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006872 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006873 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02006874 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006875 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01006876 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006877 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006878 case VAR_PARTIAL:
6879 partial_unref(varp->vval.v_partial);
6880 varp->vval.v_partial = NULL;
6881 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006882 case VAR_LIST:
6883 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006884 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006885 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006886 case VAR_DICT:
6887 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006888 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006889 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006890 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006891 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006892 varp->vval.v_number = 0;
6893 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006894 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006895#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006896 varp->vval.v_float = 0.0;
6897 break;
6898#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006899 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006900#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006901 job_unref(varp->vval.v_job);
6902 varp->vval.v_job = NULL;
6903#endif
6904 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01006905 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006906#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006907 channel_unref(varp->vval.v_channel);
6908 varp->vval.v_channel = NULL;
6909#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006910 case VAR_UNKNOWN:
6911 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006913 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 }
6915}
6916
6917/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006918 * Set the value of a variable to NULL without freeing items.
6919 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006920 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006921init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006922{
6923 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00006924 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006925}
6926
6927/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 * Get the number value of a variable.
6929 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006930 * For incompatible types, return 0.
6931 * get_tv_number_chk() is similar to get_tv_number(), but informs the
6932 * caller of incompatible types: it sets *denote to TRUE if "denote"
6933 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006935 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006936get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006938 int error = FALSE;
6939
6940 return get_tv_number_chk(varp, &error); /* return 0L on error */
6941}
6942
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006943 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006944get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006945{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006946 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006948 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006950 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006951 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006952 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006953#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +00006954 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006955 break;
6956#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006957 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006958 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +00006959 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006960 break;
6961 case VAR_STRING:
6962 if (varp->vval.v_string != NULL)
6963 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006964 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006965 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006966 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +00006967 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006968 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006969 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +00006970 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006971 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006972 case VAR_SPECIAL:
6973 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
6974 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006975 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006976#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006977 EMSG(_("E910: Using a Job as a Number"));
6978 break;
6979#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006980 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006981#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006982 EMSG(_("E913: Using a Channel as a Number"));
6983 break;
6984#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01006985 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01006986 internal_error("get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006987 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006989 if (denote == NULL) /* useful for values that must be unsigned */
6990 n = -1;
6991 else
6992 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006993 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994}
6995
Bram Moolenaarf7edf402016-01-19 23:36:15 +01006996#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006997 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006998get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01006999{
7000 switch (varp->v_type)
7001 {
7002 case VAR_NUMBER:
7003 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007004 case VAR_FLOAT:
7005 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007006 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007007 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007008 EMSG(_("E891: Using a Funcref as a Float"));
7009 break;
7010 case VAR_STRING:
7011 EMSG(_("E892: Using a String as a Float"));
7012 break;
7013 case VAR_LIST:
7014 EMSG(_("E893: Using a List as a Float"));
7015 break;
7016 case VAR_DICT:
7017 EMSG(_("E894: Using a Dictionary as a Float"));
7018 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007019 case VAR_SPECIAL:
7020 EMSG(_("E907: Using a special value as a Float"));
7021 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007022 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007023# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007024 EMSG(_("E911: Using a Job as a Float"));
7025 break;
7026# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007027 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007028# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007029 EMSG(_("E914: Using a Channel as a Float"));
7030 break;
7031# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007032 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007033 internal_error("get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007034 break;
7035 }
7036 return 0;
7037}
7038#endif
7039
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 * Get the string value of a variable.
7042 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +00007043 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7044 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 * If the String variable has never been set, return an empty string.
7046 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007047 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
7048 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007050 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007051get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052{
7053 static char_u mybuf[NUMBUFLEN];
7054
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007055 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056}
7057
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007058 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007059get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007061 char_u *res = get_tv_string_buf_chk(varp, buf);
7062
7063 return res != NULL ? res : (char_u *)"";
7064}
7065
Bram Moolenaar7d647822014-04-05 21:28:56 +02007066/*
7067 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7068 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007069 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007070get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007071{
7072 static char_u mybuf[NUMBUFLEN];
7073
7074 return get_tv_string_buf_chk(varp, mybuf);
7075}
7076
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007077 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007078get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007079{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007080 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007082 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007083 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaarea391762018-04-08 13:07:22 +02007084 (long long)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007085 return buf;
7086 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007087 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007088 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007089 break;
7090 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007091 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007092 break;
7093 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007094 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007095 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007096 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007097#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +02007098 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007099 break;
7100#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007101 case VAR_STRING:
7102 if (varp->vval.v_string != NULL)
7103 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007104 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007105 case VAR_SPECIAL:
7106 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7107 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007108 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007109#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007110 {
7111 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007112 char *status;
7113
7114 if (job == NULL)
7115 return (char_u *)"no process";
7116 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007117 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007118 : "run";
7119# ifdef UNIX
7120 vim_snprintf((char *)buf, NUMBUFLEN,
7121 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007122# elif defined(WIN32)
7123 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007124 "process %ld %s",
7125 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007126 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007127# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007128 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007129 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7130# endif
7131 return buf;
7132 }
7133#endif
7134 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007135 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007136#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007137 {
7138 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007139 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007140
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007141 if (channel == NULL)
7142 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7143 else
7144 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007145 "channel %d %s", channel->ch_id, status);
7146 return buf;
7147 }
7148#endif
7149 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007150 case VAR_UNKNOWN:
7151 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007152 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007154 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155}
7156
7157/*
7158 * Find variable "name" in the list of variables.
7159 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007160 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007161 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007162 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007164 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007165find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007168 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007169 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170
Bram Moolenaara7043832005-01-21 11:56:39 +00007171 ht = find_var_ht(name, &varname);
7172 if (htp != NULL)
7173 *htp = ht;
7174 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007176 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7177 if (ret != NULL)
7178 return ret;
7179
7180 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007181 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182}
7183
7184/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007185 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007186 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007188 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007189find_var_in_ht(
7190 hashtab_T *ht,
7191 int htname,
7192 char_u *varname,
7193 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007194{
Bram Moolenaar33570922005-01-25 22:26:29 +00007195 hashitem_T *hi;
7196
7197 if (*varname == NUL)
7198 {
7199 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007200 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007201 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007202 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007203 case 'g': return &globvars_var;
7204 case 'v': return &vimvars_var;
7205 case 'b': return &curbuf->b_bufvar;
7206 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007207 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007208 case 'l': return get_funccal_local_var();
7209 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007210 }
7211 return NULL;
7212 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007213
7214 hi = hash_find(ht, varname);
7215 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007216 {
7217 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007218 * worked find the variable again. Don't auto-load a script if it was
7219 * loaded already, otherwise it would be loaded every time when
7220 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007221 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007222 {
7223 /* Note: script_autoload() may make "hi" invalid. It must either
7224 * be obtained again or not used. */
7225 if (!script_autoload(varname, FALSE) || aborting())
7226 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007227 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007228 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007229 if (HASHITEM_EMPTY(hi))
7230 return NULL;
7231 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007232 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007233}
7234
7235/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007236 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007237 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007238 * Set "varname" to the start of name without ':'.
7239 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007240 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007241find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007243 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007244 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007245
Bram Moolenaar73627d02015-08-11 15:46:09 +02007246 if (name[0] == NUL)
7247 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 if (name[1] != ':')
7249 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007250 /* The name must not start with a colon or #. */
7251 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 return NULL;
7253 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007254
7255 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007256 hi = hash_find(&compat_hashtab, name);
7257 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +00007258 return &compat_hashtab;
7259
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007260 ht = get_funccal_local_ht();
7261 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007262 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007263 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 }
7265 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007266 if (*name == 'g') /* global variable */
7267 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007268 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7269 */
7270 if (vim_strchr(name + 2, ':') != NULL
7271 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007272 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007274 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007276 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007277 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007278 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007279 if (*name == 'v') /* v: variable */
7280 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007281 if (*name == 'a') /* a: function argument */
7282 return get_funccal_args_ht();
7283 if (*name == 'l') /* l: local function variable */
7284 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 if (*name == 's' /* script variable */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007286 && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
7287 return &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 return NULL;
7289}
7290
7291/*
7292 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +02007293 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 * Returns NULL when it doesn't exist.
7295 */
7296 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007297get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298{
Bram Moolenaar33570922005-01-25 22:26:29 +00007299 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007301 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 if (v == NULL)
7303 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007304 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305}
7306
7307/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007308 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 * sourcing this script and when executing functions defined in the script.
7310 */
7311 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007312new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313{
Bram Moolenaara7043832005-01-21 11:56:39 +00007314 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007315 hashtab_T *ht;
7316 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007317
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7319 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007320 /* Re-allocating ga_data means that an ht_array pointing to
7321 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007322 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007323 for (i = 1; i <= ga_scripts.ga_len; ++i)
7324 {
7325 ht = &SCRIPT_VARS(i);
7326 if (ht->ht_mask == HT_INIT_SIZE - 1)
7327 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007328 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007329 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007330 }
7331
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 while (ga_scripts.ga_len < id)
7333 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007334 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007335 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007336 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 }
7339 }
7340}
7341
7342/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007343 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7344 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 */
7346 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007347init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348{
Bram Moolenaar33570922005-01-25 22:26:29 +00007349 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007350 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007351 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007352 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007353 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007354 dict_var->di_tv.vval.v_dict = dict;
7355 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007356 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007357 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7358 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359}
7360
7361/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007362 * Unreference a dictionary initialized by init_var_dict().
7363 */
7364 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007365unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007366{
7367 /* Now the dict needs to be freed if no one else is using it, go back to
7368 * normal reference counting. */
7369 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7370 dict_unref(dict);
7371}
7372
7373/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007375 * Frees all allocated variables and the value they contain.
7376 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 */
7378 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007379vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007380{
7381 vars_clear_ext(ht, TRUE);
7382}
7383
7384/*
7385 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7386 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007387 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007388vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389{
Bram Moolenaara7043832005-01-21 11:56:39 +00007390 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007391 hashitem_T *hi;
7392 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393
Bram Moolenaar33570922005-01-25 22:26:29 +00007394 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007395 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007396 for (hi = ht->ht_array; todo > 0; ++hi)
7397 {
7398 if (!HASHITEM_EMPTY(hi))
7399 {
7400 --todo;
7401
Bram Moolenaar33570922005-01-25 22:26:29 +00007402 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007403 * ht_array might change then. hash_clear() takes care of it
7404 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007405 v = HI2DI(hi);
7406 if (free_val)
7407 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007408 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007409 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007410 }
7411 }
7412 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007413 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414}
7415
Bram Moolenaara7043832005-01-21 11:56:39 +00007416/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007417 * Delete a variable from hashtab "ht" at item "hi".
7418 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007419 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007421delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422{
Bram Moolenaar33570922005-01-25 22:26:29 +00007423 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007424
7425 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007426 clear_tv(&di->di_tv);
7427 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428}
7429
7430/*
7431 * List the value of one internal variable.
7432 */
7433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007434list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007436 char_u *tofree;
7437 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007438 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007439
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007440 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007441 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007442 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007443 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444}
7445
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007447list_one_var_a(
7448 char_u *prefix,
7449 char_u *name,
7450 int type,
7451 char_u *string,
7452 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453{
Bram Moolenaar31859182007-08-14 20:41:13 +00007454 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7455 msg_start();
7456 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 if (name != NULL) /* "a:" vars don't have a name stored */
7458 msg_puts(name);
7459 msg_putchar(' ');
7460 msg_advance(22);
7461 if (type == VAR_NUMBER)
7462 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007463 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007464 msg_putchar('*');
7465 else if (type == VAR_LIST)
7466 {
7467 msg_putchar('[');
7468 if (*string == '[')
7469 ++string;
7470 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007471 else if (type == VAR_DICT)
7472 {
7473 msg_putchar('{');
7474 if (*string == '{')
7475 ++string;
7476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 else
7478 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007479
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007481
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007482 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007483 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007484 if (*first)
7485 {
7486 msg_clr_eos();
7487 *first = FALSE;
7488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489}
7490
7491/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007492 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 * If the variable already exists, the value is updated.
7494 * Otherwise the variable is created.
7495 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007496 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007497set_var(
7498 char_u *name,
7499 typval_T *tv,
7500 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501{
Bram Moolenaar33570922005-01-25 22:26:29 +00007502 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007504 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007506 ht = find_var_ht(name, &varname);
7507 if (ht == NULL || *varname == NUL)
7508 {
7509 EMSG2(_(e_illvar), name);
7510 return;
7511 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007512 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007513
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007514 /* Search in parent scope which is possible to reference from lambda */
7515 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007516 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007517
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007518 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7519 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007520 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007521
Bram Moolenaar33570922005-01-25 22:26:29 +00007522 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007524 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007525 if (var_check_ro(v->di_flags, name, FALSE)
7526 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007527 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007528
7529 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007530 * Handle setting internal v: variables separately where needed to
7531 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007532 */
7533 if (ht == &vimvarht)
7534 {
7535 if (v->di_tv.v_type == VAR_STRING)
7536 {
7537 vim_free(v->di_tv.vval.v_string);
7538 if (copy || tv->v_type != VAR_STRING)
7539 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
7540 else
7541 {
7542 /* Take over the string to avoid an extra alloc/free. */
7543 v->di_tv.vval.v_string = tv->vval.v_string;
7544 tv->vval.v_string = NULL;
7545 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007546 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007547 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007548 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007549 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007550 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007551 if (STRCMP(varname, "searchforward") == 0)
7552 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007553#ifdef FEAT_SEARCH_EXTRA
7554 else if (STRCMP(varname, "hlsearch") == 0)
7555 {
7556 no_hlsearch = !v->di_tv.vval.v_number;
7557 redraw_all_later(SOME_VALID);
7558 }
7559#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007560 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007561 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007562 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar95f09602016-11-10 20:01:45 +01007563 internal_error("set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +00007564 }
7565
7566 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 }
7568 else /* add a new variable */
7569 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007570 /* Can't add "v:" variable. */
7571 if (ht == &vimvarht)
7572 {
7573 EMSG2(_(e_illvar), name);
7574 return;
7575 }
7576
Bram Moolenaar92124a32005-06-17 22:03:40 +00007577 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007578 if (!valid_varname(varname))
7579 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00007580
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007581 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7582 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00007583 if (v == NULL)
7584 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007585 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00007586 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007588 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 return;
7590 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007591 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007593
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007594 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00007595 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007596 else
7597 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007598 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007599 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007600 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602}
7603
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007604/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007605 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00007606 * Also give an error message.
7607 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007608 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007609var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00007610{
7611 if (flags & DI_FLAGS_RO)
7612 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007613 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007614 return TRUE;
7615 }
7616 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
7617 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007618 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007619 return TRUE;
7620 }
7621 return FALSE;
7622}
7623
7624/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007625 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
7626 * Also give an error message.
7627 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007628 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007629var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007630{
7631 if (flags & DI_FLAGS_FIX)
7632 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007633 EMSG2(_("E795: Cannot delete variable %s"),
7634 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007635 return TRUE;
7636 }
7637 return FALSE;
7638}
7639
7640/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007641 * Check if a funcref is assigned to a valid variable name.
7642 * Return TRUE and give an error if not.
7643 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007644 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007645var_check_func_name(
7646 char_u *name, /* points to start of variable name */
7647 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007648{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02007649 /* Allow for w: b: s: and t:. */
7650 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007651 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
7652 ? name[2] : name[0]))
7653 {
7654 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
7655 name);
7656 return TRUE;
7657 }
7658 /* Don't allow hiding a function. When "v" is not NULL we might be
7659 * assigning another function to the same var, the type is checked
7660 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02007661 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007662 {
7663 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
7664 name);
7665 return TRUE;
7666 }
7667 return FALSE;
7668}
7669
7670/*
7671 * Check if a variable name is valid.
7672 * Return FALSE and give an error if not.
7673 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007674 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007675valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007676{
7677 char_u *p;
7678
7679 for (p = varname; *p != NUL; ++p)
7680 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
7681 && *p != AUTOLOAD_CHAR)
7682 {
7683 EMSG2(_(e_illvar), varname);
7684 return FALSE;
7685 }
7686 return TRUE;
7687}
7688
7689/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007690 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02007691 * Also give an error message, using "name" or _("name") when use_gettext is
7692 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007693 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007694 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007695tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007696{
7697 if (lock & VAR_LOCKED)
7698 {
7699 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007700 name == NULL ? (char_u *)_("Unknown")
7701 : use_gettext ? (char_u *)_(name)
7702 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007703 return TRUE;
7704 }
7705 if (lock & VAR_FIXED)
7706 {
7707 EMSG2(_("E742: Cannot change value of %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 return FALSE;
7714}
7715
7716/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007717 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007718 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007719 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007720 * It is OK for "from" and "to" to point to the same item. This is used to
7721 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007722 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007723 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007724copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007726 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007727 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007728 switch (from->v_type)
7729 {
7730 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007731 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007732 to->vval.v_number = from->vval.v_number;
7733 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007734 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007735#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007736 to->vval.v_float = from->vval.v_float;
7737 break;
7738#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007739 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007740#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007741 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007742 if (to->vval.v_job != NULL)
7743 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007744 break;
7745#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007746 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007747#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007748 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007749 if (to->vval.v_channel != NULL)
7750 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01007751 break;
7752#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007753 case VAR_STRING:
7754 case VAR_FUNC:
7755 if (from->vval.v_string == NULL)
7756 to->vval.v_string = NULL;
7757 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007758 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007759 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007760 if (from->v_type == VAR_FUNC)
7761 func_ref(to->vval.v_string);
7762 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007763 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007764 case VAR_PARTIAL:
7765 if (from->vval.v_partial == NULL)
7766 to->vval.v_partial = NULL;
7767 else
7768 {
7769 to->vval.v_partial = from->vval.v_partial;
7770 ++to->vval.v_partial->pt_refcount;
7771 }
7772 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007773 case VAR_LIST:
7774 if (from->vval.v_list == NULL)
7775 to->vval.v_list = NULL;
7776 else
7777 {
7778 to->vval.v_list = from->vval.v_list;
7779 ++to->vval.v_list->lv_refcount;
7780 }
7781 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007782 case VAR_DICT:
7783 if (from->vval.v_dict == NULL)
7784 to->vval.v_dict = NULL;
7785 else
7786 {
7787 to->vval.v_dict = from->vval.v_dict;
7788 ++to->vval.v_dict->dv_refcount;
7789 }
7790 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007791 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007792 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007793 break;
7794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795}
7796
7797/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007798 * Make a copy of an item.
7799 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007800 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7801 * reference to an already copied list/dict can be used.
7802 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007803 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007804 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007805item_copy(
7806 typval_T *from,
7807 typval_T *to,
7808 int deep,
7809 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007810{
7811 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007812 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007813
Bram Moolenaar33570922005-01-25 22:26:29 +00007814 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007815 {
7816 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007817 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007818 }
7819 ++recurse;
7820
7821 switch (from->v_type)
7822 {
7823 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007824 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007825 case VAR_STRING:
7826 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007827 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007828 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007829 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007830 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007831 copy_tv(from, to);
7832 break;
7833 case VAR_LIST:
7834 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007835 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007836 if (from->vval.v_list == NULL)
7837 to->vval.v_list = NULL;
7838 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
7839 {
7840 /* use the copy made earlier */
7841 to->vval.v_list = from->vval.v_list->lv_copylist;
7842 ++to->vval.v_list->lv_refcount;
7843 }
7844 else
7845 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
7846 if (to->vval.v_list == NULL)
7847 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007848 break;
7849 case VAR_DICT:
7850 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007851 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007852 if (from->vval.v_dict == NULL)
7853 to->vval.v_dict = NULL;
7854 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7855 {
7856 /* use the copy made earlier */
7857 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7858 ++to->vval.v_dict->dv_refcount;
7859 }
7860 else
7861 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
7862 if (to->vval.v_dict == NULL)
7863 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007864 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007865 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007866 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007867 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007868 }
7869 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007870 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007871}
7872
7873/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007874 * This function is used by f_input() and f_inputdialog() functions. The third
7875 * argument to f_input() specifies the type of completion to use at the
7876 * prompt. The third argument to f_inputdialog() specifies the value to return
7877 * when the user cancels the prompt.
7878 */
7879 void
7880get_user_input(
7881 typval_T *argvars,
7882 typval_T *rettv,
7883 int inputdialog,
7884 int secret)
7885{
7886 char_u *prompt = get_tv_string_chk(&argvars[0]);
7887 char_u *p = NULL;
7888 int c;
7889 char_u buf[NUMBUFLEN];
7890 int cmd_silent_save = cmd_silent;
7891 char_u *defstr = (char_u *)"";
7892 int xp_type = EXPAND_NOTHING;
7893 char_u *xp_arg = NULL;
7894
7895 rettv->v_type = VAR_STRING;
7896 rettv->vval.v_string = NULL;
7897
7898#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007899 /* While starting up, there is no place to enter text. When running tests
7900 * with --not-a-term we assume feedkeys() will be used. */
7901 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007902 return;
7903#endif
7904
7905 cmd_silent = FALSE; /* Want to see the prompt. */
7906 if (prompt != NULL)
7907 {
7908 /* Only the part of the message after the last NL is considered as
7909 * prompt for the command line */
7910 p = vim_strrchr(prompt, '\n');
7911 if (p == NULL)
7912 p = prompt;
7913 else
7914 {
7915 ++p;
7916 c = *p;
7917 *p = NUL;
7918 msg_start();
7919 msg_clr_eos();
7920 msg_puts_attr(prompt, echo_attr);
7921 msg_didout = FALSE;
7922 msg_starthere();
7923 *p = c;
7924 }
7925 cmdline_row = msg_row;
7926
7927 if (argvars[1].v_type != VAR_UNKNOWN)
7928 {
7929 defstr = get_tv_string_buf_chk(&argvars[1], buf);
7930 if (defstr != NULL)
7931 stuffReadbuffSpec(defstr);
7932
7933 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
7934 {
7935 char_u *xp_name;
7936 int xp_namelen;
7937 long argt;
7938
7939 /* input() with a third argument: completion */
7940 rettv->vval.v_string = NULL;
7941
7942 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
7943 if (xp_name == NULL)
7944 return;
7945
7946 xp_namelen = (int)STRLEN(xp_name);
7947
7948 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
7949 &xp_arg) == FAIL)
7950 return;
7951 }
7952 }
7953
7954 if (defstr != NULL)
7955 {
7956 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02007957
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007958 ex_normal_busy = 0;
7959 rettv->vval.v_string =
7960 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
7961 xp_type, xp_arg);
7962 ex_normal_busy = save_ex_normal_busy;
7963 }
7964 if (inputdialog && rettv->vval.v_string == NULL
7965 && argvars[1].v_type != VAR_UNKNOWN
7966 && argvars[2].v_type != VAR_UNKNOWN)
7967 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
7968 &argvars[2], buf));
7969
7970 vim_free(xp_arg);
7971
7972 /* since the user typed this, no need to wait for return */
7973 need_wait_return = FALSE;
7974 msg_didout = FALSE;
7975 }
7976 cmd_silent = cmd_silent_save;
7977}
7978
7979/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 * ":echo expr1 ..." print each argument separated with a space, add a
7981 * newline at the end.
7982 * ":echon expr1 ..." print each argument plain.
7983 */
7984 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007985ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986{
7987 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007988 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007989 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 char_u *p;
7991 int needclr = TRUE;
7992 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007993 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994
7995 if (eap->skip)
7996 ++emsg_skip;
7997 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
7998 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007999 /* If eval1() causes an error message the text from the command may
8000 * still need to be cleared. E.g., "echo 22,44". */
8001 need_clr_eos = needclr;
8002
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008004 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 {
8006 /*
8007 * Report the invalid expression unless the expression evaluation
8008 * has been cancelled due to an aborting error, an interrupt, or an
8009 * exception.
8010 */
8011 if (!aborting())
8012 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008013 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 break;
8015 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008016 need_clr_eos = FALSE;
8017
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 if (!eap->skip)
8019 {
8020 if (atstart)
8021 {
8022 atstart = FALSE;
8023 /* Call msg_start() after eval1(), evaluating the expression
8024 * may cause a message to appear. */
8025 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008026 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008027 /* Mark the saved text as finishing the line, so that what
8028 * follows is displayed on a new line when scrolling back
8029 * at the more prompt. */
8030 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 }
8034 else if (eap->cmdidx == CMD_echo)
8035 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008036 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008037 if (p != NULL)
8038 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008040 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008042 if (*p != TAB && needclr)
8043 {
8044 /* remove any text still there from the command */
8045 msg_clr_eos();
8046 needclr = FALSE;
8047 }
8048 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 }
8050 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008051 {
8052#ifdef FEAT_MBYTE
8053 if (has_mbyte)
8054 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008055 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008056
8057 (void)msg_outtrans_len_attr(p, i, echo_attr);
8058 p += i - 1;
8059 }
8060 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008062 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008065 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008067 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 arg = skipwhite(arg);
8069 }
8070 eap->nextcmd = check_nextcmd(arg);
8071
8072 if (eap->skip)
8073 --emsg_skip;
8074 else
8075 {
8076 /* remove text that may still be there from the command */
8077 if (needclr)
8078 msg_clr_eos();
8079 if (eap->cmdidx == CMD_echo)
8080 msg_end();
8081 }
8082}
8083
8084/*
8085 * ":echohl {name}".
8086 */
8087 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008088ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008090 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091}
8092
8093/*
8094 * ":execute expr1 ..." execute the result of an expression.
8095 * ":echomsg expr1 ..." Print a message
8096 * ":echoerr expr1 ..." Print an error
8097 * Each gets spaces around each argument and a newline at the end for
8098 * echo commands
8099 */
8100 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008101ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102{
8103 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008104 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 int ret = OK;
8106 char_u *p;
8107 garray_T ga;
8108 int len;
8109 int save_did_emsg;
8110
8111 ga_init2(&ga, 1, 80);
8112
8113 if (eap->skip)
8114 ++emsg_skip;
8115 while (*arg != NUL && *arg != '|' && *arg != '\n')
8116 {
8117 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008118 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 {
8120 /*
8121 * Report the invalid expression unless the expression evaluation
8122 * has been cancelled due to an aborting error, an interrupt, or an
8123 * exception.
8124 */
8125 if (!aborting())
8126 EMSG2(_(e_invexpr2), p);
8127 ret = FAIL;
8128 break;
8129 }
8130
8131 if (!eap->skip)
8132 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008133 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 len = (int)STRLEN(p);
8135 if (ga_grow(&ga, len + 2) == FAIL)
8136 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008137 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 ret = FAIL;
8139 break;
8140 }
8141 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 ga.ga_len += len;
8145 }
8146
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008147 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 arg = skipwhite(arg);
8149 }
8150
8151 if (ret != FAIL && ga.ga_data != NULL)
8152 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008153 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8154 {
8155 /* Mark the already saved text as finishing the line, so that what
8156 * follows is displayed on a new line when scrolling back at the
8157 * more prompt. */
8158 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008159 }
8160
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008162 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008164 out_flush();
8165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 else if (eap->cmdidx == CMD_echoerr)
8167 {
8168 /* We don't want to abort following commands, restore did_emsg. */
8169 save_did_emsg = did_emsg;
8170 EMSG((char_u *)ga.ga_data);
8171 if (!force_abort)
8172 did_emsg = save_did_emsg;
8173 }
8174 else if (eap->cmdidx == CMD_execute)
8175 do_cmdline((char_u *)ga.ga_data,
8176 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8177 }
8178
8179 ga_clear(&ga);
8180
8181 if (eap->skip)
8182 --emsg_skip;
8183
8184 eap->nextcmd = check_nextcmd(arg);
8185}
8186
8187/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008188 * Find window specified by "vp" in tabpage "tp".
8189 */
8190 win_T *
8191find_win_by_nr(
8192 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01008193 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008194{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008195 win_T *wp;
Bram Moolenaare6e39892018-10-25 12:32:11 +02008196 int nr = (int)get_tv_number_chk(vp, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008197
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008198 if (nr < 0)
8199 return NULL;
8200 if (nr == 0)
8201 return curwin;
8202
Bram Moolenaar29323592016-07-24 22:04:11 +02008203 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8204 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008205 if (nr >= LOWEST_WIN_ID)
8206 {
8207 if (wp->w_id == nr)
8208 return wp;
8209 }
8210 else if (--nr <= 0)
8211 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008212 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008213 if (nr >= LOWEST_WIN_ID)
8214 return NULL;
8215 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008216}
8217
8218/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02008219 * Find a window: When using a Window ID in any tab page, when using a number
8220 * in the current tab page.
8221 */
8222 win_T *
8223find_win_by_nr_or_id(typval_T *vp)
8224{
8225 int nr = (int)get_tv_number_chk(vp, NULL);
8226
8227 if (nr >= LOWEST_WIN_ID)
8228 return win_id2wp(vp);
8229 return find_win_by_nr(vp, NULL);
8230}
8231
8232/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008233 * Find window specified by "wvp" in tabpage "tvp".
8234 */
8235 win_T *
8236find_tabwin(
8237 typval_T *wvp, /* VAR_UNKNOWN for current window */
8238 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
8239{
8240 win_T *wp = NULL;
8241 tabpage_T *tp = NULL;
8242 long n;
8243
8244 if (wvp->v_type != VAR_UNKNOWN)
8245 {
8246 if (tvp->v_type != VAR_UNKNOWN)
8247 {
8248 n = (long)get_tv_number(tvp);
8249 if (n >= 0)
8250 tp = find_tabpage(n);
8251 }
8252 else
8253 tp = curtab;
8254
8255 if (tp != NULL)
8256 wp = find_win_by_nr(wvp, tp);
8257 }
8258 else
8259 wp = curwin;
8260
8261 return wp;
8262}
8263
8264/*
8265 * getwinvar() and gettabwinvar()
8266 */
8267 void
8268getwinvar(
8269 typval_T *argvars,
8270 typval_T *rettv,
8271 int off) /* 1 for gettabwinvar() */
8272{
8273 win_T *win;
8274 char_u *varname;
8275 dictitem_T *v;
8276 tabpage_T *tp = NULL;
8277 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008278 win_T *oldcurwin;
8279 tabpage_T *oldtabpage;
8280 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008281
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008282 if (off == 1)
8283 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8284 else
8285 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008286 win = find_win_by_nr(&argvars[off], tp);
8287 varname = get_tv_string_chk(&argvars[off + 1]);
8288 ++emsg_off;
8289
8290 rettv->v_type = VAR_STRING;
8291 rettv->vval.v_string = NULL;
8292
8293 if (win != NULL && varname != NULL)
8294 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008295 /* Set curwin to be our win, temporarily. Also set the tabpage,
8296 * otherwise the window is not valid. Only do this when needed,
8297 * autocommands get blocked. */
8298 need_switch_win = !(tp == curtab && win == curwin);
8299 if (!need_switch_win
8300 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008301 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008302 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008303 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008304 if (varname[1] == NUL)
8305 {
8306 /* get all window-local options in a dict */
8307 dict_T *opts = get_winbuf_options(FALSE);
8308
8309 if (opts != NULL)
8310 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008311 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02008312 done = TRUE;
8313 }
8314 }
8315 else if (get_option_tv(&varname, rettv, 1) == OK)
8316 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008317 done = TRUE;
8318 }
8319 else
8320 {
8321 /* Look up the variable. */
8322 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8323 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8324 varname, FALSE);
8325 if (v != NULL)
8326 {
8327 copy_tv(&v->di_tv, rettv);
8328 done = TRUE;
8329 }
8330 }
8331 }
8332
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008333 if (need_switch_win)
8334 /* restore previous notion of curwin */
8335 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008336 }
8337
8338 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8339 /* use the default return value */
8340 copy_tv(&argvars[off + 2], rettv);
8341
8342 --emsg_off;
8343}
8344
8345/*
8346 * "setwinvar()" and "settabwinvar()" functions
8347 */
8348 void
8349setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8350{
8351 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008352 win_T *save_curwin;
8353 tabpage_T *save_curtab;
8354 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008355 char_u *varname, *winvarname;
8356 typval_T *varp;
8357 char_u nbuf[NUMBUFLEN];
8358 tabpage_T *tp = NULL;
8359
8360 if (check_restricted() || check_secure())
8361 return;
8362
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008363 if (off == 1)
8364 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8365 else
8366 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008367 win = find_win_by_nr(&argvars[off], tp);
8368 varname = get_tv_string_chk(&argvars[off + 1]);
8369 varp = &argvars[off + 2];
8370
8371 if (win != NULL && varname != NULL && varp != NULL)
8372 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008373 need_switch_win = !(tp == curtab && win == curwin);
8374 if (!need_switch_win
8375 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008376 {
8377 if (*varname == '&')
8378 {
8379 long numval;
8380 char_u *strval;
8381 int error = FALSE;
8382
8383 ++varname;
8384 numval = (long)get_tv_number_chk(varp, &error);
8385 strval = get_tv_string_buf_chk(varp, nbuf);
8386 if (!error && strval != NULL)
8387 set_option_value(varname, numval, strval, OPT_LOCAL);
8388 }
8389 else
8390 {
8391 winvarname = alloc((unsigned)STRLEN(varname) + 3);
8392 if (winvarname != NULL)
8393 {
8394 STRCPY(winvarname, "w:");
8395 STRCPY(winvarname + 2, varname);
8396 set_var(winvarname, varp, TRUE);
8397 vim_free(winvarname);
8398 }
8399 }
8400 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008401 if (need_switch_win)
8402 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008403 }
8404}
8405
8406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8408 * "arg" points to the "&" or '+' when called, to "option" when returning.
8409 * Returns NULL when no option name found. Otherwise pointer to the char
8410 * after the option name.
8411 */
8412 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008413find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414{
8415 char_u *p = *arg;
8416
8417 ++p;
8418 if (*p == 'g' && p[1] == ':')
8419 {
8420 *opt_flags = OPT_GLOBAL;
8421 p += 2;
8422 }
8423 else if (*p == 'l' && p[1] == ':')
8424 {
8425 *opt_flags = OPT_LOCAL;
8426 p += 2;
8427 }
8428 else
8429 *opt_flags = 0;
8430
8431 if (!ASCII_ISALPHA(*p))
8432 return NULL;
8433 *arg = p;
8434
8435 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
8436 p += 4; /* termcap option */
8437 else
8438 while (ASCII_ISALPHA(*p))
8439 ++p;
8440 return p;
8441}
8442
8443/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008444 * Return the autoload script name for a function or variable name.
8445 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02008447 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008448autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02008449{
Bram Moolenaara1544c02013-05-30 12:35:52 +02008450 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008451 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02008452
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008453 /* Get the script file name: replace '#' with '/', append ".vim". */
8454 scriptname = alloc((unsigned)(STRLEN(name) + 14));
8455 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008456 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008457 STRCPY(scriptname, "autoload/");
8458 STRCAT(scriptname, name);
8459 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
8460 STRCAT(scriptname, ".vim");
8461 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
8462 *p = '/';
8463 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008464}
8465
8466/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008467 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008468 * Return TRUE if a package was loaded.
8469 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008470 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008471script_autoload(
8472 char_u *name,
8473 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008474{
8475 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008476 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008477 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008478 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008479
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008480 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008481 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008482 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008483 return FALSE;
8484
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008485 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008486
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008487 /* Find the name in the list of previously loaded package names. Skip
8488 * "autoload/", it's always the same. */
8489 for (i = 0; i < ga_loaded.ga_len; ++i)
8490 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
8491 break;
8492 if (!reload && i < ga_loaded.ga_len)
8493 ret = FALSE; /* was loaded already */
8494 else
8495 {
8496 /* Remember the name if it wasn't loaded already. */
8497 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
8498 {
8499 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
8500 tofree = NULL;
8501 }
8502
8503 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008504 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008505 ret = TRUE;
8506 }
8507
8508 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008509 return ret;
8510}
8511
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
8513typedef enum
8514{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008515 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
8516 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
8517 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518} var_flavour_T;
8519
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01008521var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522{
8523 char_u *p = varname;
8524
8525 if (ASCII_ISUPPER(*p))
8526 {
8527 while (*(++p))
8528 if (ASCII_ISLOWER(*p))
8529 return VAR_FLAVOUR_SESSION;
8530 return VAR_FLAVOUR_VIMINFO;
8531 }
8532 else
8533 return VAR_FLAVOUR_DEFAULT;
8534}
8535#endif
8536
8537#if defined(FEAT_VIMINFO) || defined(PROTO)
8538/*
8539 * Restore global vars that start with a capital from the viminfo file
8540 */
8541 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008542read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543{
8544 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008545 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00008546 typval_T tv;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02008547 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548
8549 if (!writing && (find_viminfo_parameter('!') != NULL))
8550 {
8551 tab = vim_strchr(virp->vir_line + 1, '\t');
8552 if (tab != NULL)
8553 {
8554 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008555 switch (*tab)
8556 {
8557 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008558#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008559 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008560#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008561 case 'D': type = VAR_DICT; break;
8562 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008563 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565
8566 tab = vim_strchr(tab, '\t');
8567 if (tab != NULL)
8568 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008569 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008570 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008571 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008573#ifdef FEAT_FLOAT
8574 else if (type == VAR_FLOAT)
8575 (void)string2float(tab + 1, &tv.vval.v_float);
8576#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008578 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008579 if (type == VAR_DICT || type == VAR_LIST)
8580 {
8581 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
8582
8583 if (etv == NULL)
8584 /* Failed to parse back the dict or list, use it as a
8585 * string. */
8586 tv.v_type = VAR_STRING;
8587 else
8588 {
8589 vim_free(tv.vval.v_string);
8590 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01008591 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008592 }
8593 }
8594
Bram Moolenaarb20e3342016-01-18 23:29:01 +01008595 /* when in a function use global variables */
Bram Moolenaar27e80c82018-10-14 21:41:01 +02008596 save_funccal(&funccal_entry);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008597 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02008598 restore_funccal();
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008599
8600 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008601 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008602 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
8603 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 }
8605 }
8606 }
8607
8608 return viminfo_readline(virp);
8609}
8610
8611/*
8612 * Write global vars that start with a capital to the viminfo file
8613 */
8614 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008615write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616{
Bram Moolenaar33570922005-01-25 22:26:29 +00008617 hashitem_T *hi;
8618 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008619 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01008620 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008621 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008622 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008623 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624
8625 if (find_viminfo_parameter('!') == NULL)
8626 return;
8627
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02008628 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00008629
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008630 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008631 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008633 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008635 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008636 this_var = HI2DI(hi);
8637 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008638 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008639 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00008640 {
8641 case VAR_STRING: s = "STR"; break;
8642 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008643 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008644 case VAR_DICT: s = "DIC"; break;
8645 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008646 case VAR_SPECIAL: s = "XPL"; break;
8647
8648 case VAR_UNKNOWN:
8649 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008650 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008651 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008652 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008653 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00008654 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008655 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008656 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008657 if (p != NULL)
8658 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00008659 vim_free(tofree);
8660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661 }
8662 }
8663}
8664#endif
8665
8666#if defined(FEAT_SESSION) || defined(PROTO)
8667 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008668store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669{
Bram Moolenaar33570922005-01-25 22:26:29 +00008670 hashitem_T *hi;
8671 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008672 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 char_u *p, *t;
8674
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008675 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008676 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008678 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008680 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008681 this_var = HI2DI(hi);
8682 if ((this_var->di_tv.v_type == VAR_NUMBER
8683 || this_var->di_tv.v_type == VAR_STRING)
8684 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008685 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008686 /* Escape special characters with a backslash. Turn a LF and
8687 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008688 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00008689 (char_u *)"\\\"\n\r");
8690 if (p == NULL) /* out of memory */
8691 break;
8692 for (t = p; *t != NUL; ++t)
8693 if (*t == '\n')
8694 *t = 'n';
8695 else if (*t == '\r')
8696 *t = 'r';
8697 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00008698 this_var->di_key,
8699 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8700 : ' ',
8701 p,
8702 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8703 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00008704 || put_eol(fd) == FAIL)
8705 {
8706 vim_free(p);
8707 return FAIL;
8708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 vim_free(p);
8710 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008711#ifdef FEAT_FLOAT
8712 else if (this_var->di_tv.v_type == VAR_FLOAT
8713 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
8714 {
8715 float_T f = this_var->di_tv.vval.v_float;
8716 int sign = ' ';
8717
8718 if (f < 0)
8719 {
8720 f = -f;
8721 sign = '-';
8722 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01008723 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008724 this_var->di_key, sign, f) < 0)
8725 || put_eol(fd) == FAIL)
8726 return FAIL;
8727 }
8728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 }
8730 }
8731 return OK;
8732}
8733#endif
8734
Bram Moolenaar661b1822005-07-28 22:36:45 +00008735/*
8736 * Display script name where an item was last set.
8737 * Should only be invoked when 'verbose' is non-zero.
8738 */
8739 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008740last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00008741{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008742 char_u *p;
8743
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008744 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00008745 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008746 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008747 if (p != NULL)
8748 {
8749 verbose_enter();
8750 MSG_PUTS(_("\n\tLast set from "));
8751 MSG_PUTS(p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008752 if (script_ctx.sc_lnum > 0)
8753 {
8754 MSG_PUTS(_(" line "));
8755 msg_outnum((long)script_ctx.sc_lnum);
8756 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008757 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008758 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008759 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00008760 }
8761}
8762
Bram Moolenaar53744302015-07-17 17:38:22 +02008763/* reset v:option_new, v:option_old and v:option_type */
8764 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008765reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02008766{
8767 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
8768 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
8769 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
8770}
8771
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008772/*
8773 * Prepare "gap" for an assert error and add the sourcing position.
8774 */
8775 void
8776prepare_assert_error(garray_T *gap)
8777{
8778 char buf[NUMBUFLEN];
8779
8780 ga_init2(gap, 1, 100);
8781 if (sourcing_name != NULL)
8782 {
8783 ga_concat(gap, sourcing_name);
8784 if (sourcing_lnum > 0)
8785 ga_concat(gap, (char_u *)" ");
8786 }
8787 if (sourcing_lnum > 0)
8788 {
8789 sprintf(buf, "line %ld", (long)sourcing_lnum);
8790 ga_concat(gap, (char_u *)buf);
8791 }
8792 if (sourcing_name != NULL || sourcing_lnum > 0)
8793 ga_concat(gap, (char_u *)": ");
8794}
8795
8796/*
8797 * Add an assert error to v:errors.
8798 */
8799 void
8800assert_error(garray_T *gap)
8801{
8802 struct vimvar *vp = &vimvars[VV_ERRORS];
8803
8804 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
8805 /* Make sure v:errors is a list. */
8806 set_vim_var_list(VV_ERRORS, list_alloc());
8807 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
8808}
8809
Bram Moolenaar65a54642018-04-28 16:56:53 +02008810 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008811assert_equal_common(typval_T *argvars, assert_type_T atype)
8812{
8813 garray_T ga;
8814
8815 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
8816 != (atype == ASSERT_EQUAL))
8817 {
8818 prepare_assert_error(&ga);
8819 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
8820 atype);
8821 assert_error(&ga);
8822 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02008823 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008824 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02008825 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008826}
8827
Bram Moolenaar65a54642018-04-28 16:56:53 +02008828 int
Bram Moolenaard96ff162018-02-18 22:13:29 +01008829assert_equalfile(typval_T *argvars)
8830{
8831 char_u buf1[NUMBUFLEN];
8832 char_u buf2[NUMBUFLEN];
8833 char_u *fname1 = get_tv_string_buf_chk(&argvars[0], buf1);
8834 char_u *fname2 = get_tv_string_buf_chk(&argvars[1], buf2);
8835 garray_T ga;
8836 FILE *fd1;
8837 FILE *fd2;
8838
8839 if (fname1 == NULL || fname2 == NULL)
Bram Moolenaar65a54642018-04-28 16:56:53 +02008840 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01008841
8842 IObuff[0] = NUL;
8843 fd1 = mch_fopen((char *)fname1, READBIN);
8844 if (fd1 == NULL)
8845 {
8846 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname1);
8847 }
8848 else
8849 {
8850 fd2 = mch_fopen((char *)fname2, READBIN);
8851 if (fd2 == NULL)
8852 {
8853 fclose(fd1);
8854 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname2);
8855 }
8856 else
8857 {
8858 int c1, c2;
8859 long count = 0;
8860
8861 for (;;)
8862 {
8863 c1 = fgetc(fd1);
8864 c2 = fgetc(fd2);
8865 if (c1 == EOF)
8866 {
8867 if (c2 != EOF)
8868 STRCPY(IObuff, "first file is shorter");
8869 break;
8870 }
8871 else if (c2 == EOF)
8872 {
8873 STRCPY(IObuff, "second file is shorter");
8874 break;
8875 }
8876 else if (c1 != c2)
8877 {
8878 vim_snprintf((char *)IObuff, IOSIZE,
8879 "difference at byte %ld", count);
8880 break;
8881 }
8882 ++count;
8883 }
Bram Moolenaar30494182018-02-20 21:46:05 +01008884 fclose(fd1);
8885 fclose(fd2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01008886 }
8887 }
8888 if (IObuff[0] != NUL)
8889 {
8890 prepare_assert_error(&ga);
8891 ga_concat(&ga, IObuff);
8892 assert_error(&ga);
8893 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02008894 return 1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01008895 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02008896 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01008897}
8898
Bram Moolenaar65a54642018-04-28 16:56:53 +02008899 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008900assert_match_common(typval_T *argvars, assert_type_T atype)
8901{
8902 garray_T ga;
8903 char_u buf1[NUMBUFLEN];
8904 char_u buf2[NUMBUFLEN];
8905 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
8906 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
8907
8908 if (pat == NULL || text == NULL)
8909 EMSG(_(e_invarg));
8910 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
8911 {
8912 prepare_assert_error(&ga);
8913 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
8914 atype);
8915 assert_error(&ga);
8916 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02008917 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008918 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02008919 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008920}
8921
Bram Moolenaar65a54642018-04-28 16:56:53 +02008922 int
Bram Moolenaar61c04492016-07-23 15:35:35 +02008923assert_inrange(typval_T *argvars)
8924{
8925 garray_T ga;
8926 int error = FALSE;
8927 varnumber_T lower = get_tv_number_chk(&argvars[0], &error);
8928 varnumber_T upper = get_tv_number_chk(&argvars[1], &error);
8929 varnumber_T actual = get_tv_number_chk(&argvars[2], &error);
8930 char_u *tofree;
8931 char msg[200];
8932 char_u numbuf[NUMBUFLEN];
8933
8934 if (error)
Bram Moolenaar65a54642018-04-28 16:56:53 +02008935 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02008936 if (actual < lower || actual > upper)
8937 {
8938 prepare_assert_error(&ga);
8939 if (argvars[3].v_type != VAR_UNKNOWN)
8940 {
8941 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
8942 vim_free(tofree);
8943 }
8944 else
8945 {
8946 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
8947 (long)lower, (long)upper, (long)actual);
8948 ga_concat(&ga, (char_u *)msg);
8949 }
8950 assert_error(&ga);
8951 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02008952 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02008953 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02008954 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02008955}
8956
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008957/*
8958 * Common for assert_true() and assert_false().
Bram Moolenaar65a54642018-04-28 16:56:53 +02008959 * Return non-zero for failure.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008960 */
Bram Moolenaar65a54642018-04-28 16:56:53 +02008961 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008962assert_bool(typval_T *argvars, int isTrue)
8963{
8964 int error = FALSE;
8965 garray_T ga;
8966
8967 if (argvars[0].v_type == VAR_SPECIAL
8968 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar65a54642018-04-28 16:56:53 +02008969 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008970 if (argvars[0].v_type != VAR_NUMBER
8971 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
8972 || error)
8973 {
8974 prepare_assert_error(&ga);
8975 fill_assert_error(&ga, &argvars[1],
8976 (char_u *)(isTrue ? "True" : "False"),
8977 NULL, &argvars[0], ASSERT_OTHER);
8978 assert_error(&ga);
8979 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02008980 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008981 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02008982 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008983}
8984
Bram Moolenaar65a54642018-04-28 16:56:53 +02008985 int
Bram Moolenaar42205552017-03-18 19:42:22 +01008986assert_report(typval_T *argvars)
8987{
8988 garray_T ga;
8989
8990 prepare_assert_error(&ga);
8991 ga_concat(&ga, get_tv_string(&argvars[0]));
8992 assert_error(&ga);
8993 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02008994 return 1;
Bram Moolenaar42205552017-03-18 19:42:22 +01008995}
8996
Bram Moolenaar65a54642018-04-28 16:56:53 +02008997 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008998assert_exception(typval_T *argvars)
8999{
9000 garray_T ga;
9001 char_u *error = get_tv_string_chk(&argvars[0]);
9002
9003 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9004 {
9005 prepare_assert_error(&ga);
9006 ga_concat(&ga, (char_u *)"v:exception is not set");
9007 assert_error(&ga);
9008 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009009 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009010 }
9011 else if (error != NULL
9012 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9013 {
9014 prepare_assert_error(&ga);
9015 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9016 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9017 assert_error(&ga);
9018 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009019 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009020 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009021 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009022}
9023
Bram Moolenaar65a54642018-04-28 16:56:53 +02009024 int
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009025assert_beeps(typval_T *argvars)
9026{
9027 char_u *cmd = get_tv_string_chk(&argvars[0]);
9028 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009029 int ret = 0;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009030
9031 called_vim_beep = FALSE;
9032 suppress_errthrow = TRUE;
9033 emsg_silent = FALSE;
9034 do_cmdline_cmd(cmd);
9035 if (!called_vim_beep)
9036 {
9037 prepare_assert_error(&ga);
9038 ga_concat(&ga, (char_u *)"command did not beep: ");
9039 ga_concat(&ga, cmd);
9040 assert_error(&ga);
9041 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009042 ret = 1;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009043 }
9044
9045 suppress_errthrow = FALSE;
9046 emsg_on_display = FALSE;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009047 return ret;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009048}
9049
Bram Moolenaar65a54642018-04-28 16:56:53 +02009050 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009051assert_fails(typval_T *argvars)
9052{
9053 char_u *cmd = get_tv_string_chk(&argvars[0]);
9054 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009055 int ret = 0;
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02009056 char_u numbuf[NUMBUFLEN];
9057 char_u *tofree;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009058
9059 called_emsg = FALSE;
9060 suppress_errthrow = TRUE;
9061 emsg_silent = TRUE;
9062 do_cmdline_cmd(cmd);
9063 if (!called_emsg)
9064 {
9065 prepare_assert_error(&ga);
9066 ga_concat(&ga, (char_u *)"command did not fail: ");
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02009067 if (argvars[1].v_type != VAR_UNKNOWN
9068 && argvars[2].v_type != VAR_UNKNOWN)
9069 {
9070 ga_concat(&ga, echo_string(&argvars[2], &tofree, numbuf, 0));
9071 vim_free(tofree);
9072 }
9073 else
9074 ga_concat(&ga, cmd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009075 assert_error(&ga);
9076 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009077 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009078 }
9079 else if (argvars[1].v_type != VAR_UNKNOWN)
9080 {
9081 char_u buf[NUMBUFLEN];
9082 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9083
9084 if (error == NULL
9085 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9086 {
9087 prepare_assert_error(&ga);
9088 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9089 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
9090 assert_error(&ga);
9091 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009092 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009093 }
9094 }
9095
9096 called_emsg = FALSE;
9097 suppress_errthrow = FALSE;
9098 emsg_silent = FALSE;
9099 emsg_on_display = FALSE;
9100 set_vim_var_string(VV_ERRMSG, NULL, 0);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009101 return ret;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009102}
9103
9104/*
9105 * Append "str" to "gap", escaping unprintable characters.
9106 * Changes NL to \n, CR to \r, etc.
9107 */
9108 static void
9109ga_concat_esc(garray_T *gap, char_u *str)
9110{
9111 char_u *p;
9112 char_u buf[NUMBUFLEN];
9113
9114 if (str == NULL)
9115 {
9116 ga_concat(gap, (char_u *)"NULL");
9117 return;
9118 }
9119
9120 for (p = str; *p != NUL; ++p)
9121 switch (*p)
9122 {
9123 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9124 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9125 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9126 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9127 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9128 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9129 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9130 default:
9131 if (*p < ' ')
9132 {
9133 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9134 ga_concat(gap, buf);
9135 }
9136 else
9137 ga_append(gap, *p);
9138 break;
9139 }
9140}
9141
9142/*
9143 * Fill "gap" with information about an assert error.
9144 */
9145 void
9146fill_assert_error(
9147 garray_T *gap,
9148 typval_T *opt_msg_tv,
9149 char_u *exp_str,
9150 typval_T *exp_tv,
9151 typval_T *got_tv,
9152 assert_type_T atype)
9153{
9154 char_u numbuf[NUMBUFLEN];
9155 char_u *tofree;
9156
9157 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9158 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009159 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9160 vim_free(tofree);
9161 ga_concat(gap, (char_u *)": ");
9162 }
9163
9164 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9165 ga_concat(gap, (char_u *)"Pattern ");
9166 else if (atype == ASSERT_NOTEQUAL)
9167 ga_concat(gap, (char_u *)"Expected not equal to ");
9168 else
9169 ga_concat(gap, (char_u *)"Expected ");
9170 if (exp_str == NULL)
9171 {
9172 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009173 vim_free(tofree);
9174 }
9175 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009176 ga_concat_esc(gap, exp_str);
9177 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009178 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009179 if (atype == ASSERT_MATCH)
9180 ga_concat(gap, (char_u *)" does not match ");
9181 else if (atype == ASSERT_NOTMATCH)
9182 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009183 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009184 ga_concat(gap, (char_u *)" but got ");
9185 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
9186 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009187 }
9188}
9189
Bram Moolenaar31988702018-02-13 12:57:42 +01009190/*
9191 * Compare "typ1" and "typ2". Put the result in "typ1".
9192 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009193 int
9194typval_compare(
9195 typval_T *typ1, /* first operand */
9196 typval_T *typ2, /* second operand */
9197 exptype_T type, /* operator */
9198 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +01009199 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009200{
9201 int i;
9202 varnumber_T n1, n2;
9203 char_u *s1, *s2;
9204 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
9205
Bram Moolenaar31988702018-02-13 12:57:42 +01009206 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009207 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009208 /* For "is" a different type always means FALSE, for "notis"
9209 * it means TRUE. */
9210 n1 = (type == TYPE_NEQUAL);
9211 }
9212 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
9213 {
9214 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009215 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009216 n1 = (typ1->v_type == typ2->v_type
9217 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009218 if (type == TYPE_NEQUAL)
9219 n1 = !n1;
9220 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009221 else if (typ1->v_type != typ2->v_type
9222 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009223 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009224 if (typ1->v_type != typ2->v_type)
9225 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009226 else
Bram Moolenaar31988702018-02-13 12:57:42 +01009227 EMSG(_("E692: Invalid operation for List"));
9228 clear_tv(typ1);
9229 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009230 }
9231 else
9232 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009233 /* Compare two Lists for being equal or unequal. */
9234 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
9235 ic, FALSE);
9236 if (type == TYPE_NEQUAL)
9237 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009238 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009239 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009240
9241 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
9242 {
9243 if (type_is)
9244 {
9245 n1 = (typ1->v_type == typ2->v_type
9246 && typ1->vval.v_dict == typ2->vval.v_dict);
9247 if (type == TYPE_NEQUAL)
9248 n1 = !n1;
9249 }
9250 else if (typ1->v_type != typ2->v_type
9251 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9252 {
9253 if (typ1->v_type != typ2->v_type)
9254 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
9255 else
9256 EMSG(_("E736: Invalid operation for Dictionary"));
9257 clear_tv(typ1);
9258 return FAIL;
9259 }
9260 else
9261 {
9262 /* Compare two Dictionaries for being equal or unequal. */
9263 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
9264 ic, FALSE);
9265 if (type == TYPE_NEQUAL)
9266 n1 = !n1;
9267 }
9268 }
9269
9270 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
9271 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
9272 {
9273 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
9274 {
9275 EMSG(_("E694: Invalid operation for Funcrefs"));
9276 clear_tv(typ1);
9277 return FAIL;
9278 }
9279 if ((typ1->v_type == VAR_PARTIAL
9280 && typ1->vval.v_partial == NULL)
9281 || (typ2->v_type == VAR_PARTIAL
9282 && typ2->vval.v_partial == NULL))
9283 /* when a partial is NULL assume not equal */
9284 n1 = FALSE;
9285 else if (type_is)
9286 {
9287 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
9288 /* strings are considered the same if their value is
9289 * the same */
9290 n1 = tv_equal(typ1, typ2, ic, FALSE);
9291 else if (typ1->v_type == VAR_PARTIAL
9292 && typ2->v_type == VAR_PARTIAL)
9293 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
9294 else
9295 n1 = FALSE;
9296 }
9297 else
9298 n1 = tv_equal(typ1, typ2, ic, FALSE);
9299 if (type == TYPE_NEQUAL)
9300 n1 = !n1;
9301 }
9302
9303#ifdef FEAT_FLOAT
9304 /*
9305 * If one of the two variables is a float, compare as a float.
9306 * When using "=~" or "!~", always compare as string.
9307 */
9308 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
9309 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9310 {
9311 float_T f1, f2;
9312
9313 if (typ1->v_type == VAR_FLOAT)
9314 f1 = typ1->vval.v_float;
9315 else
9316 f1 = get_tv_number(typ1);
9317 if (typ2->v_type == VAR_FLOAT)
9318 f2 = typ2->vval.v_float;
9319 else
9320 f2 = get_tv_number(typ2);
9321 n1 = FALSE;
9322 switch (type)
9323 {
9324 case TYPE_EQUAL: n1 = (f1 == f2); break;
9325 case TYPE_NEQUAL: n1 = (f1 != f2); break;
9326 case TYPE_GREATER: n1 = (f1 > f2); break;
9327 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
9328 case TYPE_SMALLER: n1 = (f1 < f2); break;
9329 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
9330 case TYPE_UNKNOWN:
9331 case TYPE_MATCH:
9332 case TYPE_NOMATCH: break; /* avoid gcc warning */
9333 }
9334 }
9335#endif
9336
9337 /*
9338 * If one of the two variables is a number, compare as a number.
9339 * When using "=~" or "!~", always compare as string.
9340 */
9341 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
9342 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9343 {
9344 n1 = get_tv_number(typ1);
9345 n2 = get_tv_number(typ2);
9346 switch (type)
9347 {
9348 case TYPE_EQUAL: n1 = (n1 == n2); break;
9349 case TYPE_NEQUAL: n1 = (n1 != n2); break;
9350 case TYPE_GREATER: n1 = (n1 > n2); break;
9351 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
9352 case TYPE_SMALLER: n1 = (n1 < n2); break;
9353 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
9354 case TYPE_UNKNOWN:
9355 case TYPE_MATCH:
9356 case TYPE_NOMATCH: break; /* avoid gcc warning */
9357 }
9358 }
9359 else
9360 {
9361 s1 = get_tv_string_buf(typ1, buf1);
9362 s2 = get_tv_string_buf(typ2, buf2);
9363 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
9364 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
9365 else
9366 i = 0;
9367 n1 = FALSE;
9368 switch (type)
9369 {
9370 case TYPE_EQUAL: n1 = (i == 0); break;
9371 case TYPE_NEQUAL: n1 = (i != 0); break;
9372 case TYPE_GREATER: n1 = (i > 0); break;
9373 case TYPE_GEQUAL: n1 = (i >= 0); break;
9374 case TYPE_SMALLER: n1 = (i < 0); break;
9375 case TYPE_SEQUAL: n1 = (i <= 0); break;
9376
9377 case TYPE_MATCH:
9378 case TYPE_NOMATCH:
9379 n1 = pattern_match(s2, s1, ic);
9380 if (type == TYPE_NOMATCH)
9381 n1 = !n1;
9382 break;
9383
9384 case TYPE_UNKNOWN: break; /* avoid gcc warning */
9385 }
9386 }
9387 clear_tv(typ1);
9388 typ1->v_type = VAR_NUMBER;
9389 typ1->vval.v_number = n1;
9390
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009391 return OK;
9392}
9393
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009394 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02009395typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009396{
9397 char_u *tofree;
9398 char_u numbuf[NUMBUFLEN];
9399 char_u *ret = NULL;
9400
9401 if (arg == NULL)
9402 return vim_strsave((char_u *)"(does not exist)");
9403 ret = tv2string(arg, &tofree, numbuf, 0);
9404 /* Make a copy if we have a value but it's not in allocated memory. */
9405 if (ret != NULL && tofree == NULL)
9406 ret = vim_strsave(ret);
9407 return ret;
9408}
9409
9410 int
9411var_exists(char_u *var)
9412{
9413 char_u *name;
9414 char_u *tofree;
9415 typval_T tv;
9416 int len = 0;
9417 int n = FALSE;
9418
9419 /* get_name_len() takes care of expanding curly braces */
9420 name = var;
9421 len = get_name_len(&var, &tofree, TRUE, FALSE);
9422 if (len > 0)
9423 {
9424 if (tofree != NULL)
9425 name = tofree;
9426 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
9427 if (n)
9428 {
9429 /* handle d.key, l[idx], f(expr) */
9430 n = (handle_subscript(&var, &tv, TRUE, FALSE) == OK);
9431 if (n)
9432 clear_tv(&tv);
9433 }
9434 }
9435 if (*var != NUL)
9436 n = FALSE;
9437
9438 vim_free(tofree);
9439 return n;
9440}
9441
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442#endif /* FEAT_EVAL */
9443
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009445#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446
9447#ifdef WIN3264
9448/*
9449 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9450 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451
9452/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009453 * Get the short path (8.3) for the filename in "fnamep".
9454 * Only works for a valid file name.
9455 * When the path gets longer "fnamep" is changed and the allocated buffer
9456 * is put in "bufp".
9457 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9458 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 */
9460 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009461get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009463 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464 char_u *newbuf;
9465
9466 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009467 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468 if (l > len - 1)
9469 {
9470 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009471 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472 newbuf = vim_strnsave(*fnamep, l);
9473 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009474 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475
9476 vim_free(*bufp);
9477 *fnamep = *bufp = newbuf;
9478
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009479 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009480 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 }
9482
9483 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009484 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485}
9486
9487/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009488 * Get the short path (8.3) for the filename in "fname". The converted
9489 * path is returned in "bufp".
9490 *
9491 * Some of the directories specified in "fname" may not exist. This function
9492 * will shorten the existing directories at the beginning of the path and then
9493 * append the remaining non-existing path.
9494 *
9495 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009496 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009497 * bufp - Pointer to an allocated buffer for the filename.
9498 * fnamelen - Length of the filename pointed to by fname
9499 *
9500 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501 */
9502 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009503shortpath_for_invalid_fname(
9504 char_u **fname,
9505 char_u **bufp,
9506 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009508 char_u *short_fname, *save_fname, *pbuf_unused;
9509 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009511 int old_len, len;
9512 int new_len, sfx_len;
9513 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514
9515 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009516 old_len = *fnamelen;
9517 save_fname = vim_strnsave(*fname, old_len);
9518 pbuf_unused = NULL;
9519 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009521 endp = save_fname + old_len - 1; /* Find the end of the copy */
9522 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009524 /*
9525 * Try shortening the supplied path till it succeeds by removing one
9526 * directory at a time from the tail of the path.
9527 */
9528 len = 0;
9529 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009531 /* go back one path-separator */
9532 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9533 --endp;
9534 if (endp <= save_fname)
9535 break; /* processed the complete path */
9536
9537 /*
9538 * Replace the path separator with a NUL and try to shorten the
9539 * resulting path.
9540 */
9541 ch = *endp;
9542 *endp = 0;
9543 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009544 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009545 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9546 {
9547 retval = FAIL;
9548 goto theend;
9549 }
9550 *endp = ch; /* preserve the string */
9551
9552 if (len > 0)
9553 break; /* successfully shortened the path */
9554
9555 /* failed to shorten the path. Skip the path separator */
9556 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557 }
9558
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009559 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009561 /*
9562 * Succeeded in shortening the path. Now concatenate the shortened
9563 * path with the remaining path at the tail.
9564 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009566 /* Compute the length of the new path. */
9567 sfx_len = (int)(save_endp - endp) + 1;
9568 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009570 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009572 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009574 /* There is not enough space in the currently allocated string,
9575 * copy it to a buffer big enough. */
9576 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009578 {
9579 retval = FAIL;
9580 goto theend;
9581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582 }
9583 else
9584 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009585 /* Transfer short_fname to the main buffer (it's big enough),
9586 * unless get_short_pathname() did its work in-place. */
9587 *fname = *bufp = save_fname;
9588 if (short_fname != save_fname)
9589 vim_strncpy(save_fname, short_fname, len);
9590 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009592
9593 /* concat the not-shortened part of the path */
9594 vim_strncpy(*fname + len, endp, sfx_len);
9595 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009597
9598theend:
9599 vim_free(pbuf_unused);
9600 vim_free(save_fname);
9601
9602 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603}
9604
9605/*
9606 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009607 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608 */
9609 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009610shortpath_for_partial(
9611 char_u **fnamep,
9612 char_u **bufp,
9613 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614{
9615 int sepcount, len, tflen;
9616 char_u *p;
9617 char_u *pbuf, *tfname;
9618 int hasTilde;
9619
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009620 /* Count up the path separators from the RHS.. so we know which part
9621 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009623 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 if (vim_ispathsep(*p))
9625 ++sepcount;
9626
9627 /* Need full path first (use expand_env() to remove a "~/") */
9628 hasTilde = (**fnamep == '~');
9629 if (hasTilde)
9630 pbuf = tfname = expand_env_save(*fnamep);
9631 else
9632 pbuf = tfname = FullName_save(*fnamep, FALSE);
9633
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009634 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009636 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
9637 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638
9639 if (len == 0)
9640 {
9641 /* Don't have a valid filename, so shorten the rest of the
9642 * path if we can. This CAN give us invalid 8.3 filenames, but
9643 * there's not a lot of point in guessing what it might be.
9644 */
9645 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009646 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
9647 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648 }
9649
9650 /* Count the paths backward to find the beginning of the desired string. */
9651 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009652 {
9653#ifdef FEAT_MBYTE
9654 if (has_mbyte)
9655 p -= mb_head_off(tfname, p);
9656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657 if (vim_ispathsep(*p))
9658 {
9659 if (sepcount == 0 || (hasTilde && sepcount == 1))
9660 break;
9661 else
9662 sepcount --;
9663 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665 if (hasTilde)
9666 {
9667 --p;
9668 if (p >= tfname)
9669 *p = '~';
9670 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009671 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 }
9673 else
9674 ++p;
9675
9676 /* Copy in the string - p indexes into tfname - allocated at pbuf */
9677 vim_free(*bufp);
9678 *fnamelen = (int)STRLEN(p);
9679 *bufp = pbuf;
9680 *fnamep = p;
9681
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009682 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683}
9684#endif /* WIN3264 */
9685
9686/*
9687 * Adjust a filename, according to a string of modifiers.
9688 * *fnamep must be NUL terminated when called. When returning, the length is
9689 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009690 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691 * When there is an error, *fnamep is set to NULL.
9692 */
9693 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009694modify_fname(
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009695 char_u *src, // string with modifiers
9696 int tilde_file, // "~" is a file name, not $HOME
9697 int *usedlen, // characters after src that are used
9698 char_u **fnamep, // file name so far
9699 char_u **bufp, // buffer for allocated file name or NULL
9700 int *fnamelen) // length of fnamep
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701{
9702 int valid = 0;
9703 char_u *tail;
9704 char_u *s, *p, *pbuf;
9705 char_u dirname[MAXPATHL];
9706 int c;
9707 int has_fullname = 0;
9708#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009709 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 int has_shortname = 0;
9711#endif
9712
9713repeat:
9714 /* ":p" - full path/file_name */
9715 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
9716 {
9717 has_fullname = 1;
9718
9719 valid |= VALID_PATH;
9720 *usedlen += 2;
9721
9722 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
9723 if ((*fnamep)[0] == '~'
9724#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
9725 && ((*fnamep)[1] == '/'
9726# ifdef BACKSLASH_IN_FILENAME
9727 || (*fnamep)[1] == '\\'
9728# endif
9729 || (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009731 && !(tilde_file && (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 )
9733 {
9734 *fnamep = expand_env_save(*fnamep);
9735 vim_free(*bufp); /* free any allocated file name */
9736 *bufp = *fnamep;
9737 if (*fnamep == NULL)
9738 return -1;
9739 }
9740
9741 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009742 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743 {
9744 if (vim_ispathsep(*p)
9745 && p[1] == '.'
9746 && (p[2] == NUL
9747 || vim_ispathsep(p[2])
9748 || (p[2] == '.'
9749 && (p[3] == NUL || vim_ispathsep(p[3])))))
9750 break;
9751 }
9752
9753 /* FullName_save() is slow, don't use it when not needed. */
9754 if (*p != NUL || !vim_isAbsName(*fnamep))
9755 {
9756 *fnamep = FullName_save(*fnamep, *p != NUL);
9757 vim_free(*bufp); /* free any allocated file name */
9758 *bufp = *fnamep;
9759 if (*fnamep == NULL)
9760 return -1;
9761 }
9762
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009763#ifdef WIN3264
9764# if _WIN32_WINNT >= 0x0500
9765 if (vim_strchr(*fnamep, '~') != NULL)
9766 {
9767 /* Expand 8.3 filename to full path. Needed to make sure the same
9768 * file does not have two different names.
9769 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
9770 p = alloc(_MAX_PATH + 1);
9771 if (p != NULL)
9772 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009773 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009774 {
9775 vim_free(*bufp);
9776 *bufp = *fnamep = p;
9777 }
9778 else
9779 vim_free(p);
9780 }
9781 }
9782# endif
9783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784 /* Append a path separator to a directory. */
9785 if (mch_isdir(*fnamep))
9786 {
9787 /* Make room for one or two extra characters. */
9788 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
9789 vim_free(*bufp); /* free any allocated file name */
9790 *bufp = *fnamep;
9791 if (*fnamep == NULL)
9792 return -1;
9793 add_pathsep(*fnamep);
9794 }
9795 }
9796
9797 /* ":." - path relative to the current directory */
9798 /* ":~" - path relative to the home directory */
9799 /* ":8" - shortname path - postponed till after */
9800 while (src[*usedlen] == ':'
9801 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
9802 {
9803 *usedlen += 2;
9804 if (c == '8')
9805 {
9806#ifdef WIN3264
9807 has_shortname = 1; /* Postpone this. */
9808#endif
9809 continue;
9810 }
9811 pbuf = NULL;
9812 /* Need full path first (use expand_env() to remove a "~/") */
9813 if (!has_fullname)
9814 {
9815 if (c == '.' && **fnamep == '~')
9816 p = pbuf = expand_env_save(*fnamep);
9817 else
9818 p = pbuf = FullName_save(*fnamep, FALSE);
9819 }
9820 else
9821 p = *fnamep;
9822
9823 has_fullname = 0;
9824
9825 if (p != NULL)
9826 {
9827 if (c == '.')
9828 {
9829 mch_dirname(dirname, MAXPATHL);
9830 s = shorten_fname(p, dirname);
9831 if (s != NULL)
9832 {
9833 *fnamep = s;
9834 if (pbuf != NULL)
9835 {
9836 vim_free(*bufp); /* free any allocated file name */
9837 *bufp = pbuf;
9838 pbuf = NULL;
9839 }
9840 }
9841 }
9842 else
9843 {
9844 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
9845 /* Only replace it when it starts with '~' */
9846 if (*dirname == '~')
9847 {
9848 s = vim_strsave(dirname);
9849 if (s != NULL)
9850 {
9851 *fnamep = s;
9852 vim_free(*bufp);
9853 *bufp = s;
9854 }
9855 }
9856 }
9857 vim_free(pbuf);
9858 }
9859 }
9860
9861 tail = gettail(*fnamep);
9862 *fnamelen = (int)STRLEN(*fnamep);
9863
9864 /* ":h" - head, remove "/file_name", can be repeated */
9865 /* Don't remove the first "/" or "c:\" */
9866 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
9867 {
9868 valid |= VALID_HEAD;
9869 *usedlen += 2;
9870 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009871 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009872 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873 *fnamelen = (int)(tail - *fnamep);
9874#ifdef VMS
9875 if (*fnamelen > 0)
9876 *fnamelen += 1; /* the path separator is part of the path */
9877#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009878 if (*fnamelen == 0)
9879 {
9880 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
9881 p = vim_strsave((char_u *)".");
9882 if (p == NULL)
9883 return -1;
9884 vim_free(*bufp);
9885 *bufp = *fnamep = tail = p;
9886 *fnamelen = 1;
9887 }
9888 else
9889 {
9890 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009891 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893 }
9894
9895 /* ":8" - shortname */
9896 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
9897 {
9898 *usedlen += 2;
9899#ifdef WIN3264
9900 has_shortname = 1;
9901#endif
9902 }
9903
9904#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009905 /*
9906 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907 */
9908 if (has_shortname)
9909 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009910 /* Copy the string if it is shortened by :h and when it wasn't copied
9911 * yet, because we are going to change it in place. Avoids changing
9912 * the buffer name for "%:8". */
9913 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914 {
9915 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +02009916 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917 return -1;
9918 vim_free(*bufp);
9919 *bufp = *fnamep = p;
9920 }
9921
9922 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +02009923 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 if (!has_fullname && !vim_isAbsName(*fnamep))
9925 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009926 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927 return -1;
9928 }
9929 else
9930 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009931 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932
Bram Moolenaardc935552011-08-17 15:23:23 +02009933 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009935 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 return -1;
9937
9938 if (l == 0)
9939 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009940 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009942 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943 return -1;
9944 }
9945 *fnamelen = l;
9946 }
9947 }
9948#endif /* WIN3264 */
9949
9950 /* ":t" - tail, just the basename */
9951 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
9952 {
9953 *usedlen += 2;
9954 *fnamelen -= (int)(tail - *fnamep);
9955 *fnamep = tail;
9956 }
9957
9958 /* ":e" - extension, can be repeated */
9959 /* ":r" - root, without extension, can be repeated */
9960 while (src[*usedlen] == ':'
9961 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
9962 {
9963 /* find a '.' in the tail:
9964 * - for second :e: before the current fname
9965 * - otherwise: The last '.'
9966 */
9967 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
9968 s = *fnamep - 2;
9969 else
9970 s = *fnamep + *fnamelen - 1;
9971 for ( ; s > tail; --s)
9972 if (s[0] == '.')
9973 break;
9974 if (src[*usedlen + 1] == 'e') /* :e */
9975 {
9976 if (s > tail)
9977 {
9978 *fnamelen += (int)(*fnamep - (s + 1));
9979 *fnamep = s + 1;
9980#ifdef VMS
9981 /* cut version from the extension */
9982 s = *fnamep + *fnamelen - 1;
9983 for ( ; s > *fnamep; --s)
9984 if (s[0] == ';')
9985 break;
9986 if (s > *fnamep)
9987 *fnamelen = s - *fnamep;
9988#endif
9989 }
9990 else if (*fnamep <= tail)
9991 *fnamelen = 0;
9992 }
9993 else /* :r */
9994 {
9995 if (s > tail) /* remove one extension */
9996 *fnamelen = (int)(s - *fnamep);
9997 }
9998 *usedlen += 2;
9999 }
10000
10001 /* ":s?pat?foo?" - substitute */
10002 /* ":gs?pat?foo?" - global substitute */
10003 if (src[*usedlen] == ':'
10004 && (src[*usedlen + 1] == 's'
10005 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
10006 {
10007 char_u *str;
10008 char_u *pat;
10009 char_u *sub;
10010 int sep;
10011 char_u *flags;
10012 int didit = FALSE;
10013
10014 flags = (char_u *)"";
10015 s = src + *usedlen + 2;
10016 if (src[*usedlen + 1] == 'g')
10017 {
10018 flags = (char_u *)"g";
10019 ++s;
10020 }
10021
10022 sep = *s++;
10023 if (sep)
10024 {
10025 /* find end of pattern */
10026 p = vim_strchr(s, sep);
10027 if (p != NULL)
10028 {
10029 pat = vim_strnsave(s, (int)(p - s));
10030 if (pat != NULL)
10031 {
10032 s = p + 1;
10033 /* find end of substitution */
10034 p = vim_strchr(s, sep);
10035 if (p != NULL)
10036 {
10037 sub = vim_strnsave(s, (int)(p - s));
10038 str = vim_strnsave(*fnamep, *fnamelen);
10039 if (sub != NULL && str != NULL)
10040 {
10041 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010042 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043 if (s != NULL)
10044 {
10045 *fnamep = s;
10046 *fnamelen = (int)STRLEN(s);
10047 vim_free(*bufp);
10048 *bufp = s;
10049 didit = TRUE;
10050 }
10051 }
10052 vim_free(sub);
10053 vim_free(str);
10054 }
10055 vim_free(pat);
10056 }
10057 }
10058 /* after using ":s", repeat all the modifiers */
10059 if (didit)
10060 goto repeat;
10061 }
10062 }
10063
Bram Moolenaar26df0922014-02-23 23:39:13 +010010064 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10065 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010066 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010067 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010068 if (c != NUL)
10069 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010070 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010071 if (c != NUL)
10072 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010073 if (p == NULL)
10074 return -1;
10075 vim_free(*bufp);
10076 *bufp = *fnamep = p;
10077 *fnamelen = (int)STRLEN(p);
10078 *usedlen += 2;
10079 }
10080
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081 return valid;
10082}
10083
10084/*
10085 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010086 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087 * "flags" can be "g" to do a global substitute.
10088 * Returns an allocated string, NULL for error.
10089 */
10090 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010091do_string_sub(
10092 char_u *str,
10093 char_u *pat,
10094 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010095 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010096 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097{
10098 int sublen;
10099 regmatch_T regmatch;
10100 int i;
10101 int do_all;
10102 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010103 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104 garray_T ga;
10105 char_u *ret;
10106 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010107 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108
10109 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
10110 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010111 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112
10113 ga_init2(&ga, 1, 200);
10114
10115 do_all = (flags[0] == 'g');
10116
10117 regmatch.rm_ic = p_ic;
10118 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10119 if (regmatch.regprog != NULL)
10120 {
10121 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010122 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
10124 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010010125 /* Skip empty match except for first match. */
10126 if (regmatch.startp[0] == regmatch.endp[0])
10127 {
10128 if (zero_width == regmatch.startp[0])
10129 {
10130 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020010131 i = MB_PTR2LEN(tail);
10132 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
10133 (size_t)i);
10134 ga.ga_len += i;
10135 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010136 continue;
10137 }
10138 zero_width = regmatch.startp[0];
10139 }
10140
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141 /*
10142 * Get some space for a temporary buffer to do the substitution
10143 * into. It will contain:
10144 * - The text up to where the match is.
10145 * - The substituted text.
10146 * - The text after the match.
10147 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010148 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010010149 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
10151 {
10152 ga_clear(&ga);
10153 break;
10154 }
10155
10156 /* copy the text up to where the match is */
10157 i = (int)(regmatch.startp[0] - tail);
10158 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10159 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010160 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161 + ga.ga_len + i, TRUE, TRUE, FALSE);
10162 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010163 tail = regmatch.endp[0];
10164 if (*tail == NUL)
10165 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166 if (!do_all)
10167 break;
10168 }
10169
10170 if (ga.ga_data != NULL)
10171 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10172
Bram Moolenaar473de612013-06-08 18:19:48 +020010173 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174 }
10175
10176 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10177 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010178 if (p_cpo == empty_option)
10179 p_cpo = save_cpo;
10180 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010181 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010182 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183
10184 return ret;
10185}
10186
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010187 static int
10188filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10189{
10190 typval_T rettv;
10191 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010192 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010193
10194 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10195 argv[0] = vimvars[VV_KEY].vv_tv;
10196 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010197 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10198 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010199 if (map)
10200 {
10201 /* map(): replace the list item value */
10202 clear_tv(tv);
10203 rettv.v_lock = 0;
10204 *tv = rettv;
10205 }
10206 else
10207 {
10208 int error = FALSE;
10209
10210 /* filter(): when expr is zero remove the item */
10211 *remp = (get_tv_number_chk(&rettv, &error) == 0);
10212 clear_tv(&rettv);
10213 /* On type error, nothing has been removed; return FAIL to stop the
10214 * loop. The error message was given by get_tv_number_chk(). */
10215 if (error)
10216 goto theend;
10217 }
10218 retval = OK;
10219theend:
10220 clear_tv(&vimvars[VV_VAL].vv_tv);
10221 return retval;
10222}
10223
10224
10225/*
10226 * Implementation of map() and filter().
10227 */
10228 void
10229filter_map(typval_T *argvars, typval_T *rettv, int map)
10230{
10231 typval_T *expr;
10232 listitem_T *li, *nli;
10233 list_T *l = NULL;
10234 dictitem_T *di;
10235 hashtab_T *ht;
10236 hashitem_T *hi;
10237 dict_T *d = NULL;
10238 typval_T save_val;
10239 typval_T save_key;
10240 int rem;
10241 int todo;
10242 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10243 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10244 : N_("filter() argument"));
10245 int save_did_emsg;
10246 int idx = 0;
10247
10248 if (argvars[0].v_type == VAR_LIST)
10249 {
10250 if ((l = argvars[0].vval.v_list) == NULL
10251 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
10252 return;
10253 }
10254 else if (argvars[0].v_type == VAR_DICT)
10255 {
10256 if ((d = argvars[0].vval.v_dict) == NULL
10257 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
10258 return;
10259 }
10260 else
10261 {
10262 EMSG2(_(e_listdictarg), ermsg);
10263 return;
10264 }
10265
10266 expr = &argvars[1];
10267 /* On type errors, the preceding call has already displayed an error
10268 * message. Avoid a misleading error message for an empty string that
10269 * was not passed as argument. */
10270 if (expr->v_type != VAR_UNKNOWN)
10271 {
10272 prepare_vimvar(VV_VAL, &save_val);
10273
10274 /* We reset "did_emsg" to be able to detect whether an error
10275 * occurred during evaluation of the expression. */
10276 save_did_emsg = did_emsg;
10277 did_emsg = FALSE;
10278
10279 prepare_vimvar(VV_KEY, &save_key);
10280 if (argvars[0].v_type == VAR_DICT)
10281 {
10282 vimvars[VV_KEY].vv_type = VAR_STRING;
10283
10284 ht = &d->dv_hashtab;
10285 hash_lock(ht);
10286 todo = (int)ht->ht_used;
10287 for (hi = ht->ht_array; todo > 0; ++hi)
10288 {
10289 if (!HASHITEM_EMPTY(hi))
10290 {
10291 int r;
10292
10293 --todo;
10294 di = HI2DI(hi);
10295 if (map &&
10296 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10297 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
10298 break;
10299 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10300 r = filter_map_one(&di->di_tv, expr, map, &rem);
10301 clear_tv(&vimvars[VV_KEY].vv_tv);
10302 if (r == FAIL || did_emsg)
10303 break;
10304 if (!map && rem)
10305 {
10306 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10307 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10308 break;
10309 dictitem_remove(d, di);
10310 }
10311 }
10312 }
10313 hash_unlock(ht);
10314 }
10315 else
10316 {
10317 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10318
10319 for (li = l->lv_first; li != NULL; li = nli)
10320 {
10321 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
10322 break;
10323 nli = li->li_next;
10324 vimvars[VV_KEY].vv_nr = idx;
10325 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10326 || did_emsg)
10327 break;
10328 if (!map && rem)
10329 listitem_remove(l, li);
10330 ++idx;
10331 }
10332 }
10333
10334 restore_vimvar(VV_KEY, &save_key);
10335 restore_vimvar(VV_VAL, &save_val);
10336
10337 did_emsg |= save_did_emsg;
10338 }
10339
10340 copy_tv(&argvars[0], rettv);
10341}
10342
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */