blob: 53c188baeffc813142bedd8cdd21359afa8c2688 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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 Moolenaar33570922005-01-25 22:26:29 +0000236
Bram Moolenaar33570922005-01-25 22:26:29 +0000237
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100238static int get_env_len(char_u **arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100239static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100240static typval_T *alloc_string_tv(char_u *string);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100241static hashtab_T *find_var_ht(char_u *name, char_u **varname);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100242static void delete_var(hashtab_T *ht, hashitem_T *hi);
243static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
244static 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 +0100245static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200246
247#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100248static int compare_func_name(const void *s1, const void *s2);
249static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200250#endif
251
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200252/* for VIM_VERSION_ defines */
253#include "version.h"
254
Bram Moolenaar33570922005-01-25 22:26:29 +0000255/*
256 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000257 */
258 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100259eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000260{
Bram Moolenaar33570922005-01-25 22:26:29 +0000261 int i;
262 struct vimvar *p;
263
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200264 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
265 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200266 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000267 hash_init(&compat_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200268 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000269
270 for (i = 0; i < VV_LEN; ++i)
271 {
272 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200273 if (STRLEN(p->vv_name) > 16)
274 {
275 EMSG("INTERNAL: name too long, increase size of dictitem16_T");
276 getout(1);
277 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000278 STRCPY(p->vv_di.di_key, p->vv_name);
279 if (p->vv_flags & VV_RO)
280 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
281 else if (p->vv_flags & VV_RO_SBX)
282 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
283 else
284 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000285
286 /* add to v: scope dict, unless the value is not always available */
287 if (p->vv_type != VAR_UNKNOWN)
288 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000289 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000290 /* add to compat scope dict */
291 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000292 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100293 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
294
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000295 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100296 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200297 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100298 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100299
300 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
301 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
302 set_vim_var_nr(VV_NONE, VVAL_NONE);
303 set_vim_var_nr(VV_NULL, VVAL_NULL);
304
Bram Moolenaarf562e722016-07-19 17:25:25 +0200305 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
306 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
307 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
308 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
309 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
310 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
311 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
312 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
313 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
314 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
315
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200316 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200317
318#ifdef EBCDIC
319 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100320 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200321 */
322 sortFunctions();
323#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000324}
325
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000326#if defined(EXITFREE) || defined(PROTO)
327 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100328eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000329{
330 int i;
331 struct vimvar *p;
332
333 for (i = 0; i < VV_LEN; ++i)
334 {
335 p = &vimvars[i];
336 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000337 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000338 vim_free(p->vv_str);
339 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000340 }
341 else if (p->vv_di.di_tv.v_type == VAR_LIST)
342 {
343 list_unref(p->vv_list);
344 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000345 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000346 }
347 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000348 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000349 hash_clear(&compat_hashtab);
350
Bram Moolenaard9fba312005-06-26 22:34:35 +0000351 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100352# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200353 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100354# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000355
356 /* global variables */
357 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000358
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000359 /* autoloaded script names */
360 ga_clear_strings(&ga_loaded);
361
Bram Moolenaarcca74132013-09-25 21:00:28 +0200362 /* Script-local variables. First clear all the variables and in a second
363 * loop free the scriptvar_T, because a variable in one script might hold
364 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200365 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200366 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200367 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200368 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200369 ga_clear(&ga_scripts);
370
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000371 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200372 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000373
374 /* functions */
375 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000376}
377#endif
378
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379
380/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 * Set an internal variable to a string value. Creates the variable if it does
382 * not already exist.
383 */
384 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100385set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000387 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000388 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389
390 val = vim_strsave(value);
391 if (val != NULL)
392 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000393 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000394 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000396 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000397 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 }
399 }
400}
401
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000402static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200403#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000404static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000405static char_u *redir_endp = NULL;
406static char_u *redir_varname = NULL;
407
408/*
409 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100410 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000411 * Returns OK if successfully completed the setup. FAIL otherwise.
412 */
413 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100414var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000415{
416 int save_emsg;
417 int err;
418 typval_T tv;
419
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000420 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000421 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000422 {
423 EMSG(_(e_invarg));
424 return FAIL;
425 }
426
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000427 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000428 redir_varname = vim_strsave(name);
429 if (redir_varname == NULL)
430 return FAIL;
431
432 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
433 if (redir_lval == NULL)
434 {
435 var_redir_stop();
436 return FAIL;
437 }
438
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000439 /* The output is stored in growarray "redir_ga" until redirection ends. */
440 ga_init2(&redir_ga, (int)sizeof(char), 500);
441
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000442 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100443 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000444 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000445 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
446 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200447 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000448 if (redir_endp != NULL && *redir_endp != NUL)
449 /* Trailing characters are present after the variable name */
450 EMSG(_(e_trailing));
451 else
452 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000453 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000454 var_redir_stop();
455 return FAIL;
456 }
457
458 /* check if we can write to the variable: set it to or append an empty
459 * string */
460 save_emsg = did_emsg;
461 did_emsg = FALSE;
462 tv.v_type = VAR_STRING;
463 tv.vval.v_string = (char_u *)"";
464 if (append)
465 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
466 else
467 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200468 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000469 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000470 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000471 if (err)
472 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000473 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000474 var_redir_stop();
475 return FAIL;
476 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000477
478 return OK;
479}
480
481/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000482 * Append "value[value_len]" to the variable set by var_redir_start().
483 * The actual appending is postponed until redirection ends, because the value
484 * appended may in fact be the string we write to, changing it may cause freed
485 * memory to be used:
486 * :redir => foo
487 * :let foo
488 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000489 */
490 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100491var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000492{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000493 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000494
495 if (redir_lval == NULL)
496 return;
497
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000498 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000499 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000500 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000501 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000502
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000503 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000504 {
505 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000506 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000507 }
508 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000509 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000510}
511
512/*
513 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000514 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000515 */
516 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100517var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000518{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000519 typval_T tv;
520
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200521 if (EVALCMD_BUSY)
522 {
523 redir_lval = NULL;
524 return;
525 }
526
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000527 if (redir_lval != NULL)
528 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000529 /* If there was no error: assign the text to the variable. */
530 if (redir_endp != NULL)
531 {
532 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
533 tv.v_type = VAR_STRING;
534 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200535 /* Call get_lval() again, if it's inside a Dict or List it may
536 * have changed. */
537 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100538 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200539 if (redir_endp != NULL && redir_lval->ll_name != NULL)
540 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
541 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000542 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000543
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000544 /* free the collected output */
545 vim_free(redir_ga.ga_data);
546 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000547
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000548 vim_free(redir_lval);
549 redir_lval = NULL;
550 }
551 vim_free(redir_varname);
552 redir_varname = NULL;
553}
554
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555# if defined(FEAT_MBYTE) || defined(PROTO)
556 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100557eval_charconvert(
558 char_u *enc_from,
559 char_u *enc_to,
560 char_u *fname_from,
561 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562{
563 int err = FALSE;
564
565 set_vim_var_string(VV_CC_FROM, enc_from, -1);
566 set_vim_var_string(VV_CC_TO, enc_to, -1);
567 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
568 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
569 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
570 err = TRUE;
571 set_vim_var_string(VV_CC_FROM, NULL, -1);
572 set_vim_var_string(VV_CC_TO, NULL, -1);
573 set_vim_var_string(VV_FNAME_IN, NULL, -1);
574 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
575
576 if (err)
577 return FAIL;
578 return OK;
579}
580# endif
581
582# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
583 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100584eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585{
586 int err = FALSE;
587
588 set_vim_var_string(VV_FNAME_IN, fname, -1);
589 set_vim_var_string(VV_CMDARG, args, -1);
590 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
591 err = TRUE;
592 set_vim_var_string(VV_FNAME_IN, NULL, -1);
593 set_vim_var_string(VV_CMDARG, NULL, -1);
594
595 if (err)
596 {
597 mch_remove(fname);
598 return FAIL;
599 }
600 return OK;
601}
602# endif
603
604# if defined(FEAT_DIFF) || defined(PROTO)
605 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100606eval_diff(
607 char_u *origfile,
608 char_u *newfile,
609 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610{
611 int err = FALSE;
612
613 set_vim_var_string(VV_FNAME_IN, origfile, -1);
614 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
615 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
616 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
617 set_vim_var_string(VV_FNAME_IN, NULL, -1);
618 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
619 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
620}
621
622 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100623eval_patch(
624 char_u *origfile,
625 char_u *difffile,
626 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627{
628 int err;
629
630 set_vim_var_string(VV_FNAME_IN, origfile, -1);
631 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
632 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
633 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
634 set_vim_var_string(VV_FNAME_IN, NULL, -1);
635 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
636 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
637}
638# endif
639
640/*
641 * Top level evaluation function, returning a boolean.
642 * Sets "error" to TRUE if there was an error.
643 * Return TRUE or FALSE.
644 */
645 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100646eval_to_bool(
647 char_u *arg,
648 int *error,
649 char_u **nextcmd,
650 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651{
Bram Moolenaar33570922005-01-25 22:26:29 +0000652 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200653 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654
655 if (skip)
656 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000657 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 else
660 {
661 *error = FALSE;
662 if (!skip)
663 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000664 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000665 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 }
667 }
668 if (skip)
669 --emsg_skip;
670
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200671 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672}
673
674/*
675 * Top level evaluation function, returning a string. If "skip" is TRUE,
676 * only parsing to "nextcmd" is done, without reporting errors. Return
677 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
678 */
679 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100680eval_to_string_skip(
681 char_u *arg,
682 char_u **nextcmd,
683 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684{
Bram Moolenaar33570922005-01-25 22:26:29 +0000685 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 char_u *retval;
687
688 if (skip)
689 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000690 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 retval = NULL;
692 else
693 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000694 retval = vim_strsave(get_tv_string(&tv));
695 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 }
697 if (skip)
698 --emsg_skip;
699
700 return retval;
701}
702
703/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000704 * Skip over an expression at "*pp".
705 * Return FAIL for an error, OK otherwise.
706 */
707 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100708skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000709{
Bram Moolenaar33570922005-01-25 22:26:29 +0000710 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000711
712 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000713 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000714}
715
716/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000718 * When "convert" is TRUE convert a List into a sequence of lines and convert
719 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 * Return pointer to allocated memory, or NULL for failure.
721 */
722 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100723eval_to_string(
724 char_u *arg,
725 char_u **nextcmd,
726 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727{
Bram Moolenaar33570922005-01-25 22:26:29 +0000728 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000730 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000731#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000732 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000735 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 retval = NULL;
737 else
738 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000739 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000740 {
741 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000742 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200743 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200744 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200745 if (tv.vval.v_list->lv_len > 0)
746 ga_append(&ga, NL);
747 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000748 ga_append(&ga, NUL);
749 retval = (char_u *)ga.ga_data;
750 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000751#ifdef FEAT_FLOAT
752 else if (convert && tv.v_type == VAR_FLOAT)
753 {
754 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
755 retval = vim_strsave(numbuf);
756 }
757#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000758 else
759 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000760 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 }
762
763 return retval;
764}
765
766/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000767 * Call eval_to_string() without using current local variables and using
768 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 */
770 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100771eval_to_string_safe(
772 char_u *arg,
773 char_u **nextcmd,
774 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775{
776 char_u *retval;
777 void *save_funccalp;
778
779 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000780 if (use_sandbox)
781 ++sandbox;
782 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000783 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000784 if (use_sandbox)
785 --sandbox;
786 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 restore_funccal(save_funccalp);
788 return retval;
789}
790
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791/*
792 * Top level evaluation function, returning a number.
793 * Evaluates "expr" silently.
794 * Returns -1 for an error.
795 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200796 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100797eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798{
Bram Moolenaar33570922005-01-25 22:26:29 +0000799 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200800 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000801 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802
803 ++emsg_off;
804
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000805 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 retval = -1;
807 else
808 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000809 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000810 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 }
812 --emsg_off;
813
814 return retval;
815}
816
Bram Moolenaara40058a2005-07-11 22:42:07 +0000817/*
818 * Prepare v: variable "idx" to be used.
819 * Save the current typeval in "save_tv".
820 * When not used yet add the variable to the v: hashtable.
821 */
822 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100823prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000824{
825 *save_tv = vimvars[idx].vv_tv;
826 if (vimvars[idx].vv_type == VAR_UNKNOWN)
827 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
828}
829
830/*
831 * Restore v: variable "idx" to typeval "save_tv".
832 * When no longer defined, remove the variable from the v: hashtable.
833 */
834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100835restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000836{
837 hashitem_T *hi;
838
Bram Moolenaara40058a2005-07-11 22:42:07 +0000839 vimvars[idx].vv_tv = *save_tv;
840 if (vimvars[idx].vv_type == VAR_UNKNOWN)
841 {
842 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
843 if (HASHITEM_EMPTY(hi))
844 EMSG2(_(e_intern2), "restore_vimvar()");
845 else
846 hash_remove(&vimvarht, hi);
847 }
848}
849
Bram Moolenaar3c56a962006-03-12 22:19:04 +0000850#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000851/*
852 * Evaluate an expression to a list with suggestions.
853 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000854 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000855 */
856 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100857eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000858{
859 typval_T save_val;
860 typval_T rettv;
861 list_T *list = NULL;
862 char_u *p = skipwhite(expr);
863
864 /* Set "v:val" to the bad word. */
865 prepare_vimvar(VV_VAL, &save_val);
866 vimvars[VV_VAL].vv_type = VAR_STRING;
867 vimvars[VV_VAL].vv_str = badword;
868 if (p_verbose == 0)
869 ++emsg_off;
870
871 if (eval1(&p, &rettv, TRUE) == OK)
872 {
873 if (rettv.v_type != VAR_LIST)
874 clear_tv(&rettv);
875 else
876 list = rettv.vval.v_list;
877 }
878
879 if (p_verbose == 0)
880 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000881 restore_vimvar(VV_VAL, &save_val);
882
883 return list;
884}
885
886/*
887 * "list" is supposed to contain two items: a word and a number. Return the
888 * word in "pp" and the number as the return value.
889 * Return -1 if anything isn't right.
890 * Used to get the good word and score from the eval_spell_expr() result.
891 */
892 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100893get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000894{
895 listitem_T *li;
896
897 li = list->lv_first;
898 if (li == NULL)
899 return -1;
900 *pp = get_tv_string(&li->li_tv);
901
902 li = li->li_next;
903 if (li == NULL)
904 return -1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200905 return (int)get_tv_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000906}
907#endif
908
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000909/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000910 * Top level evaluation function.
911 * Returns an allocated typval_T with the result.
912 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000913 */
914 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100915eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000916{
917 typval_T *tv;
918
919 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000920 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000921 {
922 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +0000923 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000924 }
925
926 return tv;
927}
928
929
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000931 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000932 * Uses argv[argc] for the function arguments. Only Number and String
933 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000934 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200936 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100937call_vim_function(
938 char_u *func,
939 int argc,
940 char_u **argv,
941 int safe, /* use the sandbox */
942 int str_arg_only, /* all arguments are strings */
943 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944{
Bram Moolenaar33570922005-01-25 22:26:29 +0000945 typval_T *argvars;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200946 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 int len;
948 int i;
949 int doesrange;
950 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000951 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000953 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000955 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956
957 for (i = 0; i < argc; i++)
958 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000959 /* Pass a NULL or empty argument as an empty string */
960 if (argv[i] == NULL || *argv[i] == NUL)
961 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000962 argvars[i].v_type = VAR_STRING;
963 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000964 continue;
965 }
966
Bram Moolenaar0cbba942012-07-25 16:47:03 +0200967 if (str_arg_only)
968 len = 0;
969 else
970 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100971 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 if (len != 0 && len == (int)STRLEN(argv[i]))
973 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000974 argvars[i].v_type = VAR_NUMBER;
975 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 }
977 else
978 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000979 argvars[i].v_type = VAR_STRING;
980 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 }
982 }
983
984 if (safe)
985 {
986 save_funccalp = save_funccal();
987 ++sandbox;
988 }
989
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000990 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaardf48fb42016-07-22 21:50:18 +0200991 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100993 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 if (safe)
995 {
996 --sandbox;
997 restore_funccal(save_funccalp);
998 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000999 vim_free(argvars);
1000
1001 if (ret == FAIL)
1002 clear_tv(rettv);
1003
1004 return ret;
1005}
1006
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001007/*
1008 * Call vimL function "func" and return the result as a number.
1009 * Returns -1 when calling the function fails.
1010 * Uses argv[argc] for the function arguments.
1011 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001012 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001013call_func_retnr(
1014 char_u *func,
1015 int argc,
1016 char_u **argv,
1017 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001018{
1019 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001020 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001021
1022 /* All arguments are passed as strings, no conversion to number. */
1023 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1024 return -1;
1025
1026 retval = get_tv_number_chk(&rettv, NULL);
1027 clear_tv(&rettv);
1028 return retval;
1029}
1030
1031#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1032 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1033
Bram Moolenaar4f688582007-07-24 12:34:30 +00001034# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001035/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001036 * Call vimL function "func" and return the result as a string.
1037 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001038 * Uses argv[argc] for the function arguments.
1039 */
1040 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001041call_func_retstr(
1042 char_u *func,
1043 int argc,
1044 char_u **argv,
1045 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001046{
1047 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001048 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001049
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001050 /* All arguments are passed as strings, no conversion to number. */
1051 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001052 return NULL;
1053
1054 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001055 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 return retval;
1057}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001058# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001059
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001060/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001061 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001062 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001063 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001064 */
1065 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001066call_func_retlist(
1067 char_u *func,
1068 int argc,
1069 char_u **argv,
1070 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001071{
1072 typval_T rettv;
1073
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001074 /* All arguments are passed as strings, no conversion to number. */
1075 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001076 return NULL;
1077
1078 if (rettv.v_type != VAR_LIST)
1079 {
1080 clear_tv(&rettv);
1081 return NULL;
1082 }
1083
1084 return rettv.vval.v_list;
1085}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086#endif
1087
Bram Moolenaar05159a02005-02-26 23:04:13 +00001088
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089#ifdef FEAT_FOLDING
1090/*
1091 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1092 * it in "*cp". Doesn't give error messages.
1093 */
1094 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001095eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096{
Bram Moolenaar33570922005-01-25 22:26:29 +00001097 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001098 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001100 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1101 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102
1103 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001104 if (use_sandbox)
1105 ++sandbox;
1106 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001108 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 retval = 0;
1110 else
1111 {
1112 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001113 if (tv.v_type == VAR_NUMBER)
1114 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001115 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 retval = 0;
1117 else
1118 {
1119 /* If the result is a string, check if there is a non-digit before
1120 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001121 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 if (!VIM_ISDIGIT(*s) && *s != '-')
1123 *cp = *s++;
1124 retval = atol((char *)s);
1125 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001126 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 }
1128 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001129 if (use_sandbox)
1130 --sandbox;
1131 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001133 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134}
1135#endif
1136
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001138 * ":let" list all variable values
1139 * ":let var1 var2" list variable values
1140 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001141 * ":let var += expr" assignment command.
1142 * ":let var -= expr" assignment command.
1143 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001144 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 */
1146 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001147ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148{
1149 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001150 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001151 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001153 int var_count = 0;
1154 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001155 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001156 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001157 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158
Bram Moolenaardb552d602006-03-23 22:59:57 +00001159 argend = skip_var_list(arg, &var_count, &semicolon);
1160 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001161 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001162 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1163 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001164 expr = skipwhite(argend);
1165 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1166 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001168 /*
1169 * ":let" without "=": list variables
1170 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001171 if (*arg == '[')
1172 EMSG(_(e_invarg));
1173 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001174 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001175 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001176 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001177 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001178 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001179 list_glob_vars(&first);
1180 list_buf_vars(&first);
1181 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001182#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001183 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001184#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001185 list_script_vars(&first);
1186 list_func_vars(&first);
1187 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 eap->nextcmd = check_nextcmd(arg);
1190 }
1191 else
1192 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001193 op[0] = '=';
1194 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001195 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001196 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001197 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1198 op[0] = *expr; /* +=, -= or .= */
1199 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001200 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001201 else
1202 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001203
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 if (eap->skip)
1205 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001206 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 if (eap->skip)
1208 {
1209 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001210 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 --emsg_skip;
1212 }
1213 else if (i != FAIL)
1214 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001215 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001216 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001217 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 }
1219 }
1220}
1221
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001222/*
1223 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1224 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001225 * When "nextchars" is not NULL it points to a string with characters that
1226 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1227 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001228 * Returns OK or FAIL;
1229 */
1230 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001231ex_let_vars(
1232 char_u *arg_start,
1233 typval_T *tv,
1234 int copy, /* copy values from "tv", don't move */
1235 int semicolon, /* from skip_var_list() */
1236 int var_count, /* from skip_var_list() */
1237 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001238{
1239 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001240 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001241 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001242 listitem_T *item;
1243 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001244
1245 if (*arg != '[')
1246 {
1247 /*
1248 * ":let var = expr" or ":for var in list"
1249 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001250 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001251 return FAIL;
1252 return OK;
1253 }
1254
1255 /*
1256 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1257 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001258 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001259 {
1260 EMSG(_(e_listreq));
1261 return FAIL;
1262 }
1263
1264 i = list_len(l);
1265 if (semicolon == 0 && var_count < i)
1266 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001267 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001268 return FAIL;
1269 }
1270 if (var_count - semicolon > i)
1271 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001272 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001273 return FAIL;
1274 }
1275
1276 item = l->lv_first;
1277 while (*arg != ']')
1278 {
1279 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001280 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001281 item = item->li_next;
1282 if (arg == NULL)
1283 return FAIL;
1284
1285 arg = skipwhite(arg);
1286 if (*arg == ';')
1287 {
1288 /* Put the rest of the list (may be empty) in the var after ';'.
1289 * Create a new list for this. */
1290 l = list_alloc();
1291 if (l == NULL)
1292 return FAIL;
1293 while (item != NULL)
1294 {
1295 list_append_tv(l, &item->li_tv);
1296 item = item->li_next;
1297 }
1298
1299 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001300 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001301 ltv.vval.v_list = l;
1302 l->lv_refcount = 1;
1303
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001304 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1305 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001306 clear_tv(&ltv);
1307 if (arg == NULL)
1308 return FAIL;
1309 break;
1310 }
1311 else if (*arg != ',' && *arg != ']')
1312 {
1313 EMSG2(_(e_intern2), "ex_let_vars()");
1314 return FAIL;
1315 }
1316 }
1317
1318 return OK;
1319}
1320
1321/*
1322 * Skip over assignable variable "var" or list of variables "[var, var]".
1323 * Used for ":let varvar = expr" and ":for varvar in expr".
1324 * For "[var, var]" increment "*var_count" for each variable.
1325 * for "[var, var; var]" set "semicolon".
1326 * Return NULL for an error.
1327 */
1328 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001329skip_var_list(
1330 char_u *arg,
1331 int *var_count,
1332 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001333{
1334 char_u *p, *s;
1335
1336 if (*arg == '[')
1337 {
1338 /* "[var, var]": find the matching ']'. */
1339 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001340 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001341 {
1342 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1343 s = skip_var_one(p);
1344 if (s == p)
1345 {
1346 EMSG2(_(e_invarg2), p);
1347 return NULL;
1348 }
1349 ++*var_count;
1350
1351 p = skipwhite(s);
1352 if (*p == ']')
1353 break;
1354 else if (*p == ';')
1355 {
1356 if (*semicolon == 1)
1357 {
1358 EMSG(_("Double ; in list of variables"));
1359 return NULL;
1360 }
1361 *semicolon = 1;
1362 }
1363 else if (*p != ',')
1364 {
1365 EMSG2(_(e_invarg2), p);
1366 return NULL;
1367 }
1368 }
1369 return p + 1;
1370 }
1371 else
1372 return skip_var_one(arg);
1373}
1374
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001375/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001376 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001377 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001378 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001379 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001380skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001381{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001382 if (*arg == '@' && arg[1] != NUL)
1383 return arg + 2;
1384 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1385 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001386}
1387
Bram Moolenaara7043832005-01-21 11:56:39 +00001388/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001389 * List variables for hashtab "ht" with prefix "prefix".
1390 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001391 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001392 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001393list_hashtable_vars(
1394 hashtab_T *ht,
1395 char_u *prefix,
1396 int empty,
1397 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001398{
Bram Moolenaar33570922005-01-25 22:26:29 +00001399 hashitem_T *hi;
1400 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001401 int todo;
1402
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001403 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001404 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1405 {
1406 if (!HASHITEM_EMPTY(hi))
1407 {
1408 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001409 di = HI2DI(hi);
1410 if (empty || di->di_tv.v_type != VAR_STRING
1411 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001412 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001413 }
1414 }
1415}
1416
1417/*
1418 * List global variables.
1419 */
1420 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001421list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001422{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001423 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001424}
1425
1426/*
1427 * List buffer variables.
1428 */
1429 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001430list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001431{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001432 char_u numbuf[NUMBUFLEN];
1433
Bram Moolenaar429fa852013-04-15 12:27:36 +02001434 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001435 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001436
1437 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001438 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
1439 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001440}
1441
1442/*
1443 * List window variables.
1444 */
1445 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001446list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001447{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001448 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001449 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001450}
1451
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001452#ifdef FEAT_WINDOWS
1453/*
1454 * List tab page variables.
1455 */
1456 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001457list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001458{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001459 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001460 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001461}
1462#endif
1463
Bram Moolenaara7043832005-01-21 11:56:39 +00001464/*
1465 * List Vim variables.
1466 */
1467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001468list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001469{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001470 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001471}
1472
1473/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001474 * List script-local variables, if there is a script.
1475 */
1476 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001477list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001478{
1479 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001480 list_hashtable_vars(&SCRIPT_VARS(current_SID),
1481 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001482}
1483
1484/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001485 * List variables in "arg".
1486 */
1487 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001488list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001489{
1490 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001491 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001492 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001493 char_u *name_start;
1494 char_u *arg_subsc;
1495 char_u *tofree;
1496 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001497
1498 while (!ends_excmd(*arg) && !got_int)
1499 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001500 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001501 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001502 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001503 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1504 {
1505 emsg_severe = TRUE;
1506 EMSG(_(e_trailing));
1507 break;
1508 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001509 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001510 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001511 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001512 /* get_name_len() takes care of expanding curly braces */
1513 name_start = name = arg;
1514 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1515 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001516 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001517 /* This is mainly to keep test 49 working: when expanding
1518 * curly braces fails overrule the exception error message. */
1519 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001520 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001521 emsg_severe = TRUE;
1522 EMSG2(_(e_invarg2), arg);
1523 break;
1524 }
1525 error = TRUE;
1526 }
1527 else
1528 {
1529 if (tofree != NULL)
1530 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001531 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001532 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001533 else
1534 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001535 /* handle d.key, l[idx], f(expr) */
1536 arg_subsc = arg;
1537 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001538 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001539 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001540 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001541 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001542 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001543 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001544 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001545 case 'g': list_glob_vars(first); break;
1546 case 'b': list_buf_vars(first); break;
1547 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001548#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001549 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001550#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001551 case 'v': list_vim_vars(first); break;
1552 case 's': list_script_vars(first); break;
1553 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001554 default:
1555 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001556 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001557 }
1558 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001559 {
1560 char_u numbuf[NUMBUFLEN];
1561 char_u *tf;
1562 int c;
1563 char_u *s;
1564
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001565 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001566 c = *arg;
1567 *arg = NUL;
1568 list_one_var_a((char_u *)"",
1569 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001570 tv.v_type,
1571 s == NULL ? (char_u *)"" : s,
1572 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001573 *arg = c;
1574 vim_free(tf);
1575 }
1576 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001577 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001578 }
1579 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001580
1581 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001582 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001583
1584 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001585 }
1586
1587 return arg;
1588}
1589
1590/*
1591 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1592 * Returns a pointer to the char just after the var name.
1593 * Returns NULL if there is an error.
1594 */
1595 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001596ex_let_one(
1597 char_u *arg, /* points to variable name */
1598 typval_T *tv, /* value to assign to variable */
1599 int copy, /* copy value from "tv" */
1600 char_u *endchars, /* valid chars after variable name or NULL */
1601 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001602{
1603 int c1;
1604 char_u *name;
1605 char_u *p;
1606 char_u *arg_end = NULL;
1607 int len;
1608 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001609 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001610
1611 /*
1612 * ":let $VAR = expr": Set environment variable.
1613 */
1614 if (*arg == '$')
1615 {
1616 /* Find the end of the name. */
1617 ++arg;
1618 name = arg;
1619 len = get_env_len(&arg);
1620 if (len == 0)
1621 EMSG2(_(e_invarg2), name - 1);
1622 else
1623 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001624 if (op != NULL && (*op == '+' || *op == '-'))
1625 EMSG2(_(e_letwrong), op);
1626 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001627 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001628 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001629 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001630 {
1631 c1 = name[len];
1632 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001633 p = get_tv_string_chk(tv);
1634 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001635 {
1636 int mustfree = FALSE;
1637 char_u *s = vim_getenv(name, &mustfree);
1638
1639 if (s != NULL)
1640 {
1641 p = tofree = concat_str(s, p);
1642 if (mustfree)
1643 vim_free(s);
1644 }
1645 }
1646 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001647 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001648 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001649 if (STRICMP(name, "HOME") == 0)
1650 init_homedir();
1651 else if (didset_vim && STRICMP(name, "VIM") == 0)
1652 didset_vim = FALSE;
1653 else if (didset_vimruntime
1654 && STRICMP(name, "VIMRUNTIME") == 0)
1655 didset_vimruntime = FALSE;
1656 arg_end = arg;
1657 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001658 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001659 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001660 }
1661 }
1662 }
1663
1664 /*
1665 * ":let &option = expr": Set option value.
1666 * ":let &l:option = expr": Set local option value.
1667 * ":let &g:option = expr": Set global option value.
1668 */
1669 else if (*arg == '&')
1670 {
1671 /* Find the end of the name. */
1672 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001673 if (p == NULL || (endchars != NULL
1674 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001675 EMSG(_(e_letunexp));
1676 else
1677 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001678 long n;
1679 int opt_type;
1680 long numval;
1681 char_u *stringval = NULL;
1682 char_u *s;
1683
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001684 c1 = *p;
1685 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001686
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001687 n = (long)get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001688 s = get_tv_string_chk(tv); /* != NULL if number or string */
1689 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001690 {
1691 opt_type = get_option_value(arg, &numval,
1692 &stringval, opt_flags);
1693 if ((opt_type == 1 && *op == '.')
1694 || (opt_type == 0 && *op != '.'))
1695 EMSG2(_(e_letwrong), op);
1696 else
1697 {
1698 if (opt_type == 1) /* number */
1699 {
1700 if (*op == '+')
1701 n = numval + n;
1702 else
1703 n = numval - n;
1704 }
1705 else if (opt_type == 0 && stringval != NULL) /* string */
1706 {
1707 s = concat_str(stringval, s);
1708 vim_free(stringval);
1709 stringval = s;
1710 }
1711 }
1712 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001713 if (s != NULL)
1714 {
1715 set_option_value(arg, n, s, opt_flags);
1716 arg_end = p;
1717 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001718 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001719 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001720 }
1721 }
1722
1723 /*
1724 * ":let @r = expr": Set register contents.
1725 */
1726 else if (*arg == '@')
1727 {
1728 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001729 if (op != NULL && (*op == '+' || *op == '-'))
1730 EMSG2(_(e_letwrong), op);
1731 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001732 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001733 EMSG(_(e_letunexp));
1734 else
1735 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001736 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001737 char_u *s;
1738
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001739 p = get_tv_string_chk(tv);
1740 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001741 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02001742 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001743 if (s != NULL)
1744 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001745 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001746 vim_free(s);
1747 }
1748 }
1749 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001750 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001751 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001752 arg_end = arg + 1;
1753 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001754 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001755 }
1756 }
1757
1758 /*
1759 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001760 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001761 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001762 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001763 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001764 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001765
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001766 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001767 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001768 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001769 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1770 EMSG(_(e_letunexp));
1771 else
1772 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001773 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001774 arg_end = p;
1775 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001776 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001777 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001778 }
1779
1780 else
1781 EMSG2(_(e_invarg2), arg);
1782
1783 return arg_end;
1784}
1785
1786/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00001787 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
1788 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001789 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001790check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001791{
1792 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
1793 {
1794 EMSG2(_(e_readonlyvar), arg);
1795 return TRUE;
1796 }
1797 return FALSE;
1798}
1799
1800/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001801 * Get an lval: variable, Dict item or List item that can be assigned a value
1802 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1803 * "name.key", "name.key[expr]" etc.
1804 * Indexing only works if "name" is an existing List or Dictionary.
1805 * "name" points to the start of the name.
1806 * If "rettv" is not NULL it points to the value to be assigned.
1807 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1808 * wrong; must end in space or cmd separator.
1809 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001810 * flags:
1811 * GLV_QUIET: do not give error messages
1812 * GLV_NO_AUTOLOAD: do not use script autoloading
1813 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001814 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001815 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001816 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001817 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001818 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001819get_lval(
1820 char_u *name,
1821 typval_T *rettv,
1822 lval_T *lp,
1823 int unlet,
1824 int skip,
1825 int flags, /* GLV_ values */
1826 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001827{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001828 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001829 char_u *expr_start, *expr_end;
1830 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001831 dictitem_T *v;
1832 typval_T var1;
1833 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001834 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001835 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001836 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001837 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001838 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001839 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001840
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001841 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001842 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001843
1844 if (skip)
1845 {
1846 /* When skipping just find the end of the name. */
1847 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001848 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001849 }
1850
1851 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001852 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001853 if (expr_start != NULL)
1854 {
1855 /* Don't expand the name when we already know there is an error. */
1856 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
1857 && *p != '[' && *p != '.')
1858 {
1859 EMSG(_(e_trailing));
1860 return NULL;
1861 }
1862
1863 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1864 if (lp->ll_exp_name == NULL)
1865 {
1866 /* Report an invalid expression in braces, unless the
1867 * expression evaluation has been cancelled due to an
1868 * aborting error, an interrupt, or an exception. */
1869 if (!aborting() && !quiet)
1870 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001871 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001872 EMSG2(_(e_invarg2), name);
1873 return NULL;
1874 }
1875 }
1876 lp->ll_name = lp->ll_exp_name;
1877 }
1878 else
1879 lp->ll_name = name;
1880
1881 /* Without [idx] or .key we are done. */
1882 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1883 return p;
1884
1885 cc = *p;
1886 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001887 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001888 if (v == NULL && !quiet)
1889 EMSG2(_(e_undefvar), lp->ll_name);
1890 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001891 if (v == NULL)
1892 return NULL;
1893
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001894 /*
1895 * Loop until no more [idx] or .key is following.
1896 */
Bram Moolenaar33570922005-01-25 22:26:29 +00001897 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001898 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001899 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001900 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1901 && !(lp->ll_tv->v_type == VAR_DICT
1902 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001903 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001904 if (!quiet)
1905 EMSG(_("E689: Can only index a List or Dictionary"));
1906 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001907 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001908 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001909 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001910 if (!quiet)
1911 EMSG(_("E708: [:] must come last"));
1912 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001913 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001914
Bram Moolenaar8c711452005-01-14 21:53:12 +00001915 len = -1;
1916 if (*p == '.')
1917 {
1918 key = p + 1;
1919 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1920 ;
1921 if (len == 0)
1922 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001923 if (!quiet)
1924 EMSG(_(e_emptykey));
1925 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001926 }
1927 p = key + len;
1928 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001929 else
1930 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001931 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001932 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001933 if (*p == ':')
1934 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001935 else
1936 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001937 empty1 = FALSE;
1938 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001939 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001940 if (get_tv_string_chk(&var1) == NULL)
1941 {
1942 /* not a number or string */
1943 clear_tv(&var1);
1944 return NULL;
1945 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001946 }
1947
1948 /* Optionally get the second index [ :expr]. */
1949 if (*p == ':')
1950 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001951 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001952 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001953 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001954 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001955 if (!empty1)
1956 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001957 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001958 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001959 if (rettv != NULL && (rettv->v_type != VAR_LIST
1960 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001961 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001962 if (!quiet)
1963 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00001964 if (!empty1)
1965 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001966 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001967 }
1968 p = skipwhite(p + 1);
1969 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001970 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001971 else
1972 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001973 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001974 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
1975 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001976 if (!empty1)
1977 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001978 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001979 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001980 if (get_tv_string_chk(&var2) == NULL)
1981 {
1982 /* not a number or string */
1983 if (!empty1)
1984 clear_tv(&var1);
1985 clear_tv(&var2);
1986 return NULL;
1987 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001988 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001989 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001990 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001991 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001992 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001993
Bram Moolenaar8c711452005-01-14 21:53:12 +00001994 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001995 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001996 if (!quiet)
1997 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00001998 if (!empty1)
1999 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002000 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002001 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002002 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002003 }
2004
2005 /* Skip to past ']'. */
2006 ++p;
2007 }
2008
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002009 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002010 {
2011 if (len == -1)
2012 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002013 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002014 key = get_tv_string_chk(&var1); /* is number or string */
2015 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002016 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002017 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002018 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002019 }
2020 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002021 lp->ll_list = NULL;
2022 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002023 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002024
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002025 /* When assigning to a scope dictionary check that a function and
2026 * variable name is valid (only variable name unless it is l: or
2027 * g: dictionary). Disallow overwriting a builtin function. */
2028 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002029 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002030 int prevval;
2031 int wrong;
2032
2033 if (len != -1)
2034 {
2035 prevval = key[len];
2036 key[len] = NUL;
2037 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002038 else
2039 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002040 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2041 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002042 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002043 || !valid_varname(key);
2044 if (len != -1)
2045 key[len] = prevval;
2046 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002047 return NULL;
2048 }
2049
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002050 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002051 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002052 /* Can't add "v:" variable. */
2053 if (lp->ll_dict == &vimvardict)
2054 {
2055 EMSG2(_(e_illvar), name);
2056 return NULL;
2057 }
2058
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002059 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002060 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002061 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002062 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002063 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002064 if (len == -1)
2065 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002066 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002067 }
2068 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002069 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002070 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002071 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002072 if (len == -1)
2073 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002074 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002075 p = NULL;
2076 break;
2077 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002078 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002079 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002080 return NULL;
2081
Bram Moolenaar8c711452005-01-14 21:53:12 +00002082 if (len == -1)
2083 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002084 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002085 }
2086 else
2087 {
2088 /*
2089 * Get the number and item for the only or first index of the List.
2090 */
2091 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002092 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002093 else
2094 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002095 lp->ll_n1 = (long)get_tv_number(&var1);
2096 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002097 clear_tv(&var1);
2098 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002099 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002100 lp->ll_list = lp->ll_tv->vval.v_list;
2101 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2102 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002103 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002104 if (lp->ll_n1 < 0)
2105 {
2106 lp->ll_n1 = 0;
2107 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2108 }
2109 }
2110 if (lp->ll_li == NULL)
2111 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002112 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002113 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002114 if (!quiet)
2115 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002116 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002117 }
2118
2119 /*
2120 * May need to find the item or absolute index for the second
2121 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002122 * When no index given: "lp->ll_empty2" is TRUE.
2123 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002124 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002125 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002126 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002127 lp->ll_n2 = (long)get_tv_number(&var2);
2128 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002129 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002130 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002131 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002132 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002133 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002134 {
2135 if (!quiet)
2136 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002137 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002138 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002139 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002140 }
2141
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002142 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2143 if (lp->ll_n1 < 0)
2144 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2145 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002146 {
2147 if (!quiet)
2148 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002149 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002150 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002151 }
2152
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002153 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002154 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002155 }
2156
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002157 return p;
2158}
2159
2160/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002161 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002162 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002163 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002164clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002165{
2166 vim_free(lp->ll_exp_name);
2167 vim_free(lp->ll_newkey);
2168}
2169
2170/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002171 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002172 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002173 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002174 */
2175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002176set_var_lval(
2177 lval_T *lp,
2178 char_u *endp,
2179 typval_T *rettv,
2180 int copy,
2181 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002182{
2183 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002184 listitem_T *ri;
2185 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002186
2187 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002188 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002189 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002190 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002191 cc = *endp;
2192 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002193 if (op != NULL && *op != '=')
2194 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002195 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002196
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002197 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002198 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002199 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002200 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002201 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002202 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)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002207 set_var(lp->ll_name, &tv, FALSE);
2208 clear_tv(&tv);
2209 }
2210 }
2211 else
2212 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002213 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002215 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002216 else if (tv_check_lock(lp->ll_newkey == NULL
2217 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002218 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002219 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002220 else if (lp->ll_range)
2221 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002222 listitem_T *ll_li = lp->ll_li;
2223 int ll_n1 = lp->ll_n1;
2224
2225 /*
2226 * Check whether any of the list items is locked
2227 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002228 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002229 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002230 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002231 return;
2232 ri = ri->li_next;
2233 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2234 break;
2235 ll_li = ll_li->li_next;
2236 ++ll_n1;
2237 }
2238
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002239 /*
2240 * Assign the List values to the list items.
2241 */
2242 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002243 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002244 if (op != NULL && *op != '=')
2245 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2246 else
2247 {
2248 clear_tv(&lp->ll_li->li_tv);
2249 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2250 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002251 ri = ri->li_next;
2252 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2253 break;
2254 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002255 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002256 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002257 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002258 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002259 ri = NULL;
2260 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002261 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002262 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002263 lp->ll_li = lp->ll_li->li_next;
2264 ++lp->ll_n1;
2265 }
2266 if (ri != NULL)
2267 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002268 else if (lp->ll_empty2
2269 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002270 : lp->ll_n1 != lp->ll_n2)
2271 EMSG(_("E711: List value has not enough items"));
2272 }
2273 else
2274 {
2275 /*
2276 * Assign to a List or Dictionary item.
2277 */
2278 if (lp->ll_newkey != NULL)
2279 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002280 if (op != NULL && *op != '=')
2281 {
2282 EMSG2(_(e_letwrong), op);
2283 return;
2284 }
2285
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002286 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002287 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002288 if (di == NULL)
2289 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002290 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2291 {
2292 vim_free(di);
2293 return;
2294 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002295 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002296 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002297 else if (op != NULL && *op != '=')
2298 {
2299 tv_op(lp->ll_tv, rettv, op);
2300 return;
2301 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002302 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002303 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002304
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002305 /*
2306 * Assign the value to the variable or list item.
2307 */
2308 if (copy)
2309 copy_tv(rettv, lp->ll_tv);
2310 else
2311 {
2312 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002313 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002314 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315 }
2316 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002317}
2318
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002319/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002320 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2321 * Returns OK or FAIL.
2322 */
2323 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002324tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002325{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002326 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002327 char_u numbuf[NUMBUFLEN];
2328 char_u *s;
2329
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002330 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2331 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2332 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002333 {
2334 switch (tv1->v_type)
2335 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002336 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002337 case VAR_DICT:
2338 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002339 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002340 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002341 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002342 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002343 break;
2344
2345 case VAR_LIST:
2346 if (*op != '+' || tv2->v_type != VAR_LIST)
2347 break;
2348 /* List += List */
2349 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2350 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2351 return OK;
2352
2353 case VAR_NUMBER:
2354 case VAR_STRING:
2355 if (tv2->v_type == VAR_LIST)
2356 break;
2357 if (*op == '+' || *op == '-')
2358 {
2359 /* nr += nr or nr -= nr*/
2360 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002361#ifdef FEAT_FLOAT
2362 if (tv2->v_type == VAR_FLOAT)
2363 {
2364 float_T f = n;
2365
2366 if (*op == '+')
2367 f += tv2->vval.v_float;
2368 else
2369 f -= tv2->vval.v_float;
2370 clear_tv(tv1);
2371 tv1->v_type = VAR_FLOAT;
2372 tv1->vval.v_float = f;
2373 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002374 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002375#endif
2376 {
2377 if (*op == '+')
2378 n += get_tv_number(tv2);
2379 else
2380 n -= get_tv_number(tv2);
2381 clear_tv(tv1);
2382 tv1->v_type = VAR_NUMBER;
2383 tv1->vval.v_number = n;
2384 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002385 }
2386 else
2387 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002388 if (tv2->v_type == VAR_FLOAT)
2389 break;
2390
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002391 /* str .= str */
2392 s = get_tv_string(tv1);
2393 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2394 clear_tv(tv1);
2395 tv1->v_type = VAR_STRING;
2396 tv1->vval.v_string = s;
2397 }
2398 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002399
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002400 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002401#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002402 {
2403 float_T f;
2404
2405 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2406 && tv2->v_type != VAR_NUMBER
2407 && tv2->v_type != VAR_STRING))
2408 break;
2409 if (tv2->v_type == VAR_FLOAT)
2410 f = tv2->vval.v_float;
2411 else
2412 f = get_tv_number(tv2);
2413 if (*op == '+')
2414 tv1->vval.v_float += f;
2415 else
2416 tv1->vval.v_float -= f;
2417 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002418#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002419 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002420 }
2421 }
2422
2423 EMSG2(_(e_letwrong), op);
2424 return FAIL;
2425}
2426
2427/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002428 * Evaluate the expression used in a ":for var in expr" command.
2429 * "arg" points to "var".
2430 * Set "*errp" to TRUE for an error, FALSE otherwise;
2431 * Return a pointer that holds the info. Null when there is an error.
2432 */
2433 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002434eval_for_line(
2435 char_u *arg,
2436 int *errp,
2437 char_u **nextcmdp,
2438 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002439{
Bram Moolenaar33570922005-01-25 22:26:29 +00002440 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002441 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002442 typval_T tv;
2443 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002444
2445 *errp = TRUE; /* default: there is an error */
2446
Bram Moolenaar33570922005-01-25 22:26:29 +00002447 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002448 if (fi == NULL)
2449 return NULL;
2450
2451 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2452 if (expr == NULL)
2453 return fi;
2454
2455 expr = skipwhite(expr);
2456 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2457 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002458 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002459 return fi;
2460 }
2461
2462 if (skip)
2463 ++emsg_skip;
2464 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2465 {
2466 *errp = FALSE;
2467 if (!skip)
2468 {
2469 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002470 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002471 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002472 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002473 clear_tv(&tv);
2474 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002475 else if (l == NULL)
2476 {
2477 /* a null list is like an empty list: do nothing */
2478 clear_tv(&tv);
2479 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002480 else
2481 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002482 /* No need to increment the refcount, it's already set for the
2483 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002484 fi->fi_list = l;
2485 list_add_watch(l, &fi->fi_lw);
2486 fi->fi_lw.lw_item = l->lv_first;
2487 }
2488 }
2489 }
2490 if (skip)
2491 --emsg_skip;
2492
2493 return fi;
2494}
2495
2496/*
2497 * Use the first item in a ":for" list. Advance to the next.
2498 * Assign the values to the variable (list). "arg" points to the first one.
2499 * Return TRUE when a valid item was found, FALSE when at end of list or
2500 * something wrong.
2501 */
2502 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002503next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002504{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002505 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002506 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002507 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002508
2509 item = fi->fi_lw.lw_item;
2510 if (item == NULL)
2511 result = FALSE;
2512 else
2513 {
2514 fi->fi_lw.lw_item = item->li_next;
2515 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2516 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2517 }
2518 return result;
2519}
2520
2521/*
2522 * Free the structure used to store info used by ":for".
2523 */
2524 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002525free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002526{
Bram Moolenaar33570922005-01-25 22:26:29 +00002527 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002528
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002529 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002530 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002531 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002532 list_unref(fi->fi_list);
2533 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002534 vim_free(fi);
2535}
2536
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2538
2539 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002540set_context_for_expression(
2541 expand_T *xp,
2542 char_u *arg,
2543 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544{
2545 int got_eq = FALSE;
2546 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002547 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002549 if (cmdidx == CMD_let)
2550 {
2551 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002552 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002553 {
2554 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002555 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002556 {
2557 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002558 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002559 if (vim_iswhite(*p))
2560 break;
2561 }
2562 return;
2563 }
2564 }
2565 else
2566 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2567 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 while ((xp->xp_pattern = vim_strpbrk(arg,
2569 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2570 {
2571 c = *xp->xp_pattern;
2572 if (c == '&')
2573 {
2574 c = xp->xp_pattern[1];
2575 if (c == '&')
2576 {
2577 ++xp->xp_pattern;
2578 xp->xp_context = cmdidx != CMD_let || got_eq
2579 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2580 }
2581 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002582 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002584 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2585 xp->xp_pattern += 2;
2586
2587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 }
2589 else if (c == '$')
2590 {
2591 /* environment variable */
2592 xp->xp_context = EXPAND_ENV_VARS;
2593 }
2594 else if (c == '=')
2595 {
2596 got_eq = TRUE;
2597 xp->xp_context = EXPAND_EXPRESSION;
2598 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002599 else if (c == '#'
2600 && xp->xp_context == EXPAND_EXPRESSION)
2601 {
2602 /* Autoload function/variable contains '#'. */
2603 break;
2604 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002605 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 && xp->xp_context == EXPAND_FUNCTIONS
2607 && vim_strchr(xp->xp_pattern, '(') == NULL)
2608 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002609 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 break;
2611 }
2612 else if (cmdidx != CMD_let || got_eq)
2613 {
2614 if (c == '"') /* string */
2615 {
2616 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2617 if (c == '\\' && xp->xp_pattern[1] != NUL)
2618 ++xp->xp_pattern;
2619 xp->xp_context = EXPAND_NOTHING;
2620 }
2621 else if (c == '\'') /* literal string */
2622 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002623 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2625 /* skip */ ;
2626 xp->xp_context = EXPAND_NOTHING;
2627 }
2628 else if (c == '|')
2629 {
2630 if (xp->xp_pattern[1] == '|')
2631 {
2632 ++xp->xp_pattern;
2633 xp->xp_context = EXPAND_EXPRESSION;
2634 }
2635 else
2636 xp->xp_context = EXPAND_COMMANDS;
2637 }
2638 else
2639 xp->xp_context = EXPAND_EXPRESSION;
2640 }
2641 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002642 /* Doesn't look like something valid, expand as an expression
2643 * anyway. */
2644 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 arg = xp->xp_pattern;
2646 if (*arg != NUL)
2647 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2648 /* skip */ ;
2649 }
2650 xp->xp_pattern = arg;
2651}
2652
2653#endif /* FEAT_CMDL_COMPL */
2654
2655/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 * ":unlet[!] var1 ... " command.
2657 */
2658 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002659ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002661 ex_unletlock(eap, eap->arg, 0);
2662}
2663
2664/*
2665 * ":lockvar" and ":unlockvar" commands
2666 */
2667 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002668ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002669{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002671 int deep = 2;
2672
2673 if (eap->forceit)
2674 deep = -1;
2675 else if (vim_isdigit(*arg))
2676 {
2677 deep = getdigits(&arg);
2678 arg = skipwhite(arg);
2679 }
2680
2681 ex_unletlock(eap, arg, deep);
2682}
2683
2684/*
2685 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2686 */
2687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002688ex_unletlock(
2689 exarg_T *eap,
2690 char_u *argstart,
2691 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002692{
2693 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002696 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697
2698 do
2699 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002701 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002702 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 if (lv.ll_name == NULL)
2704 error = TRUE; /* error but continue parsing */
2705 if (name_end == NULL || (!vim_iswhite(*name_end)
2706 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 if (name_end != NULL)
2709 {
2710 emsg_severe = TRUE;
2711 EMSG(_(e_trailing));
2712 }
2713 if (!(eap->skip || error))
2714 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 break;
2716 }
2717
2718 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002719 {
2720 if (eap->cmdidx == CMD_unlet)
2721 {
2722 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2723 error = TRUE;
2724 }
2725 else
2726 {
2727 if (do_lock_var(&lv, name_end, deep,
2728 eap->cmdidx == CMD_lockvar) == FAIL)
2729 error = TRUE;
2730 }
2731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 if (!eap->skip)
2734 clear_lval(&lv);
2735
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 arg = skipwhite(name_end);
2737 } while (!ends_excmd(*arg));
2738
2739 eap->nextcmd = check_nextcmd(arg);
2740}
2741
Bram Moolenaar8c711452005-01-14 21:53:12 +00002742 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002743do_unlet_var(
2744 lval_T *lp,
2745 char_u *name_end,
2746 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 int ret = OK;
2749 int cc;
2750
2751 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 cc = *name_end;
2754 *name_end = NUL;
2755
2756 /* Normal name or expanded name. */
2757 if (check_changedtick(lp->ll_name))
2758 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002759 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002762 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002763 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002764 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002765 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002766 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002767 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002768 else if (lp->ll_range)
2769 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002770 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002771 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01002772 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002773
2774 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
2775 {
2776 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02002777 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002778 return FAIL;
2779 ll_li = li;
2780 ++ll_n1;
2781 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782
2783 /* Delete a range of List items. */
2784 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2785 {
2786 li = lp->ll_li->li_next;
2787 listitem_remove(lp->ll_list, lp->ll_li);
2788 lp->ll_li = li;
2789 ++lp->ll_n1;
2790 }
2791 }
2792 else
2793 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002795 /* unlet a List item. */
2796 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002798 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002799 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 }
2801
2802 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002803}
2804
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805/*
2806 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002807 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 */
2809 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002810do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811{
Bram Moolenaar33570922005-01-25 22:26:29 +00002812 hashtab_T *ht;
2813 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002814 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002815 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002816 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817
Bram Moolenaar33570922005-01-25 22:26:29 +00002818 ht = find_var_ht(name, &varname);
2819 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002821 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002822 if (d == NULL)
2823 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002824 if (ht == &globvarht)
2825 d = &globvardict;
2826 else if (ht == &compat_hashtab)
2827 d = &vimvardict;
2828 else
2829 {
2830 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2831 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2832 }
2833 if (d == NULL)
2834 {
2835 EMSG2(_(e_intern2), "do_unlet()");
2836 return FAIL;
2837 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002838 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002839 hi = hash_find(ht, varname);
2840 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00002841 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002842 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02002843 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002844 || var_check_ro(di->di_flags, name, FALSE)
2845 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01002846 return FAIL;
2847
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002848 delete_var(ht, hi);
2849 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00002850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002852 if (forceit)
2853 return OK;
2854 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 return FAIL;
2856}
2857
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002858/*
2859 * Lock or unlock variable indicated by "lp".
2860 * "deep" is the levels to go (-1 for unlimited);
2861 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2862 */
2863 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002864do_lock_var(
2865 lval_T *lp,
2866 char_u *name_end,
2867 int deep,
2868 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002869{
2870 int ret = OK;
2871 int cc;
2872 dictitem_T *di;
2873
2874 if (deep == 0) /* nothing to do */
2875 return OK;
2876
2877 if (lp->ll_tv == NULL)
2878 {
2879 cc = *name_end;
2880 *name_end = NUL;
2881
2882 /* Normal name or expanded name. */
2883 if (check_changedtick(lp->ll_name))
2884 ret = FAIL;
2885 else
2886 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002887 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002888 if (di == NULL)
2889 ret = FAIL;
2890 else
2891 {
2892 if (lock)
2893 di->di_flags |= DI_FLAGS_LOCK;
2894 else
2895 di->di_flags &= ~DI_FLAGS_LOCK;
2896 item_lock(&di->di_tv, deep, lock);
2897 }
2898 }
2899 *name_end = cc;
2900 }
2901 else if (lp->ll_range)
2902 {
2903 listitem_T *li = lp->ll_li;
2904
2905 /* (un)lock a range of List items. */
2906 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2907 {
2908 item_lock(&li->li_tv, deep, lock);
2909 li = li->li_next;
2910 ++lp->ll_n1;
2911 }
2912 }
2913 else if (lp->ll_list != NULL)
2914 /* (un)lock a List item. */
2915 item_lock(&lp->ll_li->li_tv, deep, lock);
2916 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02002917 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002918 item_lock(&lp->ll_di->di_tv, deep, lock);
2919
2920 return ret;
2921}
2922
2923/*
2924 * Lock or unlock an item. "deep" is nr of levels to go.
2925 */
2926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002927item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002928{
2929 static int recurse = 0;
2930 list_T *l;
2931 listitem_T *li;
2932 dict_T *d;
2933 hashitem_T *hi;
2934 int todo;
2935
2936 if (recurse >= DICT_MAXNEST)
2937 {
2938 EMSG(_("E743: variable nested too deep for (un)lock"));
2939 return;
2940 }
2941 if (deep == 0)
2942 return;
2943 ++recurse;
2944
2945 /* lock/unlock the item itself */
2946 if (lock)
2947 tv->v_lock |= VAR_LOCKED;
2948 else
2949 tv->v_lock &= ~VAR_LOCKED;
2950
2951 switch (tv->v_type)
2952 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01002953 case VAR_UNKNOWN:
2954 case VAR_NUMBER:
2955 case VAR_STRING:
2956 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002957 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002958 case VAR_FLOAT:
2959 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002960 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002961 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002962 break;
2963
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002964 case VAR_LIST:
2965 if ((l = tv->vval.v_list) != NULL)
2966 {
2967 if (lock)
2968 l->lv_lock |= VAR_LOCKED;
2969 else
2970 l->lv_lock &= ~VAR_LOCKED;
2971 if (deep < 0 || deep > 1)
2972 /* recursive: lock/unlock the items the List contains */
2973 for (li = l->lv_first; li != NULL; li = li->li_next)
2974 item_lock(&li->li_tv, deep - 1, lock);
2975 }
2976 break;
2977 case VAR_DICT:
2978 if ((d = tv->vval.v_dict) != NULL)
2979 {
2980 if (lock)
2981 d->dv_lock |= VAR_LOCKED;
2982 else
2983 d->dv_lock &= ~VAR_LOCKED;
2984 if (deep < 0 || deep > 1)
2985 {
2986 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002987 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002988 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2989 {
2990 if (!HASHITEM_EMPTY(hi))
2991 {
2992 --todo;
2993 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
2994 }
2995 }
2996 }
2997 }
2998 }
2999 --recurse;
3000}
3001
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3003/*
3004 * Delete all "menutrans_" variables.
3005 */
3006 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003007del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008{
Bram Moolenaar33570922005-01-25 22:26:29 +00003009 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003010 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011
Bram Moolenaar33570922005-01-25 22:26:29 +00003012 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003013 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003014 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003015 {
3016 if (!HASHITEM_EMPTY(hi))
3017 {
3018 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003019 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3020 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003021 }
3022 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003023 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024}
3025#endif
3026
3027#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3028
3029/*
3030 * Local string buffer for the next two functions to store a variable name
3031 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3032 * get_user_var_name().
3033 */
3034
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003035static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036
3037static char_u *varnamebuf = NULL;
3038static int varnamebuflen = 0;
3039
3040/*
3041 * Function to concatenate a prefix and a variable name.
3042 */
3043 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003044cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045{
3046 int len;
3047
3048 len = (int)STRLEN(name) + 3;
3049 if (len > varnamebuflen)
3050 {
3051 vim_free(varnamebuf);
3052 len += 10; /* some additional space */
3053 varnamebuf = alloc(len);
3054 if (varnamebuf == NULL)
3055 {
3056 varnamebuflen = 0;
3057 return NULL;
3058 }
3059 varnamebuflen = len;
3060 }
3061 *varnamebuf = prefix;
3062 varnamebuf[1] = ':';
3063 STRCPY(varnamebuf + 2, name);
3064 return varnamebuf;
3065}
3066
3067/*
3068 * Function given to ExpandGeneric() to obtain the list of user defined
3069 * (global/buffer/window/built-in) variable names.
3070 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003072get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003074 static long_u gdone;
3075 static long_u bdone;
3076 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003077#ifdef FEAT_WINDOWS
3078 static long_u tdone;
3079#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003080 static int vidx;
3081 static hashitem_T *hi;
3082 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083
3084 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003085 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003086 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003087#ifdef FEAT_WINDOWS
3088 tdone = 0;
3089#endif
3090 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003091
3092 /* Global variables */
3093 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003095 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003096 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003097 else
3098 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003099 while (HASHITEM_EMPTY(hi))
3100 ++hi;
3101 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3102 return cat_prefix_varname('g', hi->hi_key);
3103 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003105
3106 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003107 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003108 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003110 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003111 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003112 else
3113 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003114 while (HASHITEM_EMPTY(hi))
3115 ++hi;
3116 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003118 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003120 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 return (char_u *)"b:changedtick";
3122 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003123
3124 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003125 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003126 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003128 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003129 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003130 else
3131 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003132 while (HASHITEM_EMPTY(hi))
3133 ++hi;
3134 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003136
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003137#ifdef FEAT_WINDOWS
3138 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003139 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003140 if (tdone < ht->ht_used)
3141 {
3142 if (tdone++ == 0)
3143 hi = ht->ht_array;
3144 else
3145 ++hi;
3146 while (HASHITEM_EMPTY(hi))
3147 ++hi;
3148 return cat_prefix_varname('t', hi->hi_key);
3149 }
3150#endif
3151
Bram Moolenaar33570922005-01-25 22:26:29 +00003152 /* v: variables */
3153 if (vidx < VV_LEN)
3154 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155
3156 vim_free(varnamebuf);
3157 varnamebuf = NULL;
3158 varnamebuflen = 0;
3159 return NULL;
3160}
3161
3162#endif /* FEAT_CMDL_COMPL */
3163
3164/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003165 * Return TRUE if "pat" matches "text".
3166 * Does not use 'cpo' and always uses 'magic'.
3167 */
3168 static int
3169pattern_match(char_u *pat, char_u *text, int ic)
3170{
3171 int matches = FALSE;
3172 char_u *save_cpo;
3173 regmatch_T regmatch;
3174
3175 /* avoid 'l' flag in 'cpoptions' */
3176 save_cpo = p_cpo;
3177 p_cpo = (char_u *)"";
3178 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3179 if (regmatch.regprog != NULL)
3180 {
3181 regmatch.rm_ic = ic;
3182 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3183 vim_regfree(regmatch.regprog);
3184 }
3185 p_cpo = save_cpo;
3186 return matches;
3187}
3188
3189/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 * types for expressions.
3191 */
3192typedef enum
3193{
3194 TYPE_UNKNOWN = 0
3195 , TYPE_EQUAL /* == */
3196 , TYPE_NEQUAL /* != */
3197 , TYPE_GREATER /* > */
3198 , TYPE_GEQUAL /* >= */
3199 , TYPE_SMALLER /* < */
3200 , TYPE_SEQUAL /* <= */
3201 , TYPE_MATCH /* =~ */
3202 , TYPE_NOMATCH /* !~ */
3203} exptype_T;
3204
3205/*
3206 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003207 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3209 */
3210
3211/*
3212 * Handle zero level expression.
3213 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003214 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003215 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 * Return OK or FAIL.
3217 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003218 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003219eval0(
3220 char_u *arg,
3221 typval_T *rettv,
3222 char_u **nextcmd,
3223 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224{
3225 int ret;
3226 char_u *p;
3227
3228 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003229 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 if (ret == FAIL || !ends_excmd(*p))
3231 {
3232 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003233 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 /*
3235 * Report the invalid expression unless the expression evaluation has
3236 * been cancelled due to an aborting error, an interrupt, or an
3237 * exception.
3238 */
3239 if (!aborting())
3240 EMSG2(_(e_invexpr2), arg);
3241 ret = FAIL;
3242 }
3243 if (nextcmd != NULL)
3244 *nextcmd = check_nextcmd(p);
3245
3246 return ret;
3247}
3248
3249/*
3250 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003251 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 *
3253 * "arg" must point to the first non-white of the expression.
3254 * "arg" is advanced to the next non-white after the recognized expression.
3255 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003256 * Note: "rettv.v_lock" is not set.
3257 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 * Return OK or FAIL.
3259 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003260 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003261eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262{
3263 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003264 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265
3266 /*
3267 * Get the first variable.
3268 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003269 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 return FAIL;
3271
3272 if ((*arg)[0] == '?')
3273 {
3274 result = FALSE;
3275 if (evaluate)
3276 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003277 int error = FALSE;
3278
3279 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003281 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003282 if (error)
3283 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 }
3285
3286 /*
3287 * Get the second variable.
3288 */
3289 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003290 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 return FAIL;
3292
3293 /*
3294 * Check for the ":".
3295 */
3296 if ((*arg)[0] != ':')
3297 {
3298 EMSG(_("E109: Missing ':' after '?'"));
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
3304 /*
3305 * Get the third variable.
3306 */
3307 *arg = skipwhite(*arg + 1);
3308 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3309 {
3310 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003311 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 return FAIL;
3313 }
3314 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003315 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 }
3317
3318 return OK;
3319}
3320
3321/*
3322 * Handle first level expression:
3323 * expr2 || expr2 || expr2 logical OR
3324 *
3325 * "arg" must point to the first non-white of the expression.
3326 * "arg" is advanced to the next non-white after the recognized expression.
3327 *
3328 * Return OK or FAIL.
3329 */
3330 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003331eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332{
Bram Moolenaar33570922005-01-25 22:26:29 +00003333 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 long result;
3335 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003336 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337
3338 /*
3339 * Get the first variable.
3340 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003341 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 return FAIL;
3343
3344 /*
3345 * Repeat until there is no following "||".
3346 */
3347 first = TRUE;
3348 result = FALSE;
3349 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3350 {
3351 if (evaluate && first)
3352 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003353 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003355 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003356 if (error)
3357 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 first = FALSE;
3359 }
3360
3361 /*
3362 * Get the second variable.
3363 */
3364 *arg = skipwhite(*arg + 2);
3365 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3366 return FAIL;
3367
3368 /*
3369 * Compute the result.
3370 */
3371 if (evaluate && !result)
3372 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003373 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003375 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003376 if (error)
3377 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 }
3379 if (evaluate)
3380 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003381 rettv->v_type = VAR_NUMBER;
3382 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 }
3384 }
3385
3386 return OK;
3387}
3388
3389/*
3390 * Handle second level expression:
3391 * expr3 && expr3 && expr3 logical AND
3392 *
3393 * "arg" must point to the first non-white of the expression.
3394 * "arg" is advanced to the next non-white after the recognized expression.
3395 *
3396 * Return OK or FAIL.
3397 */
3398 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003399eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400{
Bram Moolenaar33570922005-01-25 22:26:29 +00003401 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 long result;
3403 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003404 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405
3406 /*
3407 * Get the first variable.
3408 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003409 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 return FAIL;
3411
3412 /*
3413 * Repeat until there is no following "&&".
3414 */
3415 first = TRUE;
3416 result = TRUE;
3417 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3418 {
3419 if (evaluate && first)
3420 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003421 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003423 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003424 if (error)
3425 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 first = FALSE;
3427 }
3428
3429 /*
3430 * Get the second variable.
3431 */
3432 *arg = skipwhite(*arg + 2);
3433 if (eval4(arg, &var2, evaluate && result) == FAIL)
3434 return FAIL;
3435
3436 /*
3437 * Compute the result.
3438 */
3439 if (evaluate && result)
3440 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003441 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003443 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003444 if (error)
3445 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 }
3447 if (evaluate)
3448 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003449 rettv->v_type = VAR_NUMBER;
3450 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 }
3452 }
3453
3454 return OK;
3455}
3456
3457/*
3458 * Handle third level expression:
3459 * var1 == var2
3460 * var1 =~ var2
3461 * var1 != var2
3462 * var1 !~ var2
3463 * var1 > var2
3464 * var1 >= var2
3465 * var1 < var2
3466 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003467 * var1 is var2
3468 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 *
3470 * "arg" must point to the first non-white of the expression.
3471 * "arg" is advanced to the next non-white after the recognized expression.
3472 *
3473 * Return OK or FAIL.
3474 */
3475 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003476eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477{
Bram Moolenaar33570922005-01-25 22:26:29 +00003478 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 char_u *p;
3480 int i;
3481 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003482 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 int len = 2;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003484 varnumber_T n1, n2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 char_u *s1, *s2;
3486 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488
3489 /*
3490 * Get the first variable.
3491 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003492 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 return FAIL;
3494
3495 p = *arg;
3496 switch (p[0])
3497 {
3498 case '=': if (p[1] == '=')
3499 type = TYPE_EQUAL;
3500 else if (p[1] == '~')
3501 type = TYPE_MATCH;
3502 break;
3503 case '!': if (p[1] == '=')
3504 type = TYPE_NEQUAL;
3505 else if (p[1] == '~')
3506 type = TYPE_NOMATCH;
3507 break;
3508 case '>': if (p[1] != '=')
3509 {
3510 type = TYPE_GREATER;
3511 len = 1;
3512 }
3513 else
3514 type = TYPE_GEQUAL;
3515 break;
3516 case '<': if (p[1] != '=')
3517 {
3518 type = TYPE_SMALLER;
3519 len = 1;
3520 }
3521 else
3522 type = TYPE_SEQUAL;
3523 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003524 case 'i': if (p[1] == 's')
3525 {
3526 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3527 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003528 i = p[len];
3529 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003530 {
3531 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3532 type_is = TRUE;
3533 }
3534 }
3535 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 }
3537
3538 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003539 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 */
3541 if (type != TYPE_UNKNOWN)
3542 {
3543 /* extra question mark appended: ignore case */
3544 if (p[len] == '?')
3545 {
3546 ic = TRUE;
3547 ++len;
3548 }
3549 /* extra '#' appended: match case */
3550 else if (p[len] == '#')
3551 {
3552 ic = FALSE;
3553 ++len;
3554 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003555 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 else
3557 ic = p_ic;
3558
3559 /*
3560 * Get the second variable.
3561 */
3562 *arg = skipwhite(p + len);
3563 if (eval5(arg, &var2, evaluate) == FAIL)
3564 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003565 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 return FAIL;
3567 }
3568
3569 if (evaluate)
3570 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003571 if (type_is && rettv->v_type != var2.v_type)
3572 {
3573 /* For "is" a different type always means FALSE, for "notis"
3574 * it means TRUE. */
3575 n1 = (type == TYPE_NEQUAL);
3576 }
3577 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3578 {
3579 if (type_is)
3580 {
3581 n1 = (rettv->v_type == var2.v_type
3582 && rettv->vval.v_list == var2.vval.v_list);
3583 if (type == TYPE_NEQUAL)
3584 n1 = !n1;
3585 }
3586 else if (rettv->v_type != var2.v_type
3587 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3588 {
3589 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003590 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003591 else
Bram Moolenaar59838522014-05-13 13:46:33 +02003592 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003593 clear_tv(rettv);
3594 clear_tv(&var2);
3595 return FAIL;
3596 }
3597 else
3598 {
3599 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003600 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
3601 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003602 if (type == TYPE_NEQUAL)
3603 n1 = !n1;
3604 }
3605 }
3606
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003607 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3608 {
3609 if (type_is)
3610 {
3611 n1 = (rettv->v_type == var2.v_type
3612 && rettv->vval.v_dict == var2.vval.v_dict);
3613 if (type == TYPE_NEQUAL)
3614 n1 = !n1;
3615 }
3616 else if (rettv->v_type != var2.v_type
3617 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3618 {
3619 if (rettv->v_type != var2.v_type)
3620 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3621 else
3622 EMSG(_("E736: Invalid operation for Dictionary"));
3623 clear_tv(rettv);
3624 clear_tv(&var2);
3625 return FAIL;
3626 }
3627 else
3628 {
3629 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003630 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
3631 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003632 if (type == TYPE_NEQUAL)
3633 n1 = !n1;
3634 }
3635 }
3636
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003637 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
3638 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003639 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003640 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003641 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003642 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003643 clear_tv(rettv);
3644 clear_tv(&var2);
3645 return FAIL;
3646 }
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003647 if ((rettv->v_type == VAR_PARTIAL
3648 && rettv->vval.v_partial == NULL)
3649 || (var2.v_type == VAR_PARTIAL
3650 && var2.vval.v_partial == NULL))
3651 /* when a partial is NULL assume not equal */
3652 n1 = FALSE;
3653 else if (type_is)
3654 {
3655 if (rettv->v_type == VAR_FUNC && var2.v_type == VAR_FUNC)
3656 /* strings are considered the same if their value is
3657 * the same */
3658 n1 = tv_equal(rettv, &var2, ic, FALSE);
3659 else if (rettv->v_type == VAR_PARTIAL
3660 && var2.v_type == VAR_PARTIAL)
3661 n1 = (rettv->vval.v_partial == var2.vval.v_partial);
3662 else
3663 n1 = FALSE;
3664 }
3665 else
3666 n1 = tv_equal(rettv, &var2, ic, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003667 if (type == TYPE_NEQUAL)
3668 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003669 }
3670
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003671#ifdef FEAT_FLOAT
3672 /*
3673 * If one of the two variables is a float, compare as a float.
3674 * When using "=~" or "!~", always compare as string.
3675 */
3676 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3677 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3678 {
3679 float_T f1, f2;
3680
3681 if (rettv->v_type == VAR_FLOAT)
3682 f1 = rettv->vval.v_float;
3683 else
3684 f1 = get_tv_number(rettv);
3685 if (var2.v_type == VAR_FLOAT)
3686 f2 = var2.vval.v_float;
3687 else
3688 f2 = get_tv_number(&var2);
3689 n1 = FALSE;
3690 switch (type)
3691 {
3692 case TYPE_EQUAL: n1 = (f1 == f2); break;
3693 case TYPE_NEQUAL: n1 = (f1 != f2); break;
3694 case TYPE_GREATER: n1 = (f1 > f2); break;
3695 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
3696 case TYPE_SMALLER: n1 = (f1 < f2); break;
3697 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
3698 case TYPE_UNKNOWN:
3699 case TYPE_MATCH:
3700 case TYPE_NOMATCH: break; /* avoid gcc warning */
3701 }
3702 }
3703#endif
3704
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 /*
3706 * If one of the two variables is a number, compare as a number.
3707 * When using "=~" or "!~", always compare as string.
3708 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003709 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3711 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003712 n1 = get_tv_number(rettv);
3713 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 switch (type)
3715 {
3716 case TYPE_EQUAL: n1 = (n1 == n2); break;
3717 case TYPE_NEQUAL: n1 = (n1 != n2); break;
3718 case TYPE_GREATER: n1 = (n1 > n2); break;
3719 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
3720 case TYPE_SMALLER: n1 = (n1 < n2); break;
3721 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
3722 case TYPE_UNKNOWN:
3723 case TYPE_MATCH:
3724 case TYPE_NOMATCH: break; /* avoid gcc warning */
3725 }
3726 }
3727 else
3728 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003729 s1 = get_tv_string_buf(rettv, buf1);
3730 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
3732 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
3733 else
3734 i = 0;
3735 n1 = FALSE;
3736 switch (type)
3737 {
3738 case TYPE_EQUAL: n1 = (i == 0); break;
3739 case TYPE_NEQUAL: n1 = (i != 0); break;
3740 case TYPE_GREATER: n1 = (i > 0); break;
3741 case TYPE_GEQUAL: n1 = (i >= 0); break;
3742 case TYPE_SMALLER: n1 = (i < 0); break;
3743 case TYPE_SEQUAL: n1 = (i <= 0); break;
3744
3745 case TYPE_MATCH:
3746 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003747 n1 = pattern_match(s2, s1, ic);
3748 if (type == TYPE_NOMATCH)
3749 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 break;
3751
3752 case TYPE_UNKNOWN: break; /* avoid gcc warning */
3753 }
3754 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003755 clear_tv(rettv);
3756 clear_tv(&var2);
3757 rettv->v_type = VAR_NUMBER;
3758 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 }
3760 }
3761
3762 return OK;
3763}
3764
3765/*
3766 * Handle fourth level expression:
3767 * + number addition
3768 * - number subtraction
3769 * . string concatenation
3770 *
3771 * "arg" must point to the first non-white of the expression.
3772 * "arg" is advanced to the next non-white after the recognized expression.
3773 *
3774 * Return OK or FAIL.
3775 */
3776 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003777eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778{
Bram Moolenaar33570922005-01-25 22:26:29 +00003779 typval_T var2;
3780 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003782 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003783#ifdef FEAT_FLOAT
3784 float_T f1 = 0, f2 = 0;
3785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 char_u *s1, *s2;
3787 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3788 char_u *p;
3789
3790 /*
3791 * Get the first variable.
3792 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003793 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 return FAIL;
3795
3796 /*
3797 * Repeat computing, until no '+', '-' or '.' is following.
3798 */
3799 for (;;)
3800 {
3801 op = **arg;
3802 if (op != '+' && op != '-' && op != '.')
3803 break;
3804
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003805 if ((op != '+' || rettv->v_type != VAR_LIST)
3806#ifdef FEAT_FLOAT
3807 && (op == '.' || rettv->v_type != VAR_FLOAT)
3808#endif
3809 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003810 {
3811 /* For "list + ...", an illegal use of the first operand as
3812 * a number cannot be determined before evaluating the 2nd
3813 * operand: if this is also a list, all is ok.
3814 * For "something . ...", "something - ..." or "non-list + ...",
3815 * we know that the first operand needs to be a string or number
3816 * without evaluating the 2nd operand. So check before to avoid
3817 * side effects after an error. */
3818 if (evaluate && get_tv_string_chk(rettv) == NULL)
3819 {
3820 clear_tv(rettv);
3821 return FAIL;
3822 }
3823 }
3824
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 /*
3826 * Get the second variable.
3827 */
3828 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003829 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003831 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 return FAIL;
3833 }
3834
3835 if (evaluate)
3836 {
3837 /*
3838 * Compute the result.
3839 */
3840 if (op == '.')
3841 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003842 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
3843 s2 = get_tv_string_buf_chk(&var2, buf2);
3844 if (s2 == NULL) /* type error ? */
3845 {
3846 clear_tv(rettv);
3847 clear_tv(&var2);
3848 return FAIL;
3849 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003850 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003851 clear_tv(rettv);
3852 rettv->v_type = VAR_STRING;
3853 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003855 else if (op == '+' && rettv->v_type == VAR_LIST
3856 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003857 {
3858 /* concatenate Lists */
3859 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3860 &var3) == FAIL)
3861 {
3862 clear_tv(rettv);
3863 clear_tv(&var2);
3864 return FAIL;
3865 }
3866 clear_tv(rettv);
3867 *rettv = var3;
3868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 else
3870 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003871 int error = FALSE;
3872
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003873#ifdef FEAT_FLOAT
3874 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003875 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003876 f1 = rettv->vval.v_float;
3877 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003878 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003879 else
3880#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003881 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003882 n1 = get_tv_number_chk(rettv, &error);
3883 if (error)
3884 {
3885 /* This can only happen for "list + non-list". For
3886 * "non-list + ..." or "something - ...", we returned
3887 * before evaluating the 2nd operand. */
3888 clear_tv(rettv);
3889 return FAIL;
3890 }
3891#ifdef FEAT_FLOAT
3892 if (var2.v_type == VAR_FLOAT)
3893 f1 = n1;
3894#endif
3895 }
3896#ifdef FEAT_FLOAT
3897 if (var2.v_type == VAR_FLOAT)
3898 {
3899 f2 = var2.vval.v_float;
3900 n2 = 0;
3901 }
3902 else
3903#endif
3904 {
3905 n2 = get_tv_number_chk(&var2, &error);
3906 if (error)
3907 {
3908 clear_tv(rettv);
3909 clear_tv(&var2);
3910 return FAIL;
3911 }
3912#ifdef FEAT_FLOAT
3913 if (rettv->v_type == VAR_FLOAT)
3914 f2 = n2;
3915#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003916 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003917 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003918
3919#ifdef FEAT_FLOAT
3920 /* If there is a float on either side the result is a float. */
3921 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3922 {
3923 if (op == '+')
3924 f1 = f1 + f2;
3925 else
3926 f1 = f1 - f2;
3927 rettv->v_type = VAR_FLOAT;
3928 rettv->vval.v_float = f1;
3929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003931#endif
3932 {
3933 if (op == '+')
3934 n1 = n1 + n2;
3935 else
3936 n1 = n1 - n2;
3937 rettv->v_type = VAR_NUMBER;
3938 rettv->vval.v_number = n1;
3939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003941 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 }
3943 }
3944 return OK;
3945}
3946
3947/*
3948 * Handle fifth level expression:
3949 * * number multiplication
3950 * / number division
3951 * % number modulo
3952 *
3953 * "arg" must point to the first non-white of the expression.
3954 * "arg" is advanced to the next non-white after the recognized expression.
3955 *
3956 * Return OK or FAIL.
3957 */
3958 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003959eval6(
3960 char_u **arg,
3961 typval_T *rettv,
3962 int evaluate,
3963 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964{
Bram Moolenaar33570922005-01-25 22:26:29 +00003965 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003967 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003968#ifdef FEAT_FLOAT
3969 int use_float = FALSE;
3970 float_T f1 = 0, f2;
3971#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003972 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973
3974 /*
3975 * Get the first variable.
3976 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003977 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 return FAIL;
3979
3980 /*
3981 * Repeat computing, until no '*', '/' or '%' is following.
3982 */
3983 for (;;)
3984 {
3985 op = **arg;
3986 if (op != '*' && op != '/' && op != '%')
3987 break;
3988
3989 if (evaluate)
3990 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003991#ifdef FEAT_FLOAT
3992 if (rettv->v_type == VAR_FLOAT)
3993 {
3994 f1 = rettv->vval.v_float;
3995 use_float = TRUE;
3996 n1 = 0;
3997 }
3998 else
3999#endif
4000 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004001 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004002 if (error)
4003 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 }
4005 else
4006 n1 = 0;
4007
4008 /*
4009 * Get the second variable.
4010 */
4011 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004012 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 return FAIL;
4014
4015 if (evaluate)
4016 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004017#ifdef FEAT_FLOAT
4018 if (var2.v_type == VAR_FLOAT)
4019 {
4020 if (!use_float)
4021 {
4022 f1 = n1;
4023 use_float = TRUE;
4024 }
4025 f2 = var2.vval.v_float;
4026 n2 = 0;
4027 }
4028 else
4029#endif
4030 {
4031 n2 = get_tv_number_chk(&var2, &error);
4032 clear_tv(&var2);
4033 if (error)
4034 return FAIL;
4035#ifdef FEAT_FLOAT
4036 if (use_float)
4037 f2 = n2;
4038#endif
4039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040
4041 /*
4042 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004043 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004045#ifdef FEAT_FLOAT
4046 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004048 if (op == '*')
4049 f1 = f1 * f2;
4050 else if (op == '/')
4051 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004052# ifdef VMS
4053 /* VMS crashes on divide by zero, work around it */
4054 if (f2 == 0.0)
4055 {
4056 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004057 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004058 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004059 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004060 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004061 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004062 }
4063 else
4064 f1 = f1 / f2;
4065# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004066 /* We rely on the floating point library to handle divide
4067 * by zero to result in "inf" and not a crash. */
4068 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004069# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004072 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004073 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004074 return FAIL;
4075 }
4076 rettv->v_type = VAR_FLOAT;
4077 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 }
4079 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004082 if (op == '*')
4083 n1 = n1 * n2;
4084 else if (op == '/')
4085 {
4086 if (n2 == 0) /* give an error message? */
4087 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004088#ifdef FEAT_NUM64
4089 if (n1 == 0)
4090 n1 = -0x7fffffffffffffff - 1; /* similar to NaN */
4091 else if (n1 < 0)
4092 n1 = -0x7fffffffffffffff;
4093 else
4094 n1 = 0x7fffffffffffffff;
4095#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004096 if (n1 == 0)
4097 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4098 else if (n1 < 0)
4099 n1 = -0x7fffffffL;
4100 else
4101 n1 = 0x7fffffffL;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004102#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004103 }
4104 else
4105 n1 = n1 / n2;
4106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004108 {
4109 if (n2 == 0) /* give an error message? */
4110 n1 = 0;
4111 else
4112 n1 = n1 % n2;
4113 }
4114 rettv->v_type = VAR_NUMBER;
4115 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 }
4118 }
4119
4120 return OK;
4121}
4122
4123/*
4124 * Handle sixth level expression:
4125 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004126 * "string" string constant
4127 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 * &option-name option value
4129 * @r register contents
4130 * identifier variable value
4131 * function() function call
4132 * $VAR environment variable
4133 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004134 * [expr, expr] List
4135 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 *
4137 * Also handle:
4138 * ! in front logical NOT
4139 * - in front unary minus
4140 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004141 * trailing [] subscript in String or List
4142 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 *
4144 * "arg" must point to the first non-white of the expression.
4145 * "arg" is advanced to the next non-white after the recognized expression.
4146 *
4147 * Return OK or FAIL.
4148 */
4149 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004150eval7(
4151 char_u **arg,
4152 typval_T *rettv,
4153 int evaluate,
4154 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004156 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 int len;
4158 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 char_u *start_leader, *end_leader;
4160 int ret = OK;
4161 char_u *alias;
4162
4163 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004164 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004165 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004167 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168
4169 /*
4170 * Skip '!' and '-' characters. They are handled later.
4171 */
4172 start_leader = *arg;
4173 while (**arg == '!' || **arg == '-' || **arg == '+')
4174 *arg = skipwhite(*arg + 1);
4175 end_leader = *arg;
4176
4177 switch (**arg)
4178 {
4179 /*
4180 * Number constant.
4181 */
4182 case '0':
4183 case '1':
4184 case '2':
4185 case '3':
4186 case '4':
4187 case '5':
4188 case '6':
4189 case '7':
4190 case '8':
4191 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004192 {
4193#ifdef FEAT_FLOAT
4194 char_u *p = skipdigits(*arg + 1);
4195 int get_float = FALSE;
4196
4197 /* We accept a float when the format matches
4198 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004199 * strict to avoid backwards compatibility problems.
4200 * Don't look for a float after the "." operator, so that
4201 * ":let vers = 1.2.3" doesn't fail. */
4202 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004204 get_float = TRUE;
4205 p = skipdigits(p + 2);
4206 if (*p == 'e' || *p == 'E')
4207 {
4208 ++p;
4209 if (*p == '-' || *p == '+')
4210 ++p;
4211 if (!vim_isdigit(*p))
4212 get_float = FALSE;
4213 else
4214 p = skipdigits(p + 1);
4215 }
4216 if (ASCII_ISALPHA(*p) || *p == '.')
4217 get_float = FALSE;
4218 }
4219 if (get_float)
4220 {
4221 float_T f;
4222
4223 *arg += string2float(*arg, &f);
4224 if (evaluate)
4225 {
4226 rettv->v_type = VAR_FLOAT;
4227 rettv->vval.v_float = f;
4228 }
4229 }
4230 else
4231#endif
4232 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01004233 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004234 *arg += len;
4235 if (evaluate)
4236 {
4237 rettv->v_type = VAR_NUMBER;
4238 rettv->vval.v_number = n;
4239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 }
4241 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243
4244 /*
4245 * String constant: "string".
4246 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004247 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 break;
4249
4250 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004251 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004253 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004254 break;
4255
4256 /*
4257 * List: [expr, expr]
4258 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004259 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 break;
4261
4262 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004263 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004264 * Dictionary: {key: val, key: val}
4265 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004266 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4267 if (ret == NOTDONE)
4268 ret = get_dict_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004269 break;
4270
4271 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004272 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004274 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 break;
4276
4277 /*
4278 * Environment variable: $VAR.
4279 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004280 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 break;
4282
4283 /*
4284 * Register contents: @r.
4285 */
4286 case '@': ++*arg;
4287 if (evaluate)
4288 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004289 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004290 rettv->vval.v_string = get_reg_contents(**arg,
4291 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 }
4293 if (**arg != NUL)
4294 ++*arg;
4295 break;
4296
4297 /*
4298 * nested expression: (expression).
4299 */
4300 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004301 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 if (**arg == ')')
4303 ++*arg;
4304 else if (ret == OK)
4305 {
4306 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004307 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 ret = FAIL;
4309 }
4310 break;
4311
Bram Moolenaar8c711452005-01-14 21:53:12 +00004312 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 break;
4314 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004315
4316 if (ret == NOTDONE)
4317 {
4318 /*
4319 * Must be a variable or function name.
4320 * Can also be a curly-braces kind of name: {expr}.
4321 */
4322 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004323 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004324 if (alias != NULL)
4325 s = alias;
4326
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004327 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004328 ret = FAIL;
4329 else
4330 {
4331 if (**arg == '(') /* recursive! */
4332 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004333 partial_T *partial;
4334
Bram Moolenaar8c711452005-01-14 21:53:12 +00004335 /* If "s" is the name of a variable of type VAR_FUNC
4336 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004337 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004338
4339 /* Invoke the function. */
4340 ret = get_func_tv(s, len, rettv, arg,
4341 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004342 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004343
4344 /* If evaluate is FALSE rettv->v_type was not set in
4345 * get_func_tv, but it's needed in handle_subscript() to parse
4346 * what follows. So set it here. */
4347 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4348 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004349 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004350 rettv->v_type = VAR_FUNC;
4351 }
4352
Bram Moolenaar8c711452005-01-14 21:53:12 +00004353 /* Stop the expression evaluation when immediately
4354 * aborting on error, or when an interrupt occurred or
4355 * an exception was thrown but not caught. */
4356 if (aborting())
4357 {
4358 if (ret == OK)
4359 clear_tv(rettv);
4360 ret = FAIL;
4361 }
4362 }
4363 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004364 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004365 else
4366 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004367 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004368 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004369 }
4370
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 *arg = skipwhite(*arg);
4372
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004373 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4374 * expr(expr). */
4375 if (ret == OK)
4376 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377
4378 /*
4379 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4380 */
4381 if (ret == OK && evaluate && end_leader > start_leader)
4382 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004383 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004384 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004385#ifdef FEAT_FLOAT
4386 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004387
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004388 if (rettv->v_type == VAR_FLOAT)
4389 f = rettv->vval.v_float;
4390 else
4391#endif
4392 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004393 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004395 clear_tv(rettv);
4396 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004398 else
4399 {
4400 while (end_leader > start_leader)
4401 {
4402 --end_leader;
4403 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004404 {
4405#ifdef FEAT_FLOAT
4406 if (rettv->v_type == VAR_FLOAT)
4407 f = !f;
4408 else
4409#endif
4410 val = !val;
4411 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004412 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004413 {
4414#ifdef FEAT_FLOAT
4415 if (rettv->v_type == VAR_FLOAT)
4416 f = -f;
4417 else
4418#endif
4419 val = -val;
4420 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004421 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004422#ifdef FEAT_FLOAT
4423 if (rettv->v_type == VAR_FLOAT)
4424 {
4425 clear_tv(rettv);
4426 rettv->vval.v_float = f;
4427 }
4428 else
4429#endif
4430 {
4431 clear_tv(rettv);
4432 rettv->v_type = VAR_NUMBER;
4433 rettv->vval.v_number = val;
4434 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 }
4437
4438 return ret;
4439}
4440
4441/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004442 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4443 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004444 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4445 */
4446 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004447eval_index(
4448 char_u **arg,
4449 typval_T *rettv,
4450 int evaluate,
4451 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004452{
4453 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004454 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004455 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004456 long len = -1;
4457 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004458 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004459 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004460
Bram Moolenaara03f2332016-02-06 18:09:59 +01004461 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004462 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004463 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004464 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004465 if (verbose)
4466 EMSG(_("E695: Cannot index a Funcref"));
4467 return FAIL;
4468 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004469#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004470 if (verbose)
4471 EMSG(_(e_float_as_string));
4472 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004473#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004474 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004475 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004476 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004477 if (verbose)
4478 EMSG(_("E909: Cannot index a special variable"));
4479 return FAIL;
4480 case VAR_UNKNOWN:
4481 if (evaluate)
4482 return FAIL;
4483 /* FALLTHROUGH */
4484
4485 case VAR_STRING:
4486 case VAR_NUMBER:
4487 case VAR_LIST:
4488 case VAR_DICT:
4489 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004490 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004491
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004492 init_tv(&var1);
4493 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004494 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004495 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004496 /*
4497 * dict.name
4498 */
4499 key = *arg + 1;
4500 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4501 ;
4502 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004503 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004504 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004505 }
4506 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004507 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004508 /*
4509 * something[idx]
4510 *
4511 * Get the (first) variable from inside the [].
4512 */
4513 *arg = skipwhite(*arg + 1);
4514 if (**arg == ':')
4515 empty1 = TRUE;
4516 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4517 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004518 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4519 {
4520 /* not a number or string */
4521 clear_tv(&var1);
4522 return FAIL;
4523 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004524
4525 /*
4526 * Get the second variable from inside the [:].
4527 */
4528 if (**arg == ':')
4529 {
4530 range = TRUE;
4531 *arg = skipwhite(*arg + 1);
4532 if (**arg == ']')
4533 empty2 = TRUE;
4534 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4535 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004536 if (!empty1)
4537 clear_tv(&var1);
4538 return FAIL;
4539 }
4540 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4541 {
4542 /* not a number or string */
4543 if (!empty1)
4544 clear_tv(&var1);
4545 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004546 return FAIL;
4547 }
4548 }
4549
4550 /* Check for the ']'. */
4551 if (**arg != ']')
4552 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004553 if (verbose)
4554 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004555 clear_tv(&var1);
4556 if (range)
4557 clear_tv(&var2);
4558 return FAIL;
4559 }
4560 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004561 }
4562
4563 if (evaluate)
4564 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004565 n1 = 0;
4566 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004567 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004568 n1 = get_tv_number(&var1);
4569 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004570 }
4571 if (range)
4572 {
4573 if (empty2)
4574 n2 = -1;
4575 else
4576 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004577 n2 = get_tv_number(&var2);
4578 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004579 }
4580 }
4581
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004582 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004583 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004584 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004585 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004586 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004587 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004588 case VAR_SPECIAL:
4589 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004590 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004591 break; /* not evaluating, skipping over subscript */
4592
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004593 case VAR_NUMBER:
4594 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004595 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004596 len = (long)STRLEN(s);
4597 if (range)
4598 {
4599 /* The resulting variable is a substring. If the indexes
4600 * are out of range the result is empty. */
4601 if (n1 < 0)
4602 {
4603 n1 = len + n1;
4604 if (n1 < 0)
4605 n1 = 0;
4606 }
4607 if (n2 < 0)
4608 n2 = len + n2;
4609 else if (n2 >= len)
4610 n2 = len;
4611 if (n1 >= len || n2 < 0 || n1 > n2)
4612 s = NULL;
4613 else
4614 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4615 }
4616 else
4617 {
4618 /* The resulting variable is a string of a single
4619 * character. If the index is too big or negative the
4620 * result is empty. */
4621 if (n1 >= len || n1 < 0)
4622 s = NULL;
4623 else
4624 s = vim_strnsave(s + n1, 1);
4625 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004626 clear_tv(rettv);
4627 rettv->v_type = VAR_STRING;
4628 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004629 break;
4630
4631 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004632 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004633 if (n1 < 0)
4634 n1 = len + n1;
4635 if (!empty1 && (n1 < 0 || n1 >= len))
4636 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004637 /* For a range we allow invalid values and return an empty
4638 * list. A list index out of range is an error. */
4639 if (!range)
4640 {
4641 if (verbose)
4642 EMSGN(_(e_listidx), n1);
4643 return FAIL;
4644 }
4645 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004646 }
4647 if (range)
4648 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004649 list_T *l;
4650 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004651
4652 if (n2 < 0)
4653 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004654 else if (n2 >= len)
4655 n2 = len - 1;
4656 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004657 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004658 l = list_alloc();
4659 if (l == NULL)
4660 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004661 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004662 n1 <= n2; ++n1)
4663 {
4664 if (list_append_tv(l, &item->li_tv) == FAIL)
4665 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004666 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004667 return FAIL;
4668 }
4669 item = item->li_next;
4670 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004671 clear_tv(rettv);
4672 rettv->v_type = VAR_LIST;
4673 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004674 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004675 }
4676 else
4677 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004678 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004679 clear_tv(rettv);
4680 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004681 }
4682 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004683
4684 case VAR_DICT:
4685 if (range)
4686 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004687 if (verbose)
4688 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004689 if (len == -1)
4690 clear_tv(&var1);
4691 return FAIL;
4692 }
4693 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004694 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004695
4696 if (len == -1)
4697 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02004698 key = get_tv_string_chk(&var1);
4699 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004700 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004701 clear_tv(&var1);
4702 return FAIL;
4703 }
4704 }
4705
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004706 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004707
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004708 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004709 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004710 if (len == -1)
4711 clear_tv(&var1);
4712 if (item == NULL)
4713 return FAIL;
4714
4715 copy_tv(&item->di_tv, &var1);
4716 clear_tv(rettv);
4717 *rettv = var1;
4718 }
4719 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004720 }
4721 }
4722
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004723 return OK;
4724}
4725
4726/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 * Get an option value.
4728 * "arg" points to the '&' or '+' before the option name.
4729 * "arg" is advanced to character after the option name.
4730 * Return OK or FAIL.
4731 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004732 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004733get_option_tv(
4734 char_u **arg,
4735 typval_T *rettv, /* when NULL, only check if option exists */
4736 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737{
4738 char_u *option_end;
4739 long numval;
4740 char_u *stringval;
4741 int opt_type;
4742 int c;
4743 int working = (**arg == '+'); /* has("+option") */
4744 int ret = OK;
4745 int opt_flags;
4746
4747 /*
4748 * Isolate the option name and find its value.
4749 */
4750 option_end = find_option_end(arg, &opt_flags);
4751 if (option_end == NULL)
4752 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004753 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 EMSG2(_("E112: Option name missing: %s"), *arg);
4755 return FAIL;
4756 }
4757
4758 if (!evaluate)
4759 {
4760 *arg = option_end;
4761 return OK;
4762 }
4763
4764 c = *option_end;
4765 *option_end = NUL;
4766 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004767 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768
4769 if (opt_type == -3) /* invalid name */
4770 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004771 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 EMSG2(_("E113: Unknown option: %s"), *arg);
4773 ret = FAIL;
4774 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004775 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 {
4777 if (opt_type == -2) /* hidden string option */
4778 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004779 rettv->v_type = VAR_STRING;
4780 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 }
4782 else if (opt_type == -1) /* hidden number option */
4783 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004784 rettv->v_type = VAR_NUMBER;
4785 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 }
4787 else if (opt_type == 1) /* number option */
4788 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004789 rettv->v_type = VAR_NUMBER;
4790 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 }
4792 else /* string option */
4793 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004794 rettv->v_type = VAR_STRING;
4795 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 }
4797 }
4798 else if (working && (opt_type == -2 || opt_type == -1))
4799 ret = FAIL;
4800
4801 *option_end = c; /* put back for error messages */
4802 *arg = option_end;
4803
4804 return ret;
4805}
4806
4807/*
4808 * Allocate a variable for a string constant.
4809 * Return OK or FAIL.
4810 */
4811 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004812get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813{
4814 char_u *p;
4815 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 int extra = 0;
4817
4818 /*
4819 * Find the end of the string, skipping backslashed characters.
4820 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004821 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 {
4823 if (*p == '\\' && p[1] != NUL)
4824 {
4825 ++p;
4826 /* A "\<x>" form occupies at least 4 characters, and produces up
4827 * to 6 characters: reserve space for 2 extra */
4828 if (*p == '<')
4829 extra += 2;
4830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 }
4832
4833 if (*p != '"')
4834 {
4835 EMSG2(_("E114: Missing quote: %s"), *arg);
4836 return FAIL;
4837 }
4838
4839 /* If only parsing, set *arg and return here */
4840 if (!evaluate)
4841 {
4842 *arg = p + 1;
4843 return OK;
4844 }
4845
4846 /*
4847 * Copy the string into allocated memory, handling backslashed
4848 * characters.
4849 */
4850 name = alloc((unsigned)(p - *arg + extra));
4851 if (name == NULL)
4852 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004853 rettv->v_type = VAR_STRING;
4854 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855
Bram Moolenaar8c711452005-01-14 21:53:12 +00004856 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 {
4858 if (*p == '\\')
4859 {
4860 switch (*++p)
4861 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004862 case 'b': *name++ = BS; ++p; break;
4863 case 'e': *name++ = ESC; ++p; break;
4864 case 'f': *name++ = FF; ++p; break;
4865 case 'n': *name++ = NL; ++p; break;
4866 case 'r': *name++ = CAR; ++p; break;
4867 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868
4869 case 'X': /* hex: "\x1", "\x12" */
4870 case 'x':
4871 case 'u': /* Unicode: "\u0023" */
4872 case 'U':
4873 if (vim_isxdigit(p[1]))
4874 {
4875 int n, nr;
4876 int c = toupper(*p);
4877
4878 if (c == 'X')
4879 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004880 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004882 else
4883 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 nr = 0;
4885 while (--n >= 0 && vim_isxdigit(p[1]))
4886 {
4887 ++p;
4888 nr = (nr << 4) + hex2nr(*p);
4889 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004890 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891#ifdef FEAT_MBYTE
4892 /* For "\u" store the number according to
4893 * 'encoding'. */
4894 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004895 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 else
4897#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004898 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 break;
4901
4902 /* octal: "\1", "\12", "\123" */
4903 case '0':
4904 case '1':
4905 case '2':
4906 case '3':
4907 case '4':
4908 case '5':
4909 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004910 case '7': *name = *p++ - '0';
4911 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004913 *name = (*name << 3) + *p++ - '0';
4914 if (*p >= '0' && *p <= '7')
4915 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004917 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 break;
4919
4920 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00004921 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 if (extra != 0)
4923 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004924 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 break;
4926 }
4927 /* FALLTHROUGH */
4928
Bram Moolenaar8c711452005-01-14 21:53:12 +00004929 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 break;
4931 }
4932 }
4933 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004934 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004937 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 *arg = p + 1;
4939
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 return OK;
4941}
4942
4943/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004944 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 * Return OK or FAIL.
4946 */
4947 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004948get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949{
4950 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004951 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004952 int reduce = 0;
4953
4954 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004955 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004956 */
4957 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
4958 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004959 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004960 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004961 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004962 break;
4963 ++reduce;
4964 ++p;
4965 }
4966 }
4967
Bram Moolenaar8c711452005-01-14 21:53:12 +00004968 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004969 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004970 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004971 return FAIL;
4972 }
4973
Bram Moolenaar8c711452005-01-14 21:53:12 +00004974 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004975 if (!evaluate)
4976 {
4977 *arg = p + 1;
4978 return OK;
4979 }
4980
4981 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004982 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004983 */
4984 str = alloc((unsigned)((p - *arg) - reduce));
4985 if (str == NULL)
4986 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004987 rettv->v_type = VAR_STRING;
4988 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004989
Bram Moolenaar8c711452005-01-14 21:53:12 +00004990 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004991 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004992 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004993 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004994 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004995 break;
4996 ++p;
4997 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004998 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004999 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005000 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005001 *arg = p + 1;
5002
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005003 return OK;
5004}
5005
Bram Moolenaarddecc252016-04-06 22:59:37 +02005006 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005007partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005008{
5009 int i;
5010
5011 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005012 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005013 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005014 dict_unref(pt->pt_dict);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005015 func_unref(pt->pt_name);
5016 vim_free(pt->pt_name);
5017 vim_free(pt);
5018}
5019
5020/*
5021 * Unreference a closure: decrement the reference count and free it when it
5022 * becomes zero.
5023 */
5024 void
5025partial_unref(partial_T *pt)
5026{
5027 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005028 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005029}
5030
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005031static int tv_equal_recurse_limit;
5032
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005033 static int
5034func_equal(
5035 typval_T *tv1,
5036 typval_T *tv2,
5037 int ic) /* ignore case */
5038{
5039 char_u *s1, *s2;
5040 dict_T *d1, *d2;
5041 int a1, a2;
5042 int i;
5043
5044 /* empty and NULL function name considered the same */
5045 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
5046 : tv1->vval.v_partial->pt_name;
5047 if (s1 != NULL && *s1 == NUL)
5048 s1 = NULL;
5049 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
5050 : tv2->vval.v_partial->pt_name;
5051 if (s2 != NULL && *s2 == NUL)
5052 s2 = NULL;
5053 if (s1 == NULL || s2 == NULL)
5054 {
5055 if (s1 != s2)
5056 return FALSE;
5057 }
5058 else if (STRCMP(s1, s2) != 0)
5059 return FALSE;
5060
5061 /* empty dict and NULL dict is different */
5062 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5063 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5064 if (d1 == NULL || d2 == NULL)
5065 {
5066 if (d1 != d2)
5067 return FALSE;
5068 }
5069 else if (!dict_equal(d1, d2, ic, TRUE))
5070 return FALSE;
5071
5072 /* empty list and no list considered the same */
5073 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5074 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5075 if (a1 != a2)
5076 return FALSE;
5077 for (i = 0; i < a1; ++i)
5078 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5079 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5080 return FALSE;
5081
5082 return TRUE;
5083}
5084
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005085/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005086 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005087 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005088 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005089 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005090 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005091tv_equal(
5092 typval_T *tv1,
5093 typval_T *tv2,
5094 int ic, /* ignore case */
5095 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005096{
5097 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005098 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005099 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005100 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005101
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005102 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005103 * recursiveness to a limit. We guess they are equal then.
5104 * A fixed limit has the problem of still taking an awful long time.
5105 * Reduce the limit every time running into it. That should work fine for
5106 * deeply linked structures that are not recursively linked and catch
5107 * recursiveness quickly. */
5108 if (!recursive)
5109 tv_equal_recurse_limit = 1000;
5110 if (recursive_cnt >= tv_equal_recurse_limit)
5111 {
5112 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005113 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005114 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005115
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005116 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5117 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005118 if ((tv1->v_type == VAR_FUNC
5119 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5120 && (tv2->v_type == VAR_FUNC
5121 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5122 {
5123 ++recursive_cnt;
5124 r = func_equal(tv1, tv2, ic);
5125 --recursive_cnt;
5126 return r;
5127 }
5128
5129 if (tv1->v_type != tv2->v_type)
5130 return FALSE;
5131
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005132 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005133 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005134 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005135 ++recursive_cnt;
5136 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5137 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005138 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005139
5140 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005141 ++recursive_cnt;
5142 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5143 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005144 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005145
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005146 case VAR_NUMBER:
5147 return tv1->vval.v_number == tv2->vval.v_number;
5148
5149 case VAR_STRING:
5150 s1 = get_tv_string_buf(tv1, buf1);
5151 s2 = get_tv_string_buf(tv2, buf2);
5152 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005153
5154 case VAR_SPECIAL:
5155 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005156
5157 case VAR_FLOAT:
5158#ifdef FEAT_FLOAT
5159 return tv1->vval.v_float == tv2->vval.v_float;
5160#endif
5161 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005162#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005163 return tv1->vval.v_job == tv2->vval.v_job;
5164#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005165 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005166#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005167 return tv1->vval.v_channel == tv2->vval.v_channel;
5168#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005169 case VAR_FUNC:
5170 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005171 case VAR_UNKNOWN:
5172 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005173 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005174
Bram Moolenaara03f2332016-02-06 18:09:59 +01005175 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5176 * does not equal anything, not even itself. */
5177 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005178}
5179
5180/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005181 * Return the next (unique) copy ID.
5182 * Used for serializing nested structures.
5183 */
5184 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005185get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005186{
5187 current_copyID += COPYID_INC;
5188 return current_copyID;
5189}
5190
5191/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005192 * Garbage collection for lists and dictionaries.
5193 *
5194 * We use reference counts to be able to free most items right away when they
5195 * are no longer used. But for composite items it's possible that it becomes
5196 * unused while the reference count is > 0: When there is a recursive
5197 * reference. Example:
5198 * :let l = [1, 2, 3]
5199 * :let d = {9: l}
5200 * :let l[1] = d
5201 *
5202 * Since this is quite unusual we handle this with garbage collection: every
5203 * once in a while find out which lists and dicts are not referenced from any
5204 * variable.
5205 *
5206 * Here is a good reference text about garbage collection (refers to Python
5207 * but it applies to all reference-counting mechanisms):
5208 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005209 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005210
5211/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005212 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005213 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005214 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005215 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005216 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005217garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005218{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005219 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005220 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005221 buf_T *buf;
5222 win_T *wp;
5223 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005224 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005225#ifdef FEAT_WINDOWS
5226 tabpage_T *tp;
5227#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005228
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005229 if (!testing)
5230 {
5231 /* Only do this once. */
5232 want_garbage_collect = FALSE;
5233 may_garbage_collect = FALSE;
5234 garbage_collect_at_exit = FALSE;
5235 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005236
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005237 /* We advance by two because we add one for items referenced through
5238 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005239 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005240
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005241 /*
5242 * 1. Go through all accessible variables and mark all lists and dicts
5243 * with copyID.
5244 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005245
5246 /* Don't free variables in the previous_funccal list unless they are only
5247 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005248 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005249 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005250
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005251 /* script-local variables */
5252 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005253 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005254
5255 /* buffer-local variables */
5256 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005257 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5258 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005259
5260 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005261 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005262 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5263 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005264#ifdef FEAT_AUTOCMD
5265 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005266 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5267 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005268#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005269
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005270#ifdef FEAT_WINDOWS
5271 /* tabpage-local variables */
5272 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005273 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5274 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005275#endif
5276
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005277 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005278 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005279
5280 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005281 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005282
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005283 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005284 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005285
Bram Moolenaard812df62008-11-09 12:46:09 +00005286 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005287 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005288
Bram Moolenaar1dced572012-04-05 16:54:08 +02005289#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005290 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005291#endif
5292
Bram Moolenaardb913952012-06-29 12:54:53 +02005293#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005294 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005295#endif
5296
5297#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005298 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005299#endif
5300
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005301#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005302 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005303 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005304#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005305#ifdef FEAT_NETBEANS_INTG
5306 abort = abort || set_ref_in_nb_channel(copyID);
5307#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005308
Bram Moolenaare3188e22016-05-31 21:13:04 +02005309#ifdef FEAT_TIMERS
5310 abort = abort || set_ref_in_timer(copyID);
5311#endif
5312
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005313 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005314 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005315 /*
5316 * 2. Free lists and dictionaries that are not referenced.
5317 */
5318 did_free = free_unref_items(copyID);
5319
5320 /*
5321 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005322 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005323 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005324 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005325 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005326 else if (p_verbose > 0)
5327 {
5328 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
5329 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005330
5331 return did_free;
5332}
5333
5334/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005335 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005336 */
5337 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005338free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005339{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005340 int did_free = FALSE;
5341
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005342 /* Let all "free" functions know that we are here. This means no
5343 * dictionaries, lists, channels or jobs are to be freed, because we will
5344 * do that here. */
5345 in_free_unref_items = TRUE;
5346
5347 /*
5348 * PASS 1: free the contents of the items. We don't free the items
5349 * themselves yet, so that it is possible to decrement refcount counters
5350 */
5351
Bram Moolenaarcd524592016-07-17 14:57:05 +02005352 /* Go through the list of dicts and free items without the copyID. */
5353 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005354
Bram Moolenaarda861d62016-07-17 15:46:27 +02005355 /* Go through the list of lists and free items without the copyID. */
5356 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005357
5358#ifdef FEAT_JOB_CHANNEL
5359 /* Go through the list of jobs and free items without the copyID. This
5360 * must happen before doing channels, because jobs refer to channels, but
5361 * the reference from the channel to the job isn't tracked. */
5362 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5363
5364 /* Go through the list of channels and free items without the copyID. */
5365 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5366#endif
5367
5368 /*
5369 * PASS 2: free the items themselves.
5370 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005371 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005372 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005373
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005374#ifdef FEAT_JOB_CHANNEL
5375 /* Go through the list of jobs and free items without the copyID. This
5376 * must happen before doing channels, because jobs refer to channels, but
5377 * the reference from the channel to the job isn't tracked. */
5378 free_unused_jobs(copyID, COPYID_MASK);
5379
5380 /* Go through the list of channels and free items without the copyID. */
5381 free_unused_channels(copyID, COPYID_MASK);
5382#endif
5383
5384 in_free_unref_items = FALSE;
5385
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005386 return did_free;
5387}
5388
5389/*
5390 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005391 * "list_stack" is used to add lists to be marked. Can be NULL.
5392 *
5393 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005394 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005395 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005396set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005397{
5398 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005399 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005400 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005401 hashtab_T *cur_ht;
5402 ht_stack_T *ht_stack = NULL;
5403 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005404
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005405 cur_ht = ht;
5406 for (;;)
5407 {
5408 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005409 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005410 /* Mark each item in the hashtab. If the item contains a hashtab
5411 * it is added to ht_stack, if it contains a list it is added to
5412 * list_stack. */
5413 todo = (int)cur_ht->ht_used;
5414 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5415 if (!HASHITEM_EMPTY(hi))
5416 {
5417 --todo;
5418 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5419 &ht_stack, list_stack);
5420 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005421 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005422
5423 if (ht_stack == NULL)
5424 break;
5425
5426 /* take an item from the stack */
5427 cur_ht = ht_stack->ht;
5428 tempitem = ht_stack;
5429 ht_stack = ht_stack->prev;
5430 free(tempitem);
5431 }
5432
5433 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005434}
5435
5436/*
5437 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005438 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5439 *
5440 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005441 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005442 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005443set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005444{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005445 listitem_T *li;
5446 int abort = FALSE;
5447 list_T *cur_l;
5448 list_stack_T *list_stack = NULL;
5449 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005450
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005451 cur_l = l;
5452 for (;;)
5453 {
5454 if (!abort)
5455 /* Mark each item in the list. If the item contains a hashtab
5456 * it is added to ht_stack, if it contains a list it is added to
5457 * list_stack. */
5458 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5459 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5460 ht_stack, &list_stack);
5461 if (list_stack == NULL)
5462 break;
5463
5464 /* take an item from the stack */
5465 cur_l = list_stack->list;
5466 tempitem = list_stack;
5467 list_stack = list_stack->prev;
5468 free(tempitem);
5469 }
5470
5471 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005472}
5473
5474/*
5475 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005476 * "list_stack" is used to add lists to be marked. Can be NULL.
5477 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5478 *
5479 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005480 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005481 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005482set_ref_in_item(
5483 typval_T *tv,
5484 int copyID,
5485 ht_stack_T **ht_stack,
5486 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005487{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005488 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005489
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005490 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005491 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005492 dict_T *dd = tv->vval.v_dict;
5493
Bram Moolenaara03f2332016-02-06 18:09:59 +01005494 if (dd != NULL && dd->dv_copyID != copyID)
5495 {
5496 /* Didn't see this dict yet. */
5497 dd->dv_copyID = copyID;
5498 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005499 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005500 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5501 }
5502 else
5503 {
5504 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5505 if (newitem == NULL)
5506 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005507 else
5508 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005509 newitem->ht = &dd->dv_hashtab;
5510 newitem->prev = *ht_stack;
5511 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005512 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005513 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005514 }
5515 }
5516 else if (tv->v_type == VAR_LIST)
5517 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005518 list_T *ll = tv->vval.v_list;
5519
Bram Moolenaara03f2332016-02-06 18:09:59 +01005520 if (ll != NULL && ll->lv_copyID != copyID)
5521 {
5522 /* Didn't see this list yet. */
5523 ll->lv_copyID = copyID;
5524 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005525 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005526 abort = set_ref_in_list(ll, copyID, ht_stack);
5527 }
5528 else
5529 {
5530 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005531 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005532 if (newitem == NULL)
5533 abort = TRUE;
5534 else
5535 {
5536 newitem->list = ll;
5537 newitem->prev = *list_stack;
5538 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005539 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005540 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005541 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005542 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005543 else if (tv->v_type == VAR_PARTIAL)
5544 {
5545 partial_T *pt = tv->vval.v_partial;
5546 int i;
5547
5548 /* A partial does not have a copyID, because it cannot contain itself.
5549 */
5550 if (pt != NULL)
5551 {
5552 if (pt->pt_dict != NULL)
5553 {
5554 typval_T dtv;
5555
5556 dtv.v_type = VAR_DICT;
5557 dtv.vval.v_dict = pt->pt_dict;
5558 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5559 }
5560
5561 for (i = 0; i < pt->pt_argc; ++i)
5562 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5563 ht_stack, list_stack);
5564 }
5565 }
5566#ifdef FEAT_JOB_CHANNEL
5567 else if (tv->v_type == VAR_JOB)
5568 {
5569 job_T *job = tv->vval.v_job;
5570 typval_T dtv;
5571
5572 if (job != NULL && job->jv_copyID != copyID)
5573 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005574 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005575 if (job->jv_channel != NULL)
5576 {
5577 dtv.v_type = VAR_CHANNEL;
5578 dtv.vval.v_channel = job->jv_channel;
5579 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5580 }
5581 if (job->jv_exit_partial != NULL)
5582 {
5583 dtv.v_type = VAR_PARTIAL;
5584 dtv.vval.v_partial = job->jv_exit_partial;
5585 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5586 }
5587 }
5588 }
5589 else if (tv->v_type == VAR_CHANNEL)
5590 {
5591 channel_T *ch =tv->vval.v_channel;
5592 int part;
5593 typval_T dtv;
5594 jsonq_T *jq;
5595 cbq_T *cq;
5596
5597 if (ch != NULL && ch->ch_copyID != copyID)
5598 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005599 ch->ch_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005600 for (part = PART_SOCK; part <= PART_IN; ++part)
5601 {
5602 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5603 jq = jq->jq_next)
5604 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5605 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5606 cq = cq->cq_next)
5607 if (cq->cq_partial != NULL)
5608 {
5609 dtv.v_type = VAR_PARTIAL;
5610 dtv.vval.v_partial = cq->cq_partial;
5611 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5612 }
5613 if (ch->ch_part[part].ch_partial != NULL)
5614 {
5615 dtv.v_type = VAR_PARTIAL;
5616 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5617 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5618 }
5619 }
5620 if (ch->ch_partial != NULL)
5621 {
5622 dtv.v_type = VAR_PARTIAL;
5623 dtv.vval.v_partial = ch->ch_partial;
5624 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5625 }
5626 if (ch->ch_close_partial != NULL)
5627 {
5628 dtv.v_type = VAR_PARTIAL;
5629 dtv.vval.v_partial = ch->ch_close_partial;
5630 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5631 }
5632 }
5633 }
5634#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005635 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005636}
5637
Bram Moolenaar17a13432016-01-24 14:22:10 +01005638 static char *
5639get_var_special_name(int nr)
5640{
5641 switch (nr)
5642 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005643 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005644 case VVAL_TRUE: return "v:true";
5645 case VVAL_NONE: return "v:none";
5646 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005647 }
5648 EMSG2(_(e_intern2), "get_var_special_name()");
5649 return "42";
5650}
5651
Bram Moolenaar8c711452005-01-14 21:53:12 +00005652/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005653 * Return a string with the string representation of a variable.
5654 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005655 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005656 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005657 * When both "echo_style" and "dict_val" are FALSE, put quotes around stings as
5658 * "string()", otherwise does not put quotes around strings, as ":echo"
5659 * displays values.
5660 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5661 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005662 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005663 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005664 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005665echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005666 typval_T *tv,
5667 char_u **tofree,
5668 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005669 int copyID,
5670 int echo_style,
5671 int restore_copyID,
5672 int dict_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005673{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005674 static int recurse = 0;
5675 char_u *r = NULL;
5676
Bram Moolenaar33570922005-01-25 22:26:29 +00005677 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005678 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005679 if (!did_echo_string_emsg)
5680 {
5681 /* Only give this message once for a recursive call to avoid
5682 * flooding the user with errors. And stop iterating over lists
5683 * and dicts. */
5684 did_echo_string_emsg = TRUE;
5685 EMSG(_("E724: variable nested too deep for displaying"));
5686 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005687 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005688 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005689 }
5690 ++recurse;
5691
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005692 switch (tv->v_type)
5693 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005694 case VAR_STRING:
5695 if (echo_style && !dict_val)
5696 {
5697 *tofree = NULL;
5698 r = get_tv_string_buf(tv, numbuf);
5699 }
5700 else
5701 {
5702 *tofree = string_quote(tv->vval.v_string, FALSE);
5703 r = *tofree;
5704 }
5705 break;
5706
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005707 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005708 if (echo_style)
5709 {
5710 *tofree = NULL;
5711 r = tv->vval.v_string;
5712 }
5713 else
5714 {
5715 *tofree = string_quote(tv->vval.v_string, TRUE);
5716 r = *tofree;
5717 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005718 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005719
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005720 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005721 {
5722 partial_T *pt = tv->vval.v_partial;
5723 char_u *fname = string_quote(pt == NULL ? NULL
5724 : pt->pt_name, FALSE);
5725 garray_T ga;
5726 int i;
5727 char_u *tf;
5728
5729 ga_init2(&ga, 1, 100);
5730 ga_concat(&ga, (char_u *)"function(");
5731 if (fname != NULL)
5732 {
5733 ga_concat(&ga, fname);
5734 vim_free(fname);
5735 }
5736 if (pt != NULL && pt->pt_argc > 0)
5737 {
5738 ga_concat(&ga, (char_u *)", [");
5739 for (i = 0; i < pt->pt_argc; ++i)
5740 {
5741 if (i > 0)
5742 ga_concat(&ga, (char_u *)", ");
5743 ga_concat(&ga,
5744 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5745 vim_free(tf);
5746 }
5747 ga_concat(&ga, (char_u *)"]");
5748 }
5749 if (pt != NULL && pt->pt_dict != NULL)
5750 {
5751 typval_T dtv;
5752
5753 ga_concat(&ga, (char_u *)", ");
5754 dtv.v_type = VAR_DICT;
5755 dtv.vval.v_dict = pt->pt_dict;
5756 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5757 vim_free(tf);
5758 }
5759 ga_concat(&ga, (char_u *)")");
5760
5761 *tofree = ga.ga_data;
5762 r = *tofree;
5763 break;
5764 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005765
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005766 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005767 if (tv->vval.v_list == NULL)
5768 {
5769 *tofree = NULL;
5770 r = NULL;
5771 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005772 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5773 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005774 {
5775 *tofree = NULL;
5776 r = (char_u *)"[...]";
5777 }
5778 else
5779 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005780 int old_copyID = tv->vval.v_list->lv_copyID;
5781
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005782 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005783 *tofree = list2string(tv, copyID, restore_copyID);
5784 if (restore_copyID)
5785 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005786 r = *tofree;
5787 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005788 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005789
Bram Moolenaar8c711452005-01-14 21:53:12 +00005790 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005791 if (tv->vval.v_dict == NULL)
5792 {
5793 *tofree = NULL;
5794 r = NULL;
5795 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005796 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5797 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005798 {
5799 *tofree = NULL;
5800 r = (char_u *)"{...}";
5801 }
5802 else
5803 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005804 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005805 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005806 *tofree = dict2string(tv, copyID, restore_copyID);
5807 if (restore_copyID)
5808 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005809 r = *tofree;
5810 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005811 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005812
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005813 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005814 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005815 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005816 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005817 *tofree = NULL;
5818 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005819 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005820
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005821 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005822#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005823 *tofree = NULL;
5824 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5825 r = numbuf;
5826 break;
5827#endif
5828
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005829 case VAR_SPECIAL:
5830 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005831 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005832 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005833 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005834
Bram Moolenaar8502c702014-06-17 12:51:16 +02005835 if (--recurse == 0)
5836 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005837 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005838}
5839
5840/*
5841 * Return a string with the string representation of a variable.
5842 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5843 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005844 * Does not put quotes around strings, as ":echo" displays values.
5845 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5846 * May return NULL.
5847 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005848 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005849echo_string(
5850 typval_T *tv,
5851 char_u **tofree,
5852 char_u *numbuf,
5853 int copyID)
5854{
5855 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5856}
5857
5858/*
5859 * Return a string with the string representation of a variable.
5860 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5861 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005862 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005863 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005864 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02005865 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005866tv2string(
5867 typval_T *tv,
5868 char_u **tofree,
5869 char_u *numbuf,
5870 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005871{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005872 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005873}
5874
5875/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005876 * Return string "str" in ' quotes, doubling ' characters.
5877 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005878 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005879 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005880 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005881string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005882{
Bram Moolenaar33570922005-01-25 22:26:29 +00005883 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005884 char_u *p, *r, *s;
5885
Bram Moolenaar33570922005-01-25 22:26:29 +00005886 len = (function ? 13 : 3);
5887 if (str != NULL)
5888 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005889 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00005890 for (p = str; *p != NUL; mb_ptr_adv(p))
5891 if (*p == '\'')
5892 ++len;
5893 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005894 s = r = alloc(len);
5895 if (r != NULL)
5896 {
5897 if (function)
5898 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005899 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005900 r += 10;
5901 }
5902 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005903 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005904 if (str != NULL)
5905 for (p = str; *p != NUL; )
5906 {
5907 if (*p == '\'')
5908 *r++ = '\'';
5909 MB_COPY_CHAR(p, r);
5910 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005911 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005912 if (function)
5913 *r++ = ')';
5914 *r++ = NUL;
5915 }
5916 return s;
5917}
5918
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005919#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005920/*
5921 * Convert the string "text" to a floating point number.
5922 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5923 * this always uses a decimal point.
5924 * Returns the length of the text that was consumed.
5925 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005926 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005927string2float(
5928 char_u *text,
5929 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005930{
5931 char *s = (char *)text;
5932 float_T f;
5933
5934 f = strtod(s, &s);
5935 *value = f;
5936 return (int)((char_u *)s - text);
5937}
5938#endif
5939
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005940/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 * Get the value of an environment variable.
5942 * "arg" is pointing to the '$'. It is advanced to after the name.
5943 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005944 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 */
5946 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005947get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948{
5949 char_u *string = NULL;
5950 int len;
5951 int cc;
5952 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005953 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954
5955 ++*arg;
5956 name = *arg;
5957 len = get_env_len(arg);
5958 if (evaluate)
5959 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005960 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01005961 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005962
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005963 cc = name[len];
5964 name[len] = NUL;
5965 /* first try vim_getenv(), fast for normal environment vars */
5966 string = vim_getenv(name, &mustfree);
5967 if (string != NULL && *string != NUL)
5968 {
5969 if (!mustfree)
5970 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005972 else
5973 {
5974 if (mustfree)
5975 vim_free(string);
5976
5977 /* next try expanding things like $VIM and ${HOME} */
5978 string = expand_env_save(name - 1);
5979 if (string != NULL && *string == '$')
5980 {
5981 vim_free(string);
5982 string = NULL;
5983 }
5984 }
5985 name[len] = cc;
5986
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005987 rettv->v_type = VAR_STRING;
5988 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 }
5990
5991 return OK;
5992}
5993
Bram Moolenaard6e256c2011-12-14 15:32:50 +01005994
5995
5996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005998 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006000 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006001var2fpos(
6002 typval_T *varp,
6003 int dollar_lnum, /* TRUE when $ is last line */
6004 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006006 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006008 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009
Bram Moolenaara5525202006-03-02 22:52:09 +00006010 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006011 if (varp->v_type == VAR_LIST)
6012 {
6013 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006014 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006015 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006016 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006017
6018 l = varp->vval.v_list;
6019 if (l == NULL)
6020 return NULL;
6021
6022 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006023 pos.lnum = list_find_nr(l, 0L, &error);
6024 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006025 return NULL; /* invalid line number */
6026
6027 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006028 pos.col = list_find_nr(l, 1L, &error);
6029 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006030 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006031 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006032
6033 /* We accept "$" for the column number: last column. */
6034 li = list_find(l, 1L);
6035 if (li != NULL && li->li_tv.v_type == VAR_STRING
6036 && li->li_tv.vval.v_string != NULL
6037 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6038 pos.col = len + 1;
6039
Bram Moolenaara5525202006-03-02 22:52:09 +00006040 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006041 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006042 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006043 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006044
Bram Moolenaara5525202006-03-02 22:52:09 +00006045#ifdef FEAT_VIRTUALEDIT
6046 /* Get the virtual offset. Defaults to zero. */
6047 pos.coladd = list_find_nr(l, 2L, &error);
6048 if (error)
6049 pos.coladd = 0;
6050#endif
6051
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006052 return &pos;
6053 }
6054
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006055 name = get_tv_string_chk(varp);
6056 if (name == NULL)
6057 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006058 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006060 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6061 {
6062 if (VIsual_active)
6063 return &VIsual;
6064 return &curwin->w_cursor;
6065 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006066 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006068 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6070 return NULL;
6071 return pp;
6072 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006073
6074#ifdef FEAT_VIRTUALEDIT
6075 pos.coladd = 0;
6076#endif
6077
Bram Moolenaar477933c2007-07-17 14:32:23 +00006078 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006079 {
6080 pos.col = 0;
6081 if (name[1] == '0') /* "w0": first visible line */
6082 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006083 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006084 pos.lnum = curwin->w_topline;
6085 return &pos;
6086 }
6087 else if (name[1] == '$') /* "w$": last visible line */
6088 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006089 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006090 pos.lnum = curwin->w_botline - 1;
6091 return &pos;
6092 }
6093 }
6094 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006096 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 {
6098 pos.lnum = curbuf->b_ml.ml_line_count;
6099 pos.col = 0;
6100 }
6101 else
6102 {
6103 pos.lnum = curwin->w_cursor.lnum;
6104 pos.col = (colnr_T)STRLEN(ml_get_curline());
6105 }
6106 return &pos;
6107 }
6108 return NULL;
6109}
6110
6111/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006112 * Convert list in "arg" into a position and optional file number.
6113 * When "fnump" is NULL there is no file number, only 3 items.
6114 * Note that the column is passed on as-is, the caller may want to decrement
6115 * it to use 1 for the first column.
6116 * Return FAIL when conversion is not possible, doesn't check the position for
6117 * validity.
6118 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006119 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006120list2fpos(
6121 typval_T *arg,
6122 pos_T *posp,
6123 int *fnump,
6124 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006125{
6126 list_T *l = arg->vval.v_list;
6127 long i = 0;
6128 long n;
6129
Bram Moolenaar493c1782014-05-28 14:34:46 +02006130 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6131 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006132 if (arg->v_type != VAR_LIST
6133 || l == NULL
6134 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006135 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006136 return FAIL;
6137
6138 if (fnump != NULL)
6139 {
6140 n = list_find_nr(l, i++, NULL); /* fnum */
6141 if (n < 0)
6142 return FAIL;
6143 if (n == 0)
6144 n = curbuf->b_fnum; /* current buffer */
6145 *fnump = n;
6146 }
6147
6148 n = list_find_nr(l, i++, NULL); /* lnum */
6149 if (n < 0)
6150 return FAIL;
6151 posp->lnum = n;
6152
6153 n = list_find_nr(l, i++, NULL); /* col */
6154 if (n < 0)
6155 return FAIL;
6156 posp->col = n;
6157
6158#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +02006159 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006160 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006161 posp->coladd = 0;
6162 else
6163 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006164#endif
6165
Bram Moolenaar493c1782014-05-28 14:34:46 +02006166 if (curswantp != NULL)
6167 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6168
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006169 return OK;
6170}
6171
6172/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 * Get the length of an environment variable name.
6174 * Advance "arg" to the first character after the name.
6175 * Return 0 for error.
6176 */
6177 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006178get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179{
6180 char_u *p;
6181 int len;
6182
6183 for (p = *arg; vim_isIDc(*p); ++p)
6184 ;
6185 if (p == *arg) /* no name found */
6186 return 0;
6187
6188 len = (int)(p - *arg);
6189 *arg = p;
6190 return len;
6191}
6192
6193/*
6194 * Get the length of the name of a function or internal variable.
6195 * "arg" is advanced to the first non-white character after the name.
6196 * Return 0 if something is wrong.
6197 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006198 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006199get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200{
6201 char_u *p;
6202 int len;
6203
6204 /* Find the end of the name. */
6205 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006206 {
6207 if (*p == ':')
6208 {
6209 /* "s:" is start of "s:var", but "n:" is not and can be used in
6210 * slice "[n:]". Also "xx:" is not a namespace. */
6211 len = (int)(p - *arg);
6212 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6213 || len > 1)
6214 break;
6215 }
6216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 if (p == *arg) /* no name found */
6218 return 0;
6219
6220 len = (int)(p - *arg);
6221 *arg = skipwhite(p);
6222
6223 return len;
6224}
6225
6226/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006227 * Get the length of the name of a variable or function.
6228 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006230 * Return -1 if curly braces expansion failed.
6231 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 * If the name contains 'magic' {}'s, expand them and return the
6233 * expanded name in an allocated string via 'alias' - caller must free.
6234 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006235 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006236get_name_len(
6237 char_u **arg,
6238 char_u **alias,
6239 int evaluate,
6240 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241{
6242 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 char_u *p;
6244 char_u *expr_start;
6245 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246
6247 *alias = NULL; /* default to no alias */
6248
6249 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6250 && (*arg)[2] == (int)KE_SNR)
6251 {
6252 /* hard coded <SNR>, already translated */
6253 *arg += 3;
6254 return get_id_len(arg) + 3;
6255 }
6256 len = eval_fname_script(*arg);
6257 if (len > 0)
6258 {
6259 /* literal "<SID>", "s:" or "<SNR>" */
6260 *arg += len;
6261 }
6262
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006264 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006266 p = find_name_end(*arg, &expr_start, &expr_end,
6267 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 if (expr_start != NULL)
6269 {
6270 char_u *temp_string;
6271
6272 if (!evaluate)
6273 {
6274 len += (int)(p - *arg);
6275 *arg = skipwhite(p);
6276 return len;
6277 }
6278
6279 /*
6280 * Include any <SID> etc in the expanded string:
6281 * Thus the -len here.
6282 */
6283 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6284 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006285 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 *alias = temp_string;
6287 *arg = skipwhite(p);
6288 return (int)STRLEN(temp_string);
6289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290
6291 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006292 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 EMSG2(_(e_invexpr2), *arg);
6294
6295 return len;
6296}
6297
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006298/*
6299 * Find the end of a variable or function name, taking care of magic braces.
6300 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6301 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006302 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006303 * Return a pointer to just after the name. Equal to "arg" if there is no
6304 * valid name.
6305 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006306 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006307find_name_end(
6308 char_u *arg,
6309 char_u **expr_start,
6310 char_u **expr_end,
6311 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006313 int mb_nest = 0;
6314 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006316 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006318 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006320 *expr_start = NULL;
6321 *expr_end = NULL;
6322 }
6323
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006324 /* Quick check for valid starting character. */
6325 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6326 return arg;
6327
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006328 for (p = arg; *p != NUL
6329 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006330 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006331 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006332 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +00006333 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006334 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006335 if (*p == '\'')
6336 {
6337 /* skip over 'string' to avoid counting [ and ] inside it. */
6338 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
6339 ;
6340 if (*p == NUL)
6341 break;
6342 }
6343 else if (*p == '"')
6344 {
6345 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
6346 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
6347 if (*p == '\\' && p[1] != NUL)
6348 ++p;
6349 if (*p == NUL)
6350 break;
6351 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006352 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6353 {
6354 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006355 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006356 len = (int)(p - arg);
6357 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006358 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006359 break;
6360 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006361
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006362 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006364 if (*p == '[')
6365 ++br_nest;
6366 else if (*p == ']')
6367 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006369
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006370 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006372 if (*p == '{')
6373 {
6374 mb_nest++;
6375 if (expr_start != NULL && *expr_start == NULL)
6376 *expr_start = p;
6377 }
6378 else if (*p == '}')
6379 {
6380 mb_nest--;
6381 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6382 *expr_end = p;
6383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 }
6386
6387 return p;
6388}
6389
6390/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006391 * Expands out the 'magic' {}'s in a variable/function name.
6392 * Note that this can call itself recursively, to deal with
6393 * constructs like foo{bar}{baz}{bam}
6394 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6395 * "in_start" ^
6396 * "expr_start" ^
6397 * "expr_end" ^
6398 * "in_end" ^
6399 *
6400 * Returns a new allocated string, which the caller must free.
6401 * Returns NULL for failure.
6402 */
6403 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006404make_expanded_name(
6405 char_u *in_start,
6406 char_u *expr_start,
6407 char_u *expr_end,
6408 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006409{
6410 char_u c1;
6411 char_u *retval = NULL;
6412 char_u *temp_result;
6413 char_u *nextcmd = NULL;
6414
6415 if (expr_end == NULL || in_end == NULL)
6416 return NULL;
6417 *expr_start = NUL;
6418 *expr_end = NUL;
6419 c1 = *in_end;
6420 *in_end = NUL;
6421
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006422 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006423 if (temp_result != NULL && nextcmd == NULL)
6424 {
6425 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6426 + (in_end - expr_end) + 1));
6427 if (retval != NULL)
6428 {
6429 STRCPY(retval, in_start);
6430 STRCAT(retval, temp_result);
6431 STRCAT(retval, expr_end + 1);
6432 }
6433 }
6434 vim_free(temp_result);
6435
6436 *in_end = c1; /* put char back for error messages */
6437 *expr_start = '{';
6438 *expr_end = '}';
6439
6440 if (retval != NULL)
6441 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006442 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006443 if (expr_start != NULL)
6444 {
6445 /* Further expansion! */
6446 temp_result = make_expanded_name(retval, expr_start,
6447 expr_end, temp_result);
6448 vim_free(retval);
6449 retval = temp_result;
6450 }
6451 }
6452
6453 return retval;
6454}
6455
6456/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006458 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006460 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006461eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006463 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6464}
6465
6466/*
6467 * Return TRUE if character "c" can be used as the first character in a
6468 * variable or function name (excluding '{' and '}').
6469 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006470 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006471eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006472{
6473 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474}
6475
6476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 * Set number v: variable to "val".
6478 */
6479 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006480set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006482 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483}
6484
6485/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006486 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006488 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006489get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006491 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492}
6493
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006494/*
6495 * Get string v: variable value. Uses a static buffer, can only be used once.
6496 */
6497 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006498get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006499{
6500 return get_tv_string(&vimvars[idx].vv_tv);
6501}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006502
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006504 * Get List v: variable value. Caller must take care of reference count when
6505 * needed.
6506 */
6507 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006508get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006509{
6510 return vimvars[idx].vv_list;
6511}
6512
6513/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006514 * Set v:char to character "c".
6515 */
6516 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006517set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006518{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006519 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006520
6521#ifdef FEAT_MBYTE
6522 if (has_mbyte)
6523 buf[(*mb_char2bytes)(c, buf)] = NUL;
6524 else
6525#endif
6526 {
6527 buf[0] = c;
6528 buf[1] = NUL;
6529 }
6530 set_vim_var_string(VV_CHAR, buf, -1);
6531}
6532
6533/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006534 * Set v:count to "count" and v:count1 to "count1".
6535 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 */
6537 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006538set_vcount(
6539 long count,
6540 long count1,
6541 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006543 if (set_prevcount)
6544 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006545 vimvars[VV_COUNT].vv_nr = count;
6546 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547}
6548
6549/*
6550 * Set string v: variable to a copy of "val".
6551 */
6552 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006553set_vim_var_string(
6554 int idx,
6555 char_u *val,
6556 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557{
Bram Moolenaara542c682016-01-31 16:28:04 +01006558 clear_tv(&vimvars[idx].vv_di.di_tv);
6559 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006561 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006563 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006565 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566}
6567
6568/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006569 * Set List v: variable to "val".
6570 */
6571 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006572set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006573{
Bram Moolenaara542c682016-01-31 16:28:04 +01006574 clear_tv(&vimvars[idx].vv_di.di_tv);
6575 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006576 vimvars[idx].vv_list = val;
6577 if (val != NULL)
6578 ++val->lv_refcount;
6579}
6580
6581/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006582 * Set Dictionary v: variable to "val".
6583 */
6584 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006585set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006586{
6587 int todo;
6588 hashitem_T *hi;
6589
Bram Moolenaara542c682016-01-31 16:28:04 +01006590 clear_tv(&vimvars[idx].vv_di.di_tv);
6591 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006592 vimvars[idx].vv_dict = val;
6593 if (val != NULL)
6594 {
6595 ++val->dv_refcount;
6596
6597 /* Set readonly */
6598 todo = (int)val->dv_hashtab.ht_used;
6599 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
6600 {
6601 if (HASHITEM_EMPTY(hi))
6602 continue;
6603 --todo;
6604 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
6605 }
6606 }
6607}
6608
6609/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 * Set v:register if needed.
6611 */
6612 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006613set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614{
6615 char_u regname;
6616
6617 if (c == 0 || c == ' ')
6618 regname = '"';
6619 else
6620 regname = c;
6621 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006622 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 set_vim_var_string(VV_REG, &regname, 1);
6624}
6625
6626/*
6627 * Get or set v:exception. If "oldval" == NULL, return the current value.
6628 * Otherwise, restore the value to "oldval" and return NULL.
6629 * Must always be called in pairs to save and restore v:exception! Does not
6630 * take care of memory allocations.
6631 */
6632 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006633v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634{
6635 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006636 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637
Bram Moolenaare9a41262005-01-15 22:18:47 +00006638 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 return NULL;
6640}
6641
6642/*
6643 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
6644 * Otherwise, restore the value to "oldval" and return NULL.
6645 * Must always be called in pairs to save and restore v:throwpoint! Does not
6646 * take care of memory allocations.
6647 */
6648 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006649v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650{
6651 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006652 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653
Bram Moolenaare9a41262005-01-15 22:18:47 +00006654 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 return NULL;
6656}
6657
6658#if defined(FEAT_AUTOCMD) || defined(PROTO)
6659/*
6660 * Set v:cmdarg.
6661 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6662 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6663 * Must always be called in pairs!
6664 */
6665 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006666set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667{
6668 char_u *oldval;
6669 char_u *newval;
6670 unsigned len;
6671
Bram Moolenaare9a41262005-01-15 22:18:47 +00006672 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006673 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006675 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006676 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006677 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 }
6679
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006680 if (eap->force_bin == FORCE_BIN)
6681 len = 6;
6682 else if (eap->force_bin == FORCE_NOBIN)
6683 len = 8;
6684 else
6685 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006686
6687 if (eap->read_edit)
6688 len += 7;
6689
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006690 if (eap->force_ff != 0)
6691 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
6692# ifdef FEAT_MBYTE
6693 if (eap->force_enc != 0)
6694 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006695 if (eap->bad_char != 0)
6696 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006697# endif
6698
6699 newval = alloc(len + 1);
6700 if (newval == NULL)
6701 return NULL;
6702
6703 if (eap->force_bin == FORCE_BIN)
6704 sprintf((char *)newval, " ++bin");
6705 else if (eap->force_bin == FORCE_NOBIN)
6706 sprintf((char *)newval, " ++nobin");
6707 else
6708 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006709
6710 if (eap->read_edit)
6711 STRCAT(newval, " ++edit");
6712
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006713 if (eap->force_ff != 0)
6714 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
6715 eap->cmd + eap->force_ff);
6716# ifdef FEAT_MBYTE
6717 if (eap->force_enc != 0)
6718 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6719 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006720 if (eap->bad_char == BAD_KEEP)
6721 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6722 else if (eap->bad_char == BAD_DROP)
6723 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6724 else if (eap->bad_char != 0)
6725 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006726# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00006727 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006728 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729}
6730#endif
6731
6732/*
6733 * Get the value of internal variable "name".
6734 * Return OK or FAIL.
6735 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006736 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006737get_var_tv(
6738 char_u *name,
6739 int len, /* length of "name" */
6740 typval_T *rettv, /* NULL when only checking existence */
6741 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
6742 int verbose, /* may give error message */
6743 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744{
6745 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006746 typval_T *tv = NULL;
6747 typval_T atv;
6748 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750
6751 /* truncate the name, so that we can use strcmp() */
6752 cc = name[len];
6753 name[len] = NUL;
6754
6755 /*
6756 * Check for "b:changedtick".
6757 */
6758 if (STRCMP(name, "b:changedtick") == 0)
6759 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00006760 atv.v_type = VAR_NUMBER;
6761 atv.vval.v_number = curbuf->b_changedtick;
6762 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 }
6764
6765 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 * Check for user-defined variables.
6767 */
6768 else
6769 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01006770 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02006772 {
Bram Moolenaar33570922005-01-25 22:26:29 +00006773 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02006774 if (dip != NULL)
6775 *dip = v;
6776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 }
6778
Bram Moolenaare9a41262005-01-15 22:18:47 +00006779 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006781 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006782 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 ret = FAIL;
6784 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006785 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006786 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787
6788 name[len] = cc;
6789
6790 return ret;
6791}
6792
6793/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006794 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
6795 * Also handle function call with Funcref variable: func(expr)
6796 * Can all be combined: dict.func(expr)[idx]['func'](expr)
6797 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006798 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006799handle_subscript(
6800 char_u **arg,
6801 typval_T *rettv,
6802 int evaluate, /* do more than finding the end */
6803 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006804{
6805 int ret = OK;
6806 dict_T *selfdict = NULL;
6807 char_u *s;
6808 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006809 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006810
6811 while (ret == OK
6812 && (**arg == '['
6813 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006814 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6815 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006816 && !vim_iswhite(*(*arg - 1)))
6817 {
6818 if (**arg == '(')
6819 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006820 partial_T *pt = NULL;
6821
Bram Moolenaard9fba312005-06-26 22:34:35 +00006822 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006823 if (evaluate)
6824 {
6825 functv = *rettv;
6826 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006827
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006828 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006829 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006830 {
6831 pt = functv.vval.v_partial;
6832 s = pt->pt_name;
6833 }
6834 else
6835 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006836 }
6837 else
6838 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006839 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00006840 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006841 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006842
6843 /* Clear the funcref afterwards, so that deleting it while
6844 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006845 if (evaluate)
6846 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006847
6848 /* Stop the expression evaluation when immediately aborting on
6849 * error, or when an interrupt occurred or an exception was thrown
6850 * but not caught. */
6851 if (aborting())
6852 {
6853 if (ret == OK)
6854 clear_tv(rettv);
6855 ret = FAIL;
6856 }
6857 dict_unref(selfdict);
6858 selfdict = NULL;
6859 }
6860 else /* **arg == '[' || **arg == '.' */
6861 {
6862 dict_unref(selfdict);
6863 if (rettv->v_type == VAR_DICT)
6864 {
6865 selfdict = rettv->vval.v_dict;
6866 if (selfdict != NULL)
6867 ++selfdict->dv_refcount;
6868 }
6869 else
6870 selfdict = NULL;
6871 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
6872 {
6873 clear_tv(rettv);
6874 ret = FAIL;
6875 }
6876 }
6877 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006878
Bram Moolenaar1d429612016-05-24 15:44:17 +02006879 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
6880 * Don't do this when "Func" is already a partial that was bound
6881 * explicitly (pt_auto is FALSE). */
6882 if (selfdict != NULL
6883 && (rettv->v_type == VAR_FUNC
6884 || (rettv->v_type == VAR_PARTIAL
6885 && (rettv->vval.v_partial->pt_auto
6886 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006887 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006888
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006889 dict_unref(selfdict);
6890 return ret;
6891}
6892
6893/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006894 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006895 * value).
6896 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01006897 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006898alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006899{
Bram Moolenaar33570922005-01-25 22:26:29 +00006900 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006901}
6902
6903/*
6904 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 * The string "s" must have been allocated, it is consumed.
6906 * Return NULL for out of memory, the variable otherwise.
6907 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006908 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006909alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910{
Bram Moolenaar33570922005-01-25 22:26:29 +00006911 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006913 rettv = alloc_tv();
6914 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006916 rettv->v_type = VAR_STRING;
6917 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 }
6919 else
6920 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006921 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922}
6923
6924/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006925 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006927 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006928free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929{
6930 if (varp != NULL)
6931 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006932 switch (varp->v_type)
6933 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006934 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006935 func_unref(varp->vval.v_string);
6936 /*FALLTHROUGH*/
6937 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006938 vim_free(varp->vval.v_string);
6939 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006940 case VAR_PARTIAL:
6941 partial_unref(varp->vval.v_partial);
6942 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006943 case VAR_LIST:
6944 list_unref(varp->vval.v_list);
6945 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006946 case VAR_DICT:
6947 dict_unref(varp->vval.v_dict);
6948 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006949 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006950#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006951 job_unref(varp->vval.v_job);
6952 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006953#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006954 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006955#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006956 channel_unref(varp->vval.v_channel);
6957 break;
6958#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006959 case VAR_NUMBER:
6960 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00006961 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01006962 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00006963 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 vim_free(varp);
6966 }
6967}
6968
6969/*
6970 * Free the memory for a variable value and set the value to NULL or 0.
6971 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006972 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006973clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974{
6975 if (varp != NULL)
6976 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006977 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006979 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006980 func_unref(varp->vval.v_string);
6981 /*FALLTHROUGH*/
6982 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006983 vim_free(varp->vval.v_string);
6984 varp->vval.v_string = NULL;
6985 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006986 case VAR_PARTIAL:
6987 partial_unref(varp->vval.v_partial);
6988 varp->vval.v_partial = NULL;
6989 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006990 case VAR_LIST:
6991 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006992 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006993 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006994 case VAR_DICT:
6995 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006996 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006997 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006998 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006999 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007000 varp->vval.v_number = 0;
7001 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007002 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007003#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007004 varp->vval.v_float = 0.0;
7005 break;
7006#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007007 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007008#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007009 job_unref(varp->vval.v_job);
7010 varp->vval.v_job = NULL;
7011#endif
7012 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007013 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007014#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007015 channel_unref(varp->vval.v_channel);
7016 varp->vval.v_channel = NULL;
7017#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007018 case VAR_UNKNOWN:
7019 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007021 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 }
7023}
7024
7025/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007026 * Set the value of a variable to NULL without freeing items.
7027 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007028 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007029init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007030{
7031 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007032 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007033}
7034
7035/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 * Get the number value of a variable.
7037 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007038 * For incompatible types, return 0.
7039 * get_tv_number_chk() is similar to get_tv_number(), but informs the
7040 * caller of incompatible types: it sets *denote to TRUE if "denote"
7041 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007043 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007044get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007046 int error = FALSE;
7047
7048 return get_tv_number_chk(varp, &error); /* return 0L on error */
7049}
7050
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007051 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007052get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007053{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007054 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007056 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007058 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007059 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007060 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007061#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +00007062 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007063 break;
7064#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007065 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007066 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007067 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007068 break;
7069 case VAR_STRING:
7070 if (varp->vval.v_string != NULL)
7071 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01007072 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007073 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007074 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007075 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007076 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007077 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007078 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007079 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007080 case VAR_SPECIAL:
7081 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7082 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007083 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007084#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007085 EMSG(_("E910: Using a Job as a Number"));
7086 break;
7087#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007088 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007089#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007090 EMSG(_("E913: Using a Channel as a Number"));
7091 break;
7092#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007093 case VAR_UNKNOWN:
7094 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007095 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007097 if (denote == NULL) /* useful for values that must be unsigned */
7098 n = -1;
7099 else
7100 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007101 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102}
7103
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007104#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007105 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007106get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007107{
7108 switch (varp->v_type)
7109 {
7110 case VAR_NUMBER:
7111 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007112 case VAR_FLOAT:
7113 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007114 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007115 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007116 EMSG(_("E891: Using a Funcref as a Float"));
7117 break;
7118 case VAR_STRING:
7119 EMSG(_("E892: Using a String as a Float"));
7120 break;
7121 case VAR_LIST:
7122 EMSG(_("E893: Using a List as a Float"));
7123 break;
7124 case VAR_DICT:
7125 EMSG(_("E894: Using a Dictionary as a Float"));
7126 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007127 case VAR_SPECIAL:
7128 EMSG(_("E907: Using a special value as a Float"));
7129 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007130 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007131# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007132 EMSG(_("E911: Using a Job as a Float"));
7133 break;
7134# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007135 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007136# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007137 EMSG(_("E914: Using a Channel as a Float"));
7138 break;
7139# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007140 case VAR_UNKNOWN:
7141 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007142 break;
7143 }
7144 return 0;
7145}
7146#endif
7147
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 * Get the string value of a variable.
7150 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +00007151 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7152 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 * If the String variable has never been set, return an empty string.
7154 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007155 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
7156 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007158 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007159get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160{
7161 static char_u mybuf[NUMBUFLEN];
7162
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007163 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164}
7165
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007166 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007167get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007169 char_u *res = get_tv_string_buf_chk(varp, buf);
7170
7171 return res != NULL ? res : (char_u *)"";
7172}
7173
Bram Moolenaar7d647822014-04-05 21:28:56 +02007174/*
7175 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7176 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007177 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007178get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007179{
7180 static char_u mybuf[NUMBUFLEN];
7181
7182 return get_tv_string_buf_chk(varp, mybuf);
7183}
7184
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007185 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007186get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007187{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007188 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007190 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007191 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
7192 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007193 return buf;
7194 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007195 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007196 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007197 break;
7198 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007199 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007200 break;
7201 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007202 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007203 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007204 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007205#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +02007206 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007207 break;
7208#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007209 case VAR_STRING:
7210 if (varp->vval.v_string != NULL)
7211 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007212 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007213 case VAR_SPECIAL:
7214 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7215 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007216 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007217#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007218 {
7219 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007220 char *status;
7221
7222 if (job == NULL)
7223 return (char_u *)"no process";
7224 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007225 : job->jv_status == JOB_ENDED ? "dead"
7226 : "run";
7227# ifdef UNIX
7228 vim_snprintf((char *)buf, NUMBUFLEN,
7229 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007230# elif defined(WIN32)
7231 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007232 "process %ld %s",
7233 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007234 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007235# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007236 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007237 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7238# endif
7239 return buf;
7240 }
7241#endif
7242 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007243 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007244#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007245 {
7246 channel_T *channel = varp->vval.v_channel;
7247 char *status = channel_status(channel);
7248
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007249 if (channel == NULL)
7250 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7251 else
7252 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007253 "channel %d %s", channel->ch_id, status);
7254 return buf;
7255 }
7256#endif
7257 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007258 case VAR_UNKNOWN:
7259 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007260 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007262 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263}
7264
7265/*
7266 * Find variable "name" in the list of variables.
7267 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007268 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007269 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007270 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007272 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007273find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007276 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277
Bram Moolenaara7043832005-01-21 11:56:39 +00007278 ht = find_var_ht(name, &varname);
7279 if (htp != NULL)
7280 *htp = ht;
7281 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007283 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284}
7285
7286/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007287 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007288 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007290 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007291find_var_in_ht(
7292 hashtab_T *ht,
7293 int htname,
7294 char_u *varname,
7295 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007296{
Bram Moolenaar33570922005-01-25 22:26:29 +00007297 hashitem_T *hi;
7298
7299 if (*varname == NUL)
7300 {
7301 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007302 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007303 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007304 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007305 case 'g': return &globvars_var;
7306 case 'v': return &vimvars_var;
7307 case 'b': return &curbuf->b_bufvar;
7308 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007309#ifdef FEAT_WINDOWS
7310 case 't': return &curtab->tp_winvar;
7311#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007312 case 'l': return get_funccal_local_var();
7313 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007314 }
7315 return NULL;
7316 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007317
7318 hi = hash_find(ht, varname);
7319 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007320 {
7321 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007322 * worked find the variable again. Don't auto-load a script if it was
7323 * loaded already, otherwise it would be loaded every time when
7324 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007325 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007326 {
7327 /* Note: script_autoload() may make "hi" invalid. It must either
7328 * be obtained again or not used. */
7329 if (!script_autoload(varname, FALSE) || aborting())
7330 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007331 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007332 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007333 if (HASHITEM_EMPTY(hi))
7334 return NULL;
7335 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007336 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007337}
7338
7339/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007340 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007341 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007342 * Set "varname" to the start of name without ':'.
7343 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007344 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007345find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007347 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007348 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007349
Bram Moolenaar73627d02015-08-11 15:46:09 +02007350 if (name[0] == NUL)
7351 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 if (name[1] != ':')
7353 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007354 /* The name must not start with a colon or #. */
7355 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 return NULL;
7357 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007358
7359 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007360 hi = hash_find(&compat_hashtab, name);
7361 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +00007362 return &compat_hashtab;
7363
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007364 ht = get_funccal_local_ht();
7365 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007366 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007367 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 }
7369 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007370 if (*name == 'g') /* global variable */
7371 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007372 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7373 */
7374 if (vim_strchr(name + 2, ':') != NULL
7375 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007376 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007378 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007380 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007381#ifdef FEAT_WINDOWS
7382 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007383 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007384#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007385 if (*name == 'v') /* v: variable */
7386 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007387 if (*name == 'a') /* a: function argument */
7388 return get_funccal_args_ht();
7389 if (*name == 'l') /* l: local function variable */
7390 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 if (*name == 's' /* script variable */
7392 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
7393 return &SCRIPT_VARS(current_SID);
7394 return NULL;
7395}
7396
7397/*
7398 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +02007399 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 * Returns NULL when it doesn't exist.
7401 */
7402 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007403get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404{
Bram Moolenaar33570922005-01-25 22:26:29 +00007405 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007407 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 if (v == NULL)
7409 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007410 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411}
7412
7413/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007414 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 * sourcing this script and when executing functions defined in the script.
7416 */
7417 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007418new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419{
Bram Moolenaara7043832005-01-21 11:56:39 +00007420 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007421 hashtab_T *ht;
7422 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007423
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7425 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007426 /* Re-allocating ga_data means that an ht_array pointing to
7427 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007428 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007429 for (i = 1; i <= ga_scripts.ga_len; ++i)
7430 {
7431 ht = &SCRIPT_VARS(i);
7432 if (ht->ht_mask == HT_INIT_SIZE - 1)
7433 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007434 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007435 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007436 }
7437
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 while (ga_scripts.ga_len < id)
7439 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007440 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007441 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007442 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 }
7445 }
7446}
7447
7448/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007449 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7450 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 */
7452 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007453init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454{
Bram Moolenaar33570922005-01-25 22:26:29 +00007455 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007456 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007457 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007458 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007459 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007460 dict_var->di_tv.vval.v_dict = dict;
7461 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007462 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007463 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7464 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465}
7466
7467/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007468 * Unreference a dictionary initialized by init_var_dict().
7469 */
7470 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007471unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007472{
7473 /* Now the dict needs to be freed if no one else is using it, go back to
7474 * normal reference counting. */
7475 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7476 dict_unref(dict);
7477}
7478
7479/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007481 * Frees all allocated variables and the value they contain.
7482 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 */
7484 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007485vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007486{
7487 vars_clear_ext(ht, TRUE);
7488}
7489
7490/*
7491 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7492 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007493 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007494vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495{
Bram Moolenaara7043832005-01-21 11:56:39 +00007496 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007497 hashitem_T *hi;
7498 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499
Bram Moolenaar33570922005-01-25 22:26:29 +00007500 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007501 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007502 for (hi = ht->ht_array; todo > 0; ++hi)
7503 {
7504 if (!HASHITEM_EMPTY(hi))
7505 {
7506 --todo;
7507
Bram Moolenaar33570922005-01-25 22:26:29 +00007508 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007509 * ht_array might change then. hash_clear() takes care of it
7510 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007511 v = HI2DI(hi);
7512 if (free_val)
7513 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007514 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007515 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007516 }
7517 }
7518 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007519 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520}
7521
Bram Moolenaara7043832005-01-21 11:56:39 +00007522/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007523 * Delete a variable from hashtab "ht" at item "hi".
7524 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007525 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007527delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528{
Bram Moolenaar33570922005-01-25 22:26:29 +00007529 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007530
7531 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007532 clear_tv(&di->di_tv);
7533 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534}
7535
7536/*
7537 * List the value of one internal variable.
7538 */
7539 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007540list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007542 char_u *tofree;
7543 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007544 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007545
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007546 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007547 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007548 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007549 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550}
7551
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007553list_one_var_a(
7554 char_u *prefix,
7555 char_u *name,
7556 int type,
7557 char_u *string,
7558 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559{
Bram Moolenaar31859182007-08-14 20:41:13 +00007560 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7561 msg_start();
7562 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 if (name != NULL) /* "a:" vars don't have a name stored */
7564 msg_puts(name);
7565 msg_putchar(' ');
7566 msg_advance(22);
7567 if (type == VAR_NUMBER)
7568 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007569 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007570 msg_putchar('*');
7571 else if (type == VAR_LIST)
7572 {
7573 msg_putchar('[');
7574 if (*string == '[')
7575 ++string;
7576 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007577 else if (type == VAR_DICT)
7578 {
7579 msg_putchar('{');
7580 if (*string == '{')
7581 ++string;
7582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 else
7584 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007585
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007587
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007588 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007589 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007590 if (*first)
7591 {
7592 msg_clr_eos();
7593 *first = FALSE;
7594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595}
7596
7597/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007598 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 * If the variable already exists, the value is updated.
7600 * Otherwise the variable is created.
7601 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007602 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007603set_var(
7604 char_u *name,
7605 typval_T *tv,
7606 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607{
Bram Moolenaar33570922005-01-25 22:26:29 +00007608 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007610 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007612 ht = find_var_ht(name, &varname);
7613 if (ht == NULL || *varname == NUL)
7614 {
7615 EMSG2(_(e_illvar), name);
7616 return;
7617 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007618 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007619
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007620 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7621 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007622 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007623
Bram Moolenaar33570922005-01-25 22:26:29 +00007624 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007626 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007627 if (var_check_ro(v->di_flags, name, FALSE)
7628 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007629 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007630
7631 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007632 * Handle setting internal v: variables separately where needed to
7633 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007634 */
7635 if (ht == &vimvarht)
7636 {
7637 if (v->di_tv.v_type == VAR_STRING)
7638 {
7639 vim_free(v->di_tv.vval.v_string);
7640 if (copy || tv->v_type != VAR_STRING)
7641 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
7642 else
7643 {
7644 /* Take over the string to avoid an extra alloc/free. */
7645 v->di_tv.vval.v_string = tv->vval.v_string;
7646 tv->vval.v_string = NULL;
7647 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007648 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007649 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007650 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007651 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007652 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007653 if (STRCMP(varname, "searchforward") == 0)
7654 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007655#ifdef FEAT_SEARCH_EXTRA
7656 else if (STRCMP(varname, "hlsearch") == 0)
7657 {
7658 no_hlsearch = !v->di_tv.vval.v_number;
7659 redraw_all_later(SOME_VALID);
7660 }
7661#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007662 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007663 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007664 else if (v->di_tv.v_type != tv->v_type)
7665 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +00007666 }
7667
7668 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 }
7670 else /* add a new variable */
7671 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007672 /* Can't add "v:" variable. */
7673 if (ht == &vimvarht)
7674 {
7675 EMSG2(_(e_illvar), name);
7676 return;
7677 }
7678
Bram Moolenaar92124a32005-06-17 22:03:40 +00007679 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007680 if (!valid_varname(varname))
7681 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00007682
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007683 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7684 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00007685 if (v == NULL)
7686 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007687 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00007688 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007690 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 return;
7692 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007693 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007695
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007696 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00007697 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007698 else
7699 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007700 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007701 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007702 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704}
7705
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007706/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007707 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00007708 * Also give an error message.
7709 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007710 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007711var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00007712{
7713 if (flags & DI_FLAGS_RO)
7714 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007715 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007716 return TRUE;
7717 }
7718 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
7719 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007720 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007721 return TRUE;
7722 }
7723 return FALSE;
7724}
7725
7726/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007727 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
7728 * Also give an error message.
7729 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007730 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007731var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007732{
7733 if (flags & DI_FLAGS_FIX)
7734 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007735 EMSG2(_("E795: Cannot delete variable %s"),
7736 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007737 return TRUE;
7738 }
7739 return FALSE;
7740}
7741
7742/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007743 * Check if a funcref is assigned to a valid variable name.
7744 * Return TRUE and give an error if not.
7745 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007746 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007747var_check_func_name(
7748 char_u *name, /* points to start of variable name */
7749 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007750{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02007751 /* Allow for w: b: s: and t:. */
7752 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007753 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
7754 ? name[2] : name[0]))
7755 {
7756 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
7757 name);
7758 return TRUE;
7759 }
7760 /* Don't allow hiding a function. When "v" is not NULL we might be
7761 * assigning another function to the same var, the type is checked
7762 * below. */
7763 if (new_var && function_exists(name))
7764 {
7765 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
7766 name);
7767 return TRUE;
7768 }
7769 return FALSE;
7770}
7771
7772/*
7773 * Check if a variable name is valid.
7774 * Return FALSE and give an error if not.
7775 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007776 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007777valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007778{
7779 char_u *p;
7780
7781 for (p = varname; *p != NUL; ++p)
7782 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
7783 && *p != AUTOLOAD_CHAR)
7784 {
7785 EMSG2(_(e_illvar), varname);
7786 return FALSE;
7787 }
7788 return TRUE;
7789}
7790
7791/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007792 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02007793 * Also give an error message, using "name" or _("name") when use_gettext is
7794 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007795 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007796 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007797tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007798{
7799 if (lock & VAR_LOCKED)
7800 {
7801 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007802 name == NULL ? (char_u *)_("Unknown")
7803 : use_gettext ? (char_u *)_(name)
7804 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007805 return TRUE;
7806 }
7807 if (lock & VAR_FIXED)
7808 {
7809 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007810 name == NULL ? (char_u *)_("Unknown")
7811 : use_gettext ? (char_u *)_(name)
7812 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007813 return TRUE;
7814 }
7815 return FALSE;
7816}
7817
7818/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007819 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007820 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007821 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007822 * It is OK for "from" and "to" to point to the same item. This is used to
7823 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007824 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007825 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007826copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007828 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007829 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007830 switch (from->v_type)
7831 {
7832 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007833 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007834 to->vval.v_number = from->vval.v_number;
7835 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007836 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007837#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007838 to->vval.v_float = from->vval.v_float;
7839 break;
7840#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007841 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007842#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007843 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007844 if (to->vval.v_job != NULL)
7845 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007846 break;
7847#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007848 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007849#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007850 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007851 if (to->vval.v_channel != NULL)
7852 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01007853 break;
7854#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007855 case VAR_STRING:
7856 case VAR_FUNC:
7857 if (from->vval.v_string == NULL)
7858 to->vval.v_string = NULL;
7859 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007860 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007861 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007862 if (from->v_type == VAR_FUNC)
7863 func_ref(to->vval.v_string);
7864 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007865 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007866 case VAR_PARTIAL:
7867 if (from->vval.v_partial == NULL)
7868 to->vval.v_partial = NULL;
7869 else
7870 {
7871 to->vval.v_partial = from->vval.v_partial;
7872 ++to->vval.v_partial->pt_refcount;
7873 }
7874 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007875 case VAR_LIST:
7876 if (from->vval.v_list == NULL)
7877 to->vval.v_list = NULL;
7878 else
7879 {
7880 to->vval.v_list = from->vval.v_list;
7881 ++to->vval.v_list->lv_refcount;
7882 }
7883 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007884 case VAR_DICT:
7885 if (from->vval.v_dict == NULL)
7886 to->vval.v_dict = NULL;
7887 else
7888 {
7889 to->vval.v_dict = from->vval.v_dict;
7890 ++to->vval.v_dict->dv_refcount;
7891 }
7892 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007893 case VAR_UNKNOWN:
7894 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007895 break;
7896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897}
7898
7899/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007900 * Make a copy of an item.
7901 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007902 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7903 * reference to an already copied list/dict can be used.
7904 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007905 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007906 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007907item_copy(
7908 typval_T *from,
7909 typval_T *to,
7910 int deep,
7911 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007912{
7913 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007914 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007915
Bram Moolenaar33570922005-01-25 22:26:29 +00007916 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007917 {
7918 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007919 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007920 }
7921 ++recurse;
7922
7923 switch (from->v_type)
7924 {
7925 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007926 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007927 case VAR_STRING:
7928 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007929 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007930 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007931 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007932 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007933 copy_tv(from, to);
7934 break;
7935 case VAR_LIST:
7936 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007937 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007938 if (from->vval.v_list == NULL)
7939 to->vval.v_list = NULL;
7940 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
7941 {
7942 /* use the copy made earlier */
7943 to->vval.v_list = from->vval.v_list->lv_copylist;
7944 ++to->vval.v_list->lv_refcount;
7945 }
7946 else
7947 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
7948 if (to->vval.v_list == NULL)
7949 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007950 break;
7951 case VAR_DICT:
7952 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007953 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007954 if (from->vval.v_dict == NULL)
7955 to->vval.v_dict = NULL;
7956 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7957 {
7958 /* use the copy made earlier */
7959 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7960 ++to->vval.v_dict->dv_refcount;
7961 }
7962 else
7963 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
7964 if (to->vval.v_dict == NULL)
7965 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007966 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007967 case VAR_UNKNOWN:
7968 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007969 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007970 }
7971 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007972 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007973}
7974
7975/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007976 * This function is used by f_input() and f_inputdialog() functions. The third
7977 * argument to f_input() specifies the type of completion to use at the
7978 * prompt. The third argument to f_inputdialog() specifies the value to return
7979 * when the user cancels the prompt.
7980 */
7981 void
7982get_user_input(
7983 typval_T *argvars,
7984 typval_T *rettv,
7985 int inputdialog,
7986 int secret)
7987{
7988 char_u *prompt = get_tv_string_chk(&argvars[0]);
7989 char_u *p = NULL;
7990 int c;
7991 char_u buf[NUMBUFLEN];
7992 int cmd_silent_save = cmd_silent;
7993 char_u *defstr = (char_u *)"";
7994 int xp_type = EXPAND_NOTHING;
7995 char_u *xp_arg = NULL;
7996
7997 rettv->v_type = VAR_STRING;
7998 rettv->vval.v_string = NULL;
7999
8000#ifdef NO_CONSOLE_INPUT
8001 /* While starting up, there is no place to enter text. */
8002 if (no_console_input())
8003 return;
8004#endif
8005
8006 cmd_silent = FALSE; /* Want to see the prompt. */
8007 if (prompt != NULL)
8008 {
8009 /* Only the part of the message after the last NL is considered as
8010 * prompt for the command line */
8011 p = vim_strrchr(prompt, '\n');
8012 if (p == NULL)
8013 p = prompt;
8014 else
8015 {
8016 ++p;
8017 c = *p;
8018 *p = NUL;
8019 msg_start();
8020 msg_clr_eos();
8021 msg_puts_attr(prompt, echo_attr);
8022 msg_didout = FALSE;
8023 msg_starthere();
8024 *p = c;
8025 }
8026 cmdline_row = msg_row;
8027
8028 if (argvars[1].v_type != VAR_UNKNOWN)
8029 {
8030 defstr = get_tv_string_buf_chk(&argvars[1], buf);
8031 if (defstr != NULL)
8032 stuffReadbuffSpec(defstr);
8033
8034 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8035 {
8036 char_u *xp_name;
8037 int xp_namelen;
8038 long argt;
8039
8040 /* input() with a third argument: completion */
8041 rettv->vval.v_string = NULL;
8042
8043 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
8044 if (xp_name == NULL)
8045 return;
8046
8047 xp_namelen = (int)STRLEN(xp_name);
8048
8049 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8050 &xp_arg) == FAIL)
8051 return;
8052 }
8053 }
8054
8055 if (defstr != NULL)
8056 {
8057 int save_ex_normal_busy = ex_normal_busy;
8058 ex_normal_busy = 0;
8059 rettv->vval.v_string =
8060 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8061 xp_type, xp_arg);
8062 ex_normal_busy = save_ex_normal_busy;
8063 }
8064 if (inputdialog && rettv->vval.v_string == NULL
8065 && argvars[1].v_type != VAR_UNKNOWN
8066 && argvars[2].v_type != VAR_UNKNOWN)
8067 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
8068 &argvars[2], buf));
8069
8070 vim_free(xp_arg);
8071
8072 /* since the user typed this, no need to wait for return */
8073 need_wait_return = FALSE;
8074 msg_didout = FALSE;
8075 }
8076 cmd_silent = cmd_silent_save;
8077}
8078
8079/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 * ":echo expr1 ..." print each argument separated with a space, add a
8081 * newline at the end.
8082 * ":echon expr1 ..." print each argument plain.
8083 */
8084 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008085ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086{
8087 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008088 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008089 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 char_u *p;
8091 int needclr = TRUE;
8092 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008093 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094
8095 if (eap->skip)
8096 ++emsg_skip;
8097 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8098 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008099 /* If eval1() causes an error message the text from the command may
8100 * still need to be cleared. E.g., "echo 22,44". */
8101 need_clr_eos = needclr;
8102
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008104 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 {
8106 /*
8107 * Report the invalid expression unless the expression evaluation
8108 * has been cancelled due to an aborting error, an interrupt, or an
8109 * exception.
8110 */
8111 if (!aborting())
8112 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008113 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 break;
8115 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008116 need_clr_eos = FALSE;
8117
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 if (!eap->skip)
8119 {
8120 if (atstart)
8121 {
8122 atstart = FALSE;
8123 /* Call msg_start() after eval1(), evaluating the expression
8124 * may cause a message to appear. */
8125 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008126 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008127 /* Mark the saved text as finishing the line, so that what
8128 * follows is displayed on a new line when scrolling back
8129 * at the more prompt. */
8130 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 }
8134 else if (eap->cmdidx == CMD_echo)
8135 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008136 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008137 if (p != NULL)
8138 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008140 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008142 if (*p != TAB && needclr)
8143 {
8144 /* remove any text still there from the command */
8145 msg_clr_eos();
8146 needclr = FALSE;
8147 }
8148 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 }
8150 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008151 {
8152#ifdef FEAT_MBYTE
8153 if (has_mbyte)
8154 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008155 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008156
8157 (void)msg_outtrans_len_attr(p, i, echo_attr);
8158 p += i - 1;
8159 }
8160 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008162 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008165 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008167 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 arg = skipwhite(arg);
8169 }
8170 eap->nextcmd = check_nextcmd(arg);
8171
8172 if (eap->skip)
8173 --emsg_skip;
8174 else
8175 {
8176 /* remove text that may still be there from the command */
8177 if (needclr)
8178 msg_clr_eos();
8179 if (eap->cmdidx == CMD_echo)
8180 msg_end();
8181 }
8182}
8183
8184/*
8185 * ":echohl {name}".
8186 */
8187 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008188ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189{
8190 int id;
8191
8192 id = syn_name2id(eap->arg);
8193 if (id == 0)
8194 echo_attr = 0;
8195 else
8196 echo_attr = syn_id2attr(id);
8197}
8198
8199/*
8200 * ":execute expr1 ..." execute the result of an expression.
8201 * ":echomsg expr1 ..." Print a message
8202 * ":echoerr expr1 ..." Print an error
8203 * Each gets spaces around each argument and a newline at the end for
8204 * echo commands
8205 */
8206 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008207ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208{
8209 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008210 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 int ret = OK;
8212 char_u *p;
8213 garray_T ga;
8214 int len;
8215 int save_did_emsg;
8216
8217 ga_init2(&ga, 1, 80);
8218
8219 if (eap->skip)
8220 ++emsg_skip;
8221 while (*arg != NUL && *arg != '|' && *arg != '\n')
8222 {
8223 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008224 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 {
8226 /*
8227 * Report the invalid expression unless the expression evaluation
8228 * has been cancelled due to an aborting error, an interrupt, or an
8229 * exception.
8230 */
8231 if (!aborting())
8232 EMSG2(_(e_invexpr2), p);
8233 ret = FAIL;
8234 break;
8235 }
8236
8237 if (!eap->skip)
8238 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008239 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 len = (int)STRLEN(p);
8241 if (ga_grow(&ga, len + 2) == FAIL)
8242 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008243 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 ret = FAIL;
8245 break;
8246 }
8247 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 ga.ga_len += len;
8251 }
8252
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008253 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 arg = skipwhite(arg);
8255 }
8256
8257 if (ret != FAIL && ga.ga_data != NULL)
8258 {
8259 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008260 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008262 out_flush();
8263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 else if (eap->cmdidx == CMD_echoerr)
8265 {
8266 /* We don't want to abort following commands, restore did_emsg. */
8267 save_did_emsg = did_emsg;
8268 EMSG((char_u *)ga.ga_data);
8269 if (!force_abort)
8270 did_emsg = save_did_emsg;
8271 }
8272 else if (eap->cmdidx == CMD_execute)
8273 do_cmdline((char_u *)ga.ga_data,
8274 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8275 }
8276
8277 ga_clear(&ga);
8278
8279 if (eap->skip)
8280 --emsg_skip;
8281
8282 eap->nextcmd = check_nextcmd(arg);
8283}
8284
8285/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008286 * Find window specified by "vp" in tabpage "tp".
8287 */
8288 win_T *
8289find_win_by_nr(
8290 typval_T *vp,
8291 tabpage_T *tp UNUSED) /* NULL for current tab page */
8292{
8293#ifdef FEAT_WINDOWS
8294 win_T *wp;
8295#endif
8296 int nr;
8297
8298 nr = (int)get_tv_number_chk(vp, NULL);
8299
8300#ifdef FEAT_WINDOWS
8301 if (nr < 0)
8302 return NULL;
8303 if (nr == 0)
8304 return curwin;
8305
8306 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
8307 wp != NULL; wp = wp->w_next)
8308 if (nr >= LOWEST_WIN_ID)
8309 {
8310 if (wp->w_id == nr)
8311 return wp;
8312 }
8313 else if (--nr <= 0)
8314 break;
8315 if (nr >= LOWEST_WIN_ID)
8316 return NULL;
8317 return wp;
8318#else
8319 if (nr == 0 || nr == 1 || nr == curwin->w_id)
8320 return curwin;
8321 return NULL;
8322#endif
8323}
8324
8325/*
8326 * Find window specified by "wvp" in tabpage "tvp".
8327 */
8328 win_T *
8329find_tabwin(
8330 typval_T *wvp, /* VAR_UNKNOWN for current window */
8331 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
8332{
8333 win_T *wp = NULL;
8334 tabpage_T *tp = NULL;
8335 long n;
8336
8337 if (wvp->v_type != VAR_UNKNOWN)
8338 {
8339 if (tvp->v_type != VAR_UNKNOWN)
8340 {
8341 n = (long)get_tv_number(tvp);
8342 if (n >= 0)
8343 tp = find_tabpage(n);
8344 }
8345 else
8346 tp = curtab;
8347
8348 if (tp != NULL)
8349 wp = find_win_by_nr(wvp, tp);
8350 }
8351 else
8352 wp = curwin;
8353
8354 return wp;
8355}
8356
8357/*
8358 * getwinvar() and gettabwinvar()
8359 */
8360 void
8361getwinvar(
8362 typval_T *argvars,
8363 typval_T *rettv,
8364 int off) /* 1 for gettabwinvar() */
8365{
8366 win_T *win;
8367 char_u *varname;
8368 dictitem_T *v;
8369 tabpage_T *tp = NULL;
8370 int done = FALSE;
8371#ifdef FEAT_WINDOWS
8372 win_T *oldcurwin;
8373 tabpage_T *oldtabpage;
8374 int need_switch_win;
8375#endif
8376
8377#ifdef FEAT_WINDOWS
8378 if (off == 1)
8379 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8380 else
8381 tp = curtab;
8382#endif
8383 win = find_win_by_nr(&argvars[off], tp);
8384 varname = get_tv_string_chk(&argvars[off + 1]);
8385 ++emsg_off;
8386
8387 rettv->v_type = VAR_STRING;
8388 rettv->vval.v_string = NULL;
8389
8390 if (win != NULL && varname != NULL)
8391 {
8392#ifdef FEAT_WINDOWS
8393 /* Set curwin to be our win, temporarily. Also set the tabpage,
8394 * otherwise the window is not valid. Only do this when needed,
8395 * autocommands get blocked. */
8396 need_switch_win = !(tp == curtab && win == curwin);
8397 if (!need_switch_win
8398 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
8399#endif
8400 {
8401 if (*varname == '&') /* window-local-option */
8402 {
8403 if (get_option_tv(&varname, rettv, 1) == OK)
8404 done = TRUE;
8405 }
8406 else
8407 {
8408 /* Look up the variable. */
8409 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8410 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8411 varname, FALSE);
8412 if (v != NULL)
8413 {
8414 copy_tv(&v->di_tv, rettv);
8415 done = TRUE;
8416 }
8417 }
8418 }
8419
8420#ifdef FEAT_WINDOWS
8421 if (need_switch_win)
8422 /* restore previous notion of curwin */
8423 restore_win(oldcurwin, oldtabpage, TRUE);
8424#endif
8425 }
8426
8427 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8428 /* use the default return value */
8429 copy_tv(&argvars[off + 2], rettv);
8430
8431 --emsg_off;
8432}
8433
8434/*
8435 * "setwinvar()" and "settabwinvar()" functions
8436 */
8437 void
8438setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8439{
8440 win_T *win;
8441#ifdef FEAT_WINDOWS
8442 win_T *save_curwin;
8443 tabpage_T *save_curtab;
8444 int need_switch_win;
8445#endif
8446 char_u *varname, *winvarname;
8447 typval_T *varp;
8448 char_u nbuf[NUMBUFLEN];
8449 tabpage_T *tp = NULL;
8450
8451 if (check_restricted() || check_secure())
8452 return;
8453
8454#ifdef FEAT_WINDOWS
8455 if (off == 1)
8456 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8457 else
8458 tp = curtab;
8459#endif
8460 win = find_win_by_nr(&argvars[off], tp);
8461 varname = get_tv_string_chk(&argvars[off + 1]);
8462 varp = &argvars[off + 2];
8463
8464 if (win != NULL && varname != NULL && varp != NULL)
8465 {
8466#ifdef FEAT_WINDOWS
8467 need_switch_win = !(tp == curtab && win == curwin);
8468 if (!need_switch_win
8469 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
8470#endif
8471 {
8472 if (*varname == '&')
8473 {
8474 long numval;
8475 char_u *strval;
8476 int error = FALSE;
8477
8478 ++varname;
8479 numval = (long)get_tv_number_chk(varp, &error);
8480 strval = get_tv_string_buf_chk(varp, nbuf);
8481 if (!error && strval != NULL)
8482 set_option_value(varname, numval, strval, OPT_LOCAL);
8483 }
8484 else
8485 {
8486 winvarname = alloc((unsigned)STRLEN(varname) + 3);
8487 if (winvarname != NULL)
8488 {
8489 STRCPY(winvarname, "w:");
8490 STRCPY(winvarname + 2, varname);
8491 set_var(winvarname, varp, TRUE);
8492 vim_free(winvarname);
8493 }
8494 }
8495 }
8496#ifdef FEAT_WINDOWS
8497 if (need_switch_win)
8498 restore_win(save_curwin, save_curtab, TRUE);
8499#endif
8500 }
8501}
8502
8503/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8505 * "arg" points to the "&" or '+' when called, to "option" when returning.
8506 * Returns NULL when no option name found. Otherwise pointer to the char
8507 * after the option name.
8508 */
8509 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008510find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511{
8512 char_u *p = *arg;
8513
8514 ++p;
8515 if (*p == 'g' && p[1] == ':')
8516 {
8517 *opt_flags = OPT_GLOBAL;
8518 p += 2;
8519 }
8520 else if (*p == 'l' && p[1] == ':')
8521 {
8522 *opt_flags = OPT_LOCAL;
8523 p += 2;
8524 }
8525 else
8526 *opt_flags = 0;
8527
8528 if (!ASCII_ISALPHA(*p))
8529 return NULL;
8530 *arg = p;
8531
8532 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
8533 p += 4; /* termcap option */
8534 else
8535 while (ASCII_ISALPHA(*p))
8536 ++p;
8537 return p;
8538}
8539
8540/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008541 * Return the autoload script name for a function or variable name.
8542 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02008544 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008545autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02008546{
Bram Moolenaara1544c02013-05-30 12:35:52 +02008547 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008548 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02008549
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008550 /* Get the script file name: replace '#' with '/', append ".vim". */
8551 scriptname = alloc((unsigned)(STRLEN(name) + 14));
8552 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008553 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008554 STRCPY(scriptname, "autoload/");
8555 STRCAT(scriptname, name);
8556 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
8557 STRCAT(scriptname, ".vim");
8558 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
8559 *p = '/';
8560 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008561}
8562
8563/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008564 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008565 * Return TRUE if a package was loaded.
8566 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008567 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008568script_autoload(
8569 char_u *name,
8570 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008571{
8572 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008573 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008574 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008575 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008576
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008577 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008578 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008579 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008580 return FALSE;
8581
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008582 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008583
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008584 /* Find the name in the list of previously loaded package names. Skip
8585 * "autoload/", it's always the same. */
8586 for (i = 0; i < ga_loaded.ga_len; ++i)
8587 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
8588 break;
8589 if (!reload && i < ga_loaded.ga_len)
8590 ret = FALSE; /* was loaded already */
8591 else
8592 {
8593 /* Remember the name if it wasn't loaded already. */
8594 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
8595 {
8596 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
8597 tofree = NULL;
8598 }
8599
8600 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008601 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008602 ret = TRUE;
8603 }
8604
8605 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008606 return ret;
8607}
8608
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
8610typedef enum
8611{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008612 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
8613 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
8614 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615} var_flavour_T;
8616
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008617static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618
8619 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01008620var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621{
8622 char_u *p = varname;
8623
8624 if (ASCII_ISUPPER(*p))
8625 {
8626 while (*(++p))
8627 if (ASCII_ISLOWER(*p))
8628 return VAR_FLAVOUR_SESSION;
8629 return VAR_FLAVOUR_VIMINFO;
8630 }
8631 else
8632 return VAR_FLAVOUR_DEFAULT;
8633}
8634#endif
8635
8636#if defined(FEAT_VIMINFO) || defined(PROTO)
8637/*
8638 * Restore global vars that start with a capital from the viminfo file
8639 */
8640 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008641read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642{
8643 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008644 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00008645 typval_T tv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008646 void *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647
8648 if (!writing && (find_viminfo_parameter('!') != NULL))
8649 {
8650 tab = vim_strchr(virp->vir_line + 1, '\t');
8651 if (tab != NULL)
8652 {
8653 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008654 switch (*tab)
8655 {
8656 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008657#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008658 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008659#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008660 case 'D': type = VAR_DICT; break;
8661 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008662 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664
8665 tab = vim_strchr(tab, '\t');
8666 if (tab != NULL)
8667 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008668 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008669 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008670 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008672#ifdef FEAT_FLOAT
8673 else if (type == VAR_FLOAT)
8674 (void)string2float(tab + 1, &tv.vval.v_float);
8675#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008677 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008678 if (type == VAR_DICT || type == VAR_LIST)
8679 {
8680 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
8681
8682 if (etv == NULL)
8683 /* Failed to parse back the dict or list, use it as a
8684 * string. */
8685 tv.v_type = VAR_STRING;
8686 else
8687 {
8688 vim_free(tv.vval.v_string);
8689 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01008690 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008691 }
8692 }
8693
Bram Moolenaarb20e3342016-01-18 23:29:01 +01008694 /* when in a function use global variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008695 save_funccal = clear_current_funccal();
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008696 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008697 restore_current_funccal(save_funccal);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008698
8699 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008700 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008701 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
8702 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 }
8704 }
8705 }
8706
8707 return viminfo_readline(virp);
8708}
8709
8710/*
8711 * Write global vars that start with a capital to the viminfo file
8712 */
8713 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008714write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715{
Bram Moolenaar33570922005-01-25 22:26:29 +00008716 hashitem_T *hi;
8717 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008718 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01008719 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008720 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008721 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008722 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723
8724 if (find_viminfo_parameter('!') == NULL)
8725 return;
8726
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02008727 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00008728
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008729 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008730 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008732 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008734 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008735 this_var = HI2DI(hi);
8736 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008737 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008738 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00008739 {
8740 case VAR_STRING: s = "STR"; break;
8741 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008742 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008743 case VAR_DICT: s = "DIC"; break;
8744 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008745 case VAR_SPECIAL: s = "XPL"; break;
8746
8747 case VAR_UNKNOWN:
8748 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008749 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008750 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008751 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008752 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00008753 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008754 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008755 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008756 if (p != NULL)
8757 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00008758 vim_free(tofree);
8759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 }
8761 }
8762}
8763#endif
8764
8765#if defined(FEAT_SESSION) || defined(PROTO)
8766 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008767store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768{
Bram Moolenaar33570922005-01-25 22:26:29 +00008769 hashitem_T *hi;
8770 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008771 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 char_u *p, *t;
8773
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008774 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008775 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008777 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008779 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008780 this_var = HI2DI(hi);
8781 if ((this_var->di_tv.v_type == VAR_NUMBER
8782 || this_var->di_tv.v_type == VAR_STRING)
8783 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008784 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008785 /* Escape special characters with a backslash. Turn a LF and
8786 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008787 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00008788 (char_u *)"\\\"\n\r");
8789 if (p == NULL) /* out of memory */
8790 break;
8791 for (t = p; *t != NUL; ++t)
8792 if (*t == '\n')
8793 *t = 'n';
8794 else if (*t == '\r')
8795 *t = 'r';
8796 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00008797 this_var->di_key,
8798 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8799 : ' ',
8800 p,
8801 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8802 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00008803 || put_eol(fd) == FAIL)
8804 {
8805 vim_free(p);
8806 return FAIL;
8807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 vim_free(p);
8809 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008810#ifdef FEAT_FLOAT
8811 else if (this_var->di_tv.v_type == VAR_FLOAT
8812 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
8813 {
8814 float_T f = this_var->di_tv.vval.v_float;
8815 int sign = ' ';
8816
8817 if (f < 0)
8818 {
8819 f = -f;
8820 sign = '-';
8821 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01008822 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008823 this_var->di_key, sign, f) < 0)
8824 || put_eol(fd) == FAIL)
8825 return FAIL;
8826 }
8827#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828 }
8829 }
8830 return OK;
8831}
8832#endif
8833
Bram Moolenaar661b1822005-07-28 22:36:45 +00008834/*
8835 * Display script name where an item was last set.
8836 * Should only be invoked when 'verbose' is non-zero.
8837 */
8838 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008839last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +00008840{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008841 char_u *p;
8842
Bram Moolenaar661b1822005-07-28 22:36:45 +00008843 if (scriptID != 0)
8844 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008845 p = home_replace_save(NULL, get_scriptname(scriptID));
8846 if (p != NULL)
8847 {
8848 verbose_enter();
8849 MSG_PUTS(_("\n\tLast set from "));
8850 MSG_PUTS(p);
8851 vim_free(p);
8852 verbose_leave();
8853 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00008854 }
8855}
8856
Bram Moolenaard812df62008-11-09 12:46:09 +00008857/*
8858 * List v:oldfiles in a nice way.
8859 */
Bram Moolenaard812df62008-11-09 12:46:09 +00008860 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008861ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +00008862{
8863 list_T *l = vimvars[VV_OLDFILES].vv_list;
8864 listitem_T *li;
8865 int nr = 0;
8866
8867 if (l == NULL)
8868 msg((char_u *)_("No old files"));
8869 else
8870 {
8871 msg_start();
8872 msg_scroll = TRUE;
8873 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
8874 {
8875 msg_outnum((long)++nr);
8876 MSG_PUTS(": ");
8877 msg_outtrans(get_tv_string(&li->li_tv));
8878 msg_putchar('\n');
8879 out_flush(); /* output one line at a time */
8880 ui_breakcheck();
8881 }
8882 /* Assume "got_int" was set to truncate the listing. */
8883 got_int = FALSE;
8884
8885#ifdef FEAT_BROWSE_CMD
8886 if (cmdmod.browse)
8887 {
8888 quit_more = FALSE;
8889 nr = prompt_for_number(FALSE);
8890 msg_starthere();
8891 if (nr > 0)
8892 {
8893 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
8894 (long)nr);
8895
8896 if (p != NULL)
8897 {
8898 p = expand_env_save(p);
8899 eap->arg = p;
8900 eap->cmdidx = CMD_edit;
8901 cmdmod.browse = FALSE;
8902 do_exedit(eap, NULL);
8903 vim_free(p);
8904 }
8905 }
8906 }
8907#endif
8908 }
8909}
8910
Bram Moolenaar53744302015-07-17 17:38:22 +02008911/* reset v:option_new, v:option_old and v:option_type */
8912 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008913reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02008914{
8915 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
8916 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
8917 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
8918}
8919
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008920/*
8921 * Prepare "gap" for an assert error and add the sourcing position.
8922 */
8923 void
8924prepare_assert_error(garray_T *gap)
8925{
8926 char buf[NUMBUFLEN];
8927
8928 ga_init2(gap, 1, 100);
8929 if (sourcing_name != NULL)
8930 {
8931 ga_concat(gap, sourcing_name);
8932 if (sourcing_lnum > 0)
8933 ga_concat(gap, (char_u *)" ");
8934 }
8935 if (sourcing_lnum > 0)
8936 {
8937 sprintf(buf, "line %ld", (long)sourcing_lnum);
8938 ga_concat(gap, (char_u *)buf);
8939 }
8940 if (sourcing_name != NULL || sourcing_lnum > 0)
8941 ga_concat(gap, (char_u *)": ");
8942}
8943
8944/*
8945 * Add an assert error to v:errors.
8946 */
8947 void
8948assert_error(garray_T *gap)
8949{
8950 struct vimvar *vp = &vimvars[VV_ERRORS];
8951
8952 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
8953 /* Make sure v:errors is a list. */
8954 set_vim_var_list(VV_ERRORS, list_alloc());
8955 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
8956}
8957
8958 void
8959assert_equal_common(typval_T *argvars, assert_type_T atype)
8960{
8961 garray_T ga;
8962
8963 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
8964 != (atype == ASSERT_EQUAL))
8965 {
8966 prepare_assert_error(&ga);
8967 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
8968 atype);
8969 assert_error(&ga);
8970 ga_clear(&ga);
8971 }
8972}
8973
8974 void
8975assert_match_common(typval_T *argvars, assert_type_T atype)
8976{
8977 garray_T ga;
8978 char_u buf1[NUMBUFLEN];
8979 char_u buf2[NUMBUFLEN];
8980 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
8981 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
8982
8983 if (pat == NULL || text == NULL)
8984 EMSG(_(e_invarg));
8985 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
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/*
8996 * Common for assert_true() and assert_false().
8997 */
8998 void
8999assert_bool(typval_T *argvars, int isTrue)
9000{
9001 int error = FALSE;
9002 garray_T ga;
9003
9004 if (argvars[0].v_type == VAR_SPECIAL
9005 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
9006 return;
9007 if (argvars[0].v_type != VAR_NUMBER
9008 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9009 || error)
9010 {
9011 prepare_assert_error(&ga);
9012 fill_assert_error(&ga, &argvars[1],
9013 (char_u *)(isTrue ? "True" : "False"),
9014 NULL, &argvars[0], ASSERT_OTHER);
9015 assert_error(&ga);
9016 ga_clear(&ga);
9017 }
9018}
9019
9020 void
9021assert_exception(typval_T *argvars)
9022{
9023 garray_T ga;
9024 char_u *error = get_tv_string_chk(&argvars[0]);
9025
9026 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9027 {
9028 prepare_assert_error(&ga);
9029 ga_concat(&ga, (char_u *)"v:exception is not set");
9030 assert_error(&ga);
9031 ga_clear(&ga);
9032 }
9033 else if (error != NULL
9034 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9035 {
9036 prepare_assert_error(&ga);
9037 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9038 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9039 assert_error(&ga);
9040 ga_clear(&ga);
9041 }
9042}
9043
9044 void
9045assert_fails(typval_T *argvars)
9046{
9047 char_u *cmd = get_tv_string_chk(&argvars[0]);
9048 garray_T ga;
9049
9050 called_emsg = FALSE;
9051 suppress_errthrow = TRUE;
9052 emsg_silent = TRUE;
9053 do_cmdline_cmd(cmd);
9054 if (!called_emsg)
9055 {
9056 prepare_assert_error(&ga);
9057 ga_concat(&ga, (char_u *)"command did not fail: ");
9058 ga_concat(&ga, cmd);
9059 assert_error(&ga);
9060 ga_clear(&ga);
9061 }
9062 else if (argvars[1].v_type != VAR_UNKNOWN)
9063 {
9064 char_u buf[NUMBUFLEN];
9065 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9066
9067 if (error == NULL
9068 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9069 {
9070 prepare_assert_error(&ga);
9071 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9072 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
9073 assert_error(&ga);
9074 ga_clear(&ga);
9075 }
9076 }
9077
9078 called_emsg = FALSE;
9079 suppress_errthrow = FALSE;
9080 emsg_silent = FALSE;
9081 emsg_on_display = FALSE;
9082 set_vim_var_string(VV_ERRMSG, NULL, 0);
9083}
9084
9085/*
9086 * Append "str" to "gap", escaping unprintable characters.
9087 * Changes NL to \n, CR to \r, etc.
9088 */
9089 static void
9090ga_concat_esc(garray_T *gap, char_u *str)
9091{
9092 char_u *p;
9093 char_u buf[NUMBUFLEN];
9094
9095 if (str == NULL)
9096 {
9097 ga_concat(gap, (char_u *)"NULL");
9098 return;
9099 }
9100
9101 for (p = str; *p != NUL; ++p)
9102 switch (*p)
9103 {
9104 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9105 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9106 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9107 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9108 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9109 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9110 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9111 default:
9112 if (*p < ' ')
9113 {
9114 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9115 ga_concat(gap, buf);
9116 }
9117 else
9118 ga_append(gap, *p);
9119 break;
9120 }
9121}
9122
9123/*
9124 * Fill "gap" with information about an assert error.
9125 */
9126 void
9127fill_assert_error(
9128 garray_T *gap,
9129 typval_T *opt_msg_tv,
9130 char_u *exp_str,
9131 typval_T *exp_tv,
9132 typval_T *got_tv,
9133 assert_type_T atype)
9134{
9135 char_u numbuf[NUMBUFLEN];
9136 char_u *tofree;
9137
9138 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9139 {
9140 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9141 vim_free(tofree);
9142 }
9143 else
9144 {
9145 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9146 ga_concat(gap, (char_u *)"Pattern ");
9147 else
9148 ga_concat(gap, (char_u *)"Expected ");
9149 if (exp_str == NULL)
9150 {
9151 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9152 vim_free(tofree);
9153 }
9154 else
9155 ga_concat_esc(gap, exp_str);
9156 if (atype == ASSERT_MATCH)
9157 ga_concat(gap, (char_u *)" does not match ");
9158 else if (atype == ASSERT_NOTMATCH)
9159 ga_concat(gap, (char_u *)" does match ");
9160 else if (atype == ASSERT_NOTEQUAL)
9161 ga_concat(gap, (char_u *)" differs from ");
9162 else
9163 ga_concat(gap, (char_u *)" but got ");
9164 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
9165 vim_free(tofree);
9166 }
9167}
9168
Bram Moolenaar53744302015-07-17 17:38:22 +02009169
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170#endif /* FEAT_EVAL */
9171
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009173#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174
9175#ifdef WIN3264
9176/*
9177 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9178 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009179static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
9180static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
9181static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182
9183/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009184 * Get the short path (8.3) for the filename in "fnamep".
9185 * Only works for a valid file name.
9186 * When the path gets longer "fnamep" is changed and the allocated buffer
9187 * is put in "bufp".
9188 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9189 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190 */
9191 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009192get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009194 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 char_u *newbuf;
9196
9197 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009198 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199 if (l > len - 1)
9200 {
9201 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009202 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203 newbuf = vim_strnsave(*fnamep, l);
9204 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009205 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206
9207 vim_free(*bufp);
9208 *fnamep = *bufp = newbuf;
9209
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009210 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009211 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 }
9213
9214 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009215 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216}
9217
9218/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009219 * Get the short path (8.3) for the filename in "fname". The converted
9220 * path is returned in "bufp".
9221 *
9222 * Some of the directories specified in "fname" may not exist. This function
9223 * will shorten the existing directories at the beginning of the path and then
9224 * append the remaining non-existing path.
9225 *
9226 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009227 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009228 * bufp - Pointer to an allocated buffer for the filename.
9229 * fnamelen - Length of the filename pointed to by fname
9230 *
9231 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232 */
9233 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009234shortpath_for_invalid_fname(
9235 char_u **fname,
9236 char_u **bufp,
9237 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009239 char_u *short_fname, *save_fname, *pbuf_unused;
9240 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009242 int old_len, len;
9243 int new_len, sfx_len;
9244 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009245
9246 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009247 old_len = *fnamelen;
9248 save_fname = vim_strnsave(*fname, old_len);
9249 pbuf_unused = NULL;
9250 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009252 endp = save_fname + old_len - 1; /* Find the end of the copy */
9253 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009255 /*
9256 * Try shortening the supplied path till it succeeds by removing one
9257 * directory at a time from the tail of the path.
9258 */
9259 len = 0;
9260 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009262 /* go back one path-separator */
9263 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9264 --endp;
9265 if (endp <= save_fname)
9266 break; /* processed the complete path */
9267
9268 /*
9269 * Replace the path separator with a NUL and try to shorten the
9270 * resulting path.
9271 */
9272 ch = *endp;
9273 *endp = 0;
9274 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009275 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009276 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9277 {
9278 retval = FAIL;
9279 goto theend;
9280 }
9281 *endp = ch; /* preserve the string */
9282
9283 if (len > 0)
9284 break; /* successfully shortened the path */
9285
9286 /* failed to shorten the path. Skip the path separator */
9287 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288 }
9289
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009290 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009292 /*
9293 * Succeeded in shortening the path. Now concatenate the shortened
9294 * path with the remaining path at the tail.
9295 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009297 /* Compute the length of the new path. */
9298 sfx_len = (int)(save_endp - endp) + 1;
9299 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009301 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009303 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009305 /* There is not enough space in the currently allocated string,
9306 * copy it to a buffer big enough. */
9307 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009309 {
9310 retval = FAIL;
9311 goto theend;
9312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313 }
9314 else
9315 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009316 /* Transfer short_fname to the main buffer (it's big enough),
9317 * unless get_short_pathname() did its work in-place. */
9318 *fname = *bufp = save_fname;
9319 if (short_fname != save_fname)
9320 vim_strncpy(save_fname, short_fname, len);
9321 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009323
9324 /* concat the not-shortened part of the path */
9325 vim_strncpy(*fname + len, endp, sfx_len);
9326 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009328
9329theend:
9330 vim_free(pbuf_unused);
9331 vim_free(save_fname);
9332
9333 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334}
9335
9336/*
9337 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009338 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339 */
9340 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009341shortpath_for_partial(
9342 char_u **fnamep,
9343 char_u **bufp,
9344 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345{
9346 int sepcount, len, tflen;
9347 char_u *p;
9348 char_u *pbuf, *tfname;
9349 int hasTilde;
9350
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009351 /* Count up the path separators from the RHS.. so we know which part
9352 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009354 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 if (vim_ispathsep(*p))
9356 ++sepcount;
9357
9358 /* Need full path first (use expand_env() to remove a "~/") */
9359 hasTilde = (**fnamep == '~');
9360 if (hasTilde)
9361 pbuf = tfname = expand_env_save(*fnamep);
9362 else
9363 pbuf = tfname = FullName_save(*fnamep, FALSE);
9364
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009365 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009367 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
9368 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369
9370 if (len == 0)
9371 {
9372 /* Don't have a valid filename, so shorten the rest of the
9373 * path if we can. This CAN give us invalid 8.3 filenames, but
9374 * there's not a lot of point in guessing what it might be.
9375 */
9376 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009377 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
9378 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379 }
9380
9381 /* Count the paths backward to find the beginning of the desired string. */
9382 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009383 {
9384#ifdef FEAT_MBYTE
9385 if (has_mbyte)
9386 p -= mb_head_off(tfname, p);
9387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 if (vim_ispathsep(*p))
9389 {
9390 if (sepcount == 0 || (hasTilde && sepcount == 1))
9391 break;
9392 else
9393 sepcount --;
9394 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396 if (hasTilde)
9397 {
9398 --p;
9399 if (p >= tfname)
9400 *p = '~';
9401 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009402 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 }
9404 else
9405 ++p;
9406
9407 /* Copy in the string - p indexes into tfname - allocated at pbuf */
9408 vim_free(*bufp);
9409 *fnamelen = (int)STRLEN(p);
9410 *bufp = pbuf;
9411 *fnamep = p;
9412
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009413 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414}
9415#endif /* WIN3264 */
9416
9417/*
9418 * Adjust a filename, according to a string of modifiers.
9419 * *fnamep must be NUL terminated when called. When returning, the length is
9420 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009421 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422 * When there is an error, *fnamep is set to NULL.
9423 */
9424 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009425modify_fname(
9426 char_u *src, /* string with modifiers */
9427 int *usedlen, /* characters after src that are used */
9428 char_u **fnamep, /* file name so far */
9429 char_u **bufp, /* buffer for allocated file name or NULL */
9430 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431{
9432 int valid = 0;
9433 char_u *tail;
9434 char_u *s, *p, *pbuf;
9435 char_u dirname[MAXPATHL];
9436 int c;
9437 int has_fullname = 0;
9438#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009439 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440 int has_shortname = 0;
9441#endif
9442
9443repeat:
9444 /* ":p" - full path/file_name */
9445 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
9446 {
9447 has_fullname = 1;
9448
9449 valid |= VALID_PATH;
9450 *usedlen += 2;
9451
9452 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
9453 if ((*fnamep)[0] == '~'
9454#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
9455 && ((*fnamep)[1] == '/'
9456# ifdef BACKSLASH_IN_FILENAME
9457 || (*fnamep)[1] == '\\'
9458# endif
9459 || (*fnamep)[1] == NUL)
9460
9461#endif
9462 )
9463 {
9464 *fnamep = expand_env_save(*fnamep);
9465 vim_free(*bufp); /* free any allocated file name */
9466 *bufp = *fnamep;
9467 if (*fnamep == NULL)
9468 return -1;
9469 }
9470
9471 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009472 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473 {
9474 if (vim_ispathsep(*p)
9475 && p[1] == '.'
9476 && (p[2] == NUL
9477 || vim_ispathsep(p[2])
9478 || (p[2] == '.'
9479 && (p[3] == NUL || vim_ispathsep(p[3])))))
9480 break;
9481 }
9482
9483 /* FullName_save() is slow, don't use it when not needed. */
9484 if (*p != NUL || !vim_isAbsName(*fnamep))
9485 {
9486 *fnamep = FullName_save(*fnamep, *p != NUL);
9487 vim_free(*bufp); /* free any allocated file name */
9488 *bufp = *fnamep;
9489 if (*fnamep == NULL)
9490 return -1;
9491 }
9492
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009493#ifdef WIN3264
9494# if _WIN32_WINNT >= 0x0500
9495 if (vim_strchr(*fnamep, '~') != NULL)
9496 {
9497 /* Expand 8.3 filename to full path. Needed to make sure the same
9498 * file does not have two different names.
9499 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
9500 p = alloc(_MAX_PATH + 1);
9501 if (p != NULL)
9502 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009503 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009504 {
9505 vim_free(*bufp);
9506 *bufp = *fnamep = p;
9507 }
9508 else
9509 vim_free(p);
9510 }
9511 }
9512# endif
9513#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514 /* Append a path separator to a directory. */
9515 if (mch_isdir(*fnamep))
9516 {
9517 /* Make room for one or two extra characters. */
9518 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
9519 vim_free(*bufp); /* free any allocated file name */
9520 *bufp = *fnamep;
9521 if (*fnamep == NULL)
9522 return -1;
9523 add_pathsep(*fnamep);
9524 }
9525 }
9526
9527 /* ":." - path relative to the current directory */
9528 /* ":~" - path relative to the home directory */
9529 /* ":8" - shortname path - postponed till after */
9530 while (src[*usedlen] == ':'
9531 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
9532 {
9533 *usedlen += 2;
9534 if (c == '8')
9535 {
9536#ifdef WIN3264
9537 has_shortname = 1; /* Postpone this. */
9538#endif
9539 continue;
9540 }
9541 pbuf = NULL;
9542 /* Need full path first (use expand_env() to remove a "~/") */
9543 if (!has_fullname)
9544 {
9545 if (c == '.' && **fnamep == '~')
9546 p = pbuf = expand_env_save(*fnamep);
9547 else
9548 p = pbuf = FullName_save(*fnamep, FALSE);
9549 }
9550 else
9551 p = *fnamep;
9552
9553 has_fullname = 0;
9554
9555 if (p != NULL)
9556 {
9557 if (c == '.')
9558 {
9559 mch_dirname(dirname, MAXPATHL);
9560 s = shorten_fname(p, dirname);
9561 if (s != NULL)
9562 {
9563 *fnamep = s;
9564 if (pbuf != NULL)
9565 {
9566 vim_free(*bufp); /* free any allocated file name */
9567 *bufp = pbuf;
9568 pbuf = NULL;
9569 }
9570 }
9571 }
9572 else
9573 {
9574 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
9575 /* Only replace it when it starts with '~' */
9576 if (*dirname == '~')
9577 {
9578 s = vim_strsave(dirname);
9579 if (s != NULL)
9580 {
9581 *fnamep = s;
9582 vim_free(*bufp);
9583 *bufp = s;
9584 }
9585 }
9586 }
9587 vim_free(pbuf);
9588 }
9589 }
9590
9591 tail = gettail(*fnamep);
9592 *fnamelen = (int)STRLEN(*fnamep);
9593
9594 /* ":h" - head, remove "/file_name", can be repeated */
9595 /* Don't remove the first "/" or "c:\" */
9596 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
9597 {
9598 valid |= VALID_HEAD;
9599 *usedlen += 2;
9600 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009601 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009602 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 *fnamelen = (int)(tail - *fnamep);
9604#ifdef VMS
9605 if (*fnamelen > 0)
9606 *fnamelen += 1; /* the path separator is part of the path */
9607#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009608 if (*fnamelen == 0)
9609 {
9610 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
9611 p = vim_strsave((char_u *)".");
9612 if (p == NULL)
9613 return -1;
9614 vim_free(*bufp);
9615 *bufp = *fnamep = tail = p;
9616 *fnamelen = 1;
9617 }
9618 else
9619 {
9620 while (tail > s && !after_pathsep(s, tail))
9621 mb_ptr_back(*fnamep, tail);
9622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 }
9624
9625 /* ":8" - shortname */
9626 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
9627 {
9628 *usedlen += 2;
9629#ifdef WIN3264
9630 has_shortname = 1;
9631#endif
9632 }
9633
9634#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009635 /*
9636 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637 */
9638 if (has_shortname)
9639 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009640 /* Copy the string if it is shortened by :h and when it wasn't copied
9641 * yet, because we are going to change it in place. Avoids changing
9642 * the buffer name for "%:8". */
9643 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644 {
9645 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +02009646 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 return -1;
9648 vim_free(*bufp);
9649 *bufp = *fnamep = p;
9650 }
9651
9652 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +02009653 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 if (!has_fullname && !vim_isAbsName(*fnamep))
9655 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009656 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657 return -1;
9658 }
9659 else
9660 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009661 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662
Bram Moolenaardc935552011-08-17 15:23:23 +02009663 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009665 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666 return -1;
9667
9668 if (l == 0)
9669 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009670 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009672 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 return -1;
9674 }
9675 *fnamelen = l;
9676 }
9677 }
9678#endif /* WIN3264 */
9679
9680 /* ":t" - tail, just the basename */
9681 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
9682 {
9683 *usedlen += 2;
9684 *fnamelen -= (int)(tail - *fnamep);
9685 *fnamep = tail;
9686 }
9687
9688 /* ":e" - extension, can be repeated */
9689 /* ":r" - root, without extension, can be repeated */
9690 while (src[*usedlen] == ':'
9691 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
9692 {
9693 /* find a '.' in the tail:
9694 * - for second :e: before the current fname
9695 * - otherwise: The last '.'
9696 */
9697 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
9698 s = *fnamep - 2;
9699 else
9700 s = *fnamep + *fnamelen - 1;
9701 for ( ; s > tail; --s)
9702 if (s[0] == '.')
9703 break;
9704 if (src[*usedlen + 1] == 'e') /* :e */
9705 {
9706 if (s > tail)
9707 {
9708 *fnamelen += (int)(*fnamep - (s + 1));
9709 *fnamep = s + 1;
9710#ifdef VMS
9711 /* cut version from the extension */
9712 s = *fnamep + *fnamelen - 1;
9713 for ( ; s > *fnamep; --s)
9714 if (s[0] == ';')
9715 break;
9716 if (s > *fnamep)
9717 *fnamelen = s - *fnamep;
9718#endif
9719 }
9720 else if (*fnamep <= tail)
9721 *fnamelen = 0;
9722 }
9723 else /* :r */
9724 {
9725 if (s > tail) /* remove one extension */
9726 *fnamelen = (int)(s - *fnamep);
9727 }
9728 *usedlen += 2;
9729 }
9730
9731 /* ":s?pat?foo?" - substitute */
9732 /* ":gs?pat?foo?" - global substitute */
9733 if (src[*usedlen] == ':'
9734 && (src[*usedlen + 1] == 's'
9735 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
9736 {
9737 char_u *str;
9738 char_u *pat;
9739 char_u *sub;
9740 int sep;
9741 char_u *flags;
9742 int didit = FALSE;
9743
9744 flags = (char_u *)"";
9745 s = src + *usedlen + 2;
9746 if (src[*usedlen + 1] == 'g')
9747 {
9748 flags = (char_u *)"g";
9749 ++s;
9750 }
9751
9752 sep = *s++;
9753 if (sep)
9754 {
9755 /* find end of pattern */
9756 p = vim_strchr(s, sep);
9757 if (p != NULL)
9758 {
9759 pat = vim_strnsave(s, (int)(p - s));
9760 if (pat != NULL)
9761 {
9762 s = p + 1;
9763 /* find end of substitution */
9764 p = vim_strchr(s, sep);
9765 if (p != NULL)
9766 {
9767 sub = vim_strnsave(s, (int)(p - s));
9768 str = vim_strnsave(*fnamep, *fnamelen);
9769 if (sub != NULL && str != NULL)
9770 {
9771 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009772 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 if (s != NULL)
9774 {
9775 *fnamep = s;
9776 *fnamelen = (int)STRLEN(s);
9777 vim_free(*bufp);
9778 *bufp = s;
9779 didit = TRUE;
9780 }
9781 }
9782 vim_free(sub);
9783 vim_free(str);
9784 }
9785 vim_free(pat);
9786 }
9787 }
9788 /* after using ":s", repeat all the modifiers */
9789 if (didit)
9790 goto repeat;
9791 }
9792 }
9793
Bram Moolenaar26df0922014-02-23 23:39:13 +01009794 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
9795 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +01009796 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +01009797 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +01009798 if (c != NUL)
9799 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +01009800 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +01009801 if (c != NUL)
9802 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +01009803 if (p == NULL)
9804 return -1;
9805 vim_free(*bufp);
9806 *bufp = *fnamep = p;
9807 *fnamelen = (int)STRLEN(p);
9808 *usedlen += 2;
9809 }
9810
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811 return valid;
9812}
9813
9814/*
9815 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009816 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817 * "flags" can be "g" to do a global substitute.
9818 * Returns an allocated string, NULL for error.
9819 */
9820 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009821do_string_sub(
9822 char_u *str,
9823 char_u *pat,
9824 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009825 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01009826 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827{
9828 int sublen;
9829 regmatch_T regmatch;
9830 int i;
9831 int do_all;
9832 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01009833 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834 garray_T ga;
9835 char_u *ret;
9836 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01009837 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838
9839 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
9840 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00009841 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842
9843 ga_init2(&ga, 1, 200);
9844
9845 do_all = (flags[0] == 'g');
9846
9847 regmatch.rm_ic = p_ic;
9848 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
9849 if (regmatch.regprog != NULL)
9850 {
9851 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01009852 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
9854 {
Bram Moolenaar8af26912014-01-23 20:09:34 +01009855 /* Skip empty match except for first match. */
9856 if (regmatch.startp[0] == regmatch.endp[0])
9857 {
9858 if (zero_width == regmatch.startp[0])
9859 {
9860 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02009861 i = MB_PTR2LEN(tail);
9862 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
9863 (size_t)i);
9864 ga.ga_len += i;
9865 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01009866 continue;
9867 }
9868 zero_width = regmatch.startp[0];
9869 }
9870
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 /*
9872 * Get some space for a temporary buffer to do the substitution
9873 * into. It will contain:
9874 * - The text up to where the match is.
9875 * - The substituted text.
9876 * - The text after the match.
9877 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009878 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01009879 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
9881 {
9882 ga_clear(&ga);
9883 break;
9884 }
9885
9886 /* copy the text up to where the match is */
9887 i = (int)(regmatch.startp[0] - tail);
9888 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
9889 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009890 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891 + ga.ga_len + i, TRUE, TRUE, FALSE);
9892 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02009893 tail = regmatch.endp[0];
9894 if (*tail == NUL)
9895 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896 if (!do_all)
9897 break;
9898 }
9899
9900 if (ga.ga_data != NULL)
9901 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
9902
Bram Moolenaar473de612013-06-08 18:19:48 +02009903 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 }
9905
9906 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
9907 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00009908 if (p_cpo == empty_option)
9909 p_cpo = save_cpo;
9910 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009911 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00009912 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913
9914 return ret;
9915}
9916
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009917 static int
9918filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
9919{
9920 typval_T rettv;
9921 typval_T argv[3];
9922 char_u buf[NUMBUFLEN];
9923 char_u *s;
9924 int retval = FAIL;
9925 int dummy;
9926
9927 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
9928 argv[0] = vimvars[VV_KEY].vv_tv;
9929 argv[1] = vimvars[VV_VAL].vv_tv;
9930 if (expr->v_type == VAR_FUNC)
9931 {
9932 s = expr->vval.v_string;
Bram Moolenaardf48fb42016-07-22 21:50:18 +02009933 if (call_func(s, (int)STRLEN(s), &rettv, 2, argv, NULL,
9934 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009935 goto theend;
9936 }
9937 else if (expr->v_type == VAR_PARTIAL)
9938 {
9939 partial_T *partial = expr->vval.v_partial;
9940
9941 s = partial->pt_name;
Bram Moolenaardf48fb42016-07-22 21:50:18 +02009942 if (call_func(s, (int)STRLEN(s), &rettv, 2, argv, NULL,
9943 0L, 0L, &dummy, TRUE, partial, NULL) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009944 goto theend;
9945 }
9946 else
9947 {
9948 s = get_tv_string_buf_chk(expr, buf);
9949 if (s == NULL)
9950 goto theend;
9951 s = skipwhite(s);
9952 if (eval1(&s, &rettv, TRUE) == FAIL)
9953 goto theend;
9954 if (*s != NUL) /* check for trailing chars after expr */
9955 {
9956 EMSG2(_(e_invexpr2), s);
9957 goto theend;
9958 }
9959 }
9960 if (map)
9961 {
9962 /* map(): replace the list item value */
9963 clear_tv(tv);
9964 rettv.v_lock = 0;
9965 *tv = rettv;
9966 }
9967 else
9968 {
9969 int error = FALSE;
9970
9971 /* filter(): when expr is zero remove the item */
9972 *remp = (get_tv_number_chk(&rettv, &error) == 0);
9973 clear_tv(&rettv);
9974 /* On type error, nothing has been removed; return FAIL to stop the
9975 * loop. The error message was given by get_tv_number_chk(). */
9976 if (error)
9977 goto theend;
9978 }
9979 retval = OK;
9980theend:
9981 clear_tv(&vimvars[VV_VAL].vv_tv);
9982 return retval;
9983}
9984
9985
9986/*
9987 * Implementation of map() and filter().
9988 */
9989 void
9990filter_map(typval_T *argvars, typval_T *rettv, int map)
9991{
9992 typval_T *expr;
9993 listitem_T *li, *nli;
9994 list_T *l = NULL;
9995 dictitem_T *di;
9996 hashtab_T *ht;
9997 hashitem_T *hi;
9998 dict_T *d = NULL;
9999 typval_T save_val;
10000 typval_T save_key;
10001 int rem;
10002 int todo;
10003 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10004 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10005 : N_("filter() argument"));
10006 int save_did_emsg;
10007 int idx = 0;
10008
10009 if (argvars[0].v_type == VAR_LIST)
10010 {
10011 if ((l = argvars[0].vval.v_list) == NULL
10012 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
10013 return;
10014 }
10015 else if (argvars[0].v_type == VAR_DICT)
10016 {
10017 if ((d = argvars[0].vval.v_dict) == NULL
10018 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
10019 return;
10020 }
10021 else
10022 {
10023 EMSG2(_(e_listdictarg), ermsg);
10024 return;
10025 }
10026
10027 expr = &argvars[1];
10028 /* On type errors, the preceding call has already displayed an error
10029 * message. Avoid a misleading error message for an empty string that
10030 * was not passed as argument. */
10031 if (expr->v_type != VAR_UNKNOWN)
10032 {
10033 prepare_vimvar(VV_VAL, &save_val);
10034
10035 /* We reset "did_emsg" to be able to detect whether an error
10036 * occurred during evaluation of the expression. */
10037 save_did_emsg = did_emsg;
10038 did_emsg = FALSE;
10039
10040 prepare_vimvar(VV_KEY, &save_key);
10041 if (argvars[0].v_type == VAR_DICT)
10042 {
10043 vimvars[VV_KEY].vv_type = VAR_STRING;
10044
10045 ht = &d->dv_hashtab;
10046 hash_lock(ht);
10047 todo = (int)ht->ht_used;
10048 for (hi = ht->ht_array; todo > 0; ++hi)
10049 {
10050 if (!HASHITEM_EMPTY(hi))
10051 {
10052 int r;
10053
10054 --todo;
10055 di = HI2DI(hi);
10056 if (map &&
10057 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10058 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
10059 break;
10060 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10061 r = filter_map_one(&di->di_tv, expr, map, &rem);
10062 clear_tv(&vimvars[VV_KEY].vv_tv);
10063 if (r == FAIL || did_emsg)
10064 break;
10065 if (!map && rem)
10066 {
10067 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10068 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10069 break;
10070 dictitem_remove(d, di);
10071 }
10072 }
10073 }
10074 hash_unlock(ht);
10075 }
10076 else
10077 {
10078 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10079
10080 for (li = l->lv_first; li != NULL; li = nli)
10081 {
10082 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
10083 break;
10084 nli = li->li_next;
10085 vimvars[VV_KEY].vv_nr = idx;
10086 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10087 || did_emsg)
10088 break;
10089 if (!map && rem)
10090 listitem_remove(l, li);
10091 ++idx;
10092 }
10093 }
10094
10095 restore_vimvar(VV_KEY, &save_key);
10096 restore_vimvar(VV_VAL, &save_val);
10097
10098 did_emsg |= save_did_emsg;
10099 }
10100
10101 copy_tv(&argvars[0], rettv);
10102}
10103
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */