blob: 0bb18824142e0bee6e0088d8036788db0d93260a [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 Moolenaar33570922005-01-25 22:26:29 +0000190};
191
192/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000193#define vv_type vv_di.di_tv.v_type
194#define vv_nr vv_di.di_tv.vval.v_number
195#define vv_float vv_di.di_tv.vval.v_float
196#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000197#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200198#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000199#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000200
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200201static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000202#define vimvarht vimvardict.dv_hashtab
203
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100204static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
205static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
206static char_u *skip_var_one(char_u *arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100207static void list_glob_vars(int *first);
208static void list_buf_vars(int *first);
209static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000210#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100211static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000212#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100213static void list_vim_vars(int *first);
214static void list_script_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100215static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
216static 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 +0100217static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
218static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100219static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
220static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
221static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
222static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000223
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100224static int eval2(char_u **arg, typval_T *rettv, int evaluate);
225static int eval3(char_u **arg, typval_T *rettv, int evaluate);
226static int eval4(char_u **arg, typval_T *rettv, int evaluate);
227static int eval5(char_u **arg, typval_T *rettv, int evaluate);
228static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
229static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000230
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100231static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100232static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
233static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100234static int free_unref_items(int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100235static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100236static int get_env_len(char_u **arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100237static 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 +0200238static void check_vars(char_u *name, int len);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100239static typval_T *alloc_string_tv(char_u *string);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100240static void delete_var(hashtab_T *ht, hashitem_T *hi);
241static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
242static 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 +0100243static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200244
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200245/* for VIM_VERSION_ defines */
246#include "version.h"
247
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100248
249#if defined(EBCDIC) || defined(PROTO)
250/*
251 * Compare struct fst by function name.
252 */
253 static int
254compare_func_name(const void *s1, const void *s2)
255{
256 struct fst *p1 = (struct fst *)s1;
257 struct fst *p2 = (struct fst *)s2;
258
259 return STRCMP(p1->f_name, p2->f_name);
260}
261
262/*
263 * Sort the function table by function name.
264 * The sorting of the table above is ASCII dependant.
265 * On machines using EBCDIC we have to sort it.
266 */
267 static void
268sortFunctions(void)
269{
270 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
271
272 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
273}
274#endif
275
276
Bram Moolenaar33570922005-01-25 22:26:29 +0000277/*
278 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000279 */
280 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100281eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000282{
Bram Moolenaar33570922005-01-25 22:26:29 +0000283 int i;
284 struct vimvar *p;
285
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200286 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
287 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200288 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000289 hash_init(&compat_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200290 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000291
292 for (i = 0; i < VV_LEN; ++i)
293 {
294 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200295 if (STRLEN(p->vv_name) > 16)
296 {
Bram Moolenaarde330112017-01-08 20:50:52 +0100297 IEMSG("INTERNAL: name too long, increase size of dictitem16_T");
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200298 getout(1);
299 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000300 STRCPY(p->vv_di.di_key, p->vv_name);
301 if (p->vv_flags & VV_RO)
302 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
303 else if (p->vv_flags & VV_RO_SBX)
304 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
305 else
306 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000307
308 /* add to v: scope dict, unless the value is not always available */
309 if (p->vv_type != VAR_UNKNOWN)
310 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000311 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000312 /* add to compat scope dict */
313 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000314 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100315 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
316
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000317 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100318 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200319 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100320 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100321
322 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
323 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
324 set_vim_var_nr(VV_NONE, VVAL_NONE);
325 set_vim_var_nr(VV_NULL, VVAL_NULL);
326
Bram Moolenaarf562e722016-07-19 17:25:25 +0200327 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
328 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
329 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
330 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
331 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
332 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
333 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
334 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
335 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
336 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
337
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200338 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200339
340#ifdef EBCDIC
341 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100342 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200343 */
344 sortFunctions();
345#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000346}
347
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000348#if defined(EXITFREE) || defined(PROTO)
349 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100350eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000351{
352 int i;
353 struct vimvar *p;
354
355 for (i = 0; i < VV_LEN; ++i)
356 {
357 p = &vimvars[i];
358 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000359 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000360 vim_free(p->vv_str);
361 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000362 }
363 else if (p->vv_di.di_tv.v_type == VAR_LIST)
364 {
365 list_unref(p->vv_list);
366 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000367 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000368 }
369 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000370 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000371 hash_clear(&compat_hashtab);
372
Bram Moolenaard9fba312005-06-26 22:34:35 +0000373 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100374# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200375 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100376# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000377
378 /* global variables */
379 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000380
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000381 /* autoloaded script names */
382 ga_clear_strings(&ga_loaded);
383
Bram Moolenaarcca74132013-09-25 21:00:28 +0200384 /* Script-local variables. First clear all the variables and in a second
385 * loop free the scriptvar_T, because a variable in one script might hold
386 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200387 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200388 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200389 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200390 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200391 ga_clear(&ga_scripts);
392
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000393 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200394 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000395
396 /* functions */
397 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000398}
399#endif
400
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401
402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 * Set an internal variable to a string value. Creates the variable if it does
404 * not already exist.
405 */
406 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100407set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000409 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000410 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411
412 val = vim_strsave(value);
413 if (val != NULL)
414 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000415 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000416 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000418 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000419 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 }
421 }
422}
423
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000424static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200425#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000426static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000427static char_u *redir_endp = NULL;
428static char_u *redir_varname = NULL;
429
430/*
431 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100432 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000433 * Returns OK if successfully completed the setup. FAIL otherwise.
434 */
435 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100436var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000437{
438 int save_emsg;
439 int err;
440 typval_T tv;
441
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000442 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000443 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000444 {
445 EMSG(_(e_invarg));
446 return FAIL;
447 }
448
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000449 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000450 redir_varname = vim_strsave(name);
451 if (redir_varname == NULL)
452 return FAIL;
453
454 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
455 if (redir_lval == NULL)
456 {
457 var_redir_stop();
458 return FAIL;
459 }
460
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000461 /* The output is stored in growarray "redir_ga" until redirection ends. */
462 ga_init2(&redir_ga, (int)sizeof(char), 500);
463
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000464 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100465 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000466 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000467 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
468 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200469 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000470 if (redir_endp != NULL && *redir_endp != NUL)
471 /* Trailing characters are present after the variable name */
472 EMSG(_(e_trailing));
473 else
474 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000475 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000476 var_redir_stop();
477 return FAIL;
478 }
479
480 /* check if we can write to the variable: set it to or append an empty
481 * string */
482 save_emsg = did_emsg;
483 did_emsg = FALSE;
484 tv.v_type = VAR_STRING;
485 tv.vval.v_string = (char_u *)"";
486 if (append)
487 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
488 else
489 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200490 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000491 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000492 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000493 if (err)
494 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000495 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000496 var_redir_stop();
497 return FAIL;
498 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000499
500 return OK;
501}
502
503/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000504 * Append "value[value_len]" to the variable set by var_redir_start().
505 * The actual appending is postponed until redirection ends, because the value
506 * appended may in fact be the string we write to, changing it may cause freed
507 * memory to be used:
508 * :redir => foo
509 * :let foo
510 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000511 */
512 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100513var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000514{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000515 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000516
517 if (redir_lval == NULL)
518 return;
519
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000520 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000521 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000522 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000523 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000524
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000525 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000526 {
527 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000528 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000529 }
530 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000531 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000532}
533
534/*
535 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000536 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000537 */
538 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100539var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000540{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000541 typval_T tv;
542
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200543 if (EVALCMD_BUSY)
544 {
545 redir_lval = NULL;
546 return;
547 }
548
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000549 if (redir_lval != NULL)
550 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000551 /* If there was no error: assign the text to the variable. */
552 if (redir_endp != NULL)
553 {
554 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
555 tv.v_type = VAR_STRING;
556 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200557 /* Call get_lval() again, if it's inside a Dict or List it may
558 * have changed. */
559 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100560 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200561 if (redir_endp != NULL && redir_lval->ll_name != NULL)
562 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
563 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000564 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000565
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000566 /* free the collected output */
567 vim_free(redir_ga.ga_data);
568 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000569
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000570 vim_free(redir_lval);
571 redir_lval = NULL;
572 }
573 vim_free(redir_varname);
574 redir_varname = NULL;
575}
576
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577# if defined(FEAT_MBYTE) || defined(PROTO)
578 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100579eval_charconvert(
580 char_u *enc_from,
581 char_u *enc_to,
582 char_u *fname_from,
583 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584{
585 int err = FALSE;
586
587 set_vim_var_string(VV_CC_FROM, enc_from, -1);
588 set_vim_var_string(VV_CC_TO, enc_to, -1);
589 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
590 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
591 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
592 err = TRUE;
593 set_vim_var_string(VV_CC_FROM, NULL, -1);
594 set_vim_var_string(VV_CC_TO, NULL, -1);
595 set_vim_var_string(VV_FNAME_IN, NULL, -1);
596 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
597
598 if (err)
599 return FAIL;
600 return OK;
601}
602# endif
603
604# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
605 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100606eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607{
608 int err = FALSE;
609
610 set_vim_var_string(VV_FNAME_IN, fname, -1);
611 set_vim_var_string(VV_CMDARG, args, -1);
612 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
613 err = TRUE;
614 set_vim_var_string(VV_FNAME_IN, NULL, -1);
615 set_vim_var_string(VV_CMDARG, NULL, -1);
616
617 if (err)
618 {
619 mch_remove(fname);
620 return FAIL;
621 }
622 return OK;
623}
624# endif
625
626# if defined(FEAT_DIFF) || defined(PROTO)
627 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100628eval_diff(
629 char_u *origfile,
630 char_u *newfile,
631 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632{
633 int err = FALSE;
634
635 set_vim_var_string(VV_FNAME_IN, origfile, -1);
636 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
637 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
638 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
639 set_vim_var_string(VV_FNAME_IN, NULL, -1);
640 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
641 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
642}
643
644 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100645eval_patch(
646 char_u *origfile,
647 char_u *difffile,
648 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649{
650 int err;
651
652 set_vim_var_string(VV_FNAME_IN, origfile, -1);
653 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
654 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
655 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
656 set_vim_var_string(VV_FNAME_IN, NULL, -1);
657 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
658 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
659}
660# endif
661
662/*
663 * Top level evaluation function, returning a boolean.
664 * Sets "error" to TRUE if there was an error.
665 * Return TRUE or FALSE.
666 */
667 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100668eval_to_bool(
669 char_u *arg,
670 int *error,
671 char_u **nextcmd,
672 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673{
Bram Moolenaar33570922005-01-25 22:26:29 +0000674 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200675 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676
677 if (skip)
678 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000679 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 else
682 {
683 *error = FALSE;
684 if (!skip)
685 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000686 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000687 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 }
689 }
690 if (skip)
691 --emsg_skip;
692
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200693 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694}
695
696/*
697 * Top level evaluation function, returning a string. If "skip" is TRUE,
698 * only parsing to "nextcmd" is done, without reporting errors. Return
699 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
700 */
701 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100702eval_to_string_skip(
703 char_u *arg,
704 char_u **nextcmd,
705 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706{
Bram Moolenaar33570922005-01-25 22:26:29 +0000707 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 char_u *retval;
709
710 if (skip)
711 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000712 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 retval = NULL;
714 else
715 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000716 retval = vim_strsave(get_tv_string(&tv));
717 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 }
719 if (skip)
720 --emsg_skip;
721
722 return retval;
723}
724
725/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000726 * Skip over an expression at "*pp".
727 * Return FAIL for an error, OK otherwise.
728 */
729 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100730skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000731{
Bram Moolenaar33570922005-01-25 22:26:29 +0000732 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000733
734 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000735 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000736}
737
738/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000740 * When "convert" is TRUE convert a List into a sequence of lines and convert
741 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 * Return pointer to allocated memory, or NULL for failure.
743 */
744 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100745eval_to_string(
746 char_u *arg,
747 char_u **nextcmd,
748 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749{
Bram Moolenaar33570922005-01-25 22:26:29 +0000750 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000752 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000753#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000754 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000757 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 retval = NULL;
759 else
760 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000761 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000762 {
763 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000764 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200765 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200766 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200767 if (tv.vval.v_list->lv_len > 0)
768 ga_append(&ga, NL);
769 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000770 ga_append(&ga, NUL);
771 retval = (char_u *)ga.ga_data;
772 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000773#ifdef FEAT_FLOAT
774 else if (convert && tv.v_type == VAR_FLOAT)
775 {
776 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
777 retval = vim_strsave(numbuf);
778 }
779#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000780 else
781 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000782 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 }
784
785 return retval;
786}
787
788/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000789 * Call eval_to_string() without using current local variables and using
790 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 */
792 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100793eval_to_string_safe(
794 char_u *arg,
795 char_u **nextcmd,
796 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797{
798 char_u *retval;
799 void *save_funccalp;
800
801 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000802 if (use_sandbox)
803 ++sandbox;
804 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000805 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000806 if (use_sandbox)
807 --sandbox;
808 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 restore_funccal(save_funccalp);
810 return retval;
811}
812
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813/*
814 * Top level evaluation function, returning a number.
815 * Evaluates "expr" silently.
816 * Returns -1 for an error.
817 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200818 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100819eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820{
Bram Moolenaar33570922005-01-25 22:26:29 +0000821 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200822 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000823 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824
825 ++emsg_off;
826
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000827 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 retval = -1;
829 else
830 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000831 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000832 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 }
834 --emsg_off;
835
836 return retval;
837}
838
Bram Moolenaara40058a2005-07-11 22:42:07 +0000839/*
840 * Prepare v: variable "idx" to be used.
841 * Save the current typeval in "save_tv".
842 * When not used yet add the variable to the v: hashtable.
843 */
844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100845prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000846{
847 *save_tv = vimvars[idx].vv_tv;
848 if (vimvars[idx].vv_type == VAR_UNKNOWN)
849 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
850}
851
852/*
853 * Restore v: variable "idx" to typeval "save_tv".
854 * When no longer defined, remove the variable from the v: hashtable.
855 */
856 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100857restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000858{
859 hashitem_T *hi;
860
Bram Moolenaara40058a2005-07-11 22:42:07 +0000861 vimvars[idx].vv_tv = *save_tv;
862 if (vimvars[idx].vv_type == VAR_UNKNOWN)
863 {
864 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
865 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100866 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +0000867 else
868 hash_remove(&vimvarht, hi);
869 }
870}
871
Bram Moolenaar3c56a962006-03-12 22:19:04 +0000872#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000873/*
874 * Evaluate an expression to a list with suggestions.
875 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000876 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000877 */
878 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100879eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000880{
881 typval_T save_val;
882 typval_T rettv;
883 list_T *list = NULL;
884 char_u *p = skipwhite(expr);
885
886 /* Set "v:val" to the bad word. */
887 prepare_vimvar(VV_VAL, &save_val);
888 vimvars[VV_VAL].vv_type = VAR_STRING;
889 vimvars[VV_VAL].vv_str = badword;
890 if (p_verbose == 0)
891 ++emsg_off;
892
893 if (eval1(&p, &rettv, TRUE) == OK)
894 {
895 if (rettv.v_type != VAR_LIST)
896 clear_tv(&rettv);
897 else
898 list = rettv.vval.v_list;
899 }
900
901 if (p_verbose == 0)
902 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000903 restore_vimvar(VV_VAL, &save_val);
904
905 return list;
906}
907
908/*
909 * "list" is supposed to contain two items: a word and a number. Return the
910 * word in "pp" and the number as the return value.
911 * Return -1 if anything isn't right.
912 * Used to get the good word and score from the eval_spell_expr() result.
913 */
914 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100915get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000916{
917 listitem_T *li;
918
919 li = list->lv_first;
920 if (li == NULL)
921 return -1;
922 *pp = get_tv_string(&li->li_tv);
923
924 li = li->li_next;
925 if (li == NULL)
926 return -1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200927 return (int)get_tv_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000928}
929#endif
930
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000931/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000932 * Top level evaluation function.
933 * Returns an allocated typval_T with the result.
934 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000935 */
936 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100937eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000938{
939 typval_T *tv;
940
941 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000942 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000943 {
944 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +0000945 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000946 }
947
948 return tv;
949}
950
951
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000953 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000954 * Uses argv[argc] for the function arguments. Only Number and String
955 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000956 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200958 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100959call_vim_function(
960 char_u *func,
961 int argc,
962 char_u **argv,
963 int safe, /* use the sandbox */
964 int str_arg_only, /* all arguments are strings */
965 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966{
Bram Moolenaar33570922005-01-25 22:26:29 +0000967 typval_T *argvars;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200968 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 int len;
970 int i;
971 int doesrange;
972 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000973 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000975 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000977 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978
979 for (i = 0; i < argc; i++)
980 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000981 /* Pass a NULL or empty argument as an empty string */
982 if (argv[i] == NULL || *argv[i] == NUL)
983 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000984 argvars[i].v_type = VAR_STRING;
985 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000986 continue;
987 }
988
Bram Moolenaar0cbba942012-07-25 16:47:03 +0200989 if (str_arg_only)
990 len = 0;
991 else
992 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100993 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 if (len != 0 && len == (int)STRLEN(argv[i]))
995 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000996 argvars[i].v_type = VAR_NUMBER;
997 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 }
999 else
1000 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001001 argvars[i].v_type = VAR_STRING;
1002 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 }
1004 }
1005
1006 if (safe)
1007 {
1008 save_funccalp = save_funccal();
1009 ++sandbox;
1010 }
1011
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001012 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001013 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001015 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 if (safe)
1017 {
1018 --sandbox;
1019 restore_funccal(save_funccalp);
1020 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001021 vim_free(argvars);
1022
1023 if (ret == FAIL)
1024 clear_tv(rettv);
1025
1026 return ret;
1027}
1028
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001029/*
1030 * Call vimL function "func" and return the result as a number.
1031 * Returns -1 when calling the function fails.
1032 * Uses argv[argc] for the function arguments.
1033 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001034 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001035call_func_retnr(
1036 char_u *func,
1037 int argc,
1038 char_u **argv,
1039 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001040{
1041 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001042 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001043
1044 /* All arguments are passed as strings, no conversion to number. */
1045 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1046 return -1;
1047
1048 retval = get_tv_number_chk(&rettv, NULL);
1049 clear_tv(&rettv);
1050 return retval;
1051}
1052
1053#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1054 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1055
Bram Moolenaar4f688582007-07-24 12:34:30 +00001056# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001057/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001058 * Call vimL function "func" and return the result as a string.
1059 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001060 * Uses argv[argc] for the function arguments.
1061 */
1062 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001063call_func_retstr(
1064 char_u *func,
1065 int argc,
1066 char_u **argv,
1067 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001068{
1069 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001070 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001071
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001072 /* All arguments are passed as strings, no conversion to number. */
1073 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001074 return NULL;
1075
1076 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001077 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 return retval;
1079}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001080# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001081
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001082/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001083 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001084 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001085 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001086 */
1087 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001088call_func_retlist(
1089 char_u *func,
1090 int argc,
1091 char_u **argv,
1092 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001093{
1094 typval_T rettv;
1095
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001096 /* All arguments are passed as strings, no conversion to number. */
1097 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001098 return NULL;
1099
1100 if (rettv.v_type != VAR_LIST)
1101 {
1102 clear_tv(&rettv);
1103 return NULL;
1104 }
1105
1106 return rettv.vval.v_list;
1107}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108#endif
1109
Bram Moolenaar05159a02005-02-26 23:04:13 +00001110
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111#ifdef FEAT_FOLDING
1112/*
1113 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1114 * it in "*cp". Doesn't give error messages.
1115 */
1116 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001117eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118{
Bram Moolenaar33570922005-01-25 22:26:29 +00001119 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001120 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001122 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1123 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124
1125 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001126 if (use_sandbox)
1127 ++sandbox;
1128 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001130 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 retval = 0;
1132 else
1133 {
1134 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001135 if (tv.v_type == VAR_NUMBER)
1136 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001137 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 retval = 0;
1139 else
1140 {
1141 /* If the result is a string, check if there is a non-digit before
1142 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001143 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 if (!VIM_ISDIGIT(*s) && *s != '-')
1145 *cp = *s++;
1146 retval = atol((char *)s);
1147 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001148 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 }
1150 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001151 if (use_sandbox)
1152 --sandbox;
1153 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001155 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156}
1157#endif
1158
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001160 * ":let" list all variable values
1161 * ":let var1 var2" list variable values
1162 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001163 * ":let var += expr" assignment command.
1164 * ":let var -= expr" assignment command.
1165 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001166 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 */
1168 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001169ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170{
1171 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001172 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001173 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001175 int var_count = 0;
1176 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001177 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001178 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001179 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180
Bram Moolenaardb552d602006-03-23 22:59:57 +00001181 argend = skip_var_list(arg, &var_count, &semicolon);
1182 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001183 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001184 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1185 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001186 expr = skipwhite(argend);
1187 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1188 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001190 /*
1191 * ":let" without "=": list variables
1192 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001193 if (*arg == '[')
1194 EMSG(_(e_invarg));
1195 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001196 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001197 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001198 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001199 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001200 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001201 list_glob_vars(&first);
1202 list_buf_vars(&first);
1203 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001204#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001205 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001206#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001207 list_script_vars(&first);
1208 list_func_vars(&first);
1209 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 eap->nextcmd = check_nextcmd(arg);
1212 }
1213 else
1214 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001215 op[0] = '=';
1216 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001217 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001218 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001219 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1220 op[0] = *expr; /* +=, -= or .= */
1221 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001222 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001223 else
1224 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001225
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 if (eap->skip)
1227 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001228 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 if (eap->skip)
1230 {
1231 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001232 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 --emsg_skip;
1234 }
1235 else if (i != FAIL)
1236 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001237 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001238 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001239 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 }
1241 }
1242}
1243
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001244/*
1245 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1246 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001247 * When "nextchars" is not NULL it points to a string with characters that
1248 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1249 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001250 * Returns OK or FAIL;
1251 */
1252 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001253ex_let_vars(
1254 char_u *arg_start,
1255 typval_T *tv,
1256 int copy, /* copy values from "tv", don't move */
1257 int semicolon, /* from skip_var_list() */
1258 int var_count, /* from skip_var_list() */
1259 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001260{
1261 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001262 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001263 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001264 listitem_T *item;
1265 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001266
1267 if (*arg != '[')
1268 {
1269 /*
1270 * ":let var = expr" or ":for var in list"
1271 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001272 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001273 return FAIL;
1274 return OK;
1275 }
1276
1277 /*
1278 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1279 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001280 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001281 {
1282 EMSG(_(e_listreq));
1283 return FAIL;
1284 }
1285
1286 i = list_len(l);
1287 if (semicolon == 0 && var_count < i)
1288 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001289 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001290 return FAIL;
1291 }
1292 if (var_count - semicolon > i)
1293 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001294 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001295 return FAIL;
1296 }
1297
1298 item = l->lv_first;
1299 while (*arg != ']')
1300 {
1301 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001302 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001303 item = item->li_next;
1304 if (arg == NULL)
1305 return FAIL;
1306
1307 arg = skipwhite(arg);
1308 if (*arg == ';')
1309 {
1310 /* Put the rest of the list (may be empty) in the var after ';'.
1311 * Create a new list for this. */
1312 l = list_alloc();
1313 if (l == NULL)
1314 return FAIL;
1315 while (item != NULL)
1316 {
1317 list_append_tv(l, &item->li_tv);
1318 item = item->li_next;
1319 }
1320
1321 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001322 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001323 ltv.vval.v_list = l;
1324 l->lv_refcount = 1;
1325
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001326 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1327 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001328 clear_tv(&ltv);
1329 if (arg == NULL)
1330 return FAIL;
1331 break;
1332 }
1333 else if (*arg != ',' && *arg != ']')
1334 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001335 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001336 return FAIL;
1337 }
1338 }
1339
1340 return OK;
1341}
1342
1343/*
1344 * Skip over assignable variable "var" or list of variables "[var, var]".
1345 * Used for ":let varvar = expr" and ":for varvar in expr".
1346 * For "[var, var]" increment "*var_count" for each variable.
1347 * for "[var, var; var]" set "semicolon".
1348 * Return NULL for an error.
1349 */
1350 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001351skip_var_list(
1352 char_u *arg,
1353 int *var_count,
1354 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001355{
1356 char_u *p, *s;
1357
1358 if (*arg == '[')
1359 {
1360 /* "[var, var]": find the matching ']'. */
1361 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001362 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001363 {
1364 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1365 s = skip_var_one(p);
1366 if (s == p)
1367 {
1368 EMSG2(_(e_invarg2), p);
1369 return NULL;
1370 }
1371 ++*var_count;
1372
1373 p = skipwhite(s);
1374 if (*p == ']')
1375 break;
1376 else if (*p == ';')
1377 {
1378 if (*semicolon == 1)
1379 {
1380 EMSG(_("Double ; in list of variables"));
1381 return NULL;
1382 }
1383 *semicolon = 1;
1384 }
1385 else if (*p != ',')
1386 {
1387 EMSG2(_(e_invarg2), p);
1388 return NULL;
1389 }
1390 }
1391 return p + 1;
1392 }
1393 else
1394 return skip_var_one(arg);
1395}
1396
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001397/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001398 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001399 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001400 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001401 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001402skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001403{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001404 if (*arg == '@' && arg[1] != NUL)
1405 return arg + 2;
1406 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1407 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001408}
1409
Bram Moolenaara7043832005-01-21 11:56:39 +00001410/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001411 * List variables for hashtab "ht" with prefix "prefix".
1412 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001413 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001414 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001415list_hashtable_vars(
1416 hashtab_T *ht,
1417 char_u *prefix,
1418 int empty,
1419 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001420{
Bram Moolenaar33570922005-01-25 22:26:29 +00001421 hashitem_T *hi;
1422 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001423 int todo;
1424
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001425 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001426 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1427 {
1428 if (!HASHITEM_EMPTY(hi))
1429 {
1430 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001431 di = HI2DI(hi);
1432 if (empty || di->di_tv.v_type != VAR_STRING
1433 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001434 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001435 }
1436 }
1437}
1438
1439/*
1440 * List global variables.
1441 */
1442 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001443list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001444{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001445 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001446}
1447
1448/*
1449 * List buffer variables.
1450 */
1451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001452list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001453{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001454 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001455 TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001456}
1457
1458/*
1459 * List window variables.
1460 */
1461 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001462list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001463{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001464 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001465 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001466}
1467
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001468#ifdef FEAT_WINDOWS
1469/*
1470 * List tab page variables.
1471 */
1472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001473list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001474{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001475 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001476 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001477}
1478#endif
1479
Bram Moolenaara7043832005-01-21 11:56:39 +00001480/*
1481 * List Vim variables.
1482 */
1483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001484list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001485{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001486 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001487}
1488
1489/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001490 * List script-local variables, if there is a script.
1491 */
1492 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001493list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001494{
1495 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001496 list_hashtable_vars(&SCRIPT_VARS(current_SID),
1497 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001498}
1499
1500/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001501 * List variables in "arg".
1502 */
1503 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001504list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001505{
1506 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001507 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001508 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001509 char_u *name_start;
1510 char_u *arg_subsc;
1511 char_u *tofree;
1512 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001513
1514 while (!ends_excmd(*arg) && !got_int)
1515 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001516 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001517 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001518 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001519 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1520 {
1521 emsg_severe = TRUE;
1522 EMSG(_(e_trailing));
1523 break;
1524 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001525 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001526 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001527 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001528 /* get_name_len() takes care of expanding curly braces */
1529 name_start = name = arg;
1530 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1531 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001532 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001533 /* This is mainly to keep test 49 working: when expanding
1534 * curly braces fails overrule the exception error message. */
1535 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001536 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001537 emsg_severe = TRUE;
1538 EMSG2(_(e_invarg2), arg);
1539 break;
1540 }
1541 error = TRUE;
1542 }
1543 else
1544 {
1545 if (tofree != NULL)
1546 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001547 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001548 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001549 else
1550 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001551 /* handle d.key, l[idx], f(expr) */
1552 arg_subsc = arg;
1553 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001554 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001555 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001556 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001557 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001558 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001559 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001560 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001561 case 'g': list_glob_vars(first); break;
1562 case 'b': list_buf_vars(first); break;
1563 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001564#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001565 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001566#endif
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 != '.'))
1711 EMSG2(_(e_letwrong), op);
1712 else
1713 {
1714 if (opt_type == 1) /* number */
1715 {
1716 if (*op == '+')
1717 n = numval + n;
1718 else
1719 n = numval - n;
1720 }
1721 else if (opt_type == 0 && stringval != NULL) /* string */
1722 {
1723 s = concat_str(stringval, s);
1724 vim_free(stringval);
1725 stringval = s;
1726 }
1727 }
1728 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001729 if (s != NULL)
1730 {
1731 set_option_value(arg, n, s, opt_flags);
1732 arg_end = p;
1733 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001734 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001735 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001736 }
1737 }
1738
1739 /*
1740 * ":let @r = expr": Set register contents.
1741 */
1742 else if (*arg == '@')
1743 {
1744 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001745 if (op != NULL && (*op == '+' || *op == '-'))
1746 EMSG2(_(e_letwrong), op);
1747 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001748 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001749 EMSG(_(e_letunexp));
1750 else
1751 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001752 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001753 char_u *s;
1754
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001755 p = get_tv_string_chk(tv);
1756 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001757 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02001758 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001759 if (s != NULL)
1760 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001761 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001762 vim_free(s);
1763 }
1764 }
1765 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001766 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001767 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001768 arg_end = arg + 1;
1769 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001770 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001771 }
1772 }
1773
1774 /*
1775 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001776 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001777 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001778 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001779 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001780 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001781
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001782 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001783 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001784 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001785 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1786 EMSG(_(e_letunexp));
1787 else
1788 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001789 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001790 arg_end = p;
1791 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001792 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001793 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001794 }
1795
1796 else
1797 EMSG2(_(e_invarg2), arg);
1798
1799 return arg_end;
1800}
1801
1802/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001803 * Get an lval: variable, Dict item or List item that can be assigned a value
1804 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1805 * "name.key", "name.key[expr]" etc.
1806 * Indexing only works if "name" is an existing List or Dictionary.
1807 * "name" points to the start of the name.
1808 * If "rettv" is not NULL it points to the value to be assigned.
1809 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1810 * wrong; must end in space or cmd separator.
1811 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001812 * flags:
1813 * GLV_QUIET: do not give error messages
1814 * GLV_NO_AUTOLOAD: do not use script autoloading
1815 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001816 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001817 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001818 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001819 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001820 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001821get_lval(
1822 char_u *name,
1823 typval_T *rettv,
1824 lval_T *lp,
1825 int unlet,
1826 int skip,
1827 int flags, /* GLV_ values */
1828 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001829{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001830 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001831 char_u *expr_start, *expr_end;
1832 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001833 dictitem_T *v;
1834 typval_T var1;
1835 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001836 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001837 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001838 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001839 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001840 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001841 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001842
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001843 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001844 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001845
1846 if (skip)
1847 {
1848 /* When skipping just find the end of the name. */
1849 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001850 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001851 }
1852
1853 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001854 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001855 if (expr_start != NULL)
1856 {
1857 /* Don't expand the name when we already know there is an error. */
1858 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
1859 && *p != '[' && *p != '.')
1860 {
1861 EMSG(_(e_trailing));
1862 return NULL;
1863 }
1864
1865 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1866 if (lp->ll_exp_name == NULL)
1867 {
1868 /* Report an invalid expression in braces, unless the
1869 * expression evaluation has been cancelled due to an
1870 * aborting error, an interrupt, or an exception. */
1871 if (!aborting() && !quiet)
1872 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001873 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001874 EMSG2(_(e_invarg2), name);
1875 return NULL;
1876 }
1877 }
1878 lp->ll_name = lp->ll_exp_name;
1879 }
1880 else
1881 lp->ll_name = name;
1882
1883 /* Without [idx] or .key we are done. */
1884 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1885 return p;
1886
1887 cc = *p;
1888 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001889 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001890 if (v == NULL && !quiet)
1891 EMSG2(_(e_undefvar), lp->ll_name);
1892 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893 if (v == NULL)
1894 return NULL;
1895
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001896 /*
1897 * Loop until no more [idx] or .key is following.
1898 */
Bram Moolenaar33570922005-01-25 22:26:29 +00001899 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001900 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001902 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1903 && !(lp->ll_tv->v_type == VAR_DICT
1904 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001905 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001906 if (!quiet)
1907 EMSG(_("E689: Can only index a List or Dictionary"));
1908 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001909 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001910 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001911 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001912 if (!quiet)
1913 EMSG(_("E708: [:] must come last"));
1914 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001915 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001916
Bram Moolenaar8c711452005-01-14 21:53:12 +00001917 len = -1;
1918 if (*p == '.')
1919 {
1920 key = p + 1;
1921 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1922 ;
1923 if (len == 0)
1924 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001925 if (!quiet)
1926 EMSG(_(e_emptykey));
1927 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001928 }
1929 p = key + len;
1930 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001931 else
1932 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001933 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001934 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001935 if (*p == ':')
1936 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001937 else
1938 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001939 empty1 = FALSE;
1940 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001941 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001942 if (get_tv_string_chk(&var1) == NULL)
1943 {
1944 /* not a number or string */
1945 clear_tv(&var1);
1946 return NULL;
1947 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001948 }
1949
1950 /* Optionally get the second index [ :expr]. */
1951 if (*p == ':')
1952 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001953 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001954 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001955 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001956 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001957 if (!empty1)
1958 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001959 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001960 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001961 if (rettv != NULL && (rettv->v_type != VAR_LIST
1962 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001963 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001964 if (!quiet)
1965 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00001966 if (!empty1)
1967 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001968 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001969 }
1970 p = skipwhite(p + 1);
1971 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001972 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001973 else
1974 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001975 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001976 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
1977 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001978 if (!empty1)
1979 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001980 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001981 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001982 if (get_tv_string_chk(&var2) == NULL)
1983 {
1984 /* not a number or string */
1985 if (!empty1)
1986 clear_tv(&var1);
1987 clear_tv(&var2);
1988 return NULL;
1989 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001990 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001991 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001992 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001993 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001994 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001995
Bram Moolenaar8c711452005-01-14 21:53:12 +00001996 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001997 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001998 if (!quiet)
1999 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002000 if (!empty1)
2001 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002002 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002003 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002004 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002005 }
2006
2007 /* Skip to past ']'. */
2008 ++p;
2009 }
2010
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002011 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002012 {
2013 if (len == -1)
2014 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002015 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002016 key = get_tv_string_chk(&var1); /* is number or string */
2017 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002018 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002019 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002020 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002021 }
2022 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002023 lp->ll_list = NULL;
2024 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002025 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002026
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002027 /* When assigning to a scope dictionary check that a function and
2028 * variable name is valid (only variable name unless it is l: or
2029 * g: dictionary). Disallow overwriting a builtin function. */
2030 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002031 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002032 int prevval;
2033 int wrong;
2034
2035 if (len != -1)
2036 {
2037 prevval = key[len];
2038 key[len] = NUL;
2039 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002040 else
2041 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002042 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2043 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002044 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002045 || !valid_varname(key);
2046 if (len != -1)
2047 key[len] = prevval;
2048 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002049 return NULL;
2050 }
2051
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002052 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002053 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002054 /* Can't add "v:" variable. */
2055 if (lp->ll_dict == &vimvardict)
2056 {
2057 EMSG2(_(e_illvar), name);
2058 return NULL;
2059 }
2060
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002061 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002062 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002063 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002064 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002065 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002066 if (len == -1)
2067 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002068 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002069 }
2070 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002071 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002072 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002073 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002074 if (len == -1)
2075 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002076 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002077 p = NULL;
2078 break;
2079 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002080 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002081 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002082 return NULL;
2083
Bram Moolenaar8c711452005-01-14 21:53:12 +00002084 if (len == -1)
2085 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002086 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002087 }
2088 else
2089 {
2090 /*
2091 * Get the number and item for the only or first index of the List.
2092 */
2093 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002094 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002095 else
2096 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002097 lp->ll_n1 = (long)get_tv_number(&var1);
2098 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002099 clear_tv(&var1);
2100 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002101 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002102 lp->ll_list = lp->ll_tv->vval.v_list;
2103 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2104 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002105 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002106 if (lp->ll_n1 < 0)
2107 {
2108 lp->ll_n1 = 0;
2109 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2110 }
2111 }
2112 if (lp->ll_li == NULL)
2113 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002114 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002115 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002116 if (!quiet)
2117 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002118 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002119 }
2120
2121 /*
2122 * May need to find the item or absolute index for the second
2123 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002124 * When no index given: "lp->ll_empty2" is TRUE.
2125 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002126 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002127 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002128 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002129 lp->ll_n2 = (long)get_tv_number(&var2);
2130 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002131 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002132 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002133 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002134 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002135 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002136 {
2137 if (!quiet)
2138 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002139 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002140 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002141 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002142 }
2143
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002144 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2145 if (lp->ll_n1 < 0)
2146 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2147 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002148 {
2149 if (!quiet)
2150 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002151 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002152 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002153 }
2154
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002155 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002156 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002157 }
2158
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002159 return p;
2160}
2161
2162/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002163 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002164 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002165 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002166clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002167{
2168 vim_free(lp->ll_exp_name);
2169 vim_free(lp->ll_newkey);
2170}
2171
2172/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002173 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002174 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002175 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002176 */
2177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002178set_var_lval(
2179 lval_T *lp,
2180 char_u *endp,
2181 typval_T *rettv,
2182 int copy,
2183 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002184{
2185 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002186 listitem_T *ri;
2187 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002188
2189 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002190 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002191 cc = *endp;
2192 *endp = NUL;
2193 if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002194 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002195 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002196
Bram Moolenaar79518e22017-02-17 16:31:35 +01002197 /* handle +=, -= and .= */
2198 di = NULL;
2199 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2200 &tv, &di, TRUE, FALSE) == OK)
2201 {
2202 if ((di == NULL
2203 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2204 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2205 FALSE)))
2206 && tv_op(&tv, rettv, op) == OK)
2207 set_var(lp->ll_name, &tv, FALSE);
2208 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002209 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002211 else
2212 set_var(lp->ll_name, rettv, copy);
2213 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002214 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002215 else if (tv_check_lock(lp->ll_newkey == NULL
2216 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002217 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002218 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002219 else if (lp->ll_range)
2220 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002221 listitem_T *ll_li = lp->ll_li;
2222 int ll_n1 = lp->ll_n1;
2223
2224 /*
2225 * Check whether any of the list items is locked
2226 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002227 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002228 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002229 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002230 return;
2231 ri = ri->li_next;
2232 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2233 break;
2234 ll_li = ll_li->li_next;
2235 ++ll_n1;
2236 }
2237
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002238 /*
2239 * Assign the List values to the list items.
2240 */
2241 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002242 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002243 if (op != NULL && *op != '=')
2244 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2245 else
2246 {
2247 clear_tv(&lp->ll_li->li_tv);
2248 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2249 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002250 ri = ri->li_next;
2251 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2252 break;
2253 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002254 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002255 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002256 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002257 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002258 ri = NULL;
2259 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002260 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002261 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002262 lp->ll_li = lp->ll_li->li_next;
2263 ++lp->ll_n1;
2264 }
2265 if (ri != NULL)
2266 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002267 else if (lp->ll_empty2
2268 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002269 : lp->ll_n1 != lp->ll_n2)
2270 EMSG(_("E711: List value has not enough items"));
2271 }
2272 else
2273 {
2274 /*
2275 * Assign to a List or Dictionary item.
2276 */
2277 if (lp->ll_newkey != NULL)
2278 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002279 if (op != NULL && *op != '=')
2280 {
2281 EMSG2(_(e_letwrong), op);
2282 return;
2283 }
2284
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002285 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002286 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002287 if (di == NULL)
2288 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002289 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2290 {
2291 vim_free(di);
2292 return;
2293 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002294 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002295 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002296 else if (op != NULL && *op != '=')
2297 {
2298 tv_op(lp->ll_tv, rettv, op);
2299 return;
2300 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002301 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002302 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002303
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002304 /*
2305 * Assign the value to the variable or list item.
2306 */
2307 if (copy)
2308 copy_tv(rettv, lp->ll_tv);
2309 else
2310 {
2311 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002312 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002313 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002314 }
2315 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002316}
2317
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002318/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002319 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2320 * Returns OK or FAIL.
2321 */
2322 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002323tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002324{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002325 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002326 char_u numbuf[NUMBUFLEN];
2327 char_u *s;
2328
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002329 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2330 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2331 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002332 {
2333 switch (tv1->v_type)
2334 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002335 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002336 case VAR_DICT:
2337 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002338 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002339 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002340 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002341 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002342 break;
2343
2344 case VAR_LIST:
2345 if (*op != '+' || tv2->v_type != VAR_LIST)
2346 break;
2347 /* List += List */
2348 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2349 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2350 return OK;
2351
2352 case VAR_NUMBER:
2353 case VAR_STRING:
2354 if (tv2->v_type == VAR_LIST)
2355 break;
2356 if (*op == '+' || *op == '-')
2357 {
2358 /* nr += nr or nr -= nr*/
2359 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002360#ifdef FEAT_FLOAT
2361 if (tv2->v_type == VAR_FLOAT)
2362 {
2363 float_T f = n;
2364
2365 if (*op == '+')
2366 f += tv2->vval.v_float;
2367 else
2368 f -= tv2->vval.v_float;
2369 clear_tv(tv1);
2370 tv1->v_type = VAR_FLOAT;
2371 tv1->vval.v_float = f;
2372 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002373 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002374#endif
2375 {
2376 if (*op == '+')
2377 n += get_tv_number(tv2);
2378 else
2379 n -= get_tv_number(tv2);
2380 clear_tv(tv1);
2381 tv1->v_type = VAR_NUMBER;
2382 tv1->vval.v_number = n;
2383 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002384 }
2385 else
2386 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002387 if (tv2->v_type == VAR_FLOAT)
2388 break;
2389
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002390 /* str .= str */
2391 s = get_tv_string(tv1);
2392 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2393 clear_tv(tv1);
2394 tv1->v_type = VAR_STRING;
2395 tv1->vval.v_string = s;
2396 }
2397 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002398
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002399 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002400#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002401 {
2402 float_T f;
2403
2404 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2405 && tv2->v_type != VAR_NUMBER
2406 && tv2->v_type != VAR_STRING))
2407 break;
2408 if (tv2->v_type == VAR_FLOAT)
2409 f = tv2->vval.v_float;
2410 else
2411 f = get_tv_number(tv2);
2412 if (*op == '+')
2413 tv1->vval.v_float += f;
2414 else
2415 tv1->vval.v_float -= f;
2416 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002417#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002418 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002419 }
2420 }
2421
2422 EMSG2(_(e_letwrong), op);
2423 return FAIL;
2424}
2425
2426/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002427 * Evaluate the expression used in a ":for var in expr" command.
2428 * "arg" points to "var".
2429 * Set "*errp" to TRUE for an error, FALSE otherwise;
2430 * Return a pointer that holds the info. Null when there is an error.
2431 */
2432 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002433eval_for_line(
2434 char_u *arg,
2435 int *errp,
2436 char_u **nextcmdp,
2437 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002438{
Bram Moolenaar33570922005-01-25 22:26:29 +00002439 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002440 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002441 typval_T tv;
2442 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002443
2444 *errp = TRUE; /* default: there is an error */
2445
Bram Moolenaar33570922005-01-25 22:26:29 +00002446 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002447 if (fi == NULL)
2448 return NULL;
2449
2450 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2451 if (expr == NULL)
2452 return fi;
2453
2454 expr = skipwhite(expr);
2455 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2456 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002457 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002458 return fi;
2459 }
2460
2461 if (skip)
2462 ++emsg_skip;
2463 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2464 {
2465 *errp = FALSE;
2466 if (!skip)
2467 {
2468 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002469 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002470 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002471 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002472 clear_tv(&tv);
2473 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002474 else if (l == NULL)
2475 {
2476 /* a null list is like an empty list: do nothing */
2477 clear_tv(&tv);
2478 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002479 else
2480 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002481 /* No need to increment the refcount, it's already set for the
2482 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002483 fi->fi_list = l;
2484 list_add_watch(l, &fi->fi_lw);
2485 fi->fi_lw.lw_item = l->lv_first;
2486 }
2487 }
2488 }
2489 if (skip)
2490 --emsg_skip;
2491
2492 return fi;
2493}
2494
2495/*
2496 * Use the first item in a ":for" list. Advance to the next.
2497 * Assign the values to the variable (list). "arg" points to the first one.
2498 * Return TRUE when a valid item was found, FALSE when at end of list or
2499 * something wrong.
2500 */
2501 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002502next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002503{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002504 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002505 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002506 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002507
2508 item = fi->fi_lw.lw_item;
2509 if (item == NULL)
2510 result = FALSE;
2511 else
2512 {
2513 fi->fi_lw.lw_item = item->li_next;
2514 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2515 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2516 }
2517 return result;
2518}
2519
2520/*
2521 * Free the structure used to store info used by ":for".
2522 */
2523 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002524free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002525{
Bram Moolenaar33570922005-01-25 22:26:29 +00002526 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002527
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002528 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002529 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002530 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002531 list_unref(fi->fi_list);
2532 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002533 vim_free(fi);
2534}
2535
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2537
2538 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002539set_context_for_expression(
2540 expand_T *xp,
2541 char_u *arg,
2542 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543{
2544 int got_eq = FALSE;
2545 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002546 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002548 if (cmdidx == CMD_let)
2549 {
2550 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002551 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002552 {
2553 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002554 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002555 {
2556 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002557 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002558 if (vim_iswhite(*p))
2559 break;
2560 }
2561 return;
2562 }
2563 }
2564 else
2565 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2566 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 while ((xp->xp_pattern = vim_strpbrk(arg,
2568 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2569 {
2570 c = *xp->xp_pattern;
2571 if (c == '&')
2572 {
2573 c = xp->xp_pattern[1];
2574 if (c == '&')
2575 {
2576 ++xp->xp_pattern;
2577 xp->xp_context = cmdidx != CMD_let || got_eq
2578 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2579 }
2580 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002581 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002583 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2584 xp->xp_pattern += 2;
2585
2586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 }
2588 else if (c == '$')
2589 {
2590 /* environment variable */
2591 xp->xp_context = EXPAND_ENV_VARS;
2592 }
2593 else if (c == '=')
2594 {
2595 got_eq = TRUE;
2596 xp->xp_context = EXPAND_EXPRESSION;
2597 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002598 else if (c == '#'
2599 && xp->xp_context == EXPAND_EXPRESSION)
2600 {
2601 /* Autoload function/variable contains '#'. */
2602 break;
2603 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002604 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 && xp->xp_context == EXPAND_FUNCTIONS
2606 && vim_strchr(xp->xp_pattern, '(') == NULL)
2607 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002608 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 break;
2610 }
2611 else if (cmdidx != CMD_let || got_eq)
2612 {
2613 if (c == '"') /* string */
2614 {
2615 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2616 if (c == '\\' && xp->xp_pattern[1] != NUL)
2617 ++xp->xp_pattern;
2618 xp->xp_context = EXPAND_NOTHING;
2619 }
2620 else if (c == '\'') /* literal string */
2621 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002622 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2624 /* skip */ ;
2625 xp->xp_context = EXPAND_NOTHING;
2626 }
2627 else if (c == '|')
2628 {
2629 if (xp->xp_pattern[1] == '|')
2630 {
2631 ++xp->xp_pattern;
2632 xp->xp_context = EXPAND_EXPRESSION;
2633 }
2634 else
2635 xp->xp_context = EXPAND_COMMANDS;
2636 }
2637 else
2638 xp->xp_context = EXPAND_EXPRESSION;
2639 }
2640 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002641 /* Doesn't look like something valid, expand as an expression
2642 * anyway. */
2643 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 arg = xp->xp_pattern;
2645 if (*arg != NUL)
2646 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2647 /* skip */ ;
2648 }
2649 xp->xp_pattern = arg;
2650}
2651
2652#endif /* FEAT_CMDL_COMPL */
2653
2654/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 * ":unlet[!] var1 ... " command.
2656 */
2657 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002658ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002660 ex_unletlock(eap, eap->arg, 0);
2661}
2662
2663/*
2664 * ":lockvar" and ":unlockvar" commands
2665 */
2666 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002667ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002668{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002670 int deep = 2;
2671
2672 if (eap->forceit)
2673 deep = -1;
2674 else if (vim_isdigit(*arg))
2675 {
2676 deep = getdigits(&arg);
2677 arg = skipwhite(arg);
2678 }
2679
2680 ex_unletlock(eap, arg, deep);
2681}
2682
2683/*
2684 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2685 */
2686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002687ex_unletlock(
2688 exarg_T *eap,
2689 char_u *argstart,
2690 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002691{
2692 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002695 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696
2697 do
2698 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002700 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002701 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 if (lv.ll_name == NULL)
2703 error = TRUE; /* error but continue parsing */
2704 if (name_end == NULL || (!vim_iswhite(*name_end)
2705 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 if (name_end != NULL)
2708 {
2709 emsg_severe = TRUE;
2710 EMSG(_(e_trailing));
2711 }
2712 if (!(eap->skip || error))
2713 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 break;
2715 }
2716
2717 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002718 {
2719 if (eap->cmdidx == CMD_unlet)
2720 {
2721 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2722 error = TRUE;
2723 }
2724 else
2725 {
2726 if (do_lock_var(&lv, name_end, deep,
2727 eap->cmdidx == CMD_lockvar) == FAIL)
2728 error = TRUE;
2729 }
2730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 if (!eap->skip)
2733 clear_lval(&lv);
2734
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 arg = skipwhite(name_end);
2736 } while (!ends_excmd(*arg));
2737
2738 eap->nextcmd = check_nextcmd(arg);
2739}
2740
Bram Moolenaar8c711452005-01-14 21:53:12 +00002741 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002742do_unlet_var(
2743 lval_T *lp,
2744 char_u *name_end,
2745 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002746{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 int ret = OK;
2748 int cc;
2749
2750 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 cc = *name_end;
2753 *name_end = NUL;
2754
2755 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002756 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002757 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002760 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002761 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002762 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002763 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002764 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765 else if (lp->ll_range)
2766 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002767 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002768 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01002769 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002770
2771 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
2772 {
2773 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02002774 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002775 return FAIL;
2776 ll_li = li;
2777 ++ll_n1;
2778 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002779
2780 /* Delete a range of List items. */
2781 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2782 {
2783 li = lp->ll_li->li_next;
2784 listitem_remove(lp->ll_list, lp->ll_li);
2785 lp->ll_li = li;
2786 ++lp->ll_n1;
2787 }
2788 }
2789 else
2790 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 /* unlet a List item. */
2793 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002795 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002796 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797 }
2798
2799 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002800}
2801
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802/*
2803 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002804 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 */
2806 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002807do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808{
Bram Moolenaar33570922005-01-25 22:26:29 +00002809 hashtab_T *ht;
2810 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002811 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002812 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002813 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814
Bram Moolenaar33570922005-01-25 22:26:29 +00002815 ht = find_var_ht(name, &varname);
2816 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002818 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002819 if (d == NULL)
2820 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002821 if (ht == &globvarht)
2822 d = &globvardict;
2823 else if (ht == &compat_hashtab)
2824 d = &vimvardict;
2825 else
2826 {
2827 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2828 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2829 }
2830 if (d == NULL)
2831 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01002832 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002833 return FAIL;
2834 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002835 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002836 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002837 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02002838 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002839 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00002840 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002841 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02002842 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002843 || var_check_ro(di->di_flags, name, FALSE)
2844 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01002845 return FAIL;
2846
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002847 delete_var(ht, hi);
2848 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00002849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002851 if (forceit)
2852 return OK;
2853 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 return FAIL;
2855}
2856
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002857/*
2858 * Lock or unlock variable indicated by "lp".
2859 * "deep" is the levels to go (-1 for unlimited);
2860 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2861 */
2862 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002863do_lock_var(
2864 lval_T *lp,
2865 char_u *name_end,
2866 int deep,
2867 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002868{
2869 int ret = OK;
2870 int cc;
2871 dictitem_T *di;
2872
2873 if (deep == 0) /* nothing to do */
2874 return OK;
2875
2876 if (lp->ll_tv == NULL)
2877 {
2878 cc = *name_end;
2879 *name_end = NUL;
2880
2881 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002882 di = find_var(lp->ll_name, NULL, TRUE);
2883 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002884 ret = FAIL;
2885 else
2886 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002887 if (lock)
2888 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002889 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01002890 di->di_flags &= ~DI_FLAGS_LOCK;
2891 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002892 }
2893 *name_end = cc;
2894 }
2895 else if (lp->ll_range)
2896 {
2897 listitem_T *li = lp->ll_li;
2898
2899 /* (un)lock a range of List items. */
2900 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2901 {
2902 item_lock(&li->li_tv, deep, lock);
2903 li = li->li_next;
2904 ++lp->ll_n1;
2905 }
2906 }
2907 else if (lp->ll_list != NULL)
2908 /* (un)lock a List item. */
2909 item_lock(&lp->ll_li->li_tv, deep, lock);
2910 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02002911 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002912 item_lock(&lp->ll_di->di_tv, deep, lock);
2913
2914 return ret;
2915}
2916
2917/*
2918 * Lock or unlock an item. "deep" is nr of levels to go.
2919 */
2920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002921item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002922{
2923 static int recurse = 0;
2924 list_T *l;
2925 listitem_T *li;
2926 dict_T *d;
2927 hashitem_T *hi;
2928 int todo;
2929
2930 if (recurse >= DICT_MAXNEST)
2931 {
2932 EMSG(_("E743: variable nested too deep for (un)lock"));
2933 return;
2934 }
2935 if (deep == 0)
2936 return;
2937 ++recurse;
2938
2939 /* lock/unlock the item itself */
2940 if (lock)
2941 tv->v_lock |= VAR_LOCKED;
2942 else
2943 tv->v_lock &= ~VAR_LOCKED;
2944
2945 switch (tv->v_type)
2946 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01002947 case VAR_UNKNOWN:
2948 case VAR_NUMBER:
2949 case VAR_STRING:
2950 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002951 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002952 case VAR_FLOAT:
2953 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002954 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002955 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002956 break;
2957
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002958 case VAR_LIST:
2959 if ((l = tv->vval.v_list) != NULL)
2960 {
2961 if (lock)
2962 l->lv_lock |= VAR_LOCKED;
2963 else
2964 l->lv_lock &= ~VAR_LOCKED;
2965 if (deep < 0 || deep > 1)
2966 /* recursive: lock/unlock the items the List contains */
2967 for (li = l->lv_first; li != NULL; li = li->li_next)
2968 item_lock(&li->li_tv, deep - 1, lock);
2969 }
2970 break;
2971 case VAR_DICT:
2972 if ((d = tv->vval.v_dict) != NULL)
2973 {
2974 if (lock)
2975 d->dv_lock |= VAR_LOCKED;
2976 else
2977 d->dv_lock &= ~VAR_LOCKED;
2978 if (deep < 0 || deep > 1)
2979 {
2980 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002981 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002982 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2983 {
2984 if (!HASHITEM_EMPTY(hi))
2985 {
2986 --todo;
2987 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
2988 }
2989 }
2990 }
2991 }
2992 }
2993 --recurse;
2994}
2995
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2997/*
2998 * Delete all "menutrans_" variables.
2999 */
3000 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003001del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002{
Bram Moolenaar33570922005-01-25 22:26:29 +00003003 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003004 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005
Bram Moolenaar33570922005-01-25 22:26:29 +00003006 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003007 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003008 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003009 {
3010 if (!HASHITEM_EMPTY(hi))
3011 {
3012 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003013 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3014 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003015 }
3016 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003017 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018}
3019#endif
3020
3021#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3022
3023/*
3024 * Local string buffer for the next two functions to store a variable name
3025 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3026 * get_user_var_name().
3027 */
3028
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003029static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030
3031static char_u *varnamebuf = NULL;
3032static int varnamebuflen = 0;
3033
3034/*
3035 * Function to concatenate a prefix and a variable name.
3036 */
3037 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003038cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039{
3040 int len;
3041
3042 len = (int)STRLEN(name) + 3;
3043 if (len > varnamebuflen)
3044 {
3045 vim_free(varnamebuf);
3046 len += 10; /* some additional space */
3047 varnamebuf = alloc(len);
3048 if (varnamebuf == NULL)
3049 {
3050 varnamebuflen = 0;
3051 return NULL;
3052 }
3053 varnamebuflen = len;
3054 }
3055 *varnamebuf = prefix;
3056 varnamebuf[1] = ':';
3057 STRCPY(varnamebuf + 2, name);
3058 return varnamebuf;
3059}
3060
3061/*
3062 * Function given to ExpandGeneric() to obtain the list of user defined
3063 * (global/buffer/window/built-in) variable names.
3064 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003066get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003068 static long_u gdone;
3069 static long_u bdone;
3070 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003071#ifdef FEAT_WINDOWS
3072 static long_u tdone;
3073#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003074 static int vidx;
3075 static hashitem_T *hi;
3076 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077
3078 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003079 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003080 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003081#ifdef FEAT_WINDOWS
3082 tdone = 0;
3083#endif
3084 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003085
3086 /* Global variables */
3087 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003089 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003090 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003091 else
3092 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003093 while (HASHITEM_EMPTY(hi))
3094 ++hi;
3095 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3096 return cat_prefix_varname('g', hi->hi_key);
3097 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003099
3100 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003101 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003102 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003104 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003105 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003106 else
3107 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003108 while (HASHITEM_EMPTY(hi))
3109 ++hi;
3110 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003112
3113 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003114 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003115 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003117 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003118 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003119 else
3120 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003121 while (HASHITEM_EMPTY(hi))
3122 ++hi;
3123 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003125
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003126#ifdef FEAT_WINDOWS
3127 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003128 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003129 if (tdone < ht->ht_used)
3130 {
3131 if (tdone++ == 0)
3132 hi = ht->ht_array;
3133 else
3134 ++hi;
3135 while (HASHITEM_EMPTY(hi))
3136 ++hi;
3137 return cat_prefix_varname('t', hi->hi_key);
3138 }
3139#endif
3140
Bram Moolenaar33570922005-01-25 22:26:29 +00003141 /* v: variables */
3142 if (vidx < VV_LEN)
3143 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144
3145 vim_free(varnamebuf);
3146 varnamebuf = NULL;
3147 varnamebuflen = 0;
3148 return NULL;
3149}
3150
3151#endif /* FEAT_CMDL_COMPL */
3152
3153/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003154 * Return TRUE if "pat" matches "text".
3155 * Does not use 'cpo' and always uses 'magic'.
3156 */
3157 static int
3158pattern_match(char_u *pat, char_u *text, int ic)
3159{
3160 int matches = FALSE;
3161 char_u *save_cpo;
3162 regmatch_T regmatch;
3163
3164 /* avoid 'l' flag in 'cpoptions' */
3165 save_cpo = p_cpo;
3166 p_cpo = (char_u *)"";
3167 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3168 if (regmatch.regprog != NULL)
3169 {
3170 regmatch.rm_ic = ic;
3171 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3172 vim_regfree(regmatch.regprog);
3173 }
3174 p_cpo = save_cpo;
3175 return matches;
3176}
3177
3178/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 * types for expressions.
3180 */
3181typedef enum
3182{
3183 TYPE_UNKNOWN = 0
3184 , TYPE_EQUAL /* == */
3185 , TYPE_NEQUAL /* != */
3186 , TYPE_GREATER /* > */
3187 , TYPE_GEQUAL /* >= */
3188 , TYPE_SMALLER /* < */
3189 , TYPE_SEQUAL /* <= */
3190 , TYPE_MATCH /* =~ */
3191 , TYPE_NOMATCH /* !~ */
3192} exptype_T;
3193
3194/*
3195 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003196 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3198 */
3199
3200/*
3201 * Handle zero level expression.
3202 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003203 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003204 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 * Return OK or FAIL.
3206 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003207 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003208eval0(
3209 char_u *arg,
3210 typval_T *rettv,
3211 char_u **nextcmd,
3212 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213{
3214 int ret;
3215 char_u *p;
3216
3217 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003218 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 if (ret == FAIL || !ends_excmd(*p))
3220 {
3221 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003222 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 /*
3224 * Report the invalid expression unless the expression evaluation has
3225 * been cancelled due to an aborting error, an interrupt, or an
3226 * exception.
3227 */
3228 if (!aborting())
3229 EMSG2(_(e_invexpr2), arg);
3230 ret = FAIL;
3231 }
3232 if (nextcmd != NULL)
3233 *nextcmd = check_nextcmd(p);
3234
3235 return ret;
3236}
3237
3238/*
3239 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003240 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 *
3242 * "arg" must point to the first non-white of the expression.
3243 * "arg" is advanced to the next non-white after the recognized expression.
3244 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003245 * Note: "rettv.v_lock" is not set.
3246 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 * Return OK or FAIL.
3248 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003249 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003250eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251{
3252 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003253 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254
3255 /*
3256 * Get the first variable.
3257 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003258 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 return FAIL;
3260
3261 if ((*arg)[0] == '?')
3262 {
3263 result = FALSE;
3264 if (evaluate)
3265 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003266 int error = FALSE;
3267
3268 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003270 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003271 if (error)
3272 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 }
3274
3275 /*
3276 * Get the second variable.
3277 */
3278 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003279 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 return FAIL;
3281
3282 /*
3283 * Check for the ":".
3284 */
3285 if ((*arg)[0] != ':')
3286 {
3287 EMSG(_("E109: Missing ':' after '?'"));
3288 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003289 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 return FAIL;
3291 }
3292
3293 /*
3294 * Get the third variable.
3295 */
3296 *arg = skipwhite(*arg + 1);
3297 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3298 {
3299 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003300 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 return FAIL;
3302 }
3303 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003304 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 }
3306
3307 return OK;
3308}
3309
3310/*
3311 * Handle first level expression:
3312 * expr2 || expr2 || expr2 logical OR
3313 *
3314 * "arg" must point to the first non-white of the expression.
3315 * "arg" is advanced to the next non-white after the recognized expression.
3316 *
3317 * Return OK or FAIL.
3318 */
3319 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003320eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321{
Bram Moolenaar33570922005-01-25 22:26:29 +00003322 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 long result;
3324 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003325 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326
3327 /*
3328 * Get the first variable.
3329 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003330 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 return FAIL;
3332
3333 /*
3334 * Repeat until there is no following "||".
3335 */
3336 first = TRUE;
3337 result = FALSE;
3338 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3339 {
3340 if (evaluate && first)
3341 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003342 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003344 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003345 if (error)
3346 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 first = FALSE;
3348 }
3349
3350 /*
3351 * Get the second variable.
3352 */
3353 *arg = skipwhite(*arg + 2);
3354 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3355 return FAIL;
3356
3357 /*
3358 * Compute the result.
3359 */
3360 if (evaluate && !result)
3361 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003362 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003364 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003365 if (error)
3366 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 }
3368 if (evaluate)
3369 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003370 rettv->v_type = VAR_NUMBER;
3371 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 }
3373 }
3374
3375 return OK;
3376}
3377
3378/*
3379 * Handle second level expression:
3380 * expr3 && expr3 && expr3 logical AND
3381 *
3382 * "arg" must point to the first non-white of the expression.
3383 * "arg" is advanced to the next non-white after the recognized expression.
3384 *
3385 * Return OK or FAIL.
3386 */
3387 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003388eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389{
Bram Moolenaar33570922005-01-25 22:26:29 +00003390 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 long result;
3392 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003393 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394
3395 /*
3396 * Get the first variable.
3397 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003398 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 return FAIL;
3400
3401 /*
3402 * Repeat until there is no following "&&".
3403 */
3404 first = TRUE;
3405 result = TRUE;
3406 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3407 {
3408 if (evaluate && first)
3409 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003410 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003412 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003413 if (error)
3414 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 first = FALSE;
3416 }
3417
3418 /*
3419 * Get the second variable.
3420 */
3421 *arg = skipwhite(*arg + 2);
3422 if (eval4(arg, &var2, evaluate && result) == FAIL)
3423 return FAIL;
3424
3425 /*
3426 * Compute the result.
3427 */
3428 if (evaluate && result)
3429 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003430 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003432 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003433 if (error)
3434 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 }
3436 if (evaluate)
3437 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003438 rettv->v_type = VAR_NUMBER;
3439 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 }
3441 }
3442
3443 return OK;
3444}
3445
3446/*
3447 * Handle third level expression:
3448 * var1 == var2
3449 * var1 =~ var2
3450 * var1 != var2
3451 * var1 !~ var2
3452 * var1 > var2
3453 * var1 >= var2
3454 * var1 < var2
3455 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003456 * var1 is var2
3457 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 *
3459 * "arg" must point to the first non-white of the expression.
3460 * "arg" is advanced to the next non-white after the recognized expression.
3461 *
3462 * Return OK or FAIL.
3463 */
3464 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003465eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466{
Bram Moolenaar33570922005-01-25 22:26:29 +00003467 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 char_u *p;
3469 int i;
3470 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003471 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 int len = 2;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003473 varnumber_T n1, n2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 char_u *s1, *s2;
3475 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477
3478 /*
3479 * Get the first variable.
3480 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003481 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 return FAIL;
3483
3484 p = *arg;
3485 switch (p[0])
3486 {
3487 case '=': if (p[1] == '=')
3488 type = TYPE_EQUAL;
3489 else if (p[1] == '~')
3490 type = TYPE_MATCH;
3491 break;
3492 case '!': if (p[1] == '=')
3493 type = TYPE_NEQUAL;
3494 else if (p[1] == '~')
3495 type = TYPE_NOMATCH;
3496 break;
3497 case '>': if (p[1] != '=')
3498 {
3499 type = TYPE_GREATER;
3500 len = 1;
3501 }
3502 else
3503 type = TYPE_GEQUAL;
3504 break;
3505 case '<': if (p[1] != '=')
3506 {
3507 type = TYPE_SMALLER;
3508 len = 1;
3509 }
3510 else
3511 type = TYPE_SEQUAL;
3512 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003513 case 'i': if (p[1] == 's')
3514 {
3515 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3516 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003517 i = p[len];
3518 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003519 {
3520 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3521 type_is = TRUE;
3522 }
3523 }
3524 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 }
3526
3527 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003528 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 */
3530 if (type != TYPE_UNKNOWN)
3531 {
3532 /* extra question mark appended: ignore case */
3533 if (p[len] == '?')
3534 {
3535 ic = TRUE;
3536 ++len;
3537 }
3538 /* extra '#' appended: match case */
3539 else if (p[len] == '#')
3540 {
3541 ic = FALSE;
3542 ++len;
3543 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003544 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 else
3546 ic = p_ic;
3547
3548 /*
3549 * Get the second variable.
3550 */
3551 *arg = skipwhite(p + len);
3552 if (eval5(arg, &var2, evaluate) == FAIL)
3553 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003554 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 return FAIL;
3556 }
3557
3558 if (evaluate)
3559 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003560 if (type_is && rettv->v_type != var2.v_type)
3561 {
3562 /* For "is" a different type always means FALSE, for "notis"
3563 * it means TRUE. */
3564 n1 = (type == TYPE_NEQUAL);
3565 }
3566 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3567 {
3568 if (type_is)
3569 {
3570 n1 = (rettv->v_type == var2.v_type
3571 && rettv->vval.v_list == var2.vval.v_list);
3572 if (type == TYPE_NEQUAL)
3573 n1 = !n1;
3574 }
3575 else if (rettv->v_type != var2.v_type
3576 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3577 {
3578 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003579 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003580 else
Bram Moolenaar59838522014-05-13 13:46:33 +02003581 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003582 clear_tv(rettv);
3583 clear_tv(&var2);
3584 return FAIL;
3585 }
3586 else
3587 {
3588 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003589 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
3590 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003591 if (type == TYPE_NEQUAL)
3592 n1 = !n1;
3593 }
3594 }
3595
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003596 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3597 {
3598 if (type_is)
3599 {
3600 n1 = (rettv->v_type == var2.v_type
3601 && rettv->vval.v_dict == var2.vval.v_dict);
3602 if (type == TYPE_NEQUAL)
3603 n1 = !n1;
3604 }
3605 else if (rettv->v_type != var2.v_type
3606 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3607 {
3608 if (rettv->v_type != var2.v_type)
3609 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3610 else
3611 EMSG(_("E736: Invalid operation for Dictionary"));
3612 clear_tv(rettv);
3613 clear_tv(&var2);
3614 return FAIL;
3615 }
3616 else
3617 {
3618 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003619 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
3620 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003621 if (type == TYPE_NEQUAL)
3622 n1 = !n1;
3623 }
3624 }
3625
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003626 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
3627 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003628 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003629 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003630 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003631 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003632 clear_tv(rettv);
3633 clear_tv(&var2);
3634 return FAIL;
3635 }
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003636 if ((rettv->v_type == VAR_PARTIAL
3637 && rettv->vval.v_partial == NULL)
3638 || (var2.v_type == VAR_PARTIAL
3639 && var2.vval.v_partial == NULL))
3640 /* when a partial is NULL assume not equal */
3641 n1 = FALSE;
3642 else if (type_is)
3643 {
3644 if (rettv->v_type == VAR_FUNC && var2.v_type == VAR_FUNC)
3645 /* strings are considered the same if their value is
3646 * the same */
3647 n1 = tv_equal(rettv, &var2, ic, FALSE);
3648 else if (rettv->v_type == VAR_PARTIAL
3649 && var2.v_type == VAR_PARTIAL)
3650 n1 = (rettv->vval.v_partial == var2.vval.v_partial);
3651 else
3652 n1 = FALSE;
3653 }
3654 else
3655 n1 = tv_equal(rettv, &var2, ic, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003656 if (type == TYPE_NEQUAL)
3657 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003658 }
3659
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003660#ifdef FEAT_FLOAT
3661 /*
3662 * If one of the two variables is a float, compare as a float.
3663 * When using "=~" or "!~", always compare as string.
3664 */
3665 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3666 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3667 {
3668 float_T f1, f2;
3669
3670 if (rettv->v_type == VAR_FLOAT)
3671 f1 = rettv->vval.v_float;
3672 else
3673 f1 = get_tv_number(rettv);
3674 if (var2.v_type == VAR_FLOAT)
3675 f2 = var2.vval.v_float;
3676 else
3677 f2 = get_tv_number(&var2);
3678 n1 = FALSE;
3679 switch (type)
3680 {
3681 case TYPE_EQUAL: n1 = (f1 == f2); break;
3682 case TYPE_NEQUAL: n1 = (f1 != f2); break;
3683 case TYPE_GREATER: n1 = (f1 > f2); break;
3684 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
3685 case TYPE_SMALLER: n1 = (f1 < f2); break;
3686 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
3687 case TYPE_UNKNOWN:
3688 case TYPE_MATCH:
3689 case TYPE_NOMATCH: break; /* avoid gcc warning */
3690 }
3691 }
3692#endif
3693
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 /*
3695 * If one of the two variables is a number, compare as a number.
3696 * When using "=~" or "!~", always compare as string.
3697 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003698 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3700 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003701 n1 = get_tv_number(rettv);
3702 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 switch (type)
3704 {
3705 case TYPE_EQUAL: n1 = (n1 == n2); break;
3706 case TYPE_NEQUAL: n1 = (n1 != n2); break;
3707 case TYPE_GREATER: n1 = (n1 > n2); break;
3708 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
3709 case TYPE_SMALLER: n1 = (n1 < n2); break;
3710 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
3711 case TYPE_UNKNOWN:
3712 case TYPE_MATCH:
3713 case TYPE_NOMATCH: break; /* avoid gcc warning */
3714 }
3715 }
3716 else
3717 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003718 s1 = get_tv_string_buf(rettv, buf1);
3719 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
3721 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
3722 else
3723 i = 0;
3724 n1 = FALSE;
3725 switch (type)
3726 {
3727 case TYPE_EQUAL: n1 = (i == 0); break;
3728 case TYPE_NEQUAL: n1 = (i != 0); break;
3729 case TYPE_GREATER: n1 = (i > 0); break;
3730 case TYPE_GEQUAL: n1 = (i >= 0); break;
3731 case TYPE_SMALLER: n1 = (i < 0); break;
3732 case TYPE_SEQUAL: n1 = (i <= 0); break;
3733
3734 case TYPE_MATCH:
3735 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003736 n1 = pattern_match(s2, s1, ic);
3737 if (type == TYPE_NOMATCH)
3738 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 break;
3740
3741 case TYPE_UNKNOWN: break; /* avoid gcc warning */
3742 }
3743 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003744 clear_tv(rettv);
3745 clear_tv(&var2);
3746 rettv->v_type = VAR_NUMBER;
3747 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 }
3749 }
3750
3751 return OK;
3752}
3753
3754/*
3755 * Handle fourth level expression:
3756 * + number addition
3757 * - number subtraction
3758 * . string concatenation
3759 *
3760 * "arg" must point to the first non-white of the expression.
3761 * "arg" is advanced to the next non-white after the recognized expression.
3762 *
3763 * Return OK or FAIL.
3764 */
3765 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003766eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767{
Bram Moolenaar33570922005-01-25 22:26:29 +00003768 typval_T var2;
3769 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003771 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003772#ifdef FEAT_FLOAT
3773 float_T f1 = 0, f2 = 0;
3774#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 char_u *s1, *s2;
3776 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3777 char_u *p;
3778
3779 /*
3780 * Get the first variable.
3781 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003782 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 return FAIL;
3784
3785 /*
3786 * Repeat computing, until no '+', '-' or '.' is following.
3787 */
3788 for (;;)
3789 {
3790 op = **arg;
3791 if (op != '+' && op != '-' && op != '.')
3792 break;
3793
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003794 if ((op != '+' || rettv->v_type != VAR_LIST)
3795#ifdef FEAT_FLOAT
3796 && (op == '.' || rettv->v_type != VAR_FLOAT)
3797#endif
3798 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003799 {
3800 /* For "list + ...", an illegal use of the first operand as
3801 * a number cannot be determined before evaluating the 2nd
3802 * operand: if this is also a list, all is ok.
3803 * For "something . ...", "something - ..." or "non-list + ...",
3804 * we know that the first operand needs to be a string or number
3805 * without evaluating the 2nd operand. So check before to avoid
3806 * side effects after an error. */
3807 if (evaluate && get_tv_string_chk(rettv) == NULL)
3808 {
3809 clear_tv(rettv);
3810 return FAIL;
3811 }
3812 }
3813
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 /*
3815 * Get the second variable.
3816 */
3817 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003818 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003820 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 return FAIL;
3822 }
3823
3824 if (evaluate)
3825 {
3826 /*
3827 * Compute the result.
3828 */
3829 if (op == '.')
3830 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003831 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
3832 s2 = get_tv_string_buf_chk(&var2, buf2);
3833 if (s2 == NULL) /* type error ? */
3834 {
3835 clear_tv(rettv);
3836 clear_tv(&var2);
3837 return FAIL;
3838 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003839 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003840 clear_tv(rettv);
3841 rettv->v_type = VAR_STRING;
3842 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003844 else if (op == '+' && rettv->v_type == VAR_LIST
3845 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003846 {
3847 /* concatenate Lists */
3848 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3849 &var3) == FAIL)
3850 {
3851 clear_tv(rettv);
3852 clear_tv(&var2);
3853 return FAIL;
3854 }
3855 clear_tv(rettv);
3856 *rettv = var3;
3857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 else
3859 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003860 int error = FALSE;
3861
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003862#ifdef FEAT_FLOAT
3863 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003864 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003865 f1 = rettv->vval.v_float;
3866 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003867 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003868 else
3869#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003870 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003871 n1 = get_tv_number_chk(rettv, &error);
3872 if (error)
3873 {
3874 /* This can only happen for "list + non-list". For
3875 * "non-list + ..." or "something - ...", we returned
3876 * before evaluating the 2nd operand. */
3877 clear_tv(rettv);
3878 return FAIL;
3879 }
3880#ifdef FEAT_FLOAT
3881 if (var2.v_type == VAR_FLOAT)
3882 f1 = n1;
3883#endif
3884 }
3885#ifdef FEAT_FLOAT
3886 if (var2.v_type == VAR_FLOAT)
3887 {
3888 f2 = var2.vval.v_float;
3889 n2 = 0;
3890 }
3891 else
3892#endif
3893 {
3894 n2 = get_tv_number_chk(&var2, &error);
3895 if (error)
3896 {
3897 clear_tv(rettv);
3898 clear_tv(&var2);
3899 return FAIL;
3900 }
3901#ifdef FEAT_FLOAT
3902 if (rettv->v_type == VAR_FLOAT)
3903 f2 = n2;
3904#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003905 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003906 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003907
3908#ifdef FEAT_FLOAT
3909 /* If there is a float on either side the result is a float. */
3910 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3911 {
3912 if (op == '+')
3913 f1 = f1 + f2;
3914 else
3915 f1 = f1 - f2;
3916 rettv->v_type = VAR_FLOAT;
3917 rettv->vval.v_float = f1;
3918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003920#endif
3921 {
3922 if (op == '+')
3923 n1 = n1 + n2;
3924 else
3925 n1 = n1 - n2;
3926 rettv->v_type = VAR_NUMBER;
3927 rettv->vval.v_number = n1;
3928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003930 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 }
3932 }
3933 return OK;
3934}
3935
3936/*
3937 * Handle fifth level expression:
3938 * * number multiplication
3939 * / number division
3940 * % number modulo
3941 *
3942 * "arg" must point to the first non-white of the expression.
3943 * "arg" is advanced to the next non-white after the recognized expression.
3944 *
3945 * Return OK or FAIL.
3946 */
3947 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003948eval6(
3949 char_u **arg,
3950 typval_T *rettv,
3951 int evaluate,
3952 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953{
Bram Moolenaar33570922005-01-25 22:26:29 +00003954 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003956 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003957#ifdef FEAT_FLOAT
3958 int use_float = FALSE;
3959 float_T f1 = 0, f2;
3960#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003961 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962
3963 /*
3964 * Get the first variable.
3965 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003966 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 return FAIL;
3968
3969 /*
3970 * Repeat computing, until no '*', '/' or '%' is following.
3971 */
3972 for (;;)
3973 {
3974 op = **arg;
3975 if (op != '*' && op != '/' && op != '%')
3976 break;
3977
3978 if (evaluate)
3979 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003980#ifdef FEAT_FLOAT
3981 if (rettv->v_type == VAR_FLOAT)
3982 {
3983 f1 = rettv->vval.v_float;
3984 use_float = TRUE;
3985 n1 = 0;
3986 }
3987 else
3988#endif
3989 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003990 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003991 if (error)
3992 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 }
3994 else
3995 n1 = 0;
3996
3997 /*
3998 * Get the second variable.
3999 */
4000 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004001 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 return FAIL;
4003
4004 if (evaluate)
4005 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004006#ifdef FEAT_FLOAT
4007 if (var2.v_type == VAR_FLOAT)
4008 {
4009 if (!use_float)
4010 {
4011 f1 = n1;
4012 use_float = TRUE;
4013 }
4014 f2 = var2.vval.v_float;
4015 n2 = 0;
4016 }
4017 else
4018#endif
4019 {
4020 n2 = get_tv_number_chk(&var2, &error);
4021 clear_tv(&var2);
4022 if (error)
4023 return FAIL;
4024#ifdef FEAT_FLOAT
4025 if (use_float)
4026 f2 = n2;
4027#endif
4028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029
4030 /*
4031 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004032 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004034#ifdef FEAT_FLOAT
4035 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004037 if (op == '*')
4038 f1 = f1 * f2;
4039 else if (op == '/')
4040 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004041# ifdef VMS
4042 /* VMS crashes on divide by zero, work around it */
4043 if (f2 == 0.0)
4044 {
4045 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004046 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004047 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004048 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004049 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004050 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004051 }
4052 else
4053 f1 = f1 / f2;
4054# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004055 /* We rely on the floating point library to handle divide
4056 * by zero to result in "inf" and not a crash. */
4057 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004058# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004061 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004062 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004063 return FAIL;
4064 }
4065 rettv->v_type = VAR_FLOAT;
4066 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 }
4068 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004071 if (op == '*')
4072 n1 = n1 * n2;
4073 else if (op == '/')
4074 {
4075 if (n2 == 0) /* give an error message? */
4076 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004077 if (n1 == 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004078 n1 = VARNUM_MIN; /* similar to NaN */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004079 else if (n1 < 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004080 n1 = -VARNUM_MAX;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004081 else
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004082 n1 = VARNUM_MAX;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004083 }
4084 else
4085 n1 = n1 / n2;
4086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004088 {
4089 if (n2 == 0) /* give an error message? */
4090 n1 = 0;
4091 else
4092 n1 = n1 % n2;
4093 }
4094 rettv->v_type = VAR_NUMBER;
4095 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 }
4098 }
4099
4100 return OK;
4101}
4102
4103/*
4104 * Handle sixth level expression:
4105 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004106 * "string" string constant
4107 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 * &option-name option value
4109 * @r register contents
4110 * identifier variable value
4111 * function() function call
4112 * $VAR environment variable
4113 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004114 * [expr, expr] List
4115 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 *
4117 * Also handle:
4118 * ! in front logical NOT
4119 * - in front unary minus
4120 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004121 * trailing [] subscript in String or List
4122 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 *
4124 * "arg" must point to the first non-white of the expression.
4125 * "arg" is advanced to the next non-white after the recognized expression.
4126 *
4127 * Return OK or FAIL.
4128 */
4129 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004130eval7(
4131 char_u **arg,
4132 typval_T *rettv,
4133 int evaluate,
4134 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004136 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 int len;
4138 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 char_u *start_leader, *end_leader;
4140 int ret = OK;
4141 char_u *alias;
4142
4143 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004144 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004145 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004147 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148
4149 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004150 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 */
4152 start_leader = *arg;
4153 while (**arg == '!' || **arg == '-' || **arg == '+')
4154 *arg = skipwhite(*arg + 1);
4155 end_leader = *arg;
4156
4157 switch (**arg)
4158 {
4159 /*
4160 * Number constant.
4161 */
4162 case '0':
4163 case '1':
4164 case '2':
4165 case '3':
4166 case '4':
4167 case '5':
4168 case '6':
4169 case '7':
4170 case '8':
4171 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004172 {
4173#ifdef FEAT_FLOAT
4174 char_u *p = skipdigits(*arg + 1);
4175 int get_float = FALSE;
4176
4177 /* We accept a float when the format matches
4178 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004179 * strict to avoid backwards compatibility problems.
4180 * Don't look for a float after the "." operator, so that
4181 * ":let vers = 1.2.3" doesn't fail. */
4182 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004184 get_float = TRUE;
4185 p = skipdigits(p + 2);
4186 if (*p == 'e' || *p == 'E')
4187 {
4188 ++p;
4189 if (*p == '-' || *p == '+')
4190 ++p;
4191 if (!vim_isdigit(*p))
4192 get_float = FALSE;
4193 else
4194 p = skipdigits(p + 1);
4195 }
4196 if (ASCII_ISALPHA(*p) || *p == '.')
4197 get_float = FALSE;
4198 }
4199 if (get_float)
4200 {
4201 float_T f;
4202
4203 *arg += string2float(*arg, &f);
4204 if (evaluate)
4205 {
4206 rettv->v_type = VAR_FLOAT;
4207 rettv->vval.v_float = f;
4208 }
4209 }
4210 else
4211#endif
4212 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01004213 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004214 *arg += len;
4215 if (evaluate)
4216 {
4217 rettv->v_type = VAR_NUMBER;
4218 rettv->vval.v_number = n;
4219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 }
4221 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223
4224 /*
4225 * String constant: "string".
4226 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004227 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 break;
4229
4230 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004231 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004233 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004234 break;
4235
4236 /*
4237 * List: [expr, expr]
4238 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004239 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 break;
4241
4242 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004243 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004244 * Dictionary: {key: val, key: val}
4245 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004246 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4247 if (ret == NOTDONE)
4248 ret = get_dict_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004249 break;
4250
4251 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004252 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004254 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 break;
4256
4257 /*
4258 * Environment variable: $VAR.
4259 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004260 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 break;
4262
4263 /*
4264 * Register contents: @r.
4265 */
4266 case '@': ++*arg;
4267 if (evaluate)
4268 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004269 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004270 rettv->vval.v_string = get_reg_contents(**arg,
4271 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 }
4273 if (**arg != NUL)
4274 ++*arg;
4275 break;
4276
4277 /*
4278 * nested expression: (expression).
4279 */
4280 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004281 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 if (**arg == ')')
4283 ++*arg;
4284 else if (ret == OK)
4285 {
4286 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004287 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 ret = FAIL;
4289 }
4290 break;
4291
Bram Moolenaar8c711452005-01-14 21:53:12 +00004292 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 break;
4294 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004295
4296 if (ret == NOTDONE)
4297 {
4298 /*
4299 * Must be a variable or function name.
4300 * Can also be a curly-braces kind of name: {expr}.
4301 */
4302 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004303 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004304 if (alias != NULL)
4305 s = alias;
4306
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004307 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004308 ret = FAIL;
4309 else
4310 {
4311 if (**arg == '(') /* recursive! */
4312 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004313 partial_T *partial;
4314
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004315 if (!evaluate)
4316 check_vars(s, len);
4317
Bram Moolenaar8c711452005-01-14 21:53:12 +00004318 /* If "s" is the name of a variable of type VAR_FUNC
4319 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004320 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004321
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004322 /* Need to make a copy, in case evaluating the arguments makes
4323 * the name invalid. */
4324 s = vim_strsave(s);
4325 if (s == NULL)
4326 ret = FAIL;
4327 else
4328 /* Invoke the function. */
4329 ret = get_func_tv(s, len, rettv, arg,
4330 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4331 &len, evaluate, partial, NULL);
4332 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004333
4334 /* If evaluate is FALSE rettv->v_type was not set in
4335 * get_func_tv, but it's needed in handle_subscript() to parse
4336 * what follows. So set it here. */
4337 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4338 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004339 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004340 rettv->v_type = VAR_FUNC;
4341 }
4342
Bram Moolenaar8c711452005-01-14 21:53:12 +00004343 /* Stop the expression evaluation when immediately
4344 * aborting on error, or when an interrupt occurred or
4345 * an exception was thrown but not caught. */
4346 if (aborting())
4347 {
4348 if (ret == OK)
4349 clear_tv(rettv);
4350 ret = FAIL;
4351 }
4352 }
4353 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004354 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004355 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004356 {
4357 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004358 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004359 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004360 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004361 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004362 }
4363
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 *arg = skipwhite(*arg);
4365
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004366 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4367 * expr(expr). */
4368 if (ret == OK)
4369 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370
4371 /*
4372 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4373 */
4374 if (ret == OK && evaluate && end_leader > start_leader)
4375 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004376 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004377 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004378#ifdef FEAT_FLOAT
4379 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004380
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004381 if (rettv->v_type == VAR_FLOAT)
4382 f = rettv->vval.v_float;
4383 else
4384#endif
4385 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004386 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004388 clear_tv(rettv);
4389 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004391 else
4392 {
4393 while (end_leader > start_leader)
4394 {
4395 --end_leader;
4396 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004397 {
4398#ifdef FEAT_FLOAT
4399 if (rettv->v_type == VAR_FLOAT)
4400 f = !f;
4401 else
4402#endif
4403 val = !val;
4404 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004405 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004406 {
4407#ifdef FEAT_FLOAT
4408 if (rettv->v_type == VAR_FLOAT)
4409 f = -f;
4410 else
4411#endif
4412 val = -val;
4413 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004414 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004415#ifdef FEAT_FLOAT
4416 if (rettv->v_type == VAR_FLOAT)
4417 {
4418 clear_tv(rettv);
4419 rettv->vval.v_float = f;
4420 }
4421 else
4422#endif
4423 {
4424 clear_tv(rettv);
4425 rettv->v_type = VAR_NUMBER;
4426 rettv->vval.v_number = val;
4427 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 }
4430
4431 return ret;
4432}
4433
4434/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004435 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4436 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004437 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4438 */
4439 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004440eval_index(
4441 char_u **arg,
4442 typval_T *rettv,
4443 int evaluate,
4444 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004445{
4446 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004447 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004448 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004449 long len = -1;
4450 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004451 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004452 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004453
Bram Moolenaara03f2332016-02-06 18:09:59 +01004454 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004455 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004456 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004457 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004458 if (verbose)
4459 EMSG(_("E695: Cannot index a Funcref"));
4460 return FAIL;
4461 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004462#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004463 if (verbose)
4464 EMSG(_(e_float_as_string));
4465 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004466#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004467 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004468 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004469 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004470 if (verbose)
4471 EMSG(_("E909: Cannot index a special variable"));
4472 return FAIL;
4473 case VAR_UNKNOWN:
4474 if (evaluate)
4475 return FAIL;
4476 /* FALLTHROUGH */
4477
4478 case VAR_STRING:
4479 case VAR_NUMBER:
4480 case VAR_LIST:
4481 case VAR_DICT:
4482 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004483 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004484
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004485 init_tv(&var1);
4486 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004487 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004488 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004489 /*
4490 * dict.name
4491 */
4492 key = *arg + 1;
4493 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4494 ;
4495 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004496 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004497 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004498 }
4499 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004500 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004501 /*
4502 * something[idx]
4503 *
4504 * Get the (first) variable from inside the [].
4505 */
4506 *arg = skipwhite(*arg + 1);
4507 if (**arg == ':')
4508 empty1 = TRUE;
4509 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4510 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004511 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4512 {
4513 /* not a number or string */
4514 clear_tv(&var1);
4515 return FAIL;
4516 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004517
4518 /*
4519 * Get the second variable from inside the [:].
4520 */
4521 if (**arg == ':')
4522 {
4523 range = TRUE;
4524 *arg = skipwhite(*arg + 1);
4525 if (**arg == ']')
4526 empty2 = TRUE;
4527 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4528 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004529 if (!empty1)
4530 clear_tv(&var1);
4531 return FAIL;
4532 }
4533 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4534 {
4535 /* not a number or string */
4536 if (!empty1)
4537 clear_tv(&var1);
4538 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004539 return FAIL;
4540 }
4541 }
4542
4543 /* Check for the ']'. */
4544 if (**arg != ']')
4545 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004546 if (verbose)
4547 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004548 clear_tv(&var1);
4549 if (range)
4550 clear_tv(&var2);
4551 return FAIL;
4552 }
4553 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004554 }
4555
4556 if (evaluate)
4557 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004558 n1 = 0;
4559 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004560 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004561 n1 = get_tv_number(&var1);
4562 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004563 }
4564 if (range)
4565 {
4566 if (empty2)
4567 n2 = -1;
4568 else
4569 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004570 n2 = get_tv_number(&var2);
4571 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004572 }
4573 }
4574
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004575 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004576 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004577 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004578 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004579 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004580 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004581 case VAR_SPECIAL:
4582 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004583 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004584 break; /* not evaluating, skipping over subscript */
4585
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004586 case VAR_NUMBER:
4587 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004588 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004589 len = (long)STRLEN(s);
4590 if (range)
4591 {
4592 /* The resulting variable is a substring. If the indexes
4593 * are out of range the result is empty. */
4594 if (n1 < 0)
4595 {
4596 n1 = len + n1;
4597 if (n1 < 0)
4598 n1 = 0;
4599 }
4600 if (n2 < 0)
4601 n2 = len + n2;
4602 else if (n2 >= len)
4603 n2 = len;
4604 if (n1 >= len || n2 < 0 || n1 > n2)
4605 s = NULL;
4606 else
4607 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4608 }
4609 else
4610 {
4611 /* The resulting variable is a string of a single
4612 * character. If the index is too big or negative the
4613 * result is empty. */
4614 if (n1 >= len || n1 < 0)
4615 s = NULL;
4616 else
4617 s = vim_strnsave(s + n1, 1);
4618 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004619 clear_tv(rettv);
4620 rettv->v_type = VAR_STRING;
4621 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004622 break;
4623
4624 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004625 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004626 if (n1 < 0)
4627 n1 = len + n1;
4628 if (!empty1 && (n1 < 0 || n1 >= len))
4629 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004630 /* For a range we allow invalid values and return an empty
4631 * list. A list index out of range is an error. */
4632 if (!range)
4633 {
4634 if (verbose)
4635 EMSGN(_(e_listidx), n1);
4636 return FAIL;
4637 }
4638 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004639 }
4640 if (range)
4641 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004642 list_T *l;
4643 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004644
4645 if (n2 < 0)
4646 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004647 else if (n2 >= len)
4648 n2 = len - 1;
4649 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004650 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004651 l = list_alloc();
4652 if (l == NULL)
4653 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004654 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004655 n1 <= n2; ++n1)
4656 {
4657 if (list_append_tv(l, &item->li_tv) == FAIL)
4658 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004659 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004660 return FAIL;
4661 }
4662 item = item->li_next;
4663 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004664 clear_tv(rettv);
4665 rettv->v_type = VAR_LIST;
4666 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004667 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004668 }
4669 else
4670 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004671 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004672 clear_tv(rettv);
4673 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004674 }
4675 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004676
4677 case VAR_DICT:
4678 if (range)
4679 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004680 if (verbose)
4681 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004682 if (len == -1)
4683 clear_tv(&var1);
4684 return FAIL;
4685 }
4686 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004687 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004688
4689 if (len == -1)
4690 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02004691 key = get_tv_string_chk(&var1);
4692 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004693 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004694 clear_tv(&var1);
4695 return FAIL;
4696 }
4697 }
4698
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004699 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004700
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004701 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004702 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004703 if (len == -1)
4704 clear_tv(&var1);
4705 if (item == NULL)
4706 return FAIL;
4707
4708 copy_tv(&item->di_tv, &var1);
4709 clear_tv(rettv);
4710 *rettv = var1;
4711 }
4712 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004713 }
4714 }
4715
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004716 return OK;
4717}
4718
4719/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 * Get an option value.
4721 * "arg" points to the '&' or '+' before the option name.
4722 * "arg" is advanced to character after the option name.
4723 * Return OK or FAIL.
4724 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004725 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004726get_option_tv(
4727 char_u **arg,
4728 typval_T *rettv, /* when NULL, only check if option exists */
4729 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730{
4731 char_u *option_end;
4732 long numval;
4733 char_u *stringval;
4734 int opt_type;
4735 int c;
4736 int working = (**arg == '+'); /* has("+option") */
4737 int ret = OK;
4738 int opt_flags;
4739
4740 /*
4741 * Isolate the option name and find its value.
4742 */
4743 option_end = find_option_end(arg, &opt_flags);
4744 if (option_end == NULL)
4745 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004746 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 EMSG2(_("E112: Option name missing: %s"), *arg);
4748 return FAIL;
4749 }
4750
4751 if (!evaluate)
4752 {
4753 *arg = option_end;
4754 return OK;
4755 }
4756
4757 c = *option_end;
4758 *option_end = NUL;
4759 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004760 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761
4762 if (opt_type == -3) /* invalid name */
4763 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004764 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 EMSG2(_("E113: Unknown option: %s"), *arg);
4766 ret = FAIL;
4767 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004768 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 {
4770 if (opt_type == -2) /* hidden string option */
4771 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004772 rettv->v_type = VAR_STRING;
4773 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 }
4775 else if (opt_type == -1) /* hidden number option */
4776 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004777 rettv->v_type = VAR_NUMBER;
4778 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 }
4780 else if (opt_type == 1) /* number option */
4781 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004782 rettv->v_type = VAR_NUMBER;
4783 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 }
4785 else /* string option */
4786 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004787 rettv->v_type = VAR_STRING;
4788 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 }
4790 }
4791 else if (working && (opt_type == -2 || opt_type == -1))
4792 ret = FAIL;
4793
4794 *option_end = c; /* put back for error messages */
4795 *arg = option_end;
4796
4797 return ret;
4798}
4799
4800/*
4801 * Allocate a variable for a string constant.
4802 * Return OK or FAIL.
4803 */
4804 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004805get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806{
4807 char_u *p;
4808 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 int extra = 0;
4810
4811 /*
4812 * Find the end of the string, skipping backslashed characters.
4813 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004814 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 {
4816 if (*p == '\\' && p[1] != NUL)
4817 {
4818 ++p;
4819 /* A "\<x>" form occupies at least 4 characters, and produces up
4820 * to 6 characters: reserve space for 2 extra */
4821 if (*p == '<')
4822 extra += 2;
4823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 }
4825
4826 if (*p != '"')
4827 {
4828 EMSG2(_("E114: Missing quote: %s"), *arg);
4829 return FAIL;
4830 }
4831
4832 /* If only parsing, set *arg and return here */
4833 if (!evaluate)
4834 {
4835 *arg = p + 1;
4836 return OK;
4837 }
4838
4839 /*
4840 * Copy the string into allocated memory, handling backslashed
4841 * characters.
4842 */
4843 name = alloc((unsigned)(p - *arg + extra));
4844 if (name == NULL)
4845 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004846 rettv->v_type = VAR_STRING;
4847 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848
Bram Moolenaar8c711452005-01-14 21:53:12 +00004849 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 {
4851 if (*p == '\\')
4852 {
4853 switch (*++p)
4854 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004855 case 'b': *name++ = BS; ++p; break;
4856 case 'e': *name++ = ESC; ++p; break;
4857 case 'f': *name++ = FF; ++p; break;
4858 case 'n': *name++ = NL; ++p; break;
4859 case 'r': *name++ = CAR; ++p; break;
4860 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861
4862 case 'X': /* hex: "\x1", "\x12" */
4863 case 'x':
4864 case 'u': /* Unicode: "\u0023" */
4865 case 'U':
4866 if (vim_isxdigit(p[1]))
4867 {
4868 int n, nr;
4869 int c = toupper(*p);
4870
4871 if (c == 'X')
4872 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004873 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004875 else
4876 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 nr = 0;
4878 while (--n >= 0 && vim_isxdigit(p[1]))
4879 {
4880 ++p;
4881 nr = (nr << 4) + hex2nr(*p);
4882 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004883 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884#ifdef FEAT_MBYTE
4885 /* For "\u" store the number according to
4886 * 'encoding'. */
4887 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004888 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 else
4890#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004891 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 break;
4894
4895 /* octal: "\1", "\12", "\123" */
4896 case '0':
4897 case '1':
4898 case '2':
4899 case '3':
4900 case '4':
4901 case '5':
4902 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004903 case '7': *name = *p++ - '0';
4904 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004906 *name = (*name << 3) + *p++ - '0';
4907 if (*p >= '0' && *p <= '7')
4908 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004910 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 break;
4912
4913 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02004914 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 if (extra != 0)
4916 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004917 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 break;
4919 }
4920 /* FALLTHROUGH */
4921
Bram Moolenaar8c711452005-01-14 21:53:12 +00004922 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 break;
4924 }
4925 }
4926 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004927 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004930 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02004931 if (*p != NUL) /* just in case */
4932 ++p;
4933 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 return OK;
4936}
4937
4938/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004939 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 * Return OK or FAIL.
4941 */
4942 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004943get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944{
4945 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004946 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004947 int reduce = 0;
4948
4949 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004950 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004951 */
4952 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
4953 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004954 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004955 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004956 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004957 break;
4958 ++reduce;
4959 ++p;
4960 }
4961 }
4962
Bram Moolenaar8c711452005-01-14 21:53:12 +00004963 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004964 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004965 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004966 return FAIL;
4967 }
4968
Bram Moolenaar8c711452005-01-14 21:53:12 +00004969 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004970 if (!evaluate)
4971 {
4972 *arg = p + 1;
4973 return OK;
4974 }
4975
4976 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004977 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004978 */
4979 str = alloc((unsigned)((p - *arg) - reduce));
4980 if (str == NULL)
4981 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004982 rettv->v_type = VAR_STRING;
4983 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004984
Bram Moolenaar8c711452005-01-14 21:53:12 +00004985 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004986 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004987 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004988 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004989 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004990 break;
4991 ++p;
4992 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004993 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004994 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004995 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004996 *arg = p + 1;
4997
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004998 return OK;
4999}
5000
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005001/*
5002 * Return the function name of the partial.
5003 */
5004 char_u *
5005partial_name(partial_T *pt)
5006{
5007 if (pt->pt_name != NULL)
5008 return pt->pt_name;
5009 return pt->pt_func->uf_name;
5010}
5011
Bram Moolenaarddecc252016-04-06 22:59:37 +02005012 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005013partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005014{
5015 int i;
5016
5017 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005018 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005019 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005020 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005021 if (pt->pt_name != NULL)
5022 {
5023 func_unref(pt->pt_name);
5024 vim_free(pt->pt_name);
5025 }
5026 else
5027 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005028 vim_free(pt);
5029}
5030
5031/*
5032 * Unreference a closure: decrement the reference count and free it when it
5033 * becomes zero.
5034 */
5035 void
5036partial_unref(partial_T *pt)
5037{
5038 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005039 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005040}
5041
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005042static int tv_equal_recurse_limit;
5043
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005044 static int
5045func_equal(
5046 typval_T *tv1,
5047 typval_T *tv2,
5048 int ic) /* ignore case */
5049{
5050 char_u *s1, *s2;
5051 dict_T *d1, *d2;
5052 int a1, a2;
5053 int i;
5054
5055 /* empty and NULL function name considered the same */
5056 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005057 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005058 if (s1 != NULL && *s1 == NUL)
5059 s1 = NULL;
5060 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005061 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005062 if (s2 != NULL && *s2 == NUL)
5063 s2 = NULL;
5064 if (s1 == NULL || s2 == NULL)
5065 {
5066 if (s1 != s2)
5067 return FALSE;
5068 }
5069 else if (STRCMP(s1, s2) != 0)
5070 return FALSE;
5071
5072 /* empty dict and NULL dict is different */
5073 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5074 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5075 if (d1 == NULL || d2 == NULL)
5076 {
5077 if (d1 != d2)
5078 return FALSE;
5079 }
5080 else if (!dict_equal(d1, d2, ic, TRUE))
5081 return FALSE;
5082
5083 /* empty list and no list considered the same */
5084 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5085 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5086 if (a1 != a2)
5087 return FALSE;
5088 for (i = 0; i < a1; ++i)
5089 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5090 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5091 return FALSE;
5092
5093 return TRUE;
5094}
5095
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005096/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005097 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005098 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005099 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005100 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005101 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005102tv_equal(
5103 typval_T *tv1,
5104 typval_T *tv2,
5105 int ic, /* ignore case */
5106 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005107{
5108 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005109 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005110 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005111 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005112
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005113 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005114 * recursiveness to a limit. We guess they are equal then.
5115 * A fixed limit has the problem of still taking an awful long time.
5116 * Reduce the limit every time running into it. That should work fine for
5117 * deeply linked structures that are not recursively linked and catch
5118 * recursiveness quickly. */
5119 if (!recursive)
5120 tv_equal_recurse_limit = 1000;
5121 if (recursive_cnt >= tv_equal_recurse_limit)
5122 {
5123 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005124 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005125 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005126
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005127 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5128 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005129 if ((tv1->v_type == VAR_FUNC
5130 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5131 && (tv2->v_type == VAR_FUNC
5132 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5133 {
5134 ++recursive_cnt;
5135 r = func_equal(tv1, tv2, ic);
5136 --recursive_cnt;
5137 return r;
5138 }
5139
5140 if (tv1->v_type != tv2->v_type)
5141 return FALSE;
5142
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005143 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005144 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005145 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005146 ++recursive_cnt;
5147 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5148 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005149 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005150
5151 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005152 ++recursive_cnt;
5153 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5154 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005155 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005156
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005157 case VAR_NUMBER:
5158 return tv1->vval.v_number == tv2->vval.v_number;
5159
5160 case VAR_STRING:
5161 s1 = get_tv_string_buf(tv1, buf1);
5162 s2 = get_tv_string_buf(tv2, buf2);
5163 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005164
5165 case VAR_SPECIAL:
5166 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005167
5168 case VAR_FLOAT:
5169#ifdef FEAT_FLOAT
5170 return tv1->vval.v_float == tv2->vval.v_float;
5171#endif
5172 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005173#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005174 return tv1->vval.v_job == tv2->vval.v_job;
5175#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005176 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005177#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005178 return tv1->vval.v_channel == tv2->vval.v_channel;
5179#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005180 case VAR_FUNC:
5181 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005182 case VAR_UNKNOWN:
5183 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005184 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005185
Bram Moolenaara03f2332016-02-06 18:09:59 +01005186 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5187 * does not equal anything, not even itself. */
5188 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005189}
5190
5191/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005192 * Return the next (unique) copy ID.
5193 * Used for serializing nested structures.
5194 */
5195 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005196get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005197{
5198 current_copyID += COPYID_INC;
5199 return current_copyID;
5200}
5201
5202/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005203 * Garbage collection for lists and dictionaries.
5204 *
5205 * We use reference counts to be able to free most items right away when they
5206 * are no longer used. But for composite items it's possible that it becomes
5207 * unused while the reference count is > 0: When there is a recursive
5208 * reference. Example:
5209 * :let l = [1, 2, 3]
5210 * :let d = {9: l}
5211 * :let l[1] = d
5212 *
5213 * Since this is quite unusual we handle this with garbage collection: every
5214 * once in a while find out which lists and dicts are not referenced from any
5215 * variable.
5216 *
5217 * Here is a good reference text about garbage collection (refers to Python
5218 * but it applies to all reference-counting mechanisms):
5219 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005220 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005221
5222/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005223 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005224 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005225 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005226 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005227 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005228garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005229{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005230 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005231 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005232 buf_T *buf;
5233 win_T *wp;
5234 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005235 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005236#ifdef FEAT_WINDOWS
5237 tabpage_T *tp;
5238#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005239
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005240 if (!testing)
5241 {
5242 /* Only do this once. */
5243 want_garbage_collect = FALSE;
5244 may_garbage_collect = FALSE;
5245 garbage_collect_at_exit = FALSE;
5246 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005247
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005248 /* We advance by two because we add one for items referenced through
5249 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005250 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005251
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005252 /*
5253 * 1. Go through all accessible variables and mark all lists and dicts
5254 * with copyID.
5255 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005256
5257 /* Don't free variables in the previous_funccal list unless they are only
5258 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005259 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005260 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005261
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005262 /* script-local variables */
5263 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005264 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005265
5266 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005267 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005268 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5269 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005270
5271 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005272 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005273 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5274 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005275#ifdef FEAT_AUTOCMD
5276 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005277 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5278 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005279#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005280
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005281#ifdef FEAT_WINDOWS
5282 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005283 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005284 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5285 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005286#endif
5287
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005288 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005289 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005290
5291 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005292 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005293
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005294 /* named functions (matters for closures) */
5295 abort = abort || set_ref_in_functions(copyID);
5296
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005297 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005298 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005299
Bram Moolenaard812df62008-11-09 12:46:09 +00005300 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005301 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005302
Bram Moolenaar1dced572012-04-05 16:54:08 +02005303#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005304 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005305#endif
5306
Bram Moolenaardb913952012-06-29 12:54:53 +02005307#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005308 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005309#endif
5310
5311#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005312 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005313#endif
5314
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005315#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005316 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005317 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005318#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005319#ifdef FEAT_NETBEANS_INTG
5320 abort = abort || set_ref_in_nb_channel(copyID);
5321#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005322
Bram Moolenaare3188e22016-05-31 21:13:04 +02005323#ifdef FEAT_TIMERS
5324 abort = abort || set_ref_in_timer(copyID);
5325#endif
5326
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005327 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005328 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005329 /*
5330 * 2. Free lists and dictionaries that are not referenced.
5331 */
5332 did_free = free_unref_items(copyID);
5333
5334 /*
5335 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005336 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005337 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005338 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005339 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005340 else if (p_verbose > 0)
5341 {
5342 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
5343 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005344
5345 return did_free;
5346}
5347
5348/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005349 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005350 */
5351 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005352free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005353{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005354 int did_free = FALSE;
5355
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005356 /* Let all "free" functions know that we are here. This means no
5357 * dictionaries, lists, channels or jobs are to be freed, because we will
5358 * do that here. */
5359 in_free_unref_items = TRUE;
5360
5361 /*
5362 * PASS 1: free the contents of the items. We don't free the items
5363 * themselves yet, so that it is possible to decrement refcount counters
5364 */
5365
Bram Moolenaarcd524592016-07-17 14:57:05 +02005366 /* Go through the list of dicts and free items without the copyID. */
5367 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005368
Bram Moolenaarda861d62016-07-17 15:46:27 +02005369 /* Go through the list of lists and free items without the copyID. */
5370 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005371
5372#ifdef FEAT_JOB_CHANNEL
5373 /* Go through the list of jobs and free items without the copyID. This
5374 * must happen before doing channels, because jobs refer to channels, but
5375 * the reference from the channel to the job isn't tracked. */
5376 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5377
5378 /* Go through the list of channels and free items without the copyID. */
5379 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5380#endif
5381
5382 /*
5383 * PASS 2: free the items themselves.
5384 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005385 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005386 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005387
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005388#ifdef FEAT_JOB_CHANNEL
5389 /* Go through the list of jobs and free items without the copyID. This
5390 * must happen before doing channels, because jobs refer to channels, but
5391 * the reference from the channel to the job isn't tracked. */
5392 free_unused_jobs(copyID, COPYID_MASK);
5393
5394 /* Go through the list of channels and free items without the copyID. */
5395 free_unused_channels(copyID, COPYID_MASK);
5396#endif
5397
5398 in_free_unref_items = FALSE;
5399
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005400 return did_free;
5401}
5402
5403/*
5404 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005405 * "list_stack" is used to add lists to be marked. Can be NULL.
5406 *
5407 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005408 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005409 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005410set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005411{
5412 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005413 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005414 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005415 hashtab_T *cur_ht;
5416 ht_stack_T *ht_stack = NULL;
5417 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005418
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005419 cur_ht = ht;
5420 for (;;)
5421 {
5422 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005423 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005424 /* Mark each item in the hashtab. If the item contains a hashtab
5425 * it is added to ht_stack, if it contains a list it is added to
5426 * list_stack. */
5427 todo = (int)cur_ht->ht_used;
5428 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5429 if (!HASHITEM_EMPTY(hi))
5430 {
5431 --todo;
5432 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5433 &ht_stack, list_stack);
5434 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005435 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005436
5437 if (ht_stack == NULL)
5438 break;
5439
5440 /* take an item from the stack */
5441 cur_ht = ht_stack->ht;
5442 tempitem = ht_stack;
5443 ht_stack = ht_stack->prev;
5444 free(tempitem);
5445 }
5446
5447 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005448}
5449
5450/*
5451 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005452 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5453 *
5454 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005455 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005456 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005457set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005458{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005459 listitem_T *li;
5460 int abort = FALSE;
5461 list_T *cur_l;
5462 list_stack_T *list_stack = NULL;
5463 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005464
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005465 cur_l = l;
5466 for (;;)
5467 {
5468 if (!abort)
5469 /* Mark each item in the list. If the item contains a hashtab
5470 * it is added to ht_stack, if it contains a list it is added to
5471 * list_stack. */
5472 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5473 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5474 ht_stack, &list_stack);
5475 if (list_stack == NULL)
5476 break;
5477
5478 /* take an item from the stack */
5479 cur_l = list_stack->list;
5480 tempitem = list_stack;
5481 list_stack = list_stack->prev;
5482 free(tempitem);
5483 }
5484
5485 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005486}
5487
5488/*
5489 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005490 * "list_stack" is used to add lists to be marked. Can be NULL.
5491 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5492 *
5493 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005494 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005495 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005496set_ref_in_item(
5497 typval_T *tv,
5498 int copyID,
5499 ht_stack_T **ht_stack,
5500 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005501{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005502 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005503
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005504 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005505 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005506 dict_T *dd = tv->vval.v_dict;
5507
Bram Moolenaara03f2332016-02-06 18:09:59 +01005508 if (dd != NULL && dd->dv_copyID != copyID)
5509 {
5510 /* Didn't see this dict yet. */
5511 dd->dv_copyID = copyID;
5512 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005513 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005514 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5515 }
5516 else
5517 {
5518 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5519 if (newitem == NULL)
5520 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005521 else
5522 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005523 newitem->ht = &dd->dv_hashtab;
5524 newitem->prev = *ht_stack;
5525 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005526 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005527 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005528 }
5529 }
5530 else if (tv->v_type == VAR_LIST)
5531 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005532 list_T *ll = tv->vval.v_list;
5533
Bram Moolenaara03f2332016-02-06 18:09:59 +01005534 if (ll != NULL && ll->lv_copyID != copyID)
5535 {
5536 /* Didn't see this list yet. */
5537 ll->lv_copyID = copyID;
5538 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005539 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005540 abort = set_ref_in_list(ll, copyID, ht_stack);
5541 }
5542 else
5543 {
5544 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005545 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005546 if (newitem == NULL)
5547 abort = TRUE;
5548 else
5549 {
5550 newitem->list = ll;
5551 newitem->prev = *list_stack;
5552 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005553 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005554 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005555 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005556 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005557 else if (tv->v_type == VAR_FUNC)
5558 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005559 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005560 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005561 else if (tv->v_type == VAR_PARTIAL)
5562 {
5563 partial_T *pt = tv->vval.v_partial;
5564 int i;
5565
5566 /* A partial does not have a copyID, because it cannot contain itself.
5567 */
5568 if (pt != NULL)
5569 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005570 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005571
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005572 if (pt->pt_dict != NULL)
5573 {
5574 typval_T dtv;
5575
5576 dtv.v_type = VAR_DICT;
5577 dtv.vval.v_dict = pt->pt_dict;
5578 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5579 }
5580
5581 for (i = 0; i < pt->pt_argc; ++i)
5582 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5583 ht_stack, list_stack);
5584 }
5585 }
5586#ifdef FEAT_JOB_CHANNEL
5587 else if (tv->v_type == VAR_JOB)
5588 {
5589 job_T *job = tv->vval.v_job;
5590 typval_T dtv;
5591
5592 if (job != NULL && job->jv_copyID != copyID)
5593 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005594 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005595 if (job->jv_channel != NULL)
5596 {
5597 dtv.v_type = VAR_CHANNEL;
5598 dtv.vval.v_channel = job->jv_channel;
5599 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5600 }
5601 if (job->jv_exit_partial != NULL)
5602 {
5603 dtv.v_type = VAR_PARTIAL;
5604 dtv.vval.v_partial = job->jv_exit_partial;
5605 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5606 }
5607 }
5608 }
5609 else if (tv->v_type == VAR_CHANNEL)
5610 {
5611 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005612 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005613 typval_T dtv;
5614 jsonq_T *jq;
5615 cbq_T *cq;
5616
5617 if (ch != NULL && ch->ch_copyID != copyID)
5618 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005619 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005620 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005621 {
5622 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5623 jq = jq->jq_next)
5624 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5625 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5626 cq = cq->cq_next)
5627 if (cq->cq_partial != NULL)
5628 {
5629 dtv.v_type = VAR_PARTIAL;
5630 dtv.vval.v_partial = cq->cq_partial;
5631 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5632 }
5633 if (ch->ch_part[part].ch_partial != NULL)
5634 {
5635 dtv.v_type = VAR_PARTIAL;
5636 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5637 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5638 }
5639 }
5640 if (ch->ch_partial != NULL)
5641 {
5642 dtv.v_type = VAR_PARTIAL;
5643 dtv.vval.v_partial = ch->ch_partial;
5644 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5645 }
5646 if (ch->ch_close_partial != NULL)
5647 {
5648 dtv.v_type = VAR_PARTIAL;
5649 dtv.vval.v_partial = ch->ch_close_partial;
5650 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5651 }
5652 }
5653 }
5654#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005655 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005656}
5657
Bram Moolenaar17a13432016-01-24 14:22:10 +01005658 static char *
5659get_var_special_name(int nr)
5660{
5661 switch (nr)
5662 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005663 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005664 case VVAL_TRUE: return "v:true";
5665 case VVAL_NONE: return "v:none";
5666 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005667 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01005668 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01005669 return "42";
5670}
5671
Bram Moolenaar8c711452005-01-14 21:53:12 +00005672/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005673 * Return a string with the string representation of a variable.
5674 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005675 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005676 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005677 * When both "echo_style" and "dict_val" are FALSE, put quotes around stings as
5678 * "string()", otherwise does not put quotes around strings, as ":echo"
5679 * displays values.
5680 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5681 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005682 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005683 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005684 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005685echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005686 typval_T *tv,
5687 char_u **tofree,
5688 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005689 int copyID,
5690 int echo_style,
5691 int restore_copyID,
5692 int dict_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005693{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005694 static int recurse = 0;
5695 char_u *r = NULL;
5696
Bram Moolenaar33570922005-01-25 22:26:29 +00005697 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005698 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005699 if (!did_echo_string_emsg)
5700 {
5701 /* Only give this message once for a recursive call to avoid
5702 * flooding the user with errors. And stop iterating over lists
5703 * and dicts. */
5704 did_echo_string_emsg = TRUE;
5705 EMSG(_("E724: variable nested too deep for displaying"));
5706 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005707 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005708 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005709 }
5710 ++recurse;
5711
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005712 switch (tv->v_type)
5713 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005714 case VAR_STRING:
5715 if (echo_style && !dict_val)
5716 {
5717 *tofree = NULL;
5718 r = get_tv_string_buf(tv, numbuf);
5719 }
5720 else
5721 {
5722 *tofree = string_quote(tv->vval.v_string, FALSE);
5723 r = *tofree;
5724 }
5725 break;
5726
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005727 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005728 if (echo_style)
5729 {
5730 *tofree = NULL;
5731 r = tv->vval.v_string;
5732 }
5733 else
5734 {
5735 *tofree = string_quote(tv->vval.v_string, TRUE);
5736 r = *tofree;
5737 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005738 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005739
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005740 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005741 {
5742 partial_T *pt = tv->vval.v_partial;
5743 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005744 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005745 garray_T ga;
5746 int i;
5747 char_u *tf;
5748
5749 ga_init2(&ga, 1, 100);
5750 ga_concat(&ga, (char_u *)"function(");
5751 if (fname != NULL)
5752 {
5753 ga_concat(&ga, fname);
5754 vim_free(fname);
5755 }
5756 if (pt != NULL && pt->pt_argc > 0)
5757 {
5758 ga_concat(&ga, (char_u *)", [");
5759 for (i = 0; i < pt->pt_argc; ++i)
5760 {
5761 if (i > 0)
5762 ga_concat(&ga, (char_u *)", ");
5763 ga_concat(&ga,
5764 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5765 vim_free(tf);
5766 }
5767 ga_concat(&ga, (char_u *)"]");
5768 }
5769 if (pt != NULL && pt->pt_dict != NULL)
5770 {
5771 typval_T dtv;
5772
5773 ga_concat(&ga, (char_u *)", ");
5774 dtv.v_type = VAR_DICT;
5775 dtv.vval.v_dict = pt->pt_dict;
5776 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5777 vim_free(tf);
5778 }
5779 ga_concat(&ga, (char_u *)")");
5780
5781 *tofree = ga.ga_data;
5782 r = *tofree;
5783 break;
5784 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005785
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005786 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005787 if (tv->vval.v_list == NULL)
5788 {
5789 *tofree = NULL;
5790 r = NULL;
5791 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005792 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5793 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005794 {
5795 *tofree = NULL;
5796 r = (char_u *)"[...]";
5797 }
5798 else
5799 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005800 int old_copyID = tv->vval.v_list->lv_copyID;
5801
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005802 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005803 *tofree = list2string(tv, copyID, restore_copyID);
5804 if (restore_copyID)
5805 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005806 r = *tofree;
5807 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005808 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005809
Bram Moolenaar8c711452005-01-14 21:53:12 +00005810 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005811 if (tv->vval.v_dict == NULL)
5812 {
5813 *tofree = NULL;
5814 r = NULL;
5815 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005816 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5817 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005818 {
5819 *tofree = NULL;
5820 r = (char_u *)"{...}";
5821 }
5822 else
5823 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005824 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005825 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005826 *tofree = dict2string(tv, copyID, restore_copyID);
5827 if (restore_copyID)
5828 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005829 r = *tofree;
5830 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005831 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005832
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005833 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005834 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005835 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005836 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005837 *tofree = NULL;
5838 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005839 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005840
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005841 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005842#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005843 *tofree = NULL;
5844 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5845 r = numbuf;
5846 break;
5847#endif
5848
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005849 case VAR_SPECIAL:
5850 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005851 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005852 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005853 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005854
Bram Moolenaar8502c702014-06-17 12:51:16 +02005855 if (--recurse == 0)
5856 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005857 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005858}
5859
5860/*
5861 * Return a string with the string representation of a variable.
5862 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5863 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005864 * Does not put quotes around strings, as ":echo" displays values.
5865 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5866 * May return NULL.
5867 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005868 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005869echo_string(
5870 typval_T *tv,
5871 char_u **tofree,
5872 char_u *numbuf,
5873 int copyID)
5874{
5875 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5876}
5877
5878/*
5879 * Return a string with the string representation of a variable.
5880 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5881 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005882 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005883 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005884 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02005885 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005886tv2string(
5887 typval_T *tv,
5888 char_u **tofree,
5889 char_u *numbuf,
5890 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005891{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005892 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005893}
5894
5895/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005896 * Return string "str" in ' quotes, doubling ' characters.
5897 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005898 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005899 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005900 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005901string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005902{
Bram Moolenaar33570922005-01-25 22:26:29 +00005903 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005904 char_u *p, *r, *s;
5905
Bram Moolenaar33570922005-01-25 22:26:29 +00005906 len = (function ? 13 : 3);
5907 if (str != NULL)
5908 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005909 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00005910 for (p = str; *p != NUL; mb_ptr_adv(p))
5911 if (*p == '\'')
5912 ++len;
5913 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005914 s = r = alloc(len);
5915 if (r != NULL)
5916 {
5917 if (function)
5918 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005919 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005920 r += 10;
5921 }
5922 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005923 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005924 if (str != NULL)
5925 for (p = str; *p != NUL; )
5926 {
5927 if (*p == '\'')
5928 *r++ = '\'';
5929 MB_COPY_CHAR(p, r);
5930 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005931 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005932 if (function)
5933 *r++ = ')';
5934 *r++ = NUL;
5935 }
5936 return s;
5937}
5938
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005939#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005940/*
5941 * Convert the string "text" to a floating point number.
5942 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5943 * this always uses a decimal point.
5944 * Returns the length of the text that was consumed.
5945 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005946 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005947string2float(
5948 char_u *text,
5949 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005950{
5951 char *s = (char *)text;
5952 float_T f;
5953
Bram Moolenaar62473612017-01-08 19:25:40 +01005954 /* MS-Windows does not deal with "inf" and "nan" properly. */
5955 if (STRNICMP(text, "inf", 3) == 0)
5956 {
5957 *value = INFINITY;
5958 return 3;
5959 }
5960 if (STRNICMP(text, "-inf", 3) == 0)
5961 {
5962 *value = -INFINITY;
5963 return 4;
5964 }
5965 if (STRNICMP(text, "nan", 3) == 0)
5966 {
5967 *value = NAN;
5968 return 3;
5969 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005970 f = strtod(s, &s);
5971 *value = f;
5972 return (int)((char_u *)s - text);
5973}
5974#endif
5975
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005976/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 * Get the value of an environment variable.
5978 * "arg" is pointing to the '$'. It is advanced to after the name.
5979 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005980 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 */
5982 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005983get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984{
5985 char_u *string = NULL;
5986 int len;
5987 int cc;
5988 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005989 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990
5991 ++*arg;
5992 name = *arg;
5993 len = get_env_len(arg);
5994 if (evaluate)
5995 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005996 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01005997 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005998
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005999 cc = name[len];
6000 name[len] = NUL;
6001 /* first try vim_getenv(), fast for normal environment vars */
6002 string = vim_getenv(name, &mustfree);
6003 if (string != NULL && *string != NUL)
6004 {
6005 if (!mustfree)
6006 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006008 else
6009 {
6010 if (mustfree)
6011 vim_free(string);
6012
6013 /* next try expanding things like $VIM and ${HOME} */
6014 string = expand_env_save(name - 1);
6015 if (string != NULL && *string == '$')
6016 {
6017 vim_free(string);
6018 string = NULL;
6019 }
6020 }
6021 name[len] = cc;
6022
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006023 rettv->v_type = VAR_STRING;
6024 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 }
6026
6027 return OK;
6028}
6029
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006030
6031
6032/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006034 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006036 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006037var2fpos(
6038 typval_T *varp,
6039 int dollar_lnum, /* TRUE when $ is last line */
6040 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006042 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006044 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045
Bram Moolenaara5525202006-03-02 22:52:09 +00006046 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006047 if (varp->v_type == VAR_LIST)
6048 {
6049 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006050 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006051 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006052 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006053
6054 l = varp->vval.v_list;
6055 if (l == NULL)
6056 return NULL;
6057
6058 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006059 pos.lnum = list_find_nr(l, 0L, &error);
6060 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006061 return NULL; /* invalid line number */
6062
6063 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006064 pos.col = list_find_nr(l, 1L, &error);
6065 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006066 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006067 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006068
6069 /* We accept "$" for the column number: last column. */
6070 li = list_find(l, 1L);
6071 if (li != NULL && li->li_tv.v_type == VAR_STRING
6072 && li->li_tv.vval.v_string != NULL
6073 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6074 pos.col = len + 1;
6075
Bram Moolenaara5525202006-03-02 22:52:09 +00006076 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006077 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006078 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006079 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006080
Bram Moolenaara5525202006-03-02 22:52:09 +00006081#ifdef FEAT_VIRTUALEDIT
6082 /* Get the virtual offset. Defaults to zero. */
6083 pos.coladd = list_find_nr(l, 2L, &error);
6084 if (error)
6085 pos.coladd = 0;
6086#endif
6087
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006088 return &pos;
6089 }
6090
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006091 name = get_tv_string_chk(varp);
6092 if (name == NULL)
6093 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006094 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006096 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6097 {
6098 if (VIsual_active)
6099 return &VIsual;
6100 return &curwin->w_cursor;
6101 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006102 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006104 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6106 return NULL;
6107 return pp;
6108 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006109
6110#ifdef FEAT_VIRTUALEDIT
6111 pos.coladd = 0;
6112#endif
6113
Bram Moolenaar477933c2007-07-17 14:32:23 +00006114 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006115 {
6116 pos.col = 0;
6117 if (name[1] == '0') /* "w0": first visible line */
6118 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006119 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006120 pos.lnum = curwin->w_topline;
6121 return &pos;
6122 }
6123 else if (name[1] == '$') /* "w$": last visible line */
6124 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006125 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006126 pos.lnum = curwin->w_botline - 1;
6127 return &pos;
6128 }
6129 }
6130 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006132 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 {
6134 pos.lnum = curbuf->b_ml.ml_line_count;
6135 pos.col = 0;
6136 }
6137 else
6138 {
6139 pos.lnum = curwin->w_cursor.lnum;
6140 pos.col = (colnr_T)STRLEN(ml_get_curline());
6141 }
6142 return &pos;
6143 }
6144 return NULL;
6145}
6146
6147/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006148 * Convert list in "arg" into a position and optional file number.
6149 * When "fnump" is NULL there is no file number, only 3 items.
6150 * Note that the column is passed on as-is, the caller may want to decrement
6151 * it to use 1 for the first column.
6152 * Return FAIL when conversion is not possible, doesn't check the position for
6153 * validity.
6154 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006155 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006156list2fpos(
6157 typval_T *arg,
6158 pos_T *posp,
6159 int *fnump,
6160 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006161{
6162 list_T *l = arg->vval.v_list;
6163 long i = 0;
6164 long n;
6165
Bram Moolenaar493c1782014-05-28 14:34:46 +02006166 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6167 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006168 if (arg->v_type != VAR_LIST
6169 || l == NULL
6170 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006171 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006172 return FAIL;
6173
6174 if (fnump != NULL)
6175 {
6176 n = list_find_nr(l, i++, NULL); /* fnum */
6177 if (n < 0)
6178 return FAIL;
6179 if (n == 0)
6180 n = curbuf->b_fnum; /* current buffer */
6181 *fnump = n;
6182 }
6183
6184 n = list_find_nr(l, i++, NULL); /* lnum */
6185 if (n < 0)
6186 return FAIL;
6187 posp->lnum = n;
6188
6189 n = list_find_nr(l, i++, NULL); /* col */
6190 if (n < 0)
6191 return FAIL;
6192 posp->col = n;
6193
6194#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +02006195 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006196 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006197 posp->coladd = 0;
6198 else
6199 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006200#endif
6201
Bram Moolenaar493c1782014-05-28 14:34:46 +02006202 if (curswantp != NULL)
6203 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6204
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006205 return OK;
6206}
6207
6208/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 * Get the length of an environment variable name.
6210 * Advance "arg" to the first character after the name.
6211 * Return 0 for error.
6212 */
6213 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006214get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215{
6216 char_u *p;
6217 int len;
6218
6219 for (p = *arg; vim_isIDc(*p); ++p)
6220 ;
6221 if (p == *arg) /* no name found */
6222 return 0;
6223
6224 len = (int)(p - *arg);
6225 *arg = p;
6226 return len;
6227}
6228
6229/*
6230 * Get the length of the name of a function or internal variable.
6231 * "arg" is advanced to the first non-white character after the name.
6232 * Return 0 if something is wrong.
6233 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006234 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006235get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236{
6237 char_u *p;
6238 int len;
6239
6240 /* Find the end of the name. */
6241 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006242 {
6243 if (*p == ':')
6244 {
6245 /* "s:" is start of "s:var", but "n:" is not and can be used in
6246 * slice "[n:]". Also "xx:" is not a namespace. */
6247 len = (int)(p - *arg);
6248 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6249 || len > 1)
6250 break;
6251 }
6252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 if (p == *arg) /* no name found */
6254 return 0;
6255
6256 len = (int)(p - *arg);
6257 *arg = skipwhite(p);
6258
6259 return len;
6260}
6261
6262/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006263 * Get the length of the name of a variable or function.
6264 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006266 * Return -1 if curly braces expansion failed.
6267 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 * If the name contains 'magic' {}'s, expand them and return the
6269 * expanded name in an allocated string via 'alias' - caller must free.
6270 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006271 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006272get_name_len(
6273 char_u **arg,
6274 char_u **alias,
6275 int evaluate,
6276 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277{
6278 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 char_u *p;
6280 char_u *expr_start;
6281 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282
6283 *alias = NULL; /* default to no alias */
6284
6285 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6286 && (*arg)[2] == (int)KE_SNR)
6287 {
6288 /* hard coded <SNR>, already translated */
6289 *arg += 3;
6290 return get_id_len(arg) + 3;
6291 }
6292 len = eval_fname_script(*arg);
6293 if (len > 0)
6294 {
6295 /* literal "<SID>", "s:" or "<SNR>" */
6296 *arg += len;
6297 }
6298
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006300 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006302 p = find_name_end(*arg, &expr_start, &expr_end,
6303 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 if (expr_start != NULL)
6305 {
6306 char_u *temp_string;
6307
6308 if (!evaluate)
6309 {
6310 len += (int)(p - *arg);
6311 *arg = skipwhite(p);
6312 return len;
6313 }
6314
6315 /*
6316 * Include any <SID> etc in the expanded string:
6317 * Thus the -len here.
6318 */
6319 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6320 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006321 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 *alias = temp_string;
6323 *arg = skipwhite(p);
6324 return (int)STRLEN(temp_string);
6325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326
6327 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006328 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 EMSG2(_(e_invexpr2), *arg);
6330
6331 return len;
6332}
6333
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006334/*
6335 * Find the end of a variable or function name, taking care of magic braces.
6336 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6337 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006338 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006339 * Return a pointer to just after the name. Equal to "arg" if there is no
6340 * valid name.
6341 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006342 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006343find_name_end(
6344 char_u *arg,
6345 char_u **expr_start,
6346 char_u **expr_end,
6347 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006349 int mb_nest = 0;
6350 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006352 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006354 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006356 *expr_start = NULL;
6357 *expr_end = NULL;
6358 }
6359
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006360 /* Quick check for valid starting character. */
6361 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6362 return arg;
6363
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006364 for (p = arg; *p != NUL
6365 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006366 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006367 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006368 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +00006369 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006370 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006371 if (*p == '\'')
6372 {
6373 /* skip over 'string' to avoid counting [ and ] inside it. */
6374 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
6375 ;
6376 if (*p == NUL)
6377 break;
6378 }
6379 else if (*p == '"')
6380 {
6381 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
6382 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
6383 if (*p == '\\' && p[1] != NUL)
6384 ++p;
6385 if (*p == NUL)
6386 break;
6387 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006388 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6389 {
6390 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006391 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006392 len = (int)(p - arg);
6393 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006394 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006395 break;
6396 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006397
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006398 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006400 if (*p == '[')
6401 ++br_nest;
6402 else if (*p == ']')
6403 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006405
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006406 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006408 if (*p == '{')
6409 {
6410 mb_nest++;
6411 if (expr_start != NULL && *expr_start == NULL)
6412 *expr_start = p;
6413 }
6414 else if (*p == '}')
6415 {
6416 mb_nest--;
6417 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6418 *expr_end = p;
6419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 }
6422
6423 return p;
6424}
6425
6426/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006427 * Expands out the 'magic' {}'s in a variable/function name.
6428 * Note that this can call itself recursively, to deal with
6429 * constructs like foo{bar}{baz}{bam}
6430 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6431 * "in_start" ^
6432 * "expr_start" ^
6433 * "expr_end" ^
6434 * "in_end" ^
6435 *
6436 * Returns a new allocated string, which the caller must free.
6437 * Returns NULL for failure.
6438 */
6439 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006440make_expanded_name(
6441 char_u *in_start,
6442 char_u *expr_start,
6443 char_u *expr_end,
6444 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006445{
6446 char_u c1;
6447 char_u *retval = NULL;
6448 char_u *temp_result;
6449 char_u *nextcmd = NULL;
6450
6451 if (expr_end == NULL || in_end == NULL)
6452 return NULL;
6453 *expr_start = NUL;
6454 *expr_end = NUL;
6455 c1 = *in_end;
6456 *in_end = NUL;
6457
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006458 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006459 if (temp_result != NULL && nextcmd == NULL)
6460 {
6461 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6462 + (in_end - expr_end) + 1));
6463 if (retval != NULL)
6464 {
6465 STRCPY(retval, in_start);
6466 STRCAT(retval, temp_result);
6467 STRCAT(retval, expr_end + 1);
6468 }
6469 }
6470 vim_free(temp_result);
6471
6472 *in_end = c1; /* put char back for error messages */
6473 *expr_start = '{';
6474 *expr_end = '}';
6475
6476 if (retval != NULL)
6477 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006478 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006479 if (expr_start != NULL)
6480 {
6481 /* Further expansion! */
6482 temp_result = make_expanded_name(retval, expr_start,
6483 expr_end, temp_result);
6484 vim_free(retval);
6485 retval = temp_result;
6486 }
6487 }
6488
6489 return retval;
6490}
6491
6492/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006494 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006496 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006497eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006499 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6500}
6501
6502/*
6503 * Return TRUE if character "c" can be used as the first character in a
6504 * variable or function name (excluding '{' and '}').
6505 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006506 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006507eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006508{
6509 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510}
6511
6512/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 * Set number v: variable to "val".
6514 */
6515 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006516set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006518 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519}
6520
6521/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006522 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006524 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006525get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006527 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528}
6529
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006530/*
6531 * Get string v: variable value. Uses a static buffer, can only be used once.
6532 */
6533 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006534get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006535{
6536 return get_tv_string(&vimvars[idx].vv_tv);
6537}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006538
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006540 * Get List v: variable value. Caller must take care of reference count when
6541 * needed.
6542 */
6543 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006544get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006545{
6546 return vimvars[idx].vv_list;
6547}
6548
6549/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006550 * Set v:char to character "c".
6551 */
6552 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006553set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006554{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006555 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006556
6557#ifdef FEAT_MBYTE
6558 if (has_mbyte)
6559 buf[(*mb_char2bytes)(c, buf)] = NUL;
6560 else
6561#endif
6562 {
6563 buf[0] = c;
6564 buf[1] = NUL;
6565 }
6566 set_vim_var_string(VV_CHAR, buf, -1);
6567}
6568
6569/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006570 * Set v:count to "count" and v:count1 to "count1".
6571 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 */
6573 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006574set_vcount(
6575 long count,
6576 long count1,
6577 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006579 if (set_prevcount)
6580 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006581 vimvars[VV_COUNT].vv_nr = count;
6582 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583}
6584
6585/*
6586 * Set string v: variable to a copy of "val".
6587 */
6588 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006589set_vim_var_string(
6590 int idx,
6591 char_u *val,
6592 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593{
Bram Moolenaara542c682016-01-31 16:28:04 +01006594 clear_tv(&vimvars[idx].vv_di.di_tv);
6595 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006597 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006599 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006601 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602}
6603
6604/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006605 * Set List v: variable to "val".
6606 */
6607 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006608set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006609{
Bram Moolenaara542c682016-01-31 16:28:04 +01006610 clear_tv(&vimvars[idx].vv_di.di_tv);
6611 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006612 vimvars[idx].vv_list = val;
6613 if (val != NULL)
6614 ++val->lv_refcount;
6615}
6616
6617/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006618 * Set Dictionary v: variable to "val".
6619 */
6620 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006621set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006622{
6623 int todo;
6624 hashitem_T *hi;
6625
Bram Moolenaara542c682016-01-31 16:28:04 +01006626 clear_tv(&vimvars[idx].vv_di.di_tv);
6627 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006628 vimvars[idx].vv_dict = val;
6629 if (val != NULL)
6630 {
6631 ++val->dv_refcount;
6632
6633 /* Set readonly */
6634 todo = (int)val->dv_hashtab.ht_used;
6635 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
6636 {
6637 if (HASHITEM_EMPTY(hi))
6638 continue;
6639 --todo;
6640 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
6641 }
6642 }
6643}
6644
6645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 * Set v:register if needed.
6647 */
6648 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006649set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650{
6651 char_u regname;
6652
6653 if (c == 0 || c == ' ')
6654 regname = '"';
6655 else
6656 regname = c;
6657 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006658 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 set_vim_var_string(VV_REG, &regname, 1);
6660}
6661
6662/*
6663 * Get or set v:exception. If "oldval" == NULL, return the current value.
6664 * Otherwise, restore the value to "oldval" and return NULL.
6665 * Must always be called in pairs to save and restore v:exception! Does not
6666 * take care of memory allocations.
6667 */
6668 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006669v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670{
6671 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006672 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673
Bram Moolenaare9a41262005-01-15 22:18:47 +00006674 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 return NULL;
6676}
6677
6678/*
6679 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
6680 * Otherwise, restore the value to "oldval" and return NULL.
6681 * Must always be called in pairs to save and restore v:throwpoint! Does not
6682 * take care of memory allocations.
6683 */
6684 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006685v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686{
6687 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006688 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689
Bram Moolenaare9a41262005-01-15 22:18:47 +00006690 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 return NULL;
6692}
6693
6694#if defined(FEAT_AUTOCMD) || defined(PROTO)
6695/*
6696 * Set v:cmdarg.
6697 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6698 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6699 * Must always be called in pairs!
6700 */
6701 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006702set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703{
6704 char_u *oldval;
6705 char_u *newval;
6706 unsigned len;
6707
Bram Moolenaare9a41262005-01-15 22:18:47 +00006708 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006709 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006711 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006712 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006713 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 }
6715
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006716 if (eap->force_bin == FORCE_BIN)
6717 len = 6;
6718 else if (eap->force_bin == FORCE_NOBIN)
6719 len = 8;
6720 else
6721 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006722
6723 if (eap->read_edit)
6724 len += 7;
6725
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006726 if (eap->force_ff != 0)
6727 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
6728# ifdef FEAT_MBYTE
6729 if (eap->force_enc != 0)
6730 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006731 if (eap->bad_char != 0)
6732 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006733# endif
6734
6735 newval = alloc(len + 1);
6736 if (newval == NULL)
6737 return NULL;
6738
6739 if (eap->force_bin == FORCE_BIN)
6740 sprintf((char *)newval, " ++bin");
6741 else if (eap->force_bin == FORCE_NOBIN)
6742 sprintf((char *)newval, " ++nobin");
6743 else
6744 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006745
6746 if (eap->read_edit)
6747 STRCAT(newval, " ++edit");
6748
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006749 if (eap->force_ff != 0)
6750 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
6751 eap->cmd + eap->force_ff);
6752# ifdef FEAT_MBYTE
6753 if (eap->force_enc != 0)
6754 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6755 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006756 if (eap->bad_char == BAD_KEEP)
6757 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6758 else if (eap->bad_char == BAD_DROP)
6759 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6760 else if (eap->bad_char != 0)
6761 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006762# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00006763 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006764 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765}
6766#endif
6767
6768/*
6769 * Get the value of internal variable "name".
6770 * Return OK or FAIL.
6771 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006772 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006773get_var_tv(
6774 char_u *name,
6775 int len, /* length of "name" */
6776 typval_T *rettv, /* NULL when only checking existence */
6777 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
6778 int verbose, /* may give error message */
6779 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780{
6781 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006782 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006783 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785
6786 /* truncate the name, so that we can use strcmp() */
6787 cc = name[len];
6788 name[len] = NUL;
6789
6790 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 * Check for user-defined variables.
6792 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01006793 v = find_var(name, NULL, no_autoload);
6794 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006796 tv = &v->di_tv;
6797 if (dip != NULL)
6798 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 }
6800
Bram Moolenaare9a41262005-01-15 22:18:47 +00006801 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006803 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006804 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 ret = FAIL;
6806 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006807 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006808 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809
6810 name[len] = cc;
6811
6812 return ret;
6813}
6814
6815/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006816 * Check if variable "name[len]" is a local variable or an argument.
6817 * If so, "*eval_lavars_used" is set to TRUE.
6818 */
6819 static void
6820check_vars(char_u *name, int len)
6821{
6822 int cc;
6823 char_u *varname;
6824 hashtab_T *ht;
6825
6826 if (eval_lavars_used == NULL)
6827 return;
6828
6829 /* truncate the name, so that we can use strcmp() */
6830 cc = name[len];
6831 name[len] = NUL;
6832
6833 ht = find_var_ht(name, &varname);
6834 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
6835 {
6836 if (find_var(name, NULL, TRUE) != NULL)
6837 *eval_lavars_used = TRUE;
6838 }
6839
6840 name[len] = cc;
6841}
6842
6843/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006844 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
6845 * Also handle function call with Funcref variable: func(expr)
6846 * Can all be combined: dict.func(expr)[idx]['func'](expr)
6847 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006848 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006849handle_subscript(
6850 char_u **arg,
6851 typval_T *rettv,
6852 int evaluate, /* do more than finding the end */
6853 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006854{
6855 int ret = OK;
6856 dict_T *selfdict = NULL;
6857 char_u *s;
6858 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006859 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006860
6861 while (ret == OK
6862 && (**arg == '['
6863 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006864 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6865 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006866 && !vim_iswhite(*(*arg - 1)))
6867 {
6868 if (**arg == '(')
6869 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006870 partial_T *pt = NULL;
6871
Bram Moolenaard9fba312005-06-26 22:34:35 +00006872 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006873 if (evaluate)
6874 {
6875 functv = *rettv;
6876 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006877
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006878 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006879 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006880 {
6881 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006882 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006883 }
6884 else
6885 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006886 }
6887 else
6888 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006889 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00006890 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006891 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006892
6893 /* Clear the funcref afterwards, so that deleting it while
6894 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006895 if (evaluate)
6896 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006897
6898 /* Stop the expression evaluation when immediately aborting on
6899 * error, or when an interrupt occurred or an exception was thrown
6900 * but not caught. */
6901 if (aborting())
6902 {
6903 if (ret == OK)
6904 clear_tv(rettv);
6905 ret = FAIL;
6906 }
6907 dict_unref(selfdict);
6908 selfdict = NULL;
6909 }
6910 else /* **arg == '[' || **arg == '.' */
6911 {
6912 dict_unref(selfdict);
6913 if (rettv->v_type == VAR_DICT)
6914 {
6915 selfdict = rettv->vval.v_dict;
6916 if (selfdict != NULL)
6917 ++selfdict->dv_refcount;
6918 }
6919 else
6920 selfdict = NULL;
6921 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
6922 {
6923 clear_tv(rettv);
6924 ret = FAIL;
6925 }
6926 }
6927 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006928
Bram Moolenaar1d429612016-05-24 15:44:17 +02006929 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
6930 * Don't do this when "Func" is already a partial that was bound
6931 * explicitly (pt_auto is FALSE). */
6932 if (selfdict != NULL
6933 && (rettv->v_type == VAR_FUNC
6934 || (rettv->v_type == VAR_PARTIAL
6935 && (rettv->vval.v_partial->pt_auto
6936 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006937 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006938
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006939 dict_unref(selfdict);
6940 return ret;
6941}
6942
6943/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006944 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006945 * value).
6946 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01006947 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006948alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006949{
Bram Moolenaar33570922005-01-25 22:26:29 +00006950 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006951}
6952
6953/*
6954 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 * The string "s" must have been allocated, it is consumed.
6956 * Return NULL for out of memory, the variable otherwise.
6957 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006958 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006959alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960{
Bram Moolenaar33570922005-01-25 22:26:29 +00006961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006963 rettv = alloc_tv();
6964 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006966 rettv->v_type = VAR_STRING;
6967 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 }
6969 else
6970 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006971 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972}
6973
6974/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006975 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006977 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006978free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979{
6980 if (varp != NULL)
6981 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006982 switch (varp->v_type)
6983 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006984 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006985 func_unref(varp->vval.v_string);
6986 /*FALLTHROUGH*/
6987 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006988 vim_free(varp->vval.v_string);
6989 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006990 case VAR_PARTIAL:
6991 partial_unref(varp->vval.v_partial);
6992 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006993 case VAR_LIST:
6994 list_unref(varp->vval.v_list);
6995 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006996 case VAR_DICT:
6997 dict_unref(varp->vval.v_dict);
6998 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006999 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007000#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007001 job_unref(varp->vval.v_job);
7002 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007003#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007004 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007005#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007006 channel_unref(varp->vval.v_channel);
7007 break;
7008#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007009 case VAR_NUMBER:
7010 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007011 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007012 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007013 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 vim_free(varp);
7016 }
7017}
7018
7019/*
7020 * Free the memory for a variable value and set the value to NULL or 0.
7021 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007022 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007023clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024{
7025 if (varp != NULL)
7026 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007027 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007029 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007030 func_unref(varp->vval.v_string);
7031 /*FALLTHROUGH*/
7032 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007033 vim_free(varp->vval.v_string);
7034 varp->vval.v_string = NULL;
7035 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007036 case VAR_PARTIAL:
7037 partial_unref(varp->vval.v_partial);
7038 varp->vval.v_partial = NULL;
7039 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007040 case VAR_LIST:
7041 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007042 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007043 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007044 case VAR_DICT:
7045 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007046 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007047 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007048 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007049 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007050 varp->vval.v_number = 0;
7051 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007052 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007053#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007054 varp->vval.v_float = 0.0;
7055 break;
7056#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007057 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007058#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007059 job_unref(varp->vval.v_job);
7060 varp->vval.v_job = NULL;
7061#endif
7062 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007063 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007064#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007065 channel_unref(varp->vval.v_channel);
7066 varp->vval.v_channel = NULL;
7067#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007068 case VAR_UNKNOWN:
7069 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007071 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 }
7073}
7074
7075/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007076 * Set the value of a variable to NULL without freeing items.
7077 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007078 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007079init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007080{
7081 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007082 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007083}
7084
7085/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 * Get the number value of a variable.
7087 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007088 * For incompatible types, return 0.
7089 * get_tv_number_chk() is similar to get_tv_number(), but informs the
7090 * caller of incompatible types: it sets *denote to TRUE if "denote"
7091 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007093 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007094get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007096 int error = FALSE;
7097
7098 return get_tv_number_chk(varp, &error); /* return 0L on error */
7099}
7100
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007101 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007102get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007103{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007104 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007106 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007108 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007109 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007110 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007111#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +00007112 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007113 break;
7114#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007115 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007116 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007117 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007118 break;
7119 case VAR_STRING:
7120 if (varp->vval.v_string != NULL)
7121 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01007122 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007123 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007124 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007125 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007126 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007127 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007128 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007129 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007130 case VAR_SPECIAL:
7131 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7132 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007133 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007134#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007135 EMSG(_("E910: Using a Job as a Number"));
7136 break;
7137#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007138 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007139#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007140 EMSG(_("E913: Using a Channel as a Number"));
7141 break;
7142#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007143 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007144 internal_error("get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007145 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007147 if (denote == NULL) /* useful for values that must be unsigned */
7148 n = -1;
7149 else
7150 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007151 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152}
7153
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007154#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007155 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007156get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007157{
7158 switch (varp->v_type)
7159 {
7160 case VAR_NUMBER:
7161 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007162 case VAR_FLOAT:
7163 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007164 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007165 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007166 EMSG(_("E891: Using a Funcref as a Float"));
7167 break;
7168 case VAR_STRING:
7169 EMSG(_("E892: Using a String as a Float"));
7170 break;
7171 case VAR_LIST:
7172 EMSG(_("E893: Using a List as a Float"));
7173 break;
7174 case VAR_DICT:
7175 EMSG(_("E894: Using a Dictionary as a Float"));
7176 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007177 case VAR_SPECIAL:
7178 EMSG(_("E907: Using a special value as a Float"));
7179 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007180 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007181# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007182 EMSG(_("E911: Using a Job as a Float"));
7183 break;
7184# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007185 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007186# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007187 EMSG(_("E914: Using a Channel as a Float"));
7188 break;
7189# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007190 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007191 internal_error("get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007192 break;
7193 }
7194 return 0;
7195}
7196#endif
7197
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 * Get the string value of a variable.
7200 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +00007201 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7202 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 * If the String variable has never been set, return an empty string.
7204 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007205 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
7206 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007208 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007209get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210{
7211 static char_u mybuf[NUMBUFLEN];
7212
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007213 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214}
7215
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007216 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007217get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007219 char_u *res = get_tv_string_buf_chk(varp, buf);
7220
7221 return res != NULL ? res : (char_u *)"";
7222}
7223
Bram Moolenaar7d647822014-04-05 21:28:56 +02007224/*
7225 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7226 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007227 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007228get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007229{
7230 static char_u mybuf[NUMBUFLEN];
7231
7232 return get_tv_string_buf_chk(varp, mybuf);
7233}
7234
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007235 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007236get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007237{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007238 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007240 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007241 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
7242 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007243 return buf;
7244 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007245 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007246 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007247 break;
7248 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007249 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007250 break;
7251 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007252 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007253 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007254 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007255#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +02007256 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007257 break;
7258#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007259 case VAR_STRING:
7260 if (varp->vval.v_string != NULL)
7261 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007262 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007263 case VAR_SPECIAL:
7264 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7265 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007266 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007267#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007268 {
7269 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007270 char *status;
7271
7272 if (job == NULL)
7273 return (char_u *)"no process";
7274 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007275 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007276 : "run";
7277# ifdef UNIX
7278 vim_snprintf((char *)buf, NUMBUFLEN,
7279 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007280# elif defined(WIN32)
7281 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007282 "process %ld %s",
7283 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007284 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007285# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007286 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007287 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7288# endif
7289 return buf;
7290 }
7291#endif
7292 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007293 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007294#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007295 {
7296 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007297 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007298
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007299 if (channel == NULL)
7300 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7301 else
7302 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007303 "channel %d %s", channel->ch_id, status);
7304 return buf;
7305 }
7306#endif
7307 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007308 case VAR_UNKNOWN:
7309 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007310 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007312 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313}
7314
7315/*
7316 * Find variable "name" in the list of variables.
7317 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007318 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007319 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007320 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007322 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007323find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007326 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007327 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328
Bram Moolenaara7043832005-01-21 11:56:39 +00007329 ht = find_var_ht(name, &varname);
7330 if (htp != NULL)
7331 *htp = ht;
7332 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007334 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7335 if (ret != NULL)
7336 return ret;
7337
7338 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007339 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340}
7341
7342/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007343 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007344 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007346 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007347find_var_in_ht(
7348 hashtab_T *ht,
7349 int htname,
7350 char_u *varname,
7351 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007352{
Bram Moolenaar33570922005-01-25 22:26:29 +00007353 hashitem_T *hi;
7354
7355 if (*varname == NUL)
7356 {
7357 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007358 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007359 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007360 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007361 case 'g': return &globvars_var;
7362 case 'v': return &vimvars_var;
7363 case 'b': return &curbuf->b_bufvar;
7364 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007365#ifdef FEAT_WINDOWS
7366 case 't': return &curtab->tp_winvar;
7367#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007368 case 'l': return get_funccal_local_var();
7369 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007370 }
7371 return NULL;
7372 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007373
7374 hi = hash_find(ht, varname);
7375 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007376 {
7377 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007378 * worked find the variable again. Don't auto-load a script if it was
7379 * loaded already, otherwise it would be loaded every time when
7380 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007381 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007382 {
7383 /* Note: script_autoload() may make "hi" invalid. It must either
7384 * be obtained again or not used. */
7385 if (!script_autoload(varname, FALSE) || aborting())
7386 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007387 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007388 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007389 if (HASHITEM_EMPTY(hi))
7390 return NULL;
7391 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007392 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007393}
7394
7395/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007396 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007397 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007398 * Set "varname" to the start of name without ':'.
7399 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007400 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007401find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007403 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007404 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007405
Bram Moolenaar73627d02015-08-11 15:46:09 +02007406 if (name[0] == NUL)
7407 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 if (name[1] != ':')
7409 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007410 /* The name must not start with a colon or #. */
7411 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 return NULL;
7413 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007414
7415 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007416 hi = hash_find(&compat_hashtab, name);
7417 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +00007418 return &compat_hashtab;
7419
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007420 ht = get_funccal_local_ht();
7421 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007422 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007423 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 }
7425 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007426 if (*name == 'g') /* global variable */
7427 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007428 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7429 */
7430 if (vim_strchr(name + 2, ':') != NULL
7431 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007432 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007434 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007436 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007437#ifdef FEAT_WINDOWS
7438 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007439 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007440#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007441 if (*name == 'v') /* v: variable */
7442 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007443 if (*name == 'a') /* a: function argument */
7444 return get_funccal_args_ht();
7445 if (*name == 'l') /* l: local function variable */
7446 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 if (*name == 's' /* script variable */
7448 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
7449 return &SCRIPT_VARS(current_SID);
7450 return NULL;
7451}
7452
7453/*
7454 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +02007455 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 * Returns NULL when it doesn't exist.
7457 */
7458 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007459get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460{
Bram Moolenaar33570922005-01-25 22:26:29 +00007461 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007463 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 if (v == NULL)
7465 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007466 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467}
7468
7469/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007470 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 * sourcing this script and when executing functions defined in the script.
7472 */
7473 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007474new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475{
Bram Moolenaara7043832005-01-21 11:56:39 +00007476 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007477 hashtab_T *ht;
7478 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007479
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7481 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007482 /* Re-allocating ga_data means that an ht_array pointing to
7483 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007484 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007485 for (i = 1; i <= ga_scripts.ga_len; ++i)
7486 {
7487 ht = &SCRIPT_VARS(i);
7488 if (ht->ht_mask == HT_INIT_SIZE - 1)
7489 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007490 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007491 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007492 }
7493
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 while (ga_scripts.ga_len < id)
7495 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007496 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007497 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007498 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 }
7501 }
7502}
7503
7504/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007505 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7506 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 */
7508 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007509init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510{
Bram Moolenaar33570922005-01-25 22:26:29 +00007511 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007512 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007513 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007514 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007515 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007516 dict_var->di_tv.vval.v_dict = dict;
7517 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007518 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007519 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7520 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521}
7522
7523/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007524 * Unreference a dictionary initialized by init_var_dict().
7525 */
7526 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007527unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007528{
7529 /* Now the dict needs to be freed if no one else is using it, go back to
7530 * normal reference counting. */
7531 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7532 dict_unref(dict);
7533}
7534
7535/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007537 * Frees all allocated variables and the value they contain.
7538 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 */
7540 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007541vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007542{
7543 vars_clear_ext(ht, TRUE);
7544}
7545
7546/*
7547 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7548 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007549 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007550vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551{
Bram Moolenaara7043832005-01-21 11:56:39 +00007552 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007553 hashitem_T *hi;
7554 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555
Bram Moolenaar33570922005-01-25 22:26:29 +00007556 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007557 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007558 for (hi = ht->ht_array; todo > 0; ++hi)
7559 {
7560 if (!HASHITEM_EMPTY(hi))
7561 {
7562 --todo;
7563
Bram Moolenaar33570922005-01-25 22:26:29 +00007564 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007565 * ht_array might change then. hash_clear() takes care of it
7566 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007567 v = HI2DI(hi);
7568 if (free_val)
7569 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007570 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007571 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007572 }
7573 }
7574 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007575 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576}
7577
Bram Moolenaara7043832005-01-21 11:56:39 +00007578/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007579 * Delete a variable from hashtab "ht" at item "hi".
7580 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007581 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007583delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584{
Bram Moolenaar33570922005-01-25 22:26:29 +00007585 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007586
7587 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007588 clear_tv(&di->di_tv);
7589 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590}
7591
7592/*
7593 * List the value of one internal variable.
7594 */
7595 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007596list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007598 char_u *tofree;
7599 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007600 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007601
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007602 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007603 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007604 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007605 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606}
7607
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007609list_one_var_a(
7610 char_u *prefix,
7611 char_u *name,
7612 int type,
7613 char_u *string,
7614 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615{
Bram Moolenaar31859182007-08-14 20:41:13 +00007616 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7617 msg_start();
7618 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 if (name != NULL) /* "a:" vars don't have a name stored */
7620 msg_puts(name);
7621 msg_putchar(' ');
7622 msg_advance(22);
7623 if (type == VAR_NUMBER)
7624 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007625 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007626 msg_putchar('*');
7627 else if (type == VAR_LIST)
7628 {
7629 msg_putchar('[');
7630 if (*string == '[')
7631 ++string;
7632 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007633 else if (type == VAR_DICT)
7634 {
7635 msg_putchar('{');
7636 if (*string == '{')
7637 ++string;
7638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 else
7640 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007641
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007643
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007644 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007645 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007646 if (*first)
7647 {
7648 msg_clr_eos();
7649 *first = FALSE;
7650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651}
7652
7653/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007654 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 * If the variable already exists, the value is updated.
7656 * Otherwise the variable is created.
7657 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007658 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007659set_var(
7660 char_u *name,
7661 typval_T *tv,
7662 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663{
Bram Moolenaar33570922005-01-25 22:26:29 +00007664 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007666 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007668 ht = find_var_ht(name, &varname);
7669 if (ht == NULL || *varname == NUL)
7670 {
7671 EMSG2(_(e_illvar), name);
7672 return;
7673 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007674 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007675
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007676 /* Search in parent scope which is possible to reference from lambda */
7677 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007678 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007679
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007680 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7681 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007682 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007683
Bram Moolenaar33570922005-01-25 22:26:29 +00007684 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007686 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007687 if (var_check_ro(v->di_flags, name, FALSE)
7688 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007689 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007690
7691 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007692 * Handle setting internal v: variables separately where needed to
7693 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007694 */
7695 if (ht == &vimvarht)
7696 {
7697 if (v->di_tv.v_type == VAR_STRING)
7698 {
7699 vim_free(v->di_tv.vval.v_string);
7700 if (copy || tv->v_type != VAR_STRING)
7701 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
7702 else
7703 {
7704 /* Take over the string to avoid an extra alloc/free. */
7705 v->di_tv.vval.v_string = tv->vval.v_string;
7706 tv->vval.v_string = NULL;
7707 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007708 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007709 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007710 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007711 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007712 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007713 if (STRCMP(varname, "searchforward") == 0)
7714 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007715#ifdef FEAT_SEARCH_EXTRA
7716 else if (STRCMP(varname, "hlsearch") == 0)
7717 {
7718 no_hlsearch = !v->di_tv.vval.v_number;
7719 redraw_all_later(SOME_VALID);
7720 }
7721#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007722 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007723 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007724 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar95f09602016-11-10 20:01:45 +01007725 internal_error("set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +00007726 }
7727
7728 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 }
7730 else /* add a new variable */
7731 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007732 /* Can't add "v:" variable. */
7733 if (ht == &vimvarht)
7734 {
7735 EMSG2(_(e_illvar), name);
7736 return;
7737 }
7738
Bram Moolenaar92124a32005-06-17 22:03:40 +00007739 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007740 if (!valid_varname(varname))
7741 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00007742
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007743 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7744 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00007745 if (v == NULL)
7746 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007747 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00007748 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007750 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 return;
7752 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007753 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007755
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007756 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00007757 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007758 else
7759 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007760 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007761 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007762 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764}
7765
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007766/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007767 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00007768 * Also give an error message.
7769 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007770 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007771var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00007772{
7773 if (flags & DI_FLAGS_RO)
7774 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007775 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007776 return TRUE;
7777 }
7778 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
7779 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007780 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007781 return TRUE;
7782 }
7783 return FALSE;
7784}
7785
7786/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007787 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
7788 * Also give an error message.
7789 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007790 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007791var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007792{
7793 if (flags & DI_FLAGS_FIX)
7794 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007795 EMSG2(_("E795: Cannot delete variable %s"),
7796 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007797 return TRUE;
7798 }
7799 return FALSE;
7800}
7801
7802/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007803 * Check if a funcref is assigned to a valid variable name.
7804 * Return TRUE and give an error if not.
7805 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007806 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007807var_check_func_name(
7808 char_u *name, /* points to start of variable name */
7809 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007810{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02007811 /* Allow for w: b: s: and t:. */
7812 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007813 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
7814 ? name[2] : name[0]))
7815 {
7816 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
7817 name);
7818 return TRUE;
7819 }
7820 /* Don't allow hiding a function. When "v" is not NULL we might be
7821 * assigning another function to the same var, the type is checked
7822 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02007823 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007824 {
7825 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
7826 name);
7827 return TRUE;
7828 }
7829 return FALSE;
7830}
7831
7832/*
7833 * Check if a variable name is valid.
7834 * Return FALSE and give an error if not.
7835 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007836 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007837valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007838{
7839 char_u *p;
7840
7841 for (p = varname; *p != NUL; ++p)
7842 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
7843 && *p != AUTOLOAD_CHAR)
7844 {
7845 EMSG2(_(e_illvar), varname);
7846 return FALSE;
7847 }
7848 return TRUE;
7849}
7850
7851/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007852 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02007853 * Also give an error message, using "name" or _("name") when use_gettext is
7854 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007855 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007856 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007857tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007858{
7859 if (lock & VAR_LOCKED)
7860 {
7861 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007862 name == NULL ? (char_u *)_("Unknown")
7863 : use_gettext ? (char_u *)_(name)
7864 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007865 return TRUE;
7866 }
7867 if (lock & VAR_FIXED)
7868 {
7869 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007870 name == NULL ? (char_u *)_("Unknown")
7871 : use_gettext ? (char_u *)_(name)
7872 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007873 return TRUE;
7874 }
7875 return FALSE;
7876}
7877
7878/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007879 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007880 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007881 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007882 * It is OK for "from" and "to" to point to the same item. This is used to
7883 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007884 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007885 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007886copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007888 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007889 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007890 switch (from->v_type)
7891 {
7892 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007893 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007894 to->vval.v_number = from->vval.v_number;
7895 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007896 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007897#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007898 to->vval.v_float = from->vval.v_float;
7899 break;
7900#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007901 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007902#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007903 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007904 if (to->vval.v_job != NULL)
7905 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007906 break;
7907#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007908 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007909#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007910 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007911 if (to->vval.v_channel != NULL)
7912 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01007913 break;
7914#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007915 case VAR_STRING:
7916 case VAR_FUNC:
7917 if (from->vval.v_string == NULL)
7918 to->vval.v_string = NULL;
7919 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007920 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007921 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007922 if (from->v_type == VAR_FUNC)
7923 func_ref(to->vval.v_string);
7924 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007925 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007926 case VAR_PARTIAL:
7927 if (from->vval.v_partial == NULL)
7928 to->vval.v_partial = NULL;
7929 else
7930 {
7931 to->vval.v_partial = from->vval.v_partial;
7932 ++to->vval.v_partial->pt_refcount;
7933 }
7934 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007935 case VAR_LIST:
7936 if (from->vval.v_list == NULL)
7937 to->vval.v_list = NULL;
7938 else
7939 {
7940 to->vval.v_list = from->vval.v_list;
7941 ++to->vval.v_list->lv_refcount;
7942 }
7943 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007944 case VAR_DICT:
7945 if (from->vval.v_dict == NULL)
7946 to->vval.v_dict = NULL;
7947 else
7948 {
7949 to->vval.v_dict = from->vval.v_dict;
7950 ++to->vval.v_dict->dv_refcount;
7951 }
7952 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007953 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007954 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007955 break;
7956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957}
7958
7959/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007960 * Make a copy of an item.
7961 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007962 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7963 * reference to an already copied list/dict can be used.
7964 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007965 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007966 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007967item_copy(
7968 typval_T *from,
7969 typval_T *to,
7970 int deep,
7971 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007972{
7973 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007974 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007975
Bram Moolenaar33570922005-01-25 22:26:29 +00007976 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007977 {
7978 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007979 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007980 }
7981 ++recurse;
7982
7983 switch (from->v_type)
7984 {
7985 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007986 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007987 case VAR_STRING:
7988 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007989 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007990 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007991 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007992 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007993 copy_tv(from, to);
7994 break;
7995 case VAR_LIST:
7996 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007997 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007998 if (from->vval.v_list == NULL)
7999 to->vval.v_list = NULL;
8000 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8001 {
8002 /* use the copy made earlier */
8003 to->vval.v_list = from->vval.v_list->lv_copylist;
8004 ++to->vval.v_list->lv_refcount;
8005 }
8006 else
8007 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8008 if (to->vval.v_list == NULL)
8009 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008010 break;
8011 case VAR_DICT:
8012 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008013 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008014 if (from->vval.v_dict == NULL)
8015 to->vval.v_dict = NULL;
8016 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8017 {
8018 /* use the copy made earlier */
8019 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8020 ++to->vval.v_dict->dv_refcount;
8021 }
8022 else
8023 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8024 if (to->vval.v_dict == NULL)
8025 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008026 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008027 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008028 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008029 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008030 }
8031 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008032 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008033}
8034
8035/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008036 * This function is used by f_input() and f_inputdialog() functions. The third
8037 * argument to f_input() specifies the type of completion to use at the
8038 * prompt. The third argument to f_inputdialog() specifies the value to return
8039 * when the user cancels the prompt.
8040 */
8041 void
8042get_user_input(
8043 typval_T *argvars,
8044 typval_T *rettv,
8045 int inputdialog,
8046 int secret)
8047{
8048 char_u *prompt = get_tv_string_chk(&argvars[0]);
8049 char_u *p = NULL;
8050 int c;
8051 char_u buf[NUMBUFLEN];
8052 int cmd_silent_save = cmd_silent;
8053 char_u *defstr = (char_u *)"";
8054 int xp_type = EXPAND_NOTHING;
8055 char_u *xp_arg = NULL;
8056
8057 rettv->v_type = VAR_STRING;
8058 rettv->vval.v_string = NULL;
8059
8060#ifdef NO_CONSOLE_INPUT
8061 /* While starting up, there is no place to enter text. */
8062 if (no_console_input())
8063 return;
8064#endif
8065
8066 cmd_silent = FALSE; /* Want to see the prompt. */
8067 if (prompt != NULL)
8068 {
8069 /* Only the part of the message after the last NL is considered as
8070 * prompt for the command line */
8071 p = vim_strrchr(prompt, '\n');
8072 if (p == NULL)
8073 p = prompt;
8074 else
8075 {
8076 ++p;
8077 c = *p;
8078 *p = NUL;
8079 msg_start();
8080 msg_clr_eos();
8081 msg_puts_attr(prompt, echo_attr);
8082 msg_didout = FALSE;
8083 msg_starthere();
8084 *p = c;
8085 }
8086 cmdline_row = msg_row;
8087
8088 if (argvars[1].v_type != VAR_UNKNOWN)
8089 {
8090 defstr = get_tv_string_buf_chk(&argvars[1], buf);
8091 if (defstr != NULL)
8092 stuffReadbuffSpec(defstr);
8093
8094 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8095 {
8096 char_u *xp_name;
8097 int xp_namelen;
8098 long argt;
8099
8100 /* input() with a third argument: completion */
8101 rettv->vval.v_string = NULL;
8102
8103 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
8104 if (xp_name == NULL)
8105 return;
8106
8107 xp_namelen = (int)STRLEN(xp_name);
8108
8109 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8110 &xp_arg) == FAIL)
8111 return;
8112 }
8113 }
8114
8115 if (defstr != NULL)
8116 {
8117 int save_ex_normal_busy = ex_normal_busy;
8118 ex_normal_busy = 0;
8119 rettv->vval.v_string =
8120 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8121 xp_type, xp_arg);
8122 ex_normal_busy = save_ex_normal_busy;
8123 }
8124 if (inputdialog && rettv->vval.v_string == NULL
8125 && argvars[1].v_type != VAR_UNKNOWN
8126 && argvars[2].v_type != VAR_UNKNOWN)
8127 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
8128 &argvars[2], buf));
8129
8130 vim_free(xp_arg);
8131
8132 /* since the user typed this, no need to wait for return */
8133 need_wait_return = FALSE;
8134 msg_didout = FALSE;
8135 }
8136 cmd_silent = cmd_silent_save;
8137}
8138
8139/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 * ":echo expr1 ..." print each argument separated with a space, add a
8141 * newline at the end.
8142 * ":echon expr1 ..." print each argument plain.
8143 */
8144 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008145ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146{
8147 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008148 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008149 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 char_u *p;
8151 int needclr = TRUE;
8152 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008153 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154
8155 if (eap->skip)
8156 ++emsg_skip;
8157 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8158 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008159 /* If eval1() causes an error message the text from the command may
8160 * still need to be cleared. E.g., "echo 22,44". */
8161 need_clr_eos = needclr;
8162
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008164 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 {
8166 /*
8167 * Report the invalid expression unless the expression evaluation
8168 * has been cancelled due to an aborting error, an interrupt, or an
8169 * exception.
8170 */
8171 if (!aborting())
8172 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008173 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 break;
8175 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008176 need_clr_eos = FALSE;
8177
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 if (!eap->skip)
8179 {
8180 if (atstart)
8181 {
8182 atstart = FALSE;
8183 /* Call msg_start() after eval1(), evaluating the expression
8184 * may cause a message to appear. */
8185 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008186 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008187 /* Mark the saved text as finishing the line, so that what
8188 * follows is displayed on a new line when scrolling back
8189 * at the more prompt. */
8190 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 }
8194 else if (eap->cmdidx == CMD_echo)
8195 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008196 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008197 if (p != NULL)
8198 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008200 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008202 if (*p != TAB && needclr)
8203 {
8204 /* remove any text still there from the command */
8205 msg_clr_eos();
8206 needclr = FALSE;
8207 }
8208 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 }
8210 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008211 {
8212#ifdef FEAT_MBYTE
8213 if (has_mbyte)
8214 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008215 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008216
8217 (void)msg_outtrans_len_attr(p, i, echo_attr);
8218 p += i - 1;
8219 }
8220 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008222 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008225 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008227 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 arg = skipwhite(arg);
8229 }
8230 eap->nextcmd = check_nextcmd(arg);
8231
8232 if (eap->skip)
8233 --emsg_skip;
8234 else
8235 {
8236 /* remove text that may still be there from the command */
8237 if (needclr)
8238 msg_clr_eos();
8239 if (eap->cmdidx == CMD_echo)
8240 msg_end();
8241 }
8242}
8243
8244/*
8245 * ":echohl {name}".
8246 */
8247 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008248ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249{
8250 int id;
8251
8252 id = syn_name2id(eap->arg);
8253 if (id == 0)
8254 echo_attr = 0;
8255 else
8256 echo_attr = syn_id2attr(id);
8257}
8258
8259/*
8260 * ":execute expr1 ..." execute the result of an expression.
8261 * ":echomsg expr1 ..." Print a message
8262 * ":echoerr expr1 ..." Print an error
8263 * Each gets spaces around each argument and a newline at the end for
8264 * echo commands
8265 */
8266 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008267ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268{
8269 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008270 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 int ret = OK;
8272 char_u *p;
8273 garray_T ga;
8274 int len;
8275 int save_did_emsg;
8276
8277 ga_init2(&ga, 1, 80);
8278
8279 if (eap->skip)
8280 ++emsg_skip;
8281 while (*arg != NUL && *arg != '|' && *arg != '\n')
8282 {
8283 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008284 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 {
8286 /*
8287 * Report the invalid expression unless the expression evaluation
8288 * has been cancelled due to an aborting error, an interrupt, or an
8289 * exception.
8290 */
8291 if (!aborting())
8292 EMSG2(_(e_invexpr2), p);
8293 ret = FAIL;
8294 break;
8295 }
8296
8297 if (!eap->skip)
8298 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008299 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 len = (int)STRLEN(p);
8301 if (ga_grow(&ga, len + 2) == FAIL)
8302 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008303 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 ret = FAIL;
8305 break;
8306 }
8307 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 ga.ga_len += len;
8311 }
8312
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008313 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 arg = skipwhite(arg);
8315 }
8316
8317 if (ret != FAIL && ga.ga_data != NULL)
8318 {
8319 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008320 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008322 out_flush();
8323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 else if (eap->cmdidx == CMD_echoerr)
8325 {
8326 /* We don't want to abort following commands, restore did_emsg. */
8327 save_did_emsg = did_emsg;
8328 EMSG((char_u *)ga.ga_data);
8329 if (!force_abort)
8330 did_emsg = save_did_emsg;
8331 }
8332 else if (eap->cmdidx == CMD_execute)
8333 do_cmdline((char_u *)ga.ga_data,
8334 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8335 }
8336
8337 ga_clear(&ga);
8338
8339 if (eap->skip)
8340 --emsg_skip;
8341
8342 eap->nextcmd = check_nextcmd(arg);
8343}
8344
8345/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008346 * Find window specified by "vp" in tabpage "tp".
8347 */
8348 win_T *
8349find_win_by_nr(
8350 typval_T *vp,
8351 tabpage_T *tp UNUSED) /* NULL for current tab page */
8352{
8353#ifdef FEAT_WINDOWS
8354 win_T *wp;
8355#endif
8356 int nr;
8357
8358 nr = (int)get_tv_number_chk(vp, NULL);
8359
8360#ifdef FEAT_WINDOWS
8361 if (nr < 0)
8362 return NULL;
8363 if (nr == 0)
8364 return curwin;
8365
Bram Moolenaar29323592016-07-24 22:04:11 +02008366 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8367 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008368 if (nr >= LOWEST_WIN_ID)
8369 {
8370 if (wp->w_id == nr)
8371 return wp;
8372 }
8373 else if (--nr <= 0)
8374 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008375 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008376 if (nr >= LOWEST_WIN_ID)
8377 return NULL;
8378 return wp;
8379#else
8380 if (nr == 0 || nr == 1 || nr == curwin->w_id)
8381 return curwin;
8382 return NULL;
8383#endif
8384}
8385
8386/*
8387 * Find window specified by "wvp" in tabpage "tvp".
8388 */
8389 win_T *
8390find_tabwin(
8391 typval_T *wvp, /* VAR_UNKNOWN for current window */
8392 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
8393{
8394 win_T *wp = NULL;
8395 tabpage_T *tp = NULL;
8396 long n;
8397
8398 if (wvp->v_type != VAR_UNKNOWN)
8399 {
8400 if (tvp->v_type != VAR_UNKNOWN)
8401 {
8402 n = (long)get_tv_number(tvp);
8403 if (n >= 0)
8404 tp = find_tabpage(n);
8405 }
8406 else
8407 tp = curtab;
8408
8409 if (tp != NULL)
8410 wp = find_win_by_nr(wvp, tp);
8411 }
8412 else
8413 wp = curwin;
8414
8415 return wp;
8416}
8417
8418/*
8419 * getwinvar() and gettabwinvar()
8420 */
8421 void
8422getwinvar(
8423 typval_T *argvars,
8424 typval_T *rettv,
8425 int off) /* 1 for gettabwinvar() */
8426{
8427 win_T *win;
8428 char_u *varname;
8429 dictitem_T *v;
8430 tabpage_T *tp = NULL;
8431 int done = FALSE;
8432#ifdef FEAT_WINDOWS
8433 win_T *oldcurwin;
8434 tabpage_T *oldtabpage;
8435 int need_switch_win;
8436#endif
8437
8438#ifdef FEAT_WINDOWS
8439 if (off == 1)
8440 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8441 else
8442 tp = curtab;
8443#endif
8444 win = find_win_by_nr(&argvars[off], tp);
8445 varname = get_tv_string_chk(&argvars[off + 1]);
8446 ++emsg_off;
8447
8448 rettv->v_type = VAR_STRING;
8449 rettv->vval.v_string = NULL;
8450
8451 if (win != NULL && varname != NULL)
8452 {
8453#ifdef FEAT_WINDOWS
8454 /* Set curwin to be our win, temporarily. Also set the tabpage,
8455 * otherwise the window is not valid. Only do this when needed,
8456 * autocommands get blocked. */
8457 need_switch_win = !(tp == curtab && win == curwin);
8458 if (!need_switch_win
8459 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
8460#endif
8461 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008462 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008463 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008464 if (varname[1] == NUL)
8465 {
8466 /* get all window-local options in a dict */
8467 dict_T *opts = get_winbuf_options(FALSE);
8468
8469 if (opts != NULL)
8470 {
8471 rettv->v_type = VAR_DICT;
8472 rettv->vval.v_dict = opts;
8473 ++opts->dv_refcount;
8474 done = TRUE;
8475 }
8476 }
8477 else if (get_option_tv(&varname, rettv, 1) == OK)
8478 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008479 done = TRUE;
8480 }
8481 else
8482 {
8483 /* Look up the variable. */
8484 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8485 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8486 varname, FALSE);
8487 if (v != NULL)
8488 {
8489 copy_tv(&v->di_tv, rettv);
8490 done = TRUE;
8491 }
8492 }
8493 }
8494
8495#ifdef FEAT_WINDOWS
8496 if (need_switch_win)
8497 /* restore previous notion of curwin */
8498 restore_win(oldcurwin, oldtabpage, TRUE);
8499#endif
8500 }
8501
8502 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8503 /* use the default return value */
8504 copy_tv(&argvars[off + 2], rettv);
8505
8506 --emsg_off;
8507}
8508
8509/*
8510 * "setwinvar()" and "settabwinvar()" functions
8511 */
8512 void
8513setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8514{
8515 win_T *win;
8516#ifdef FEAT_WINDOWS
8517 win_T *save_curwin;
8518 tabpage_T *save_curtab;
8519 int need_switch_win;
8520#endif
8521 char_u *varname, *winvarname;
8522 typval_T *varp;
8523 char_u nbuf[NUMBUFLEN];
8524 tabpage_T *tp = NULL;
8525
8526 if (check_restricted() || check_secure())
8527 return;
8528
8529#ifdef FEAT_WINDOWS
8530 if (off == 1)
8531 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8532 else
8533 tp = curtab;
8534#endif
8535 win = find_win_by_nr(&argvars[off], tp);
8536 varname = get_tv_string_chk(&argvars[off + 1]);
8537 varp = &argvars[off + 2];
8538
8539 if (win != NULL && varname != NULL && varp != NULL)
8540 {
8541#ifdef FEAT_WINDOWS
8542 need_switch_win = !(tp == curtab && win == curwin);
8543 if (!need_switch_win
8544 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
8545#endif
8546 {
8547 if (*varname == '&')
8548 {
8549 long numval;
8550 char_u *strval;
8551 int error = FALSE;
8552
8553 ++varname;
8554 numval = (long)get_tv_number_chk(varp, &error);
8555 strval = get_tv_string_buf_chk(varp, nbuf);
8556 if (!error && strval != NULL)
8557 set_option_value(varname, numval, strval, OPT_LOCAL);
8558 }
8559 else
8560 {
8561 winvarname = alloc((unsigned)STRLEN(varname) + 3);
8562 if (winvarname != NULL)
8563 {
8564 STRCPY(winvarname, "w:");
8565 STRCPY(winvarname + 2, varname);
8566 set_var(winvarname, varp, TRUE);
8567 vim_free(winvarname);
8568 }
8569 }
8570 }
8571#ifdef FEAT_WINDOWS
8572 if (need_switch_win)
8573 restore_win(save_curwin, save_curtab, TRUE);
8574#endif
8575 }
8576}
8577
8578/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8580 * "arg" points to the "&" or '+' when called, to "option" when returning.
8581 * Returns NULL when no option name found. Otherwise pointer to the char
8582 * after the option name.
8583 */
8584 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008585find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586{
8587 char_u *p = *arg;
8588
8589 ++p;
8590 if (*p == 'g' && p[1] == ':')
8591 {
8592 *opt_flags = OPT_GLOBAL;
8593 p += 2;
8594 }
8595 else if (*p == 'l' && p[1] == ':')
8596 {
8597 *opt_flags = OPT_LOCAL;
8598 p += 2;
8599 }
8600 else
8601 *opt_flags = 0;
8602
8603 if (!ASCII_ISALPHA(*p))
8604 return NULL;
8605 *arg = p;
8606
8607 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
8608 p += 4; /* termcap option */
8609 else
8610 while (ASCII_ISALPHA(*p))
8611 ++p;
8612 return p;
8613}
8614
8615/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008616 * Return the autoload script name for a function or variable name.
8617 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02008619 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008620autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02008621{
Bram Moolenaara1544c02013-05-30 12:35:52 +02008622 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008623 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02008624
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008625 /* Get the script file name: replace '#' with '/', append ".vim". */
8626 scriptname = alloc((unsigned)(STRLEN(name) + 14));
8627 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008628 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008629 STRCPY(scriptname, "autoload/");
8630 STRCAT(scriptname, name);
8631 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
8632 STRCAT(scriptname, ".vim");
8633 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
8634 *p = '/';
8635 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008636}
8637
8638/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008639 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008640 * Return TRUE if a package was loaded.
8641 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008642 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008643script_autoload(
8644 char_u *name,
8645 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008646{
8647 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008648 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008649 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008650 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008651
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008652 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008653 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008654 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008655 return FALSE;
8656
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008657 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008658
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008659 /* Find the name in the list of previously loaded package names. Skip
8660 * "autoload/", it's always the same. */
8661 for (i = 0; i < ga_loaded.ga_len; ++i)
8662 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
8663 break;
8664 if (!reload && i < ga_loaded.ga_len)
8665 ret = FALSE; /* was loaded already */
8666 else
8667 {
8668 /* Remember the name if it wasn't loaded already. */
8669 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
8670 {
8671 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
8672 tofree = NULL;
8673 }
8674
8675 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008676 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008677 ret = TRUE;
8678 }
8679
8680 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008681 return ret;
8682}
8683
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
8685typedef enum
8686{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008687 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
8688 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
8689 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690} var_flavour_T;
8691
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008692static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693
8694 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01008695var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696{
8697 char_u *p = varname;
8698
8699 if (ASCII_ISUPPER(*p))
8700 {
8701 while (*(++p))
8702 if (ASCII_ISLOWER(*p))
8703 return VAR_FLAVOUR_SESSION;
8704 return VAR_FLAVOUR_VIMINFO;
8705 }
8706 else
8707 return VAR_FLAVOUR_DEFAULT;
8708}
8709#endif
8710
8711#if defined(FEAT_VIMINFO) || defined(PROTO)
8712/*
8713 * Restore global vars that start with a capital from the viminfo file
8714 */
8715 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008716read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717{
8718 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008719 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00008720 typval_T tv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008721 void *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722
8723 if (!writing && (find_viminfo_parameter('!') != NULL))
8724 {
8725 tab = vim_strchr(virp->vir_line + 1, '\t');
8726 if (tab != NULL)
8727 {
8728 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008729 switch (*tab)
8730 {
8731 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008732#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008733 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008734#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008735 case 'D': type = VAR_DICT; break;
8736 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008737 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739
8740 tab = vim_strchr(tab, '\t');
8741 if (tab != NULL)
8742 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008743 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008744 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008745 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008747#ifdef FEAT_FLOAT
8748 else if (type == VAR_FLOAT)
8749 (void)string2float(tab + 1, &tv.vval.v_float);
8750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008752 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008753 if (type == VAR_DICT || type == VAR_LIST)
8754 {
8755 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
8756
8757 if (etv == NULL)
8758 /* Failed to parse back the dict or list, use it as a
8759 * string. */
8760 tv.v_type = VAR_STRING;
8761 else
8762 {
8763 vim_free(tv.vval.v_string);
8764 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01008765 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008766 }
8767 }
8768
Bram Moolenaarb20e3342016-01-18 23:29:01 +01008769 /* when in a function use global variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008770 save_funccal = clear_current_funccal();
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008771 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008772 restore_current_funccal(save_funccal);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008773
8774 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008775 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008776 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
8777 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 }
8779 }
8780 }
8781
8782 return viminfo_readline(virp);
8783}
8784
8785/*
8786 * Write global vars that start with a capital to the viminfo file
8787 */
8788 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008789write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790{
Bram Moolenaar33570922005-01-25 22:26:29 +00008791 hashitem_T *hi;
8792 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008793 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01008794 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008795 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008796 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008797 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798
8799 if (find_viminfo_parameter('!') == NULL)
8800 return;
8801
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02008802 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00008803
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008804 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008805 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008807 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008809 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008810 this_var = HI2DI(hi);
8811 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008812 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008813 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00008814 {
8815 case VAR_STRING: s = "STR"; break;
8816 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008817 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008818 case VAR_DICT: s = "DIC"; break;
8819 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008820 case VAR_SPECIAL: s = "XPL"; break;
8821
8822 case VAR_UNKNOWN:
8823 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008824 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008825 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008826 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008827 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00008828 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008829 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008830 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008831 if (p != NULL)
8832 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00008833 vim_free(tofree);
8834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 }
8836 }
8837}
8838#endif
8839
8840#if defined(FEAT_SESSION) || defined(PROTO)
8841 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008842store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843{
Bram Moolenaar33570922005-01-25 22:26:29 +00008844 hashitem_T *hi;
8845 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008846 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 char_u *p, *t;
8848
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008849 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008850 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008852 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008854 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008855 this_var = HI2DI(hi);
8856 if ((this_var->di_tv.v_type == VAR_NUMBER
8857 || this_var->di_tv.v_type == VAR_STRING)
8858 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008859 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008860 /* Escape special characters with a backslash. Turn a LF and
8861 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008862 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00008863 (char_u *)"\\\"\n\r");
8864 if (p == NULL) /* out of memory */
8865 break;
8866 for (t = p; *t != NUL; ++t)
8867 if (*t == '\n')
8868 *t = 'n';
8869 else if (*t == '\r')
8870 *t = 'r';
8871 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00008872 this_var->di_key,
8873 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8874 : ' ',
8875 p,
8876 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8877 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00008878 || put_eol(fd) == FAIL)
8879 {
8880 vim_free(p);
8881 return FAIL;
8882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 vim_free(p);
8884 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008885#ifdef FEAT_FLOAT
8886 else if (this_var->di_tv.v_type == VAR_FLOAT
8887 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
8888 {
8889 float_T f = this_var->di_tv.vval.v_float;
8890 int sign = ' ';
8891
8892 if (f < 0)
8893 {
8894 f = -f;
8895 sign = '-';
8896 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01008897 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008898 this_var->di_key, sign, f) < 0)
8899 || put_eol(fd) == FAIL)
8900 return FAIL;
8901 }
8902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 }
8904 }
8905 return OK;
8906}
8907#endif
8908
Bram Moolenaar661b1822005-07-28 22:36:45 +00008909/*
8910 * Display script name where an item was last set.
8911 * Should only be invoked when 'verbose' is non-zero.
8912 */
8913 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008914last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +00008915{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008916 char_u *p;
8917
Bram Moolenaar661b1822005-07-28 22:36:45 +00008918 if (scriptID != 0)
8919 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008920 p = home_replace_save(NULL, get_scriptname(scriptID));
8921 if (p != NULL)
8922 {
8923 verbose_enter();
8924 MSG_PUTS(_("\n\tLast set from "));
8925 MSG_PUTS(p);
8926 vim_free(p);
8927 verbose_leave();
8928 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00008929 }
8930}
8931
Bram Moolenaar53744302015-07-17 17:38:22 +02008932/* reset v:option_new, v:option_old and v:option_type */
8933 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008934reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02008935{
8936 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
8937 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
8938 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
8939}
8940
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008941/*
8942 * Prepare "gap" for an assert error and add the sourcing position.
8943 */
8944 void
8945prepare_assert_error(garray_T *gap)
8946{
8947 char buf[NUMBUFLEN];
8948
8949 ga_init2(gap, 1, 100);
8950 if (sourcing_name != NULL)
8951 {
8952 ga_concat(gap, sourcing_name);
8953 if (sourcing_lnum > 0)
8954 ga_concat(gap, (char_u *)" ");
8955 }
8956 if (sourcing_lnum > 0)
8957 {
8958 sprintf(buf, "line %ld", (long)sourcing_lnum);
8959 ga_concat(gap, (char_u *)buf);
8960 }
8961 if (sourcing_name != NULL || sourcing_lnum > 0)
8962 ga_concat(gap, (char_u *)": ");
8963}
8964
8965/*
8966 * Add an assert error to v:errors.
8967 */
8968 void
8969assert_error(garray_T *gap)
8970{
8971 struct vimvar *vp = &vimvars[VV_ERRORS];
8972
8973 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
8974 /* Make sure v:errors is a list. */
8975 set_vim_var_list(VV_ERRORS, list_alloc());
8976 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
8977}
8978
8979 void
8980assert_equal_common(typval_T *argvars, assert_type_T atype)
8981{
8982 garray_T ga;
8983
8984 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
8985 != (atype == ASSERT_EQUAL))
8986 {
8987 prepare_assert_error(&ga);
8988 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
8989 atype);
8990 assert_error(&ga);
8991 ga_clear(&ga);
8992 }
8993}
8994
8995 void
8996assert_match_common(typval_T *argvars, assert_type_T atype)
8997{
8998 garray_T ga;
8999 char_u buf1[NUMBUFLEN];
9000 char_u buf2[NUMBUFLEN];
9001 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9002 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9003
9004 if (pat == NULL || text == NULL)
9005 EMSG(_(e_invarg));
9006 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
9007 {
9008 prepare_assert_error(&ga);
9009 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9010 atype);
9011 assert_error(&ga);
9012 ga_clear(&ga);
9013 }
9014}
9015
Bram Moolenaar61c04492016-07-23 15:35:35 +02009016 void
9017assert_inrange(typval_T *argvars)
9018{
9019 garray_T ga;
9020 int error = FALSE;
9021 varnumber_T lower = get_tv_number_chk(&argvars[0], &error);
9022 varnumber_T upper = get_tv_number_chk(&argvars[1], &error);
9023 varnumber_T actual = get_tv_number_chk(&argvars[2], &error);
9024 char_u *tofree;
9025 char msg[200];
9026 char_u numbuf[NUMBUFLEN];
9027
9028 if (error)
9029 return;
9030 if (actual < lower || actual > upper)
9031 {
9032 prepare_assert_error(&ga);
9033 if (argvars[3].v_type != VAR_UNKNOWN)
9034 {
9035 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9036 vim_free(tofree);
9037 }
9038 else
9039 {
9040 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
9041 (long)lower, (long)upper, (long)actual);
9042 ga_concat(&ga, (char_u *)msg);
9043 }
9044 assert_error(&ga);
9045 ga_clear(&ga);
9046 }
9047}
9048
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009049/*
9050 * Common for assert_true() and assert_false().
9051 */
9052 void
9053assert_bool(typval_T *argvars, int isTrue)
9054{
9055 int error = FALSE;
9056 garray_T ga;
9057
9058 if (argvars[0].v_type == VAR_SPECIAL
9059 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
9060 return;
9061 if (argvars[0].v_type != VAR_NUMBER
9062 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9063 || error)
9064 {
9065 prepare_assert_error(&ga);
9066 fill_assert_error(&ga, &argvars[1],
9067 (char_u *)(isTrue ? "True" : "False"),
9068 NULL, &argvars[0], ASSERT_OTHER);
9069 assert_error(&ga);
9070 ga_clear(&ga);
9071 }
9072}
9073
9074 void
9075assert_exception(typval_T *argvars)
9076{
9077 garray_T ga;
9078 char_u *error = get_tv_string_chk(&argvars[0]);
9079
9080 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9081 {
9082 prepare_assert_error(&ga);
9083 ga_concat(&ga, (char_u *)"v:exception is not set");
9084 assert_error(&ga);
9085 ga_clear(&ga);
9086 }
9087 else if (error != NULL
9088 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9089 {
9090 prepare_assert_error(&ga);
9091 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9092 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9093 assert_error(&ga);
9094 ga_clear(&ga);
9095 }
9096}
9097
9098 void
9099assert_fails(typval_T *argvars)
9100{
9101 char_u *cmd = get_tv_string_chk(&argvars[0]);
9102 garray_T ga;
9103
9104 called_emsg = FALSE;
9105 suppress_errthrow = TRUE;
9106 emsg_silent = TRUE;
9107 do_cmdline_cmd(cmd);
9108 if (!called_emsg)
9109 {
9110 prepare_assert_error(&ga);
9111 ga_concat(&ga, (char_u *)"command did not fail: ");
9112 ga_concat(&ga, cmd);
9113 assert_error(&ga);
9114 ga_clear(&ga);
9115 }
9116 else if (argvars[1].v_type != VAR_UNKNOWN)
9117 {
9118 char_u buf[NUMBUFLEN];
9119 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9120
9121 if (error == NULL
9122 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9123 {
9124 prepare_assert_error(&ga);
9125 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9126 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
9127 assert_error(&ga);
9128 ga_clear(&ga);
9129 }
9130 }
9131
9132 called_emsg = FALSE;
9133 suppress_errthrow = FALSE;
9134 emsg_silent = FALSE;
9135 emsg_on_display = FALSE;
9136 set_vim_var_string(VV_ERRMSG, NULL, 0);
9137}
9138
9139/*
9140 * Append "str" to "gap", escaping unprintable characters.
9141 * Changes NL to \n, CR to \r, etc.
9142 */
9143 static void
9144ga_concat_esc(garray_T *gap, char_u *str)
9145{
9146 char_u *p;
9147 char_u buf[NUMBUFLEN];
9148
9149 if (str == NULL)
9150 {
9151 ga_concat(gap, (char_u *)"NULL");
9152 return;
9153 }
9154
9155 for (p = str; *p != NUL; ++p)
9156 switch (*p)
9157 {
9158 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9159 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9160 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9161 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9162 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9163 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9164 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9165 default:
9166 if (*p < ' ')
9167 {
9168 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9169 ga_concat(gap, buf);
9170 }
9171 else
9172 ga_append(gap, *p);
9173 break;
9174 }
9175}
9176
9177/*
9178 * Fill "gap" with information about an assert error.
9179 */
9180 void
9181fill_assert_error(
9182 garray_T *gap,
9183 typval_T *opt_msg_tv,
9184 char_u *exp_str,
9185 typval_T *exp_tv,
9186 typval_T *got_tv,
9187 assert_type_T atype)
9188{
9189 char_u numbuf[NUMBUFLEN];
9190 char_u *tofree;
9191
9192 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9193 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009194 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9195 vim_free(tofree);
9196 ga_concat(gap, (char_u *)": ");
9197 }
9198
9199 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9200 ga_concat(gap, (char_u *)"Pattern ");
9201 else if (atype == ASSERT_NOTEQUAL)
9202 ga_concat(gap, (char_u *)"Expected not equal to ");
9203 else
9204 ga_concat(gap, (char_u *)"Expected ");
9205 if (exp_str == NULL)
9206 {
9207 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009208 vim_free(tofree);
9209 }
9210 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009211 ga_concat_esc(gap, exp_str);
9212 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009213 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009214 if (atype == ASSERT_MATCH)
9215 ga_concat(gap, (char_u *)" does not match ");
9216 else if (atype == ASSERT_NOTMATCH)
9217 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009218 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009219 ga_concat(gap, (char_u *)" but got ");
9220 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
9221 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009222 }
9223}
9224
Bram Moolenaar53744302015-07-17 17:38:22 +02009225
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226#endif /* FEAT_EVAL */
9227
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009229#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230
9231#ifdef WIN3264
9232/*
9233 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9234 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009235static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
9236static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
9237static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238
9239/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009240 * Get the short path (8.3) for the filename in "fnamep".
9241 * Only works for a valid file name.
9242 * When the path gets longer "fnamep" is changed and the allocated buffer
9243 * is put in "bufp".
9244 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9245 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 */
9247 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009248get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009250 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251 char_u *newbuf;
9252
9253 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009254 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255 if (l > len - 1)
9256 {
9257 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009258 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259 newbuf = vim_strnsave(*fnamep, l);
9260 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009261 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262
9263 vim_free(*bufp);
9264 *fnamep = *bufp = newbuf;
9265
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009266 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009267 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268 }
9269
9270 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009271 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272}
9273
9274/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009275 * Get the short path (8.3) for the filename in "fname". The converted
9276 * path is returned in "bufp".
9277 *
9278 * Some of the directories specified in "fname" may not exist. This function
9279 * will shorten the existing directories at the beginning of the path and then
9280 * append the remaining non-existing path.
9281 *
9282 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009283 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009284 * bufp - Pointer to an allocated buffer for the filename.
9285 * fnamelen - Length of the filename pointed to by fname
9286 *
9287 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288 */
9289 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009290shortpath_for_invalid_fname(
9291 char_u **fname,
9292 char_u **bufp,
9293 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009295 char_u *short_fname, *save_fname, *pbuf_unused;
9296 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009298 int old_len, len;
9299 int new_len, sfx_len;
9300 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301
9302 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009303 old_len = *fnamelen;
9304 save_fname = vim_strnsave(*fname, old_len);
9305 pbuf_unused = NULL;
9306 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009308 endp = save_fname + old_len - 1; /* Find the end of the copy */
9309 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009311 /*
9312 * Try shortening the supplied path till it succeeds by removing one
9313 * directory at a time from the tail of the path.
9314 */
9315 len = 0;
9316 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009318 /* go back one path-separator */
9319 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9320 --endp;
9321 if (endp <= save_fname)
9322 break; /* processed the complete path */
9323
9324 /*
9325 * Replace the path separator with a NUL and try to shorten the
9326 * resulting path.
9327 */
9328 ch = *endp;
9329 *endp = 0;
9330 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009331 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009332 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9333 {
9334 retval = FAIL;
9335 goto theend;
9336 }
9337 *endp = ch; /* preserve the string */
9338
9339 if (len > 0)
9340 break; /* successfully shortened the path */
9341
9342 /* failed to shorten the path. Skip the path separator */
9343 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344 }
9345
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009346 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009348 /*
9349 * Succeeded in shortening the path. Now concatenate the shortened
9350 * path with the remaining path at the tail.
9351 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009353 /* Compute the length of the new path. */
9354 sfx_len = (int)(save_endp - endp) + 1;
9355 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009357 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009359 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009361 /* There is not enough space in the currently allocated string,
9362 * copy it to a buffer big enough. */
9363 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009365 {
9366 retval = FAIL;
9367 goto theend;
9368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 }
9370 else
9371 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009372 /* Transfer short_fname to the main buffer (it's big enough),
9373 * unless get_short_pathname() did its work in-place. */
9374 *fname = *bufp = save_fname;
9375 if (short_fname != save_fname)
9376 vim_strncpy(save_fname, short_fname, len);
9377 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009379
9380 /* concat the not-shortened part of the path */
9381 vim_strncpy(*fname + len, endp, sfx_len);
9382 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009384
9385theend:
9386 vim_free(pbuf_unused);
9387 vim_free(save_fname);
9388
9389 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390}
9391
9392/*
9393 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009394 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395 */
9396 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009397shortpath_for_partial(
9398 char_u **fnamep,
9399 char_u **bufp,
9400 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401{
9402 int sepcount, len, tflen;
9403 char_u *p;
9404 char_u *pbuf, *tfname;
9405 int hasTilde;
9406
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009407 /* Count up the path separators from the RHS.. so we know which part
9408 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009410 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 if (vim_ispathsep(*p))
9412 ++sepcount;
9413
9414 /* Need full path first (use expand_env() to remove a "~/") */
9415 hasTilde = (**fnamep == '~');
9416 if (hasTilde)
9417 pbuf = tfname = expand_env_save(*fnamep);
9418 else
9419 pbuf = tfname = FullName_save(*fnamep, FALSE);
9420
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009421 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009423 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
9424 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425
9426 if (len == 0)
9427 {
9428 /* Don't have a valid filename, so shorten the rest of the
9429 * path if we can. This CAN give us invalid 8.3 filenames, but
9430 * there's not a lot of point in guessing what it might be.
9431 */
9432 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009433 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
9434 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435 }
9436
9437 /* Count the paths backward to find the beginning of the desired string. */
9438 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009439 {
9440#ifdef FEAT_MBYTE
9441 if (has_mbyte)
9442 p -= mb_head_off(tfname, p);
9443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444 if (vim_ispathsep(*p))
9445 {
9446 if (sepcount == 0 || (hasTilde && sepcount == 1))
9447 break;
9448 else
9449 sepcount --;
9450 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 if (hasTilde)
9453 {
9454 --p;
9455 if (p >= tfname)
9456 *p = '~';
9457 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009458 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 }
9460 else
9461 ++p;
9462
9463 /* Copy in the string - p indexes into tfname - allocated at pbuf */
9464 vim_free(*bufp);
9465 *fnamelen = (int)STRLEN(p);
9466 *bufp = pbuf;
9467 *fnamep = p;
9468
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009469 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470}
9471#endif /* WIN3264 */
9472
9473/*
9474 * Adjust a filename, according to a string of modifiers.
9475 * *fnamep must be NUL terminated when called. When returning, the length is
9476 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009477 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478 * When there is an error, *fnamep is set to NULL.
9479 */
9480 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009481modify_fname(
9482 char_u *src, /* string with modifiers */
9483 int *usedlen, /* characters after src that are used */
9484 char_u **fnamep, /* file name so far */
9485 char_u **bufp, /* buffer for allocated file name or NULL */
9486 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487{
9488 int valid = 0;
9489 char_u *tail;
9490 char_u *s, *p, *pbuf;
9491 char_u dirname[MAXPATHL];
9492 int c;
9493 int has_fullname = 0;
9494#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009495 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496 int has_shortname = 0;
9497#endif
9498
9499repeat:
9500 /* ":p" - full path/file_name */
9501 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
9502 {
9503 has_fullname = 1;
9504
9505 valid |= VALID_PATH;
9506 *usedlen += 2;
9507
9508 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
9509 if ((*fnamep)[0] == '~'
9510#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
9511 && ((*fnamep)[1] == '/'
9512# ifdef BACKSLASH_IN_FILENAME
9513 || (*fnamep)[1] == '\\'
9514# endif
9515 || (*fnamep)[1] == NUL)
9516
9517#endif
9518 )
9519 {
9520 *fnamep = expand_env_save(*fnamep);
9521 vim_free(*bufp); /* free any allocated file name */
9522 *bufp = *fnamep;
9523 if (*fnamep == NULL)
9524 return -1;
9525 }
9526
9527 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009528 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 {
9530 if (vim_ispathsep(*p)
9531 && p[1] == '.'
9532 && (p[2] == NUL
9533 || vim_ispathsep(p[2])
9534 || (p[2] == '.'
9535 && (p[3] == NUL || vim_ispathsep(p[3])))))
9536 break;
9537 }
9538
9539 /* FullName_save() is slow, don't use it when not needed. */
9540 if (*p != NUL || !vim_isAbsName(*fnamep))
9541 {
9542 *fnamep = FullName_save(*fnamep, *p != NUL);
9543 vim_free(*bufp); /* free any allocated file name */
9544 *bufp = *fnamep;
9545 if (*fnamep == NULL)
9546 return -1;
9547 }
9548
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009549#ifdef WIN3264
9550# if _WIN32_WINNT >= 0x0500
9551 if (vim_strchr(*fnamep, '~') != NULL)
9552 {
9553 /* Expand 8.3 filename to full path. Needed to make sure the same
9554 * file does not have two different names.
9555 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
9556 p = alloc(_MAX_PATH + 1);
9557 if (p != NULL)
9558 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009559 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009560 {
9561 vim_free(*bufp);
9562 *bufp = *fnamep = p;
9563 }
9564 else
9565 vim_free(p);
9566 }
9567 }
9568# endif
9569#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 /* Append a path separator to a directory. */
9571 if (mch_isdir(*fnamep))
9572 {
9573 /* Make room for one or two extra characters. */
9574 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
9575 vim_free(*bufp); /* free any allocated file name */
9576 *bufp = *fnamep;
9577 if (*fnamep == NULL)
9578 return -1;
9579 add_pathsep(*fnamep);
9580 }
9581 }
9582
9583 /* ":." - path relative to the current directory */
9584 /* ":~" - path relative to the home directory */
9585 /* ":8" - shortname path - postponed till after */
9586 while (src[*usedlen] == ':'
9587 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
9588 {
9589 *usedlen += 2;
9590 if (c == '8')
9591 {
9592#ifdef WIN3264
9593 has_shortname = 1; /* Postpone this. */
9594#endif
9595 continue;
9596 }
9597 pbuf = NULL;
9598 /* Need full path first (use expand_env() to remove a "~/") */
9599 if (!has_fullname)
9600 {
9601 if (c == '.' && **fnamep == '~')
9602 p = pbuf = expand_env_save(*fnamep);
9603 else
9604 p = pbuf = FullName_save(*fnamep, FALSE);
9605 }
9606 else
9607 p = *fnamep;
9608
9609 has_fullname = 0;
9610
9611 if (p != NULL)
9612 {
9613 if (c == '.')
9614 {
9615 mch_dirname(dirname, MAXPATHL);
9616 s = shorten_fname(p, dirname);
9617 if (s != NULL)
9618 {
9619 *fnamep = s;
9620 if (pbuf != NULL)
9621 {
9622 vim_free(*bufp); /* free any allocated file name */
9623 *bufp = pbuf;
9624 pbuf = NULL;
9625 }
9626 }
9627 }
9628 else
9629 {
9630 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
9631 /* Only replace it when it starts with '~' */
9632 if (*dirname == '~')
9633 {
9634 s = vim_strsave(dirname);
9635 if (s != NULL)
9636 {
9637 *fnamep = s;
9638 vim_free(*bufp);
9639 *bufp = s;
9640 }
9641 }
9642 }
9643 vim_free(pbuf);
9644 }
9645 }
9646
9647 tail = gettail(*fnamep);
9648 *fnamelen = (int)STRLEN(*fnamep);
9649
9650 /* ":h" - head, remove "/file_name", can be repeated */
9651 /* Don't remove the first "/" or "c:\" */
9652 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
9653 {
9654 valid |= VALID_HEAD;
9655 *usedlen += 2;
9656 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009657 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009658 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659 *fnamelen = (int)(tail - *fnamep);
9660#ifdef VMS
9661 if (*fnamelen > 0)
9662 *fnamelen += 1; /* the path separator is part of the path */
9663#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009664 if (*fnamelen == 0)
9665 {
9666 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
9667 p = vim_strsave((char_u *)".");
9668 if (p == NULL)
9669 return -1;
9670 vim_free(*bufp);
9671 *bufp = *fnamep = tail = p;
9672 *fnamelen = 1;
9673 }
9674 else
9675 {
9676 while (tail > s && !after_pathsep(s, tail))
9677 mb_ptr_back(*fnamep, tail);
9678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679 }
9680
9681 /* ":8" - shortname */
9682 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
9683 {
9684 *usedlen += 2;
9685#ifdef WIN3264
9686 has_shortname = 1;
9687#endif
9688 }
9689
9690#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009691 /*
9692 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 */
9694 if (has_shortname)
9695 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009696 /* Copy the string if it is shortened by :h and when it wasn't copied
9697 * yet, because we are going to change it in place. Avoids changing
9698 * the buffer name for "%:8". */
9699 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 {
9701 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +02009702 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 return -1;
9704 vim_free(*bufp);
9705 *bufp = *fnamep = p;
9706 }
9707
9708 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +02009709 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 if (!has_fullname && !vim_isAbsName(*fnamep))
9711 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009712 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 return -1;
9714 }
9715 else
9716 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009717 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718
Bram Moolenaardc935552011-08-17 15:23:23 +02009719 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009721 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 return -1;
9723
9724 if (l == 0)
9725 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009726 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009728 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729 return -1;
9730 }
9731 *fnamelen = l;
9732 }
9733 }
9734#endif /* WIN3264 */
9735
9736 /* ":t" - tail, just the basename */
9737 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
9738 {
9739 *usedlen += 2;
9740 *fnamelen -= (int)(tail - *fnamep);
9741 *fnamep = tail;
9742 }
9743
9744 /* ":e" - extension, can be repeated */
9745 /* ":r" - root, without extension, can be repeated */
9746 while (src[*usedlen] == ':'
9747 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
9748 {
9749 /* find a '.' in the tail:
9750 * - for second :e: before the current fname
9751 * - otherwise: The last '.'
9752 */
9753 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
9754 s = *fnamep - 2;
9755 else
9756 s = *fnamep + *fnamelen - 1;
9757 for ( ; s > tail; --s)
9758 if (s[0] == '.')
9759 break;
9760 if (src[*usedlen + 1] == 'e') /* :e */
9761 {
9762 if (s > tail)
9763 {
9764 *fnamelen += (int)(*fnamep - (s + 1));
9765 *fnamep = s + 1;
9766#ifdef VMS
9767 /* cut version from the extension */
9768 s = *fnamep + *fnamelen - 1;
9769 for ( ; s > *fnamep; --s)
9770 if (s[0] == ';')
9771 break;
9772 if (s > *fnamep)
9773 *fnamelen = s - *fnamep;
9774#endif
9775 }
9776 else if (*fnamep <= tail)
9777 *fnamelen = 0;
9778 }
9779 else /* :r */
9780 {
9781 if (s > tail) /* remove one extension */
9782 *fnamelen = (int)(s - *fnamep);
9783 }
9784 *usedlen += 2;
9785 }
9786
9787 /* ":s?pat?foo?" - substitute */
9788 /* ":gs?pat?foo?" - global substitute */
9789 if (src[*usedlen] == ':'
9790 && (src[*usedlen + 1] == 's'
9791 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
9792 {
9793 char_u *str;
9794 char_u *pat;
9795 char_u *sub;
9796 int sep;
9797 char_u *flags;
9798 int didit = FALSE;
9799
9800 flags = (char_u *)"";
9801 s = src + *usedlen + 2;
9802 if (src[*usedlen + 1] == 'g')
9803 {
9804 flags = (char_u *)"g";
9805 ++s;
9806 }
9807
9808 sep = *s++;
9809 if (sep)
9810 {
9811 /* find end of pattern */
9812 p = vim_strchr(s, sep);
9813 if (p != NULL)
9814 {
9815 pat = vim_strnsave(s, (int)(p - s));
9816 if (pat != NULL)
9817 {
9818 s = p + 1;
9819 /* find end of substitution */
9820 p = vim_strchr(s, sep);
9821 if (p != NULL)
9822 {
9823 sub = vim_strnsave(s, (int)(p - s));
9824 str = vim_strnsave(*fnamep, *fnamelen);
9825 if (sub != NULL && str != NULL)
9826 {
9827 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009828 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829 if (s != NULL)
9830 {
9831 *fnamep = s;
9832 *fnamelen = (int)STRLEN(s);
9833 vim_free(*bufp);
9834 *bufp = s;
9835 didit = TRUE;
9836 }
9837 }
9838 vim_free(sub);
9839 vim_free(str);
9840 }
9841 vim_free(pat);
9842 }
9843 }
9844 /* after using ":s", repeat all the modifiers */
9845 if (didit)
9846 goto repeat;
9847 }
9848 }
9849
Bram Moolenaar26df0922014-02-23 23:39:13 +01009850 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
9851 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +01009852 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +01009853 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +01009854 if (c != NUL)
9855 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +01009856 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +01009857 if (c != NUL)
9858 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +01009859 if (p == NULL)
9860 return -1;
9861 vim_free(*bufp);
9862 *bufp = *fnamep = p;
9863 *fnamelen = (int)STRLEN(p);
9864 *usedlen += 2;
9865 }
9866
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867 return valid;
9868}
9869
9870/*
9871 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009872 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873 * "flags" can be "g" to do a global substitute.
9874 * Returns an allocated string, NULL for error.
9875 */
9876 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009877do_string_sub(
9878 char_u *str,
9879 char_u *pat,
9880 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009881 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01009882 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883{
9884 int sublen;
9885 regmatch_T regmatch;
9886 int i;
9887 int do_all;
9888 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01009889 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890 garray_T ga;
9891 char_u *ret;
9892 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01009893 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894
9895 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
9896 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00009897 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009898
9899 ga_init2(&ga, 1, 200);
9900
9901 do_all = (flags[0] == 'g');
9902
9903 regmatch.rm_ic = p_ic;
9904 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
9905 if (regmatch.regprog != NULL)
9906 {
9907 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01009908 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
9910 {
Bram Moolenaar8af26912014-01-23 20:09:34 +01009911 /* Skip empty match except for first match. */
9912 if (regmatch.startp[0] == regmatch.endp[0])
9913 {
9914 if (zero_width == regmatch.startp[0])
9915 {
9916 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02009917 i = MB_PTR2LEN(tail);
9918 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
9919 (size_t)i);
9920 ga.ga_len += i;
9921 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01009922 continue;
9923 }
9924 zero_width = regmatch.startp[0];
9925 }
9926
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927 /*
9928 * Get some space for a temporary buffer to do the substitution
9929 * into. It will contain:
9930 * - The text up to where the match is.
9931 * - The substituted text.
9932 * - The text after the match.
9933 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009934 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01009935 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
9937 {
9938 ga_clear(&ga);
9939 break;
9940 }
9941
9942 /* copy the text up to where the match is */
9943 i = (int)(regmatch.startp[0] - tail);
9944 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
9945 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009946 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947 + ga.ga_len + i, TRUE, TRUE, FALSE);
9948 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02009949 tail = regmatch.endp[0];
9950 if (*tail == NUL)
9951 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 if (!do_all)
9953 break;
9954 }
9955
9956 if (ga.ga_data != NULL)
9957 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
9958
Bram Moolenaar473de612013-06-08 18:19:48 +02009959 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 }
9961
9962 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
9963 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00009964 if (p_cpo == empty_option)
9965 p_cpo = save_cpo;
9966 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009967 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00009968 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969
9970 return ret;
9971}
9972
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009973 static int
9974filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
9975{
9976 typval_T rettv;
9977 typval_T argv[3];
9978 char_u buf[NUMBUFLEN];
9979 char_u *s;
9980 int retval = FAIL;
9981 int dummy;
9982
9983 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
9984 argv[0] = vimvars[VV_KEY].vv_tv;
9985 argv[1] = vimvars[VV_VAL].vv_tv;
9986 if (expr->v_type == VAR_FUNC)
9987 {
9988 s = expr->vval.v_string;
Bram Moolenaardf48fb42016-07-22 21:50:18 +02009989 if (call_func(s, (int)STRLEN(s), &rettv, 2, argv, NULL,
9990 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009991 goto theend;
9992 }
9993 else if (expr->v_type == VAR_PARTIAL)
9994 {
9995 partial_T *partial = expr->vval.v_partial;
9996
Bram Moolenaar437bafe2016-08-01 15:40:54 +02009997 s = partial_name(partial);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02009998 if (call_func(s, (int)STRLEN(s), &rettv, 2, argv, NULL,
9999 0L, 0L, &dummy, TRUE, partial, NULL) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010000 goto theend;
10001 }
10002 else
10003 {
10004 s = get_tv_string_buf_chk(expr, buf);
10005 if (s == NULL)
10006 goto theend;
10007 s = skipwhite(s);
10008 if (eval1(&s, &rettv, TRUE) == FAIL)
10009 goto theend;
10010 if (*s != NUL) /* check for trailing chars after expr */
10011 {
10012 EMSG2(_(e_invexpr2), s);
10013 goto theend;
10014 }
10015 }
10016 if (map)
10017 {
10018 /* map(): replace the list item value */
10019 clear_tv(tv);
10020 rettv.v_lock = 0;
10021 *tv = rettv;
10022 }
10023 else
10024 {
10025 int error = FALSE;
10026
10027 /* filter(): when expr is zero remove the item */
10028 *remp = (get_tv_number_chk(&rettv, &error) == 0);
10029 clear_tv(&rettv);
10030 /* On type error, nothing has been removed; return FAIL to stop the
10031 * loop. The error message was given by get_tv_number_chk(). */
10032 if (error)
10033 goto theend;
10034 }
10035 retval = OK;
10036theend:
10037 clear_tv(&vimvars[VV_VAL].vv_tv);
10038 return retval;
10039}
10040
10041
10042/*
10043 * Implementation of map() and filter().
10044 */
10045 void
10046filter_map(typval_T *argvars, typval_T *rettv, int map)
10047{
10048 typval_T *expr;
10049 listitem_T *li, *nli;
10050 list_T *l = NULL;
10051 dictitem_T *di;
10052 hashtab_T *ht;
10053 hashitem_T *hi;
10054 dict_T *d = NULL;
10055 typval_T save_val;
10056 typval_T save_key;
10057 int rem;
10058 int todo;
10059 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10060 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10061 : N_("filter() argument"));
10062 int save_did_emsg;
10063 int idx = 0;
10064
10065 if (argvars[0].v_type == VAR_LIST)
10066 {
10067 if ((l = argvars[0].vval.v_list) == NULL
10068 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
10069 return;
10070 }
10071 else if (argvars[0].v_type == VAR_DICT)
10072 {
10073 if ((d = argvars[0].vval.v_dict) == NULL
10074 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
10075 return;
10076 }
10077 else
10078 {
10079 EMSG2(_(e_listdictarg), ermsg);
10080 return;
10081 }
10082
10083 expr = &argvars[1];
10084 /* On type errors, the preceding call has already displayed an error
10085 * message. Avoid a misleading error message for an empty string that
10086 * was not passed as argument. */
10087 if (expr->v_type != VAR_UNKNOWN)
10088 {
10089 prepare_vimvar(VV_VAL, &save_val);
10090
10091 /* We reset "did_emsg" to be able to detect whether an error
10092 * occurred during evaluation of the expression. */
10093 save_did_emsg = did_emsg;
10094 did_emsg = FALSE;
10095
10096 prepare_vimvar(VV_KEY, &save_key);
10097 if (argvars[0].v_type == VAR_DICT)
10098 {
10099 vimvars[VV_KEY].vv_type = VAR_STRING;
10100
10101 ht = &d->dv_hashtab;
10102 hash_lock(ht);
10103 todo = (int)ht->ht_used;
10104 for (hi = ht->ht_array; todo > 0; ++hi)
10105 {
10106 if (!HASHITEM_EMPTY(hi))
10107 {
10108 int r;
10109
10110 --todo;
10111 di = HI2DI(hi);
10112 if (map &&
10113 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10114 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
10115 break;
10116 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10117 r = filter_map_one(&di->di_tv, expr, map, &rem);
10118 clear_tv(&vimvars[VV_KEY].vv_tv);
10119 if (r == FAIL || did_emsg)
10120 break;
10121 if (!map && rem)
10122 {
10123 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10124 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10125 break;
10126 dictitem_remove(d, di);
10127 }
10128 }
10129 }
10130 hash_unlock(ht);
10131 }
10132 else
10133 {
10134 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10135
10136 for (li = l->lv_first; li != NULL; li = nli)
10137 {
10138 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
10139 break;
10140 nli = li->li_next;
10141 vimvars[VV_KEY].vv_nr = idx;
10142 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10143 || did_emsg)
10144 break;
10145 if (!map && rem)
10146 listitem_remove(l, li);
10147 ++idx;
10148 }
10149 }
10150
10151 restore_vimvar(VV_KEY, &save_key);
10152 restore_vimvar(VV_VAL, &save_val);
10153
10154 did_emsg |= save_did_emsg;
10155 }
10156
10157 copy_tv(&argvars[0], rettv);
10158}
10159
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */