blob: 463c8927ecb609308e892277f1b0e120edeade4a [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 Moolenaar1e96d9b2016-07-29 22:15:09 +0200240static void check_vars(char_u *name, int len);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100241static typval_T *alloc_string_tv(char_u *string);
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 Moolenaar1e96d9b2016-07-29 22:15:09 +02004335 if (!evaluate)
4336 check_vars(s, len);
4337
Bram Moolenaar8c711452005-01-14 21:53:12 +00004338 /* If "s" is the name of a variable of type VAR_FUNC
4339 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004340 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004341
4342 /* Invoke the function. */
4343 ret = get_func_tv(s, len, rettv, arg,
4344 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004345 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004346
4347 /* If evaluate is FALSE rettv->v_type was not set in
4348 * get_func_tv, but it's needed in handle_subscript() to parse
4349 * what follows. So set it here. */
4350 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4351 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004352 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004353 rettv->v_type = VAR_FUNC;
4354 }
4355
Bram Moolenaar8c711452005-01-14 21:53:12 +00004356 /* Stop the expression evaluation when immediately
4357 * aborting on error, or when an interrupt occurred or
4358 * an exception was thrown but not caught. */
4359 if (aborting())
4360 {
4361 if (ret == OK)
4362 clear_tv(rettv);
4363 ret = FAIL;
4364 }
4365 }
4366 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004367 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004368 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004369 {
4370 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004371 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004372 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004373 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004374 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004375 }
4376
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 *arg = skipwhite(*arg);
4378
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004379 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4380 * expr(expr). */
4381 if (ret == OK)
4382 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383
4384 /*
4385 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4386 */
4387 if (ret == OK && evaluate && end_leader > start_leader)
4388 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004389 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004390 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004391#ifdef FEAT_FLOAT
4392 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004393
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004394 if (rettv->v_type == VAR_FLOAT)
4395 f = rettv->vval.v_float;
4396 else
4397#endif
4398 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004399 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004401 clear_tv(rettv);
4402 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004404 else
4405 {
4406 while (end_leader > start_leader)
4407 {
4408 --end_leader;
4409 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004410 {
4411#ifdef FEAT_FLOAT
4412 if (rettv->v_type == VAR_FLOAT)
4413 f = !f;
4414 else
4415#endif
4416 val = !val;
4417 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004418 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004419 {
4420#ifdef FEAT_FLOAT
4421 if (rettv->v_type == VAR_FLOAT)
4422 f = -f;
4423 else
4424#endif
4425 val = -val;
4426 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004427 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004428#ifdef FEAT_FLOAT
4429 if (rettv->v_type == VAR_FLOAT)
4430 {
4431 clear_tv(rettv);
4432 rettv->vval.v_float = f;
4433 }
4434 else
4435#endif
4436 {
4437 clear_tv(rettv);
4438 rettv->v_type = VAR_NUMBER;
4439 rettv->vval.v_number = val;
4440 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 }
4443
4444 return ret;
4445}
4446
4447/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004448 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4449 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004450 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4451 */
4452 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004453eval_index(
4454 char_u **arg,
4455 typval_T *rettv,
4456 int evaluate,
4457 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004458{
4459 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004460 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004461 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004462 long len = -1;
4463 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004464 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004465 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004466
Bram Moolenaara03f2332016-02-06 18:09:59 +01004467 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004468 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004469 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004470 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004471 if (verbose)
4472 EMSG(_("E695: Cannot index a Funcref"));
4473 return FAIL;
4474 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004475#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004476 if (verbose)
4477 EMSG(_(e_float_as_string));
4478 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004479#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004480 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004481 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004482 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004483 if (verbose)
4484 EMSG(_("E909: Cannot index a special variable"));
4485 return FAIL;
4486 case VAR_UNKNOWN:
4487 if (evaluate)
4488 return FAIL;
4489 /* FALLTHROUGH */
4490
4491 case VAR_STRING:
4492 case VAR_NUMBER:
4493 case VAR_LIST:
4494 case VAR_DICT:
4495 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004496 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004497
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004498 init_tv(&var1);
4499 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004500 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004501 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004502 /*
4503 * dict.name
4504 */
4505 key = *arg + 1;
4506 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4507 ;
4508 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004509 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004510 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004511 }
4512 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004513 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004514 /*
4515 * something[idx]
4516 *
4517 * Get the (first) variable from inside the [].
4518 */
4519 *arg = skipwhite(*arg + 1);
4520 if (**arg == ':')
4521 empty1 = TRUE;
4522 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4523 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004524 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4525 {
4526 /* not a number or string */
4527 clear_tv(&var1);
4528 return FAIL;
4529 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004530
4531 /*
4532 * Get the second variable from inside the [:].
4533 */
4534 if (**arg == ':')
4535 {
4536 range = TRUE;
4537 *arg = skipwhite(*arg + 1);
4538 if (**arg == ']')
4539 empty2 = TRUE;
4540 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4541 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004542 if (!empty1)
4543 clear_tv(&var1);
4544 return FAIL;
4545 }
4546 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4547 {
4548 /* not a number or string */
4549 if (!empty1)
4550 clear_tv(&var1);
4551 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004552 return FAIL;
4553 }
4554 }
4555
4556 /* Check for the ']'. */
4557 if (**arg != ']')
4558 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004559 if (verbose)
4560 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004561 clear_tv(&var1);
4562 if (range)
4563 clear_tv(&var2);
4564 return FAIL;
4565 }
4566 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004567 }
4568
4569 if (evaluate)
4570 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004571 n1 = 0;
4572 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004573 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004574 n1 = get_tv_number(&var1);
4575 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004576 }
4577 if (range)
4578 {
4579 if (empty2)
4580 n2 = -1;
4581 else
4582 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004583 n2 = get_tv_number(&var2);
4584 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004585 }
4586 }
4587
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004588 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004589 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004590 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004591 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004592 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004593 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004594 case VAR_SPECIAL:
4595 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004596 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004597 break; /* not evaluating, skipping over subscript */
4598
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004599 case VAR_NUMBER:
4600 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004601 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004602 len = (long)STRLEN(s);
4603 if (range)
4604 {
4605 /* The resulting variable is a substring. If the indexes
4606 * are out of range the result is empty. */
4607 if (n1 < 0)
4608 {
4609 n1 = len + n1;
4610 if (n1 < 0)
4611 n1 = 0;
4612 }
4613 if (n2 < 0)
4614 n2 = len + n2;
4615 else if (n2 >= len)
4616 n2 = len;
4617 if (n1 >= len || n2 < 0 || n1 > n2)
4618 s = NULL;
4619 else
4620 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4621 }
4622 else
4623 {
4624 /* The resulting variable is a string of a single
4625 * character. If the index is too big or negative the
4626 * result is empty. */
4627 if (n1 >= len || n1 < 0)
4628 s = NULL;
4629 else
4630 s = vim_strnsave(s + n1, 1);
4631 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004632 clear_tv(rettv);
4633 rettv->v_type = VAR_STRING;
4634 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004635 break;
4636
4637 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004638 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004639 if (n1 < 0)
4640 n1 = len + n1;
4641 if (!empty1 && (n1 < 0 || n1 >= len))
4642 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004643 /* For a range we allow invalid values and return an empty
4644 * list. A list index out of range is an error. */
4645 if (!range)
4646 {
4647 if (verbose)
4648 EMSGN(_(e_listidx), n1);
4649 return FAIL;
4650 }
4651 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004652 }
4653 if (range)
4654 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004655 list_T *l;
4656 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004657
4658 if (n2 < 0)
4659 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004660 else if (n2 >= len)
4661 n2 = len - 1;
4662 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004663 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004664 l = list_alloc();
4665 if (l == NULL)
4666 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004667 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004668 n1 <= n2; ++n1)
4669 {
4670 if (list_append_tv(l, &item->li_tv) == FAIL)
4671 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004672 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004673 return FAIL;
4674 }
4675 item = item->li_next;
4676 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004677 clear_tv(rettv);
4678 rettv->v_type = VAR_LIST;
4679 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004680 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004681 }
4682 else
4683 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004684 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004685 clear_tv(rettv);
4686 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004687 }
4688 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004689
4690 case VAR_DICT:
4691 if (range)
4692 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004693 if (verbose)
4694 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004695 if (len == -1)
4696 clear_tv(&var1);
4697 return FAIL;
4698 }
4699 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004700 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004701
4702 if (len == -1)
4703 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02004704 key = get_tv_string_chk(&var1);
4705 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004706 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004707 clear_tv(&var1);
4708 return FAIL;
4709 }
4710 }
4711
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004712 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004713
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004714 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004715 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004716 if (len == -1)
4717 clear_tv(&var1);
4718 if (item == NULL)
4719 return FAIL;
4720
4721 copy_tv(&item->di_tv, &var1);
4722 clear_tv(rettv);
4723 *rettv = var1;
4724 }
4725 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004726 }
4727 }
4728
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004729 return OK;
4730}
4731
4732/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 * Get an option value.
4734 * "arg" points to the '&' or '+' before the option name.
4735 * "arg" is advanced to character after the option name.
4736 * Return OK or FAIL.
4737 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004738 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004739get_option_tv(
4740 char_u **arg,
4741 typval_T *rettv, /* when NULL, only check if option exists */
4742 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743{
4744 char_u *option_end;
4745 long numval;
4746 char_u *stringval;
4747 int opt_type;
4748 int c;
4749 int working = (**arg == '+'); /* has("+option") */
4750 int ret = OK;
4751 int opt_flags;
4752
4753 /*
4754 * Isolate the option name and find its value.
4755 */
4756 option_end = find_option_end(arg, &opt_flags);
4757 if (option_end == NULL)
4758 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004759 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 EMSG2(_("E112: Option name missing: %s"), *arg);
4761 return FAIL;
4762 }
4763
4764 if (!evaluate)
4765 {
4766 *arg = option_end;
4767 return OK;
4768 }
4769
4770 c = *option_end;
4771 *option_end = NUL;
4772 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004773 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774
4775 if (opt_type == -3) /* invalid name */
4776 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004777 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 EMSG2(_("E113: Unknown option: %s"), *arg);
4779 ret = FAIL;
4780 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004781 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 {
4783 if (opt_type == -2) /* hidden string option */
4784 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004785 rettv->v_type = VAR_STRING;
4786 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 }
4788 else if (opt_type == -1) /* hidden number option */
4789 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004790 rettv->v_type = VAR_NUMBER;
4791 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 }
4793 else if (opt_type == 1) /* number option */
4794 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004795 rettv->v_type = VAR_NUMBER;
4796 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 }
4798 else /* string option */
4799 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004800 rettv->v_type = VAR_STRING;
4801 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 }
4803 }
4804 else if (working && (opt_type == -2 || opt_type == -1))
4805 ret = FAIL;
4806
4807 *option_end = c; /* put back for error messages */
4808 *arg = option_end;
4809
4810 return ret;
4811}
4812
4813/*
4814 * Allocate a variable for a string constant.
4815 * Return OK or FAIL.
4816 */
4817 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004818get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819{
4820 char_u *p;
4821 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 int extra = 0;
4823
4824 /*
4825 * Find the end of the string, skipping backslashed characters.
4826 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004827 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 {
4829 if (*p == '\\' && p[1] != NUL)
4830 {
4831 ++p;
4832 /* A "\<x>" form occupies at least 4 characters, and produces up
4833 * to 6 characters: reserve space for 2 extra */
4834 if (*p == '<')
4835 extra += 2;
4836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 }
4838
4839 if (*p != '"')
4840 {
4841 EMSG2(_("E114: Missing quote: %s"), *arg);
4842 return FAIL;
4843 }
4844
4845 /* If only parsing, set *arg and return here */
4846 if (!evaluate)
4847 {
4848 *arg = p + 1;
4849 return OK;
4850 }
4851
4852 /*
4853 * Copy the string into allocated memory, handling backslashed
4854 * characters.
4855 */
4856 name = alloc((unsigned)(p - *arg + extra));
4857 if (name == NULL)
4858 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004859 rettv->v_type = VAR_STRING;
4860 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861
Bram Moolenaar8c711452005-01-14 21:53:12 +00004862 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 {
4864 if (*p == '\\')
4865 {
4866 switch (*++p)
4867 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004868 case 'b': *name++ = BS; ++p; break;
4869 case 'e': *name++ = ESC; ++p; break;
4870 case 'f': *name++ = FF; ++p; break;
4871 case 'n': *name++ = NL; ++p; break;
4872 case 'r': *name++ = CAR; ++p; break;
4873 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874
4875 case 'X': /* hex: "\x1", "\x12" */
4876 case 'x':
4877 case 'u': /* Unicode: "\u0023" */
4878 case 'U':
4879 if (vim_isxdigit(p[1]))
4880 {
4881 int n, nr;
4882 int c = toupper(*p);
4883
4884 if (c == 'X')
4885 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004886 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004888 else
4889 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 nr = 0;
4891 while (--n >= 0 && vim_isxdigit(p[1]))
4892 {
4893 ++p;
4894 nr = (nr << 4) + hex2nr(*p);
4895 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004896 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897#ifdef FEAT_MBYTE
4898 /* For "\u" store the number according to
4899 * 'encoding'. */
4900 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004901 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 else
4903#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004904 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 break;
4907
4908 /* octal: "\1", "\12", "\123" */
4909 case '0':
4910 case '1':
4911 case '2':
4912 case '3':
4913 case '4':
4914 case '5':
4915 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004916 case '7': *name = *p++ - '0';
4917 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004919 *name = (*name << 3) + *p++ - '0';
4920 if (*p >= '0' && *p <= '7')
4921 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004923 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 break;
4925
4926 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00004927 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 if (extra != 0)
4929 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004930 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 break;
4932 }
4933 /* FALLTHROUGH */
4934
Bram Moolenaar8c711452005-01-14 21:53:12 +00004935 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 break;
4937 }
4938 }
4939 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004940 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004943 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 *arg = p + 1;
4945
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 return OK;
4947}
4948
4949/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004950 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 * Return OK or FAIL.
4952 */
4953 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004954get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955{
4956 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004957 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004958 int reduce = 0;
4959
4960 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004961 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004962 */
4963 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
4964 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004965 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004966 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004967 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004968 break;
4969 ++reduce;
4970 ++p;
4971 }
4972 }
4973
Bram Moolenaar8c711452005-01-14 21:53:12 +00004974 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004975 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004976 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004977 return FAIL;
4978 }
4979
Bram Moolenaar8c711452005-01-14 21:53:12 +00004980 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004981 if (!evaluate)
4982 {
4983 *arg = p + 1;
4984 return OK;
4985 }
4986
4987 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004988 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004989 */
4990 str = alloc((unsigned)((p - *arg) - reduce));
4991 if (str == NULL)
4992 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004993 rettv->v_type = VAR_STRING;
4994 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004995
Bram Moolenaar8c711452005-01-14 21:53:12 +00004996 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004997 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004998 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004999 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005000 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005001 break;
5002 ++p;
5003 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005004 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005005 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005006 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005007 *arg = p + 1;
5008
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005009 return OK;
5010}
5011
Bram Moolenaarddecc252016-04-06 22:59:37 +02005012 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005013partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005014{
5015 int i;
5016
5017 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005018 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005019 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005020 dict_unref(pt->pt_dict);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005021 func_unref(pt->pt_name);
5022 vim_free(pt->pt_name);
5023 vim_free(pt);
5024}
5025
5026/*
5027 * Unreference a closure: decrement the reference count and free it when it
5028 * becomes zero.
5029 */
5030 void
5031partial_unref(partial_T *pt)
5032{
5033 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005034 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005035}
5036
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005037static int tv_equal_recurse_limit;
5038
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005039 static int
5040func_equal(
5041 typval_T *tv1,
5042 typval_T *tv2,
5043 int ic) /* ignore case */
5044{
5045 char_u *s1, *s2;
5046 dict_T *d1, *d2;
5047 int a1, a2;
5048 int i;
5049
5050 /* empty and NULL function name considered the same */
5051 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
5052 : tv1->vval.v_partial->pt_name;
5053 if (s1 != NULL && *s1 == NUL)
5054 s1 = NULL;
5055 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
5056 : tv2->vval.v_partial->pt_name;
5057 if (s2 != NULL && *s2 == NUL)
5058 s2 = NULL;
5059 if (s1 == NULL || s2 == NULL)
5060 {
5061 if (s1 != s2)
5062 return FALSE;
5063 }
5064 else if (STRCMP(s1, s2) != 0)
5065 return FALSE;
5066
5067 /* empty dict and NULL dict is different */
5068 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5069 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5070 if (d1 == NULL || d2 == NULL)
5071 {
5072 if (d1 != d2)
5073 return FALSE;
5074 }
5075 else if (!dict_equal(d1, d2, ic, TRUE))
5076 return FALSE;
5077
5078 /* empty list and no list considered the same */
5079 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5080 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5081 if (a1 != a2)
5082 return FALSE;
5083 for (i = 0; i < a1; ++i)
5084 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5085 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5086 return FALSE;
5087
5088 return TRUE;
5089}
5090
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005091/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005092 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005093 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005094 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005095 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005096 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005097tv_equal(
5098 typval_T *tv1,
5099 typval_T *tv2,
5100 int ic, /* ignore case */
5101 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005102{
5103 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005104 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005105 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005106 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005107
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005108 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005109 * recursiveness to a limit. We guess they are equal then.
5110 * A fixed limit has the problem of still taking an awful long time.
5111 * Reduce the limit every time running into it. That should work fine for
5112 * deeply linked structures that are not recursively linked and catch
5113 * recursiveness quickly. */
5114 if (!recursive)
5115 tv_equal_recurse_limit = 1000;
5116 if (recursive_cnt >= tv_equal_recurse_limit)
5117 {
5118 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005119 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005120 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005121
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005122 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5123 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005124 if ((tv1->v_type == VAR_FUNC
5125 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5126 && (tv2->v_type == VAR_FUNC
5127 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5128 {
5129 ++recursive_cnt;
5130 r = func_equal(tv1, tv2, ic);
5131 --recursive_cnt;
5132 return r;
5133 }
5134
5135 if (tv1->v_type != tv2->v_type)
5136 return FALSE;
5137
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005138 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005139 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005140 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005141 ++recursive_cnt;
5142 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5143 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005144 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005145
5146 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005147 ++recursive_cnt;
5148 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5149 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005150 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005151
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005152 case VAR_NUMBER:
5153 return tv1->vval.v_number == tv2->vval.v_number;
5154
5155 case VAR_STRING:
5156 s1 = get_tv_string_buf(tv1, buf1);
5157 s2 = get_tv_string_buf(tv2, buf2);
5158 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005159
5160 case VAR_SPECIAL:
5161 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005162
5163 case VAR_FLOAT:
5164#ifdef FEAT_FLOAT
5165 return tv1->vval.v_float == tv2->vval.v_float;
5166#endif
5167 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005168#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005169 return tv1->vval.v_job == tv2->vval.v_job;
5170#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005171 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005172#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005173 return tv1->vval.v_channel == tv2->vval.v_channel;
5174#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005175 case VAR_FUNC:
5176 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005177 case VAR_UNKNOWN:
5178 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005179 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005180
Bram Moolenaara03f2332016-02-06 18:09:59 +01005181 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5182 * does not equal anything, not even itself. */
5183 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005184}
5185
5186/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005187 * Return the next (unique) copy ID.
5188 * Used for serializing nested structures.
5189 */
5190 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005191get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005192{
5193 current_copyID += COPYID_INC;
5194 return current_copyID;
5195}
5196
5197/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005198 * Garbage collection for lists and dictionaries.
5199 *
5200 * We use reference counts to be able to free most items right away when they
5201 * are no longer used. But for composite items it's possible that it becomes
5202 * unused while the reference count is > 0: When there is a recursive
5203 * reference. Example:
5204 * :let l = [1, 2, 3]
5205 * :let d = {9: l}
5206 * :let l[1] = d
5207 *
5208 * Since this is quite unusual we handle this with garbage collection: every
5209 * once in a while find out which lists and dicts are not referenced from any
5210 * variable.
5211 *
5212 * Here is a good reference text about garbage collection (refers to Python
5213 * but it applies to all reference-counting mechanisms):
5214 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005215 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005216
5217/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005218 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005219 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005220 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005221 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005222 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005223garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005224{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005225 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005226 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005227 buf_T *buf;
5228 win_T *wp;
5229 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005230 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005231#ifdef FEAT_WINDOWS
5232 tabpage_T *tp;
5233#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005234
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005235 if (!testing)
5236 {
5237 /* Only do this once. */
5238 want_garbage_collect = FALSE;
5239 may_garbage_collect = FALSE;
5240 garbage_collect_at_exit = FALSE;
5241 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005242
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005243 /* We advance by two because we add one for items referenced through
5244 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005245 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005246
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005247 /*
5248 * 1. Go through all accessible variables and mark all lists and dicts
5249 * with copyID.
5250 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005251
5252 /* Don't free variables in the previous_funccal list unless they are only
5253 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005254 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005255 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005256
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005257 /* script-local variables */
5258 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005259 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005260
5261 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005262 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005263 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5264 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005265
5266 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005267 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005268 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5269 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005270#ifdef FEAT_AUTOCMD
5271 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005272 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5273 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005274#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005275
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005276#ifdef FEAT_WINDOWS
5277 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005278 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005279 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5280 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005281#endif
5282
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005283 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005284 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005285
5286 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005287 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005288
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005289 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005290 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005291
Bram Moolenaard812df62008-11-09 12:46:09 +00005292 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005293 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005294
Bram Moolenaar1dced572012-04-05 16:54:08 +02005295#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005296 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005297#endif
5298
Bram Moolenaardb913952012-06-29 12:54:53 +02005299#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005300 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005301#endif
5302
5303#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005304 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005305#endif
5306
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005307#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005308 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005309 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005310#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005311#ifdef FEAT_NETBEANS_INTG
5312 abort = abort || set_ref_in_nb_channel(copyID);
5313#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005314
Bram Moolenaare3188e22016-05-31 21:13:04 +02005315#ifdef FEAT_TIMERS
5316 abort = abort || set_ref_in_timer(copyID);
5317#endif
5318
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005319 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005320 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005321 /*
5322 * 2. Free lists and dictionaries that are not referenced.
5323 */
5324 did_free = free_unref_items(copyID);
5325
5326 /*
5327 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005328 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005329 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005330 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005331 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005332 else if (p_verbose > 0)
5333 {
5334 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
5335 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005336
5337 return did_free;
5338}
5339
5340/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005341 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005342 */
5343 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005344free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005345{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005346 int did_free = FALSE;
5347
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005348 /* Let all "free" functions know that we are here. This means no
5349 * dictionaries, lists, channels or jobs are to be freed, because we will
5350 * do that here. */
5351 in_free_unref_items = TRUE;
5352
5353 /*
5354 * PASS 1: free the contents of the items. We don't free the items
5355 * themselves yet, so that it is possible to decrement refcount counters
5356 */
5357
Bram Moolenaarcd524592016-07-17 14:57:05 +02005358 /* Go through the list of dicts and free items without the copyID. */
5359 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005360
Bram Moolenaarda861d62016-07-17 15:46:27 +02005361 /* Go through the list of lists and free items without the copyID. */
5362 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005363
5364#ifdef FEAT_JOB_CHANNEL
5365 /* Go through the list of jobs and free items without the copyID. This
5366 * must happen before doing channels, because jobs refer to channels, but
5367 * the reference from the channel to the job isn't tracked. */
5368 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5369
5370 /* Go through the list of channels and free items without the copyID. */
5371 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5372#endif
5373
5374 /*
5375 * PASS 2: free the items themselves.
5376 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005377 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005378 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005379
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005380#ifdef FEAT_JOB_CHANNEL
5381 /* Go through the list of jobs and free items without the copyID. This
5382 * must happen before doing channels, because jobs refer to channels, but
5383 * the reference from the channel to the job isn't tracked. */
5384 free_unused_jobs(copyID, COPYID_MASK);
5385
5386 /* Go through the list of channels and free items without the copyID. */
5387 free_unused_channels(copyID, COPYID_MASK);
5388#endif
5389
5390 in_free_unref_items = FALSE;
5391
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005392 return did_free;
5393}
5394
5395/*
5396 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005397 * "list_stack" is used to add lists to be marked. Can be NULL.
5398 *
5399 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005400 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005401 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005402set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005403{
5404 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005405 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005406 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005407 hashtab_T *cur_ht;
5408 ht_stack_T *ht_stack = NULL;
5409 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005410
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005411 cur_ht = ht;
5412 for (;;)
5413 {
5414 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005415 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005416 /* Mark each item in the hashtab. If the item contains a hashtab
5417 * it is added to ht_stack, if it contains a list it is added to
5418 * list_stack. */
5419 todo = (int)cur_ht->ht_used;
5420 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5421 if (!HASHITEM_EMPTY(hi))
5422 {
5423 --todo;
5424 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5425 &ht_stack, list_stack);
5426 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005427 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005428
5429 if (ht_stack == NULL)
5430 break;
5431
5432 /* take an item from the stack */
5433 cur_ht = ht_stack->ht;
5434 tempitem = ht_stack;
5435 ht_stack = ht_stack->prev;
5436 free(tempitem);
5437 }
5438
5439 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005440}
5441
5442/*
5443 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005444 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5445 *
5446 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005447 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005448 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005449set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005450{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005451 listitem_T *li;
5452 int abort = FALSE;
5453 list_T *cur_l;
5454 list_stack_T *list_stack = NULL;
5455 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005456
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005457 cur_l = l;
5458 for (;;)
5459 {
5460 if (!abort)
5461 /* Mark each item in the list. If the item contains a hashtab
5462 * it is added to ht_stack, if it contains a list it is added to
5463 * list_stack. */
5464 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5465 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5466 ht_stack, &list_stack);
5467 if (list_stack == NULL)
5468 break;
5469
5470 /* take an item from the stack */
5471 cur_l = list_stack->list;
5472 tempitem = list_stack;
5473 list_stack = list_stack->prev;
5474 free(tempitem);
5475 }
5476
5477 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005478}
5479
5480/*
5481 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005482 * "list_stack" is used to add lists to be marked. Can be NULL.
5483 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5484 *
5485 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005486 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005487 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005488set_ref_in_item(
5489 typval_T *tv,
5490 int copyID,
5491 ht_stack_T **ht_stack,
5492 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005493{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005494 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005495
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005496 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005497 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005498 dict_T *dd = tv->vval.v_dict;
5499
Bram Moolenaara03f2332016-02-06 18:09:59 +01005500 if (dd != NULL && dd->dv_copyID != copyID)
5501 {
5502 /* Didn't see this dict yet. */
5503 dd->dv_copyID = copyID;
5504 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005505 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005506 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5507 }
5508 else
5509 {
5510 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5511 if (newitem == NULL)
5512 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005513 else
5514 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005515 newitem->ht = &dd->dv_hashtab;
5516 newitem->prev = *ht_stack;
5517 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005518 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005519 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005520 }
5521 }
5522 else if (tv->v_type == VAR_LIST)
5523 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005524 list_T *ll = tv->vval.v_list;
5525
Bram Moolenaara03f2332016-02-06 18:09:59 +01005526 if (ll != NULL && ll->lv_copyID != copyID)
5527 {
5528 /* Didn't see this list yet. */
5529 ll->lv_copyID = copyID;
5530 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005531 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005532 abort = set_ref_in_list(ll, copyID, ht_stack);
5533 }
5534 else
5535 {
5536 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005537 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005538 if (newitem == NULL)
5539 abort = TRUE;
5540 else
5541 {
5542 newitem->list = ll;
5543 newitem->prev = *list_stack;
5544 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005545 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005546 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005547 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005548 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005549 else if (tv->v_type == VAR_FUNC)
5550 {
5551 abort = set_ref_in_func(tv->vval.v_string, copyID);
5552 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005553 else if (tv->v_type == VAR_PARTIAL)
5554 {
5555 partial_T *pt = tv->vval.v_partial;
5556 int i;
5557
5558 /* A partial does not have a copyID, because it cannot contain itself.
5559 */
5560 if (pt != NULL)
5561 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005562 abort = set_ref_in_func(pt->pt_name, copyID);
5563
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005564 if (pt->pt_dict != NULL)
5565 {
5566 typval_T dtv;
5567
5568 dtv.v_type = VAR_DICT;
5569 dtv.vval.v_dict = pt->pt_dict;
5570 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5571 }
5572
5573 for (i = 0; i < pt->pt_argc; ++i)
5574 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5575 ht_stack, list_stack);
5576 }
5577 }
5578#ifdef FEAT_JOB_CHANNEL
5579 else if (tv->v_type == VAR_JOB)
5580 {
5581 job_T *job = tv->vval.v_job;
5582 typval_T dtv;
5583
5584 if (job != NULL && job->jv_copyID != copyID)
5585 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005586 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005587 if (job->jv_channel != NULL)
5588 {
5589 dtv.v_type = VAR_CHANNEL;
5590 dtv.vval.v_channel = job->jv_channel;
5591 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5592 }
5593 if (job->jv_exit_partial != NULL)
5594 {
5595 dtv.v_type = VAR_PARTIAL;
5596 dtv.vval.v_partial = job->jv_exit_partial;
5597 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5598 }
5599 }
5600 }
5601 else if (tv->v_type == VAR_CHANNEL)
5602 {
5603 channel_T *ch =tv->vval.v_channel;
5604 int part;
5605 typval_T dtv;
5606 jsonq_T *jq;
5607 cbq_T *cq;
5608
5609 if (ch != NULL && ch->ch_copyID != copyID)
5610 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005611 ch->ch_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005612 for (part = PART_SOCK; part <= PART_IN; ++part)
5613 {
5614 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5615 jq = jq->jq_next)
5616 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5617 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5618 cq = cq->cq_next)
5619 if (cq->cq_partial != NULL)
5620 {
5621 dtv.v_type = VAR_PARTIAL;
5622 dtv.vval.v_partial = cq->cq_partial;
5623 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5624 }
5625 if (ch->ch_part[part].ch_partial != NULL)
5626 {
5627 dtv.v_type = VAR_PARTIAL;
5628 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5629 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5630 }
5631 }
5632 if (ch->ch_partial != NULL)
5633 {
5634 dtv.v_type = VAR_PARTIAL;
5635 dtv.vval.v_partial = ch->ch_partial;
5636 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5637 }
5638 if (ch->ch_close_partial != NULL)
5639 {
5640 dtv.v_type = VAR_PARTIAL;
5641 dtv.vval.v_partial = ch->ch_close_partial;
5642 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5643 }
5644 }
5645 }
5646#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005647 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005648}
5649
Bram Moolenaar17a13432016-01-24 14:22:10 +01005650 static char *
5651get_var_special_name(int nr)
5652{
5653 switch (nr)
5654 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005655 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005656 case VVAL_TRUE: return "v:true";
5657 case VVAL_NONE: return "v:none";
5658 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005659 }
5660 EMSG2(_(e_intern2), "get_var_special_name()");
5661 return "42";
5662}
5663
Bram Moolenaar8c711452005-01-14 21:53:12 +00005664/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005665 * Return a string with the string representation of a variable.
5666 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005667 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005668 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005669 * When both "echo_style" and "dict_val" are FALSE, put quotes around stings as
5670 * "string()", otherwise does not put quotes around strings, as ":echo"
5671 * displays values.
5672 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5673 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005674 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005675 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005676 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005677echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005678 typval_T *tv,
5679 char_u **tofree,
5680 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005681 int copyID,
5682 int echo_style,
5683 int restore_copyID,
5684 int dict_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005685{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005686 static int recurse = 0;
5687 char_u *r = NULL;
5688
Bram Moolenaar33570922005-01-25 22:26:29 +00005689 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005690 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005691 if (!did_echo_string_emsg)
5692 {
5693 /* Only give this message once for a recursive call to avoid
5694 * flooding the user with errors. And stop iterating over lists
5695 * and dicts. */
5696 did_echo_string_emsg = TRUE;
5697 EMSG(_("E724: variable nested too deep for displaying"));
5698 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005699 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005700 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005701 }
5702 ++recurse;
5703
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005704 switch (tv->v_type)
5705 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005706 case VAR_STRING:
5707 if (echo_style && !dict_val)
5708 {
5709 *tofree = NULL;
5710 r = get_tv_string_buf(tv, numbuf);
5711 }
5712 else
5713 {
5714 *tofree = string_quote(tv->vval.v_string, FALSE);
5715 r = *tofree;
5716 }
5717 break;
5718
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005719 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005720 if (echo_style)
5721 {
5722 *tofree = NULL;
5723 r = tv->vval.v_string;
5724 }
5725 else
5726 {
5727 *tofree = string_quote(tv->vval.v_string, TRUE);
5728 r = *tofree;
5729 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005730 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005731
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005732 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005733 {
5734 partial_T *pt = tv->vval.v_partial;
5735 char_u *fname = string_quote(pt == NULL ? NULL
5736 : pt->pt_name, FALSE);
5737 garray_T ga;
5738 int i;
5739 char_u *tf;
5740
5741 ga_init2(&ga, 1, 100);
5742 ga_concat(&ga, (char_u *)"function(");
5743 if (fname != NULL)
5744 {
5745 ga_concat(&ga, fname);
5746 vim_free(fname);
5747 }
5748 if (pt != NULL && pt->pt_argc > 0)
5749 {
5750 ga_concat(&ga, (char_u *)", [");
5751 for (i = 0; i < pt->pt_argc; ++i)
5752 {
5753 if (i > 0)
5754 ga_concat(&ga, (char_u *)", ");
5755 ga_concat(&ga,
5756 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5757 vim_free(tf);
5758 }
5759 ga_concat(&ga, (char_u *)"]");
5760 }
5761 if (pt != NULL && pt->pt_dict != NULL)
5762 {
5763 typval_T dtv;
5764
5765 ga_concat(&ga, (char_u *)", ");
5766 dtv.v_type = VAR_DICT;
5767 dtv.vval.v_dict = pt->pt_dict;
5768 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5769 vim_free(tf);
5770 }
5771 ga_concat(&ga, (char_u *)")");
5772
5773 *tofree = ga.ga_data;
5774 r = *tofree;
5775 break;
5776 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005777
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005778 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005779 if (tv->vval.v_list == NULL)
5780 {
5781 *tofree = NULL;
5782 r = NULL;
5783 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005784 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5785 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005786 {
5787 *tofree = NULL;
5788 r = (char_u *)"[...]";
5789 }
5790 else
5791 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005792 int old_copyID = tv->vval.v_list->lv_copyID;
5793
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005794 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005795 *tofree = list2string(tv, copyID, restore_copyID);
5796 if (restore_copyID)
5797 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005798 r = *tofree;
5799 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005800 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005801
Bram Moolenaar8c711452005-01-14 21:53:12 +00005802 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005803 if (tv->vval.v_dict == NULL)
5804 {
5805 *tofree = NULL;
5806 r = NULL;
5807 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005808 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5809 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005810 {
5811 *tofree = NULL;
5812 r = (char_u *)"{...}";
5813 }
5814 else
5815 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005816 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005817 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005818 *tofree = dict2string(tv, copyID, restore_copyID);
5819 if (restore_copyID)
5820 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005821 r = *tofree;
5822 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005823 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005824
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005825 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005826 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005827 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005828 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005829 *tofree = NULL;
5830 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005831 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005832
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005833 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005834#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005835 *tofree = NULL;
5836 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5837 r = numbuf;
5838 break;
5839#endif
5840
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005841 case VAR_SPECIAL:
5842 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005843 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005844 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005845 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005846
Bram Moolenaar8502c702014-06-17 12:51:16 +02005847 if (--recurse == 0)
5848 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005849 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005850}
5851
5852/*
5853 * Return a string with the string representation of a variable.
5854 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5855 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005856 * Does not put quotes around strings, as ":echo" displays values.
5857 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5858 * May return NULL.
5859 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005860 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005861echo_string(
5862 typval_T *tv,
5863 char_u **tofree,
5864 char_u *numbuf,
5865 int copyID)
5866{
5867 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5868}
5869
5870/*
5871 * Return a string with the string representation of a variable.
5872 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5873 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005874 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005875 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005876 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02005877 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005878tv2string(
5879 typval_T *tv,
5880 char_u **tofree,
5881 char_u *numbuf,
5882 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005883{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005884 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005885}
5886
5887/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005888 * Return string "str" in ' quotes, doubling ' characters.
5889 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005890 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005891 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005892 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005893string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005894{
Bram Moolenaar33570922005-01-25 22:26:29 +00005895 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005896 char_u *p, *r, *s;
5897
Bram Moolenaar33570922005-01-25 22:26:29 +00005898 len = (function ? 13 : 3);
5899 if (str != NULL)
5900 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005901 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00005902 for (p = str; *p != NUL; mb_ptr_adv(p))
5903 if (*p == '\'')
5904 ++len;
5905 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005906 s = r = alloc(len);
5907 if (r != NULL)
5908 {
5909 if (function)
5910 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005911 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005912 r += 10;
5913 }
5914 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005915 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005916 if (str != NULL)
5917 for (p = str; *p != NUL; )
5918 {
5919 if (*p == '\'')
5920 *r++ = '\'';
5921 MB_COPY_CHAR(p, r);
5922 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005923 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005924 if (function)
5925 *r++ = ')';
5926 *r++ = NUL;
5927 }
5928 return s;
5929}
5930
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005931#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005932/*
5933 * Convert the string "text" to a floating point number.
5934 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5935 * this always uses a decimal point.
5936 * Returns the length of the text that was consumed.
5937 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005938 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005939string2float(
5940 char_u *text,
5941 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005942{
5943 char *s = (char *)text;
5944 float_T f;
5945
5946 f = strtod(s, &s);
5947 *value = f;
5948 return (int)((char_u *)s - text);
5949}
5950#endif
5951
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005952/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 * Get the value of an environment variable.
5954 * "arg" is pointing to the '$'. It is advanced to after the name.
5955 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005956 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 */
5958 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005959get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960{
5961 char_u *string = NULL;
5962 int len;
5963 int cc;
5964 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005965 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966
5967 ++*arg;
5968 name = *arg;
5969 len = get_env_len(arg);
5970 if (evaluate)
5971 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005972 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01005973 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005974
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005975 cc = name[len];
5976 name[len] = NUL;
5977 /* first try vim_getenv(), fast for normal environment vars */
5978 string = vim_getenv(name, &mustfree);
5979 if (string != NULL && *string != NUL)
5980 {
5981 if (!mustfree)
5982 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005984 else
5985 {
5986 if (mustfree)
5987 vim_free(string);
5988
5989 /* next try expanding things like $VIM and ${HOME} */
5990 string = expand_env_save(name - 1);
5991 if (string != NULL && *string == '$')
5992 {
5993 vim_free(string);
5994 string = NULL;
5995 }
5996 }
5997 name[len] = cc;
5998
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005999 rettv->v_type = VAR_STRING;
6000 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 }
6002
6003 return OK;
6004}
6005
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006006
6007
6008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006010 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006012 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006013var2fpos(
6014 typval_T *varp,
6015 int dollar_lnum, /* TRUE when $ is last line */
6016 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006018 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006020 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021
Bram Moolenaara5525202006-03-02 22:52:09 +00006022 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006023 if (varp->v_type == VAR_LIST)
6024 {
6025 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006026 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006027 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006028 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006029
6030 l = varp->vval.v_list;
6031 if (l == NULL)
6032 return NULL;
6033
6034 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006035 pos.lnum = list_find_nr(l, 0L, &error);
6036 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006037 return NULL; /* invalid line number */
6038
6039 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006040 pos.col = list_find_nr(l, 1L, &error);
6041 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006042 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006043 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006044
6045 /* We accept "$" for the column number: last column. */
6046 li = list_find(l, 1L);
6047 if (li != NULL && li->li_tv.v_type == VAR_STRING
6048 && li->li_tv.vval.v_string != NULL
6049 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6050 pos.col = len + 1;
6051
Bram Moolenaara5525202006-03-02 22:52:09 +00006052 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006053 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006054 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006055 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006056
Bram Moolenaara5525202006-03-02 22:52:09 +00006057#ifdef FEAT_VIRTUALEDIT
6058 /* Get the virtual offset. Defaults to zero. */
6059 pos.coladd = list_find_nr(l, 2L, &error);
6060 if (error)
6061 pos.coladd = 0;
6062#endif
6063
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006064 return &pos;
6065 }
6066
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006067 name = get_tv_string_chk(varp);
6068 if (name == NULL)
6069 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006070 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006072 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6073 {
6074 if (VIsual_active)
6075 return &VIsual;
6076 return &curwin->w_cursor;
6077 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006078 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006080 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6082 return NULL;
6083 return pp;
6084 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006085
6086#ifdef FEAT_VIRTUALEDIT
6087 pos.coladd = 0;
6088#endif
6089
Bram Moolenaar477933c2007-07-17 14:32:23 +00006090 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006091 {
6092 pos.col = 0;
6093 if (name[1] == '0') /* "w0": first visible line */
6094 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006095 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006096 pos.lnum = curwin->w_topline;
6097 return &pos;
6098 }
6099 else if (name[1] == '$') /* "w$": last visible line */
6100 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006101 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006102 pos.lnum = curwin->w_botline - 1;
6103 return &pos;
6104 }
6105 }
6106 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006108 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 {
6110 pos.lnum = curbuf->b_ml.ml_line_count;
6111 pos.col = 0;
6112 }
6113 else
6114 {
6115 pos.lnum = curwin->w_cursor.lnum;
6116 pos.col = (colnr_T)STRLEN(ml_get_curline());
6117 }
6118 return &pos;
6119 }
6120 return NULL;
6121}
6122
6123/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006124 * Convert list in "arg" into a position and optional file number.
6125 * When "fnump" is NULL there is no file number, only 3 items.
6126 * Note that the column is passed on as-is, the caller may want to decrement
6127 * it to use 1 for the first column.
6128 * Return FAIL when conversion is not possible, doesn't check the position for
6129 * validity.
6130 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006131 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006132list2fpos(
6133 typval_T *arg,
6134 pos_T *posp,
6135 int *fnump,
6136 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006137{
6138 list_T *l = arg->vval.v_list;
6139 long i = 0;
6140 long n;
6141
Bram Moolenaar493c1782014-05-28 14:34:46 +02006142 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6143 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006144 if (arg->v_type != VAR_LIST
6145 || l == NULL
6146 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006147 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006148 return FAIL;
6149
6150 if (fnump != NULL)
6151 {
6152 n = list_find_nr(l, i++, NULL); /* fnum */
6153 if (n < 0)
6154 return FAIL;
6155 if (n == 0)
6156 n = curbuf->b_fnum; /* current buffer */
6157 *fnump = n;
6158 }
6159
6160 n = list_find_nr(l, i++, NULL); /* lnum */
6161 if (n < 0)
6162 return FAIL;
6163 posp->lnum = n;
6164
6165 n = list_find_nr(l, i++, NULL); /* col */
6166 if (n < 0)
6167 return FAIL;
6168 posp->col = n;
6169
6170#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +02006171 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006172 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006173 posp->coladd = 0;
6174 else
6175 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006176#endif
6177
Bram Moolenaar493c1782014-05-28 14:34:46 +02006178 if (curswantp != NULL)
6179 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6180
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006181 return OK;
6182}
6183
6184/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 * Get the length of an environment variable name.
6186 * Advance "arg" to the first character after the name.
6187 * Return 0 for error.
6188 */
6189 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006190get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191{
6192 char_u *p;
6193 int len;
6194
6195 for (p = *arg; vim_isIDc(*p); ++p)
6196 ;
6197 if (p == *arg) /* no name found */
6198 return 0;
6199
6200 len = (int)(p - *arg);
6201 *arg = p;
6202 return len;
6203}
6204
6205/*
6206 * Get the length of the name of a function or internal variable.
6207 * "arg" is advanced to the first non-white character after the name.
6208 * Return 0 if something is wrong.
6209 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006210 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006211get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212{
6213 char_u *p;
6214 int len;
6215
6216 /* Find the end of the name. */
6217 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006218 {
6219 if (*p == ':')
6220 {
6221 /* "s:" is start of "s:var", but "n:" is not and can be used in
6222 * slice "[n:]". Also "xx:" is not a namespace. */
6223 len = (int)(p - *arg);
6224 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6225 || len > 1)
6226 break;
6227 }
6228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 if (p == *arg) /* no name found */
6230 return 0;
6231
6232 len = (int)(p - *arg);
6233 *arg = skipwhite(p);
6234
6235 return len;
6236}
6237
6238/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006239 * Get the length of the name of a variable or function.
6240 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006242 * Return -1 if curly braces expansion failed.
6243 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 * If the name contains 'magic' {}'s, expand them and return the
6245 * expanded name in an allocated string via 'alias' - caller must free.
6246 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006247 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006248get_name_len(
6249 char_u **arg,
6250 char_u **alias,
6251 int evaluate,
6252 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253{
6254 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 char_u *p;
6256 char_u *expr_start;
6257 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258
6259 *alias = NULL; /* default to no alias */
6260
6261 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6262 && (*arg)[2] == (int)KE_SNR)
6263 {
6264 /* hard coded <SNR>, already translated */
6265 *arg += 3;
6266 return get_id_len(arg) + 3;
6267 }
6268 len = eval_fname_script(*arg);
6269 if (len > 0)
6270 {
6271 /* literal "<SID>", "s:" or "<SNR>" */
6272 *arg += len;
6273 }
6274
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006276 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006278 p = find_name_end(*arg, &expr_start, &expr_end,
6279 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 if (expr_start != NULL)
6281 {
6282 char_u *temp_string;
6283
6284 if (!evaluate)
6285 {
6286 len += (int)(p - *arg);
6287 *arg = skipwhite(p);
6288 return len;
6289 }
6290
6291 /*
6292 * Include any <SID> etc in the expanded string:
6293 * Thus the -len here.
6294 */
6295 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6296 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006297 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 *alias = temp_string;
6299 *arg = skipwhite(p);
6300 return (int)STRLEN(temp_string);
6301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302
6303 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006304 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 EMSG2(_(e_invexpr2), *arg);
6306
6307 return len;
6308}
6309
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006310/*
6311 * Find the end of a variable or function name, taking care of magic braces.
6312 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6313 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006314 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006315 * Return a pointer to just after the name. Equal to "arg" if there is no
6316 * valid name.
6317 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006318 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006319find_name_end(
6320 char_u *arg,
6321 char_u **expr_start,
6322 char_u **expr_end,
6323 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006325 int mb_nest = 0;
6326 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006328 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006330 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006332 *expr_start = NULL;
6333 *expr_end = NULL;
6334 }
6335
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006336 /* Quick check for valid starting character. */
6337 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6338 return arg;
6339
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006340 for (p = arg; *p != NUL
6341 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006342 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006343 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006344 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +00006345 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006346 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006347 if (*p == '\'')
6348 {
6349 /* skip over 'string' to avoid counting [ and ] inside it. */
6350 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
6351 ;
6352 if (*p == NUL)
6353 break;
6354 }
6355 else if (*p == '"')
6356 {
6357 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
6358 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
6359 if (*p == '\\' && p[1] != NUL)
6360 ++p;
6361 if (*p == NUL)
6362 break;
6363 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006364 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6365 {
6366 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006367 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006368 len = (int)(p - arg);
6369 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006370 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006371 break;
6372 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006373
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006374 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006376 if (*p == '[')
6377 ++br_nest;
6378 else if (*p == ']')
6379 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006381
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006382 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006384 if (*p == '{')
6385 {
6386 mb_nest++;
6387 if (expr_start != NULL && *expr_start == NULL)
6388 *expr_start = p;
6389 }
6390 else if (*p == '}')
6391 {
6392 mb_nest--;
6393 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6394 *expr_end = p;
6395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 }
6398
6399 return p;
6400}
6401
6402/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006403 * Expands out the 'magic' {}'s in a variable/function name.
6404 * Note that this can call itself recursively, to deal with
6405 * constructs like foo{bar}{baz}{bam}
6406 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6407 * "in_start" ^
6408 * "expr_start" ^
6409 * "expr_end" ^
6410 * "in_end" ^
6411 *
6412 * Returns a new allocated string, which the caller must free.
6413 * Returns NULL for failure.
6414 */
6415 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006416make_expanded_name(
6417 char_u *in_start,
6418 char_u *expr_start,
6419 char_u *expr_end,
6420 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006421{
6422 char_u c1;
6423 char_u *retval = NULL;
6424 char_u *temp_result;
6425 char_u *nextcmd = NULL;
6426
6427 if (expr_end == NULL || in_end == NULL)
6428 return NULL;
6429 *expr_start = NUL;
6430 *expr_end = NUL;
6431 c1 = *in_end;
6432 *in_end = NUL;
6433
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006434 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006435 if (temp_result != NULL && nextcmd == NULL)
6436 {
6437 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6438 + (in_end - expr_end) + 1));
6439 if (retval != NULL)
6440 {
6441 STRCPY(retval, in_start);
6442 STRCAT(retval, temp_result);
6443 STRCAT(retval, expr_end + 1);
6444 }
6445 }
6446 vim_free(temp_result);
6447
6448 *in_end = c1; /* put char back for error messages */
6449 *expr_start = '{';
6450 *expr_end = '}';
6451
6452 if (retval != NULL)
6453 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006454 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006455 if (expr_start != NULL)
6456 {
6457 /* Further expansion! */
6458 temp_result = make_expanded_name(retval, expr_start,
6459 expr_end, temp_result);
6460 vim_free(retval);
6461 retval = temp_result;
6462 }
6463 }
6464
6465 return retval;
6466}
6467
6468/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006470 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006472 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006473eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006475 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6476}
6477
6478/*
6479 * Return TRUE if character "c" can be used as the first character in a
6480 * variable or function name (excluding '{' and '}').
6481 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006482 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006483eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006484{
6485 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486}
6487
6488/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 * Set number v: variable to "val".
6490 */
6491 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006492set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006494 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495}
6496
6497/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006498 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006500 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006501get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006503 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504}
6505
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006506/*
6507 * Get string v: variable value. Uses a static buffer, can only be used once.
6508 */
6509 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006510get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006511{
6512 return get_tv_string(&vimvars[idx].vv_tv);
6513}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006514
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006516 * Get List v: variable value. Caller must take care of reference count when
6517 * needed.
6518 */
6519 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006520get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006521{
6522 return vimvars[idx].vv_list;
6523}
6524
6525/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006526 * Set v:char to character "c".
6527 */
6528 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006529set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006530{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006531 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006532
6533#ifdef FEAT_MBYTE
6534 if (has_mbyte)
6535 buf[(*mb_char2bytes)(c, buf)] = NUL;
6536 else
6537#endif
6538 {
6539 buf[0] = c;
6540 buf[1] = NUL;
6541 }
6542 set_vim_var_string(VV_CHAR, buf, -1);
6543}
6544
6545/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006546 * Set v:count to "count" and v:count1 to "count1".
6547 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 */
6549 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006550set_vcount(
6551 long count,
6552 long count1,
6553 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006555 if (set_prevcount)
6556 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006557 vimvars[VV_COUNT].vv_nr = count;
6558 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559}
6560
6561/*
6562 * Set string v: variable to a copy of "val".
6563 */
6564 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006565set_vim_var_string(
6566 int idx,
6567 char_u *val,
6568 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569{
Bram Moolenaara542c682016-01-31 16:28:04 +01006570 clear_tv(&vimvars[idx].vv_di.di_tv);
6571 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006573 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006575 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006577 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578}
6579
6580/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006581 * Set List v: variable to "val".
6582 */
6583 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006584set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006585{
Bram Moolenaara542c682016-01-31 16:28:04 +01006586 clear_tv(&vimvars[idx].vv_di.di_tv);
6587 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006588 vimvars[idx].vv_list = val;
6589 if (val != NULL)
6590 ++val->lv_refcount;
6591}
6592
6593/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006594 * Set Dictionary v: variable to "val".
6595 */
6596 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006597set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006598{
6599 int todo;
6600 hashitem_T *hi;
6601
Bram Moolenaara542c682016-01-31 16:28:04 +01006602 clear_tv(&vimvars[idx].vv_di.di_tv);
6603 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006604 vimvars[idx].vv_dict = val;
6605 if (val != NULL)
6606 {
6607 ++val->dv_refcount;
6608
6609 /* Set readonly */
6610 todo = (int)val->dv_hashtab.ht_used;
6611 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
6612 {
6613 if (HASHITEM_EMPTY(hi))
6614 continue;
6615 --todo;
6616 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
6617 }
6618 }
6619}
6620
6621/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 * Set v:register if needed.
6623 */
6624 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006625set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626{
6627 char_u regname;
6628
6629 if (c == 0 || c == ' ')
6630 regname = '"';
6631 else
6632 regname = c;
6633 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006634 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 set_vim_var_string(VV_REG, &regname, 1);
6636}
6637
6638/*
6639 * Get or set v:exception. If "oldval" == NULL, return the current value.
6640 * Otherwise, restore the value to "oldval" and return NULL.
6641 * Must always be called in pairs to save and restore v:exception! Does not
6642 * take care of memory allocations.
6643 */
6644 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006645v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646{
6647 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006648 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649
Bram Moolenaare9a41262005-01-15 22:18:47 +00006650 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 return NULL;
6652}
6653
6654/*
6655 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
6656 * Otherwise, restore the value to "oldval" and return NULL.
6657 * Must always be called in pairs to save and restore v:throwpoint! Does not
6658 * take care of memory allocations.
6659 */
6660 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006661v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662{
6663 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006664 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665
Bram Moolenaare9a41262005-01-15 22:18:47 +00006666 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 return NULL;
6668}
6669
6670#if defined(FEAT_AUTOCMD) || defined(PROTO)
6671/*
6672 * Set v:cmdarg.
6673 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6674 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6675 * Must always be called in pairs!
6676 */
6677 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006678set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679{
6680 char_u *oldval;
6681 char_u *newval;
6682 unsigned len;
6683
Bram Moolenaare9a41262005-01-15 22:18:47 +00006684 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006685 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006687 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006688 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006689 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 }
6691
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006692 if (eap->force_bin == FORCE_BIN)
6693 len = 6;
6694 else if (eap->force_bin == FORCE_NOBIN)
6695 len = 8;
6696 else
6697 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006698
6699 if (eap->read_edit)
6700 len += 7;
6701
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006702 if (eap->force_ff != 0)
6703 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
6704# ifdef FEAT_MBYTE
6705 if (eap->force_enc != 0)
6706 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006707 if (eap->bad_char != 0)
6708 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006709# endif
6710
6711 newval = alloc(len + 1);
6712 if (newval == NULL)
6713 return NULL;
6714
6715 if (eap->force_bin == FORCE_BIN)
6716 sprintf((char *)newval, " ++bin");
6717 else if (eap->force_bin == FORCE_NOBIN)
6718 sprintf((char *)newval, " ++nobin");
6719 else
6720 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006721
6722 if (eap->read_edit)
6723 STRCAT(newval, " ++edit");
6724
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006725 if (eap->force_ff != 0)
6726 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
6727 eap->cmd + eap->force_ff);
6728# ifdef FEAT_MBYTE
6729 if (eap->force_enc != 0)
6730 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6731 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006732 if (eap->bad_char == BAD_KEEP)
6733 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6734 else if (eap->bad_char == BAD_DROP)
6735 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6736 else if (eap->bad_char != 0)
6737 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006738# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00006739 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006740 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741}
6742#endif
6743
6744/*
6745 * Get the value of internal variable "name".
6746 * Return OK or FAIL.
6747 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006748 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006749get_var_tv(
6750 char_u *name,
6751 int len, /* length of "name" */
6752 typval_T *rettv, /* NULL when only checking existence */
6753 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
6754 int verbose, /* may give error message */
6755 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756{
6757 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006758 typval_T *tv = NULL;
6759 typval_T atv;
6760 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762
6763 /* truncate the name, so that we can use strcmp() */
6764 cc = name[len];
6765 name[len] = NUL;
6766
6767 /*
6768 * Check for "b:changedtick".
6769 */
6770 if (STRCMP(name, "b:changedtick") == 0)
6771 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00006772 atv.v_type = VAR_NUMBER;
6773 atv.vval.v_number = curbuf->b_changedtick;
6774 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 }
6776
6777 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 * Check for user-defined variables.
6779 */
6780 else
6781 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01006782 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02006784 {
Bram Moolenaar33570922005-01-25 22:26:29 +00006785 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02006786 if (dip != NULL)
6787 *dip = v;
6788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 }
6790
Bram Moolenaare9a41262005-01-15 22:18:47 +00006791 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006793 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006794 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 ret = FAIL;
6796 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006797 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006798 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799
6800 name[len] = cc;
6801
6802 return ret;
6803}
6804
6805/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006806 * Check if variable "name[len]" is a local variable or an argument.
6807 * If so, "*eval_lavars_used" is set to TRUE.
6808 */
6809 static void
6810check_vars(char_u *name, int len)
6811{
6812 int cc;
6813 char_u *varname;
6814 hashtab_T *ht;
6815
6816 if (eval_lavars_used == NULL)
6817 return;
6818
6819 /* truncate the name, so that we can use strcmp() */
6820 cc = name[len];
6821 name[len] = NUL;
6822
6823 ht = find_var_ht(name, &varname);
6824 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
6825 {
6826 if (find_var(name, NULL, TRUE) != NULL)
6827 *eval_lavars_used = TRUE;
6828 }
6829
6830 name[len] = cc;
6831}
6832
6833/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006834 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
6835 * Also handle function call with Funcref variable: func(expr)
6836 * Can all be combined: dict.func(expr)[idx]['func'](expr)
6837 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006838 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006839handle_subscript(
6840 char_u **arg,
6841 typval_T *rettv,
6842 int evaluate, /* do more than finding the end */
6843 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006844{
6845 int ret = OK;
6846 dict_T *selfdict = NULL;
6847 char_u *s;
6848 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006849 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006850
6851 while (ret == OK
6852 && (**arg == '['
6853 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006854 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6855 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006856 && !vim_iswhite(*(*arg - 1)))
6857 {
6858 if (**arg == '(')
6859 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006860 partial_T *pt = NULL;
6861
Bram Moolenaard9fba312005-06-26 22:34:35 +00006862 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006863 if (evaluate)
6864 {
6865 functv = *rettv;
6866 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006867
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006868 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006869 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006870 {
6871 pt = functv.vval.v_partial;
6872 s = pt->pt_name;
6873 }
6874 else
6875 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006876 }
6877 else
6878 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006879 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00006880 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006881 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006882
6883 /* Clear the funcref afterwards, so that deleting it while
6884 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006885 if (evaluate)
6886 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006887
6888 /* Stop the expression evaluation when immediately aborting on
6889 * error, or when an interrupt occurred or an exception was thrown
6890 * but not caught. */
6891 if (aborting())
6892 {
6893 if (ret == OK)
6894 clear_tv(rettv);
6895 ret = FAIL;
6896 }
6897 dict_unref(selfdict);
6898 selfdict = NULL;
6899 }
6900 else /* **arg == '[' || **arg == '.' */
6901 {
6902 dict_unref(selfdict);
6903 if (rettv->v_type == VAR_DICT)
6904 {
6905 selfdict = rettv->vval.v_dict;
6906 if (selfdict != NULL)
6907 ++selfdict->dv_refcount;
6908 }
6909 else
6910 selfdict = NULL;
6911 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
6912 {
6913 clear_tv(rettv);
6914 ret = FAIL;
6915 }
6916 }
6917 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006918
Bram Moolenaar1d429612016-05-24 15:44:17 +02006919 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
6920 * Don't do this when "Func" is already a partial that was bound
6921 * explicitly (pt_auto is FALSE). */
6922 if (selfdict != NULL
6923 && (rettv->v_type == VAR_FUNC
6924 || (rettv->v_type == VAR_PARTIAL
6925 && (rettv->vval.v_partial->pt_auto
6926 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006927 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006928
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006929 dict_unref(selfdict);
6930 return ret;
6931}
6932
6933/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006934 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006935 * value).
6936 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01006937 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006938alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006939{
Bram Moolenaar33570922005-01-25 22:26:29 +00006940 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006941}
6942
6943/*
6944 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 * The string "s" must have been allocated, it is consumed.
6946 * Return NULL for out of memory, the variable otherwise.
6947 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006948 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006949alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950{
Bram Moolenaar33570922005-01-25 22:26:29 +00006951 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006953 rettv = alloc_tv();
6954 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006956 rettv->v_type = VAR_STRING;
6957 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 }
6959 else
6960 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006961 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962}
6963
6964/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006965 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006967 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006968free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969{
6970 if (varp != NULL)
6971 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006972 switch (varp->v_type)
6973 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006974 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006975 func_unref(varp->vval.v_string);
6976 /*FALLTHROUGH*/
6977 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006978 vim_free(varp->vval.v_string);
6979 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006980 case VAR_PARTIAL:
6981 partial_unref(varp->vval.v_partial);
6982 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006983 case VAR_LIST:
6984 list_unref(varp->vval.v_list);
6985 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006986 case VAR_DICT:
6987 dict_unref(varp->vval.v_dict);
6988 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006989 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006990#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006991 job_unref(varp->vval.v_job);
6992 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006993#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006994 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006995#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006996 channel_unref(varp->vval.v_channel);
6997 break;
6998#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006999 case VAR_NUMBER:
7000 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007001 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007002 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007003 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 vim_free(varp);
7006 }
7007}
7008
7009/*
7010 * Free the memory for a variable value and set the value to NULL or 0.
7011 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007012 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007013clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014{
7015 if (varp != NULL)
7016 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007017 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007019 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007020 func_unref(varp->vval.v_string);
7021 /*FALLTHROUGH*/
7022 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007023 vim_free(varp->vval.v_string);
7024 varp->vval.v_string = NULL;
7025 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007026 case VAR_PARTIAL:
7027 partial_unref(varp->vval.v_partial);
7028 varp->vval.v_partial = NULL;
7029 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007030 case VAR_LIST:
7031 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007032 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007033 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007034 case VAR_DICT:
7035 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007036 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007037 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007038 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007039 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007040 varp->vval.v_number = 0;
7041 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007042 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007043#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007044 varp->vval.v_float = 0.0;
7045 break;
7046#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007047 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007048#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007049 job_unref(varp->vval.v_job);
7050 varp->vval.v_job = NULL;
7051#endif
7052 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007053 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007054#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007055 channel_unref(varp->vval.v_channel);
7056 varp->vval.v_channel = NULL;
7057#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007058 case VAR_UNKNOWN:
7059 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007061 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 }
7063}
7064
7065/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007066 * Set the value of a variable to NULL without freeing items.
7067 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007068 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007069init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007070{
7071 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007072 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007073}
7074
7075/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 * Get the number value of a variable.
7077 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007078 * For incompatible types, return 0.
7079 * get_tv_number_chk() is similar to get_tv_number(), but informs the
7080 * caller of incompatible types: it sets *denote to TRUE if "denote"
7081 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007083 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007084get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007086 int error = FALSE;
7087
7088 return get_tv_number_chk(varp, &error); /* return 0L on error */
7089}
7090
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007091 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007092get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007093{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007094 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007096 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007098 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007099 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007100 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007101#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +00007102 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007103 break;
7104#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007105 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007106 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007107 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007108 break;
7109 case VAR_STRING:
7110 if (varp->vval.v_string != NULL)
7111 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01007112 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007113 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007114 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007115 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007116 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007117 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007118 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007119 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007120 case VAR_SPECIAL:
7121 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7122 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007123 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007124#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007125 EMSG(_("E910: Using a Job as a Number"));
7126 break;
7127#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007128 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007129#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007130 EMSG(_("E913: Using a Channel as a Number"));
7131 break;
7132#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007133 case VAR_UNKNOWN:
7134 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007135 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007137 if (denote == NULL) /* useful for values that must be unsigned */
7138 n = -1;
7139 else
7140 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007141 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142}
7143
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007144#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007145 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007146get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007147{
7148 switch (varp->v_type)
7149 {
7150 case VAR_NUMBER:
7151 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007152 case VAR_FLOAT:
7153 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007154 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007155 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007156 EMSG(_("E891: Using a Funcref as a Float"));
7157 break;
7158 case VAR_STRING:
7159 EMSG(_("E892: Using a String as a Float"));
7160 break;
7161 case VAR_LIST:
7162 EMSG(_("E893: Using a List as a Float"));
7163 break;
7164 case VAR_DICT:
7165 EMSG(_("E894: Using a Dictionary as a Float"));
7166 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007167 case VAR_SPECIAL:
7168 EMSG(_("E907: Using a special value as a Float"));
7169 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007170 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007171# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007172 EMSG(_("E911: Using a Job as a Float"));
7173 break;
7174# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007175 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007176# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007177 EMSG(_("E914: Using a Channel as a Float"));
7178 break;
7179# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007180 case VAR_UNKNOWN:
7181 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007182 break;
7183 }
7184 return 0;
7185}
7186#endif
7187
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 * Get the string value of a variable.
7190 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +00007191 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7192 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 * If the String variable has never been set, return an empty string.
7194 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007195 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
7196 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007198 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007199get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200{
7201 static char_u mybuf[NUMBUFLEN];
7202
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007203 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204}
7205
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007206 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007207get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007209 char_u *res = get_tv_string_buf_chk(varp, buf);
7210
7211 return res != NULL ? res : (char_u *)"";
7212}
7213
Bram Moolenaar7d647822014-04-05 21:28:56 +02007214/*
7215 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7216 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007217 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007218get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007219{
7220 static char_u mybuf[NUMBUFLEN];
7221
7222 return get_tv_string_buf_chk(varp, mybuf);
7223}
7224
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007225 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007226get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007227{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007228 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007230 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007231 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
7232 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007233 return buf;
7234 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007235 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007236 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007237 break;
7238 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007239 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007240 break;
7241 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007242 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007243 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007244 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007245#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +02007246 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007247 break;
7248#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007249 case VAR_STRING:
7250 if (varp->vval.v_string != NULL)
7251 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007252 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007253 case VAR_SPECIAL:
7254 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7255 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007256 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007257#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007258 {
7259 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007260 char *status;
7261
7262 if (job == NULL)
7263 return (char_u *)"no process";
7264 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007265 : job->jv_status == JOB_ENDED ? "dead"
7266 : "run";
7267# ifdef UNIX
7268 vim_snprintf((char *)buf, NUMBUFLEN,
7269 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007270# elif defined(WIN32)
7271 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007272 "process %ld %s",
7273 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007274 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007275# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007276 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007277 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7278# endif
7279 return buf;
7280 }
7281#endif
7282 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007283 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007284#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007285 {
7286 channel_T *channel = varp->vval.v_channel;
7287 char *status = channel_status(channel);
7288
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007289 if (channel == NULL)
7290 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7291 else
7292 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007293 "channel %d %s", channel->ch_id, status);
7294 return buf;
7295 }
7296#endif
7297 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007298 case VAR_UNKNOWN:
7299 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007300 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007302 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303}
7304
7305/*
7306 * Find variable "name" in the list of variables.
7307 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007308 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007309 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007310 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007312 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007313find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007316 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007317 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318
Bram Moolenaara7043832005-01-21 11:56:39 +00007319 ht = find_var_ht(name, &varname);
7320 if (htp != NULL)
7321 *htp = ht;
7322 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007324 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7325 if (ret != NULL)
7326 return ret;
7327
7328 /* Search in parent scope for lambda */
7329 return find_var_in_scoped_ht(name, varname ? &varname : NULL,
7330 no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331}
7332
7333/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007334 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007335 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007337 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007338find_var_in_ht(
7339 hashtab_T *ht,
7340 int htname,
7341 char_u *varname,
7342 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007343{
Bram Moolenaar33570922005-01-25 22:26:29 +00007344 hashitem_T *hi;
7345
7346 if (*varname == NUL)
7347 {
7348 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007349 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007350 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007351 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007352 case 'g': return &globvars_var;
7353 case 'v': return &vimvars_var;
7354 case 'b': return &curbuf->b_bufvar;
7355 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007356#ifdef FEAT_WINDOWS
7357 case 't': return &curtab->tp_winvar;
7358#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007359 case 'l': return get_funccal_local_var();
7360 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007361 }
7362 return NULL;
7363 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007364
7365 hi = hash_find(ht, varname);
7366 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007367 {
7368 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007369 * worked find the variable again. Don't auto-load a script if it was
7370 * loaded already, otherwise it would be loaded every time when
7371 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007372 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007373 {
7374 /* Note: script_autoload() may make "hi" invalid. It must either
7375 * be obtained again or not used. */
7376 if (!script_autoload(varname, FALSE) || aborting())
7377 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007378 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007379 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007380 if (HASHITEM_EMPTY(hi))
7381 return NULL;
7382 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007383 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007384}
7385
7386/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007387 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007388 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007389 * Set "varname" to the start of name without ':'.
7390 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007391 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007392find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007394 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007395 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007396
Bram Moolenaar73627d02015-08-11 15:46:09 +02007397 if (name[0] == NUL)
7398 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 if (name[1] != ':')
7400 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007401 /* The name must not start with a colon or #. */
7402 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 return NULL;
7404 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007405
7406 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007407 hi = hash_find(&compat_hashtab, name);
7408 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +00007409 return &compat_hashtab;
7410
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007411 ht = get_funccal_local_ht();
7412 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007413 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007414 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 }
7416 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007417 if (*name == 'g') /* global variable */
7418 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007419 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7420 */
7421 if (vim_strchr(name + 2, ':') != NULL
7422 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007423 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007425 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007427 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007428#ifdef FEAT_WINDOWS
7429 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007430 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007431#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007432 if (*name == 'v') /* v: variable */
7433 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007434 if (*name == 'a') /* a: function argument */
7435 return get_funccal_args_ht();
7436 if (*name == 'l') /* l: local function variable */
7437 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 if (*name == 's' /* script variable */
7439 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
7440 return &SCRIPT_VARS(current_SID);
7441 return NULL;
7442}
7443
7444/*
7445 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +02007446 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 * Returns NULL when it doesn't exist.
7448 */
7449 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007450get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451{
Bram Moolenaar33570922005-01-25 22:26:29 +00007452 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007454 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 if (v == NULL)
7456 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007457 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458}
7459
7460/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007461 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 * sourcing this script and when executing functions defined in the script.
7463 */
7464 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007465new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466{
Bram Moolenaara7043832005-01-21 11:56:39 +00007467 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007468 hashtab_T *ht;
7469 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007470
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7472 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007473 /* Re-allocating ga_data means that an ht_array pointing to
7474 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007475 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007476 for (i = 1; i <= ga_scripts.ga_len; ++i)
7477 {
7478 ht = &SCRIPT_VARS(i);
7479 if (ht->ht_mask == HT_INIT_SIZE - 1)
7480 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007481 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007482 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007483 }
7484
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 while (ga_scripts.ga_len < id)
7486 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007487 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007488 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007489 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 }
7492 }
7493}
7494
7495/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007496 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7497 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 */
7499 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007500init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501{
Bram Moolenaar33570922005-01-25 22:26:29 +00007502 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007503 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007504 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007505 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007506 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007507 dict_var->di_tv.vval.v_dict = dict;
7508 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007509 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007510 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7511 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512}
7513
7514/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007515 * Unreference a dictionary initialized by init_var_dict().
7516 */
7517 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007518unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007519{
7520 /* Now the dict needs to be freed if no one else is using it, go back to
7521 * normal reference counting. */
7522 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7523 dict_unref(dict);
7524}
7525
7526/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007528 * Frees all allocated variables and the value they contain.
7529 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 */
7531 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007532vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007533{
7534 vars_clear_ext(ht, TRUE);
7535}
7536
7537/*
7538 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7539 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007540 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007541vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542{
Bram Moolenaara7043832005-01-21 11:56:39 +00007543 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007544 hashitem_T *hi;
7545 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546
Bram Moolenaar33570922005-01-25 22:26:29 +00007547 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007548 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007549 for (hi = ht->ht_array; todo > 0; ++hi)
7550 {
7551 if (!HASHITEM_EMPTY(hi))
7552 {
7553 --todo;
7554
Bram Moolenaar33570922005-01-25 22:26:29 +00007555 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007556 * ht_array might change then. hash_clear() takes care of it
7557 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007558 v = HI2DI(hi);
7559 if (free_val)
7560 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007561 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007562 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007563 }
7564 }
7565 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007566 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567}
7568
Bram Moolenaara7043832005-01-21 11:56:39 +00007569/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007570 * Delete a variable from hashtab "ht" at item "hi".
7571 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007572 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007574delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575{
Bram Moolenaar33570922005-01-25 22:26:29 +00007576 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007577
7578 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007579 clear_tv(&di->di_tv);
7580 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581}
7582
7583/*
7584 * List the value of one internal variable.
7585 */
7586 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007587list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007589 char_u *tofree;
7590 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007591 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007592
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007593 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007594 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007595 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007596 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597}
7598
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007600list_one_var_a(
7601 char_u *prefix,
7602 char_u *name,
7603 int type,
7604 char_u *string,
7605 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606{
Bram Moolenaar31859182007-08-14 20:41:13 +00007607 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7608 msg_start();
7609 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 if (name != NULL) /* "a:" vars don't have a name stored */
7611 msg_puts(name);
7612 msg_putchar(' ');
7613 msg_advance(22);
7614 if (type == VAR_NUMBER)
7615 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007616 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007617 msg_putchar('*');
7618 else if (type == VAR_LIST)
7619 {
7620 msg_putchar('[');
7621 if (*string == '[')
7622 ++string;
7623 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007624 else if (type == VAR_DICT)
7625 {
7626 msg_putchar('{');
7627 if (*string == '{')
7628 ++string;
7629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 else
7631 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007632
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007634
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007635 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007636 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007637 if (*first)
7638 {
7639 msg_clr_eos();
7640 *first = FALSE;
7641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642}
7643
7644/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007645 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 * If the variable already exists, the value is updated.
7647 * Otherwise the variable is created.
7648 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007649 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007650set_var(
7651 char_u *name,
7652 typval_T *tv,
7653 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654{
Bram Moolenaar33570922005-01-25 22:26:29 +00007655 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007657 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007659 ht = find_var_ht(name, &varname);
7660 if (ht == NULL || *varname == NUL)
7661 {
7662 EMSG2(_(e_illvar), name);
7663 return;
7664 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007665 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007666
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007667 /* Search in parent scope which is possible to reference from lambda */
7668 if (v == NULL)
7669 v = find_var_in_scoped_ht(name, varname ? &varname : NULL, TRUE);
7670
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007671 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7672 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007673 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007674
Bram Moolenaar33570922005-01-25 22:26:29 +00007675 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007677 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007678 if (var_check_ro(v->di_flags, name, FALSE)
7679 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007680 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007681
7682 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007683 * Handle setting internal v: variables separately where needed to
7684 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007685 */
7686 if (ht == &vimvarht)
7687 {
7688 if (v->di_tv.v_type == VAR_STRING)
7689 {
7690 vim_free(v->di_tv.vval.v_string);
7691 if (copy || tv->v_type != VAR_STRING)
7692 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
7693 else
7694 {
7695 /* Take over the string to avoid an extra alloc/free. */
7696 v->di_tv.vval.v_string = tv->vval.v_string;
7697 tv->vval.v_string = NULL;
7698 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007699 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007700 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007701 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007702 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007703 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007704 if (STRCMP(varname, "searchforward") == 0)
7705 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007706#ifdef FEAT_SEARCH_EXTRA
7707 else if (STRCMP(varname, "hlsearch") == 0)
7708 {
7709 no_hlsearch = !v->di_tv.vval.v_number;
7710 redraw_all_later(SOME_VALID);
7711 }
7712#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007713 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007714 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007715 else if (v->di_tv.v_type != tv->v_type)
7716 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +00007717 }
7718
7719 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 }
7721 else /* add a new variable */
7722 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007723 /* Can't add "v:" variable. */
7724 if (ht == &vimvarht)
7725 {
7726 EMSG2(_(e_illvar), name);
7727 return;
7728 }
7729
Bram Moolenaar92124a32005-06-17 22:03:40 +00007730 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007731 if (!valid_varname(varname))
7732 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00007733
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007734 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7735 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00007736 if (v == NULL)
7737 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007738 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00007739 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007741 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 return;
7743 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007744 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007746
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007747 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00007748 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007749 else
7750 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007751 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007752 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007753 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755}
7756
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007757/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007758 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00007759 * Also give an error message.
7760 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007761 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007762var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00007763{
7764 if (flags & DI_FLAGS_RO)
7765 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007766 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007767 return TRUE;
7768 }
7769 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
7770 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007771 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007772 return TRUE;
7773 }
7774 return FALSE;
7775}
7776
7777/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007778 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
7779 * Also give an error message.
7780 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007781 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007782var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007783{
7784 if (flags & DI_FLAGS_FIX)
7785 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007786 EMSG2(_("E795: Cannot delete variable %s"),
7787 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007788 return TRUE;
7789 }
7790 return FALSE;
7791}
7792
7793/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007794 * Check if a funcref is assigned to a valid variable name.
7795 * Return TRUE and give an error if not.
7796 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007797 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007798var_check_func_name(
7799 char_u *name, /* points to start of variable name */
7800 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007801{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02007802 /* Allow for w: b: s: and t:. */
7803 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007804 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
7805 ? name[2] : name[0]))
7806 {
7807 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
7808 name);
7809 return TRUE;
7810 }
7811 /* Don't allow hiding a function. When "v" is not NULL we might be
7812 * assigning another function to the same var, the type is checked
7813 * below. */
7814 if (new_var && function_exists(name))
7815 {
7816 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
7817 name);
7818 return TRUE;
7819 }
7820 return FALSE;
7821}
7822
7823/*
7824 * Check if a variable name is valid.
7825 * Return FALSE and give an error if not.
7826 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007827 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007828valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007829{
7830 char_u *p;
7831
7832 for (p = varname; *p != NUL; ++p)
7833 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
7834 && *p != AUTOLOAD_CHAR)
7835 {
7836 EMSG2(_(e_illvar), varname);
7837 return FALSE;
7838 }
7839 return TRUE;
7840}
7841
7842/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007843 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02007844 * Also give an error message, using "name" or _("name") when use_gettext is
7845 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007846 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007847 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007848tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007849{
7850 if (lock & VAR_LOCKED)
7851 {
7852 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007853 name == NULL ? (char_u *)_("Unknown")
7854 : use_gettext ? (char_u *)_(name)
7855 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007856 return TRUE;
7857 }
7858 if (lock & VAR_FIXED)
7859 {
7860 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007861 name == NULL ? (char_u *)_("Unknown")
7862 : use_gettext ? (char_u *)_(name)
7863 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007864 return TRUE;
7865 }
7866 return FALSE;
7867}
7868
7869/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007870 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007871 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007872 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007873 * It is OK for "from" and "to" to point to the same item. This is used to
7874 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007875 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007876 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007877copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007879 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007880 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007881 switch (from->v_type)
7882 {
7883 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007884 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007885 to->vval.v_number = from->vval.v_number;
7886 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007887 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007888#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007889 to->vval.v_float = from->vval.v_float;
7890 break;
7891#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007892 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007893#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007894 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007895 if (to->vval.v_job != NULL)
7896 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007897 break;
7898#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007899 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007900#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007901 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007902 if (to->vval.v_channel != NULL)
7903 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01007904 break;
7905#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007906 case VAR_STRING:
7907 case VAR_FUNC:
7908 if (from->vval.v_string == NULL)
7909 to->vval.v_string = NULL;
7910 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007911 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007912 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007913 if (from->v_type == VAR_FUNC)
7914 func_ref(to->vval.v_string);
7915 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007916 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007917 case VAR_PARTIAL:
7918 if (from->vval.v_partial == NULL)
7919 to->vval.v_partial = NULL;
7920 else
7921 {
7922 to->vval.v_partial = from->vval.v_partial;
7923 ++to->vval.v_partial->pt_refcount;
7924 }
7925 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007926 case VAR_LIST:
7927 if (from->vval.v_list == NULL)
7928 to->vval.v_list = NULL;
7929 else
7930 {
7931 to->vval.v_list = from->vval.v_list;
7932 ++to->vval.v_list->lv_refcount;
7933 }
7934 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007935 case VAR_DICT:
7936 if (from->vval.v_dict == NULL)
7937 to->vval.v_dict = NULL;
7938 else
7939 {
7940 to->vval.v_dict = from->vval.v_dict;
7941 ++to->vval.v_dict->dv_refcount;
7942 }
7943 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007944 case VAR_UNKNOWN:
7945 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007946 break;
7947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948}
7949
7950/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007951 * Make a copy of an item.
7952 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007953 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7954 * reference to an already copied list/dict can be used.
7955 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007956 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007957 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007958item_copy(
7959 typval_T *from,
7960 typval_T *to,
7961 int deep,
7962 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007963{
7964 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007965 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007966
Bram Moolenaar33570922005-01-25 22:26:29 +00007967 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007968 {
7969 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007970 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007971 }
7972 ++recurse;
7973
7974 switch (from->v_type)
7975 {
7976 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007977 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007978 case VAR_STRING:
7979 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007980 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007981 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007982 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007983 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007984 copy_tv(from, to);
7985 break;
7986 case VAR_LIST:
7987 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007988 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007989 if (from->vval.v_list == NULL)
7990 to->vval.v_list = NULL;
7991 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
7992 {
7993 /* use the copy made earlier */
7994 to->vval.v_list = from->vval.v_list->lv_copylist;
7995 ++to->vval.v_list->lv_refcount;
7996 }
7997 else
7998 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
7999 if (to->vval.v_list == NULL)
8000 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008001 break;
8002 case VAR_DICT:
8003 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008004 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008005 if (from->vval.v_dict == NULL)
8006 to->vval.v_dict = NULL;
8007 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8008 {
8009 /* use the copy made earlier */
8010 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8011 ++to->vval.v_dict->dv_refcount;
8012 }
8013 else
8014 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8015 if (to->vval.v_dict == NULL)
8016 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008017 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008018 case VAR_UNKNOWN:
8019 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008020 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008021 }
8022 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008023 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008024}
8025
8026/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008027 * This function is used by f_input() and f_inputdialog() functions. The third
8028 * argument to f_input() specifies the type of completion to use at the
8029 * prompt. The third argument to f_inputdialog() specifies the value to return
8030 * when the user cancels the prompt.
8031 */
8032 void
8033get_user_input(
8034 typval_T *argvars,
8035 typval_T *rettv,
8036 int inputdialog,
8037 int secret)
8038{
8039 char_u *prompt = get_tv_string_chk(&argvars[0]);
8040 char_u *p = NULL;
8041 int c;
8042 char_u buf[NUMBUFLEN];
8043 int cmd_silent_save = cmd_silent;
8044 char_u *defstr = (char_u *)"";
8045 int xp_type = EXPAND_NOTHING;
8046 char_u *xp_arg = NULL;
8047
8048 rettv->v_type = VAR_STRING;
8049 rettv->vval.v_string = NULL;
8050
8051#ifdef NO_CONSOLE_INPUT
8052 /* While starting up, there is no place to enter text. */
8053 if (no_console_input())
8054 return;
8055#endif
8056
8057 cmd_silent = FALSE; /* Want to see the prompt. */
8058 if (prompt != NULL)
8059 {
8060 /* Only the part of the message after the last NL is considered as
8061 * prompt for the command line */
8062 p = vim_strrchr(prompt, '\n');
8063 if (p == NULL)
8064 p = prompt;
8065 else
8066 {
8067 ++p;
8068 c = *p;
8069 *p = NUL;
8070 msg_start();
8071 msg_clr_eos();
8072 msg_puts_attr(prompt, echo_attr);
8073 msg_didout = FALSE;
8074 msg_starthere();
8075 *p = c;
8076 }
8077 cmdline_row = msg_row;
8078
8079 if (argvars[1].v_type != VAR_UNKNOWN)
8080 {
8081 defstr = get_tv_string_buf_chk(&argvars[1], buf);
8082 if (defstr != NULL)
8083 stuffReadbuffSpec(defstr);
8084
8085 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8086 {
8087 char_u *xp_name;
8088 int xp_namelen;
8089 long argt;
8090
8091 /* input() with a third argument: completion */
8092 rettv->vval.v_string = NULL;
8093
8094 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
8095 if (xp_name == NULL)
8096 return;
8097
8098 xp_namelen = (int)STRLEN(xp_name);
8099
8100 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8101 &xp_arg) == FAIL)
8102 return;
8103 }
8104 }
8105
8106 if (defstr != NULL)
8107 {
8108 int save_ex_normal_busy = ex_normal_busy;
8109 ex_normal_busy = 0;
8110 rettv->vval.v_string =
8111 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8112 xp_type, xp_arg);
8113 ex_normal_busy = save_ex_normal_busy;
8114 }
8115 if (inputdialog && rettv->vval.v_string == NULL
8116 && argvars[1].v_type != VAR_UNKNOWN
8117 && argvars[2].v_type != VAR_UNKNOWN)
8118 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
8119 &argvars[2], buf));
8120
8121 vim_free(xp_arg);
8122
8123 /* since the user typed this, no need to wait for return */
8124 need_wait_return = FALSE;
8125 msg_didout = FALSE;
8126 }
8127 cmd_silent = cmd_silent_save;
8128}
8129
8130/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 * ":echo expr1 ..." print each argument separated with a space, add a
8132 * newline at the end.
8133 * ":echon expr1 ..." print each argument plain.
8134 */
8135 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008136ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137{
8138 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008139 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008140 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 char_u *p;
8142 int needclr = TRUE;
8143 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008144 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145
8146 if (eap->skip)
8147 ++emsg_skip;
8148 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8149 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008150 /* If eval1() causes an error message the text from the command may
8151 * still need to be cleared. E.g., "echo 22,44". */
8152 need_clr_eos = needclr;
8153
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008155 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 {
8157 /*
8158 * Report the invalid expression unless the expression evaluation
8159 * has been cancelled due to an aborting error, an interrupt, or an
8160 * exception.
8161 */
8162 if (!aborting())
8163 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008164 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 break;
8166 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008167 need_clr_eos = FALSE;
8168
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 if (!eap->skip)
8170 {
8171 if (atstart)
8172 {
8173 atstart = FALSE;
8174 /* Call msg_start() after eval1(), evaluating the expression
8175 * may cause a message to appear. */
8176 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008177 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008178 /* Mark the saved text as finishing the line, so that what
8179 * follows is displayed on a new line when scrolling back
8180 * at the more prompt. */
8181 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 }
8185 else if (eap->cmdidx == CMD_echo)
8186 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008187 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008188 if (p != NULL)
8189 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008191 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008193 if (*p != TAB && needclr)
8194 {
8195 /* remove any text still there from the command */
8196 msg_clr_eos();
8197 needclr = FALSE;
8198 }
8199 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 }
8201 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008202 {
8203#ifdef FEAT_MBYTE
8204 if (has_mbyte)
8205 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008206 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008207
8208 (void)msg_outtrans_len_attr(p, i, echo_attr);
8209 p += i - 1;
8210 }
8211 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008213 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008216 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008218 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 arg = skipwhite(arg);
8220 }
8221 eap->nextcmd = check_nextcmd(arg);
8222
8223 if (eap->skip)
8224 --emsg_skip;
8225 else
8226 {
8227 /* remove text that may still be there from the command */
8228 if (needclr)
8229 msg_clr_eos();
8230 if (eap->cmdidx == CMD_echo)
8231 msg_end();
8232 }
8233}
8234
8235/*
8236 * ":echohl {name}".
8237 */
8238 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008239ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240{
8241 int id;
8242
8243 id = syn_name2id(eap->arg);
8244 if (id == 0)
8245 echo_attr = 0;
8246 else
8247 echo_attr = syn_id2attr(id);
8248}
8249
8250/*
8251 * ":execute expr1 ..." execute the result of an expression.
8252 * ":echomsg expr1 ..." Print a message
8253 * ":echoerr expr1 ..." Print an error
8254 * Each gets spaces around each argument and a newline at the end for
8255 * echo commands
8256 */
8257 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008258ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259{
8260 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008261 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 int ret = OK;
8263 char_u *p;
8264 garray_T ga;
8265 int len;
8266 int save_did_emsg;
8267
8268 ga_init2(&ga, 1, 80);
8269
8270 if (eap->skip)
8271 ++emsg_skip;
8272 while (*arg != NUL && *arg != '|' && *arg != '\n')
8273 {
8274 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008275 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 {
8277 /*
8278 * Report the invalid expression unless the expression evaluation
8279 * has been cancelled due to an aborting error, an interrupt, or an
8280 * exception.
8281 */
8282 if (!aborting())
8283 EMSG2(_(e_invexpr2), p);
8284 ret = FAIL;
8285 break;
8286 }
8287
8288 if (!eap->skip)
8289 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008290 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 len = (int)STRLEN(p);
8292 if (ga_grow(&ga, len + 2) == FAIL)
8293 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008294 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 ret = FAIL;
8296 break;
8297 }
8298 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 ga.ga_len += len;
8302 }
8303
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008304 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 arg = skipwhite(arg);
8306 }
8307
8308 if (ret != FAIL && ga.ga_data != NULL)
8309 {
8310 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008311 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008313 out_flush();
8314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 else if (eap->cmdidx == CMD_echoerr)
8316 {
8317 /* We don't want to abort following commands, restore did_emsg. */
8318 save_did_emsg = did_emsg;
8319 EMSG((char_u *)ga.ga_data);
8320 if (!force_abort)
8321 did_emsg = save_did_emsg;
8322 }
8323 else if (eap->cmdidx == CMD_execute)
8324 do_cmdline((char_u *)ga.ga_data,
8325 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8326 }
8327
8328 ga_clear(&ga);
8329
8330 if (eap->skip)
8331 --emsg_skip;
8332
8333 eap->nextcmd = check_nextcmd(arg);
8334}
8335
8336/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008337 * Find window specified by "vp" in tabpage "tp".
8338 */
8339 win_T *
8340find_win_by_nr(
8341 typval_T *vp,
8342 tabpage_T *tp UNUSED) /* NULL for current tab page */
8343{
8344#ifdef FEAT_WINDOWS
8345 win_T *wp;
8346#endif
8347 int nr;
8348
8349 nr = (int)get_tv_number_chk(vp, NULL);
8350
8351#ifdef FEAT_WINDOWS
8352 if (nr < 0)
8353 return NULL;
8354 if (nr == 0)
8355 return curwin;
8356
Bram Moolenaar29323592016-07-24 22:04:11 +02008357 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8358 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008359 if (nr >= LOWEST_WIN_ID)
8360 {
8361 if (wp->w_id == nr)
8362 return wp;
8363 }
8364 else if (--nr <= 0)
8365 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008366 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008367 if (nr >= LOWEST_WIN_ID)
8368 return NULL;
8369 return wp;
8370#else
8371 if (nr == 0 || nr == 1 || nr == curwin->w_id)
8372 return curwin;
8373 return NULL;
8374#endif
8375}
8376
8377/*
8378 * Find window specified by "wvp" in tabpage "tvp".
8379 */
8380 win_T *
8381find_tabwin(
8382 typval_T *wvp, /* VAR_UNKNOWN for current window */
8383 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
8384{
8385 win_T *wp = NULL;
8386 tabpage_T *tp = NULL;
8387 long n;
8388
8389 if (wvp->v_type != VAR_UNKNOWN)
8390 {
8391 if (tvp->v_type != VAR_UNKNOWN)
8392 {
8393 n = (long)get_tv_number(tvp);
8394 if (n >= 0)
8395 tp = find_tabpage(n);
8396 }
8397 else
8398 tp = curtab;
8399
8400 if (tp != NULL)
8401 wp = find_win_by_nr(wvp, tp);
8402 }
8403 else
8404 wp = curwin;
8405
8406 return wp;
8407}
8408
8409/*
8410 * getwinvar() and gettabwinvar()
8411 */
8412 void
8413getwinvar(
8414 typval_T *argvars,
8415 typval_T *rettv,
8416 int off) /* 1 for gettabwinvar() */
8417{
8418 win_T *win;
8419 char_u *varname;
8420 dictitem_T *v;
8421 tabpage_T *tp = NULL;
8422 int done = FALSE;
8423#ifdef FEAT_WINDOWS
8424 win_T *oldcurwin;
8425 tabpage_T *oldtabpage;
8426 int need_switch_win;
8427#endif
8428
8429#ifdef FEAT_WINDOWS
8430 if (off == 1)
8431 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8432 else
8433 tp = curtab;
8434#endif
8435 win = find_win_by_nr(&argvars[off], tp);
8436 varname = get_tv_string_chk(&argvars[off + 1]);
8437 ++emsg_off;
8438
8439 rettv->v_type = VAR_STRING;
8440 rettv->vval.v_string = NULL;
8441
8442 if (win != NULL && varname != NULL)
8443 {
8444#ifdef FEAT_WINDOWS
8445 /* Set curwin to be our win, temporarily. Also set the tabpage,
8446 * otherwise the window is not valid. Only do this when needed,
8447 * autocommands get blocked. */
8448 need_switch_win = !(tp == curtab && win == curwin);
8449 if (!need_switch_win
8450 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
8451#endif
8452 {
8453 if (*varname == '&') /* window-local-option */
8454 {
8455 if (get_option_tv(&varname, rettv, 1) == OK)
8456 done = TRUE;
8457 }
8458 else
8459 {
8460 /* Look up the variable. */
8461 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8462 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8463 varname, FALSE);
8464 if (v != NULL)
8465 {
8466 copy_tv(&v->di_tv, rettv);
8467 done = TRUE;
8468 }
8469 }
8470 }
8471
8472#ifdef FEAT_WINDOWS
8473 if (need_switch_win)
8474 /* restore previous notion of curwin */
8475 restore_win(oldcurwin, oldtabpage, TRUE);
8476#endif
8477 }
8478
8479 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8480 /* use the default return value */
8481 copy_tv(&argvars[off + 2], rettv);
8482
8483 --emsg_off;
8484}
8485
8486/*
8487 * "setwinvar()" and "settabwinvar()" functions
8488 */
8489 void
8490setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8491{
8492 win_T *win;
8493#ifdef FEAT_WINDOWS
8494 win_T *save_curwin;
8495 tabpage_T *save_curtab;
8496 int need_switch_win;
8497#endif
8498 char_u *varname, *winvarname;
8499 typval_T *varp;
8500 char_u nbuf[NUMBUFLEN];
8501 tabpage_T *tp = NULL;
8502
8503 if (check_restricted() || check_secure())
8504 return;
8505
8506#ifdef FEAT_WINDOWS
8507 if (off == 1)
8508 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8509 else
8510 tp = curtab;
8511#endif
8512 win = find_win_by_nr(&argvars[off], tp);
8513 varname = get_tv_string_chk(&argvars[off + 1]);
8514 varp = &argvars[off + 2];
8515
8516 if (win != NULL && varname != NULL && varp != NULL)
8517 {
8518#ifdef FEAT_WINDOWS
8519 need_switch_win = !(tp == curtab && win == curwin);
8520 if (!need_switch_win
8521 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
8522#endif
8523 {
8524 if (*varname == '&')
8525 {
8526 long numval;
8527 char_u *strval;
8528 int error = FALSE;
8529
8530 ++varname;
8531 numval = (long)get_tv_number_chk(varp, &error);
8532 strval = get_tv_string_buf_chk(varp, nbuf);
8533 if (!error && strval != NULL)
8534 set_option_value(varname, numval, strval, OPT_LOCAL);
8535 }
8536 else
8537 {
8538 winvarname = alloc((unsigned)STRLEN(varname) + 3);
8539 if (winvarname != NULL)
8540 {
8541 STRCPY(winvarname, "w:");
8542 STRCPY(winvarname + 2, varname);
8543 set_var(winvarname, varp, TRUE);
8544 vim_free(winvarname);
8545 }
8546 }
8547 }
8548#ifdef FEAT_WINDOWS
8549 if (need_switch_win)
8550 restore_win(save_curwin, save_curtab, TRUE);
8551#endif
8552 }
8553}
8554
8555/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8557 * "arg" points to the "&" or '+' when called, to "option" when returning.
8558 * Returns NULL when no option name found. Otherwise pointer to the char
8559 * after the option name.
8560 */
8561 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008562find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563{
8564 char_u *p = *arg;
8565
8566 ++p;
8567 if (*p == 'g' && p[1] == ':')
8568 {
8569 *opt_flags = OPT_GLOBAL;
8570 p += 2;
8571 }
8572 else if (*p == 'l' && p[1] == ':')
8573 {
8574 *opt_flags = OPT_LOCAL;
8575 p += 2;
8576 }
8577 else
8578 *opt_flags = 0;
8579
8580 if (!ASCII_ISALPHA(*p))
8581 return NULL;
8582 *arg = p;
8583
8584 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
8585 p += 4; /* termcap option */
8586 else
8587 while (ASCII_ISALPHA(*p))
8588 ++p;
8589 return p;
8590}
8591
8592/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008593 * Return the autoload script name for a function or variable name.
8594 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02008596 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008597autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02008598{
Bram Moolenaara1544c02013-05-30 12:35:52 +02008599 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008600 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02008601
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008602 /* Get the script file name: replace '#' with '/', append ".vim". */
8603 scriptname = alloc((unsigned)(STRLEN(name) + 14));
8604 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008605 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008606 STRCPY(scriptname, "autoload/");
8607 STRCAT(scriptname, name);
8608 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
8609 STRCAT(scriptname, ".vim");
8610 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
8611 *p = '/';
8612 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008613}
8614
8615/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008616 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008617 * Return TRUE if a package was loaded.
8618 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008619 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008620script_autoload(
8621 char_u *name,
8622 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008623{
8624 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008625 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008626 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008627 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008628
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008629 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008630 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008631 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008632 return FALSE;
8633
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008634 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008635
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008636 /* Find the name in the list of previously loaded package names. Skip
8637 * "autoload/", it's always the same. */
8638 for (i = 0; i < ga_loaded.ga_len; ++i)
8639 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
8640 break;
8641 if (!reload && i < ga_loaded.ga_len)
8642 ret = FALSE; /* was loaded already */
8643 else
8644 {
8645 /* Remember the name if it wasn't loaded already. */
8646 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
8647 {
8648 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
8649 tofree = NULL;
8650 }
8651
8652 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008653 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008654 ret = TRUE;
8655 }
8656
8657 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008658 return ret;
8659}
8660
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
8662typedef enum
8663{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008664 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
8665 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
8666 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667} var_flavour_T;
8668
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008669static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670
8671 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01008672var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673{
8674 char_u *p = varname;
8675
8676 if (ASCII_ISUPPER(*p))
8677 {
8678 while (*(++p))
8679 if (ASCII_ISLOWER(*p))
8680 return VAR_FLAVOUR_SESSION;
8681 return VAR_FLAVOUR_VIMINFO;
8682 }
8683 else
8684 return VAR_FLAVOUR_DEFAULT;
8685}
8686#endif
8687
8688#if defined(FEAT_VIMINFO) || defined(PROTO)
8689/*
8690 * Restore global vars that start with a capital from the viminfo file
8691 */
8692 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008693read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694{
8695 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008696 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00008697 typval_T tv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008698 void *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699
8700 if (!writing && (find_viminfo_parameter('!') != NULL))
8701 {
8702 tab = vim_strchr(virp->vir_line + 1, '\t');
8703 if (tab != NULL)
8704 {
8705 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008706 switch (*tab)
8707 {
8708 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008709#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008710 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008711#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008712 case 'D': type = VAR_DICT; break;
8713 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008714 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716
8717 tab = vim_strchr(tab, '\t');
8718 if (tab != NULL)
8719 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008720 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008721 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008722 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008724#ifdef FEAT_FLOAT
8725 else if (type == VAR_FLOAT)
8726 (void)string2float(tab + 1, &tv.vval.v_float);
8727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008729 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008730 if (type == VAR_DICT || type == VAR_LIST)
8731 {
8732 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
8733
8734 if (etv == NULL)
8735 /* Failed to parse back the dict or list, use it as a
8736 * string. */
8737 tv.v_type = VAR_STRING;
8738 else
8739 {
8740 vim_free(tv.vval.v_string);
8741 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01008742 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008743 }
8744 }
8745
Bram Moolenaarb20e3342016-01-18 23:29:01 +01008746 /* when in a function use global variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008747 save_funccal = clear_current_funccal();
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008748 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008749 restore_current_funccal(save_funccal);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008750
8751 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008752 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008753 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
8754 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 }
8756 }
8757 }
8758
8759 return viminfo_readline(virp);
8760}
8761
8762/*
8763 * Write global vars that start with a capital to the viminfo file
8764 */
8765 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008766write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767{
Bram Moolenaar33570922005-01-25 22:26:29 +00008768 hashitem_T *hi;
8769 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008770 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01008771 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008772 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008773 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008774 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775
8776 if (find_viminfo_parameter('!') == NULL)
8777 return;
8778
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02008779 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00008780
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008781 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008782 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008784 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008786 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008787 this_var = HI2DI(hi);
8788 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008789 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008790 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00008791 {
8792 case VAR_STRING: s = "STR"; break;
8793 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008794 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008795 case VAR_DICT: s = "DIC"; break;
8796 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008797 case VAR_SPECIAL: s = "XPL"; break;
8798
8799 case VAR_UNKNOWN:
8800 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008801 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008802 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008803 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008804 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00008805 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008806 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008807 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008808 if (p != NULL)
8809 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00008810 vim_free(tofree);
8811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 }
8813 }
8814}
8815#endif
8816
8817#if defined(FEAT_SESSION) || defined(PROTO)
8818 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008819store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820{
Bram Moolenaar33570922005-01-25 22:26:29 +00008821 hashitem_T *hi;
8822 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008823 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 char_u *p, *t;
8825
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008826 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008827 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008829 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008831 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008832 this_var = HI2DI(hi);
8833 if ((this_var->di_tv.v_type == VAR_NUMBER
8834 || this_var->di_tv.v_type == VAR_STRING)
8835 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008836 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008837 /* Escape special characters with a backslash. Turn a LF and
8838 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008839 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00008840 (char_u *)"\\\"\n\r");
8841 if (p == NULL) /* out of memory */
8842 break;
8843 for (t = p; *t != NUL; ++t)
8844 if (*t == '\n')
8845 *t = 'n';
8846 else if (*t == '\r')
8847 *t = 'r';
8848 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00008849 this_var->di_key,
8850 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8851 : ' ',
8852 p,
8853 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8854 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00008855 || put_eol(fd) == FAIL)
8856 {
8857 vim_free(p);
8858 return FAIL;
8859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 vim_free(p);
8861 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008862#ifdef FEAT_FLOAT
8863 else if (this_var->di_tv.v_type == VAR_FLOAT
8864 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
8865 {
8866 float_T f = this_var->di_tv.vval.v_float;
8867 int sign = ' ';
8868
8869 if (f < 0)
8870 {
8871 f = -f;
8872 sign = '-';
8873 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01008874 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008875 this_var->di_key, sign, f) < 0)
8876 || put_eol(fd) == FAIL)
8877 return FAIL;
8878 }
8879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 }
8881 }
8882 return OK;
8883}
8884#endif
8885
Bram Moolenaar661b1822005-07-28 22:36:45 +00008886/*
8887 * Display script name where an item was last set.
8888 * Should only be invoked when 'verbose' is non-zero.
8889 */
8890 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008891last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +00008892{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008893 char_u *p;
8894
Bram Moolenaar661b1822005-07-28 22:36:45 +00008895 if (scriptID != 0)
8896 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008897 p = home_replace_save(NULL, get_scriptname(scriptID));
8898 if (p != NULL)
8899 {
8900 verbose_enter();
8901 MSG_PUTS(_("\n\tLast set from "));
8902 MSG_PUTS(p);
8903 vim_free(p);
8904 verbose_leave();
8905 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00008906 }
8907}
8908
Bram Moolenaard812df62008-11-09 12:46:09 +00008909/*
8910 * List v:oldfiles in a nice way.
8911 */
Bram Moolenaard812df62008-11-09 12:46:09 +00008912 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008913ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +00008914{
8915 list_T *l = vimvars[VV_OLDFILES].vv_list;
8916 listitem_T *li;
8917 int nr = 0;
8918
8919 if (l == NULL)
8920 msg((char_u *)_("No old files"));
8921 else
8922 {
8923 msg_start();
8924 msg_scroll = TRUE;
8925 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
8926 {
8927 msg_outnum((long)++nr);
8928 MSG_PUTS(": ");
8929 msg_outtrans(get_tv_string(&li->li_tv));
8930 msg_putchar('\n');
8931 out_flush(); /* output one line at a time */
8932 ui_breakcheck();
8933 }
8934 /* Assume "got_int" was set to truncate the listing. */
8935 got_int = FALSE;
8936
8937#ifdef FEAT_BROWSE_CMD
8938 if (cmdmod.browse)
8939 {
8940 quit_more = FALSE;
8941 nr = prompt_for_number(FALSE);
8942 msg_starthere();
8943 if (nr > 0)
8944 {
8945 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
8946 (long)nr);
8947
8948 if (p != NULL)
8949 {
8950 p = expand_env_save(p);
8951 eap->arg = p;
8952 eap->cmdidx = CMD_edit;
8953 cmdmod.browse = FALSE;
8954 do_exedit(eap, NULL);
8955 vim_free(p);
8956 }
8957 }
8958 }
8959#endif
8960 }
8961}
8962
Bram Moolenaar53744302015-07-17 17:38:22 +02008963/* reset v:option_new, v:option_old and v:option_type */
8964 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008965reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02008966{
8967 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
8968 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
8969 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
8970}
8971
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008972/*
8973 * Prepare "gap" for an assert error and add the sourcing position.
8974 */
8975 void
8976prepare_assert_error(garray_T *gap)
8977{
8978 char buf[NUMBUFLEN];
8979
8980 ga_init2(gap, 1, 100);
8981 if (sourcing_name != NULL)
8982 {
8983 ga_concat(gap, sourcing_name);
8984 if (sourcing_lnum > 0)
8985 ga_concat(gap, (char_u *)" ");
8986 }
8987 if (sourcing_lnum > 0)
8988 {
8989 sprintf(buf, "line %ld", (long)sourcing_lnum);
8990 ga_concat(gap, (char_u *)buf);
8991 }
8992 if (sourcing_name != NULL || sourcing_lnum > 0)
8993 ga_concat(gap, (char_u *)": ");
8994}
8995
8996/*
8997 * Add an assert error to v:errors.
8998 */
8999 void
9000assert_error(garray_T *gap)
9001{
9002 struct vimvar *vp = &vimvars[VV_ERRORS];
9003
9004 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9005 /* Make sure v:errors is a list. */
9006 set_vim_var_list(VV_ERRORS, list_alloc());
9007 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9008}
9009
9010 void
9011assert_equal_common(typval_T *argvars, assert_type_T atype)
9012{
9013 garray_T ga;
9014
9015 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9016 != (atype == ASSERT_EQUAL))
9017 {
9018 prepare_assert_error(&ga);
9019 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9020 atype);
9021 assert_error(&ga);
9022 ga_clear(&ga);
9023 }
9024}
9025
9026 void
9027assert_match_common(typval_T *argvars, assert_type_T atype)
9028{
9029 garray_T ga;
9030 char_u buf1[NUMBUFLEN];
9031 char_u buf2[NUMBUFLEN];
9032 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9033 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9034
9035 if (pat == NULL || text == NULL)
9036 EMSG(_(e_invarg));
9037 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
9038 {
9039 prepare_assert_error(&ga);
9040 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9041 atype);
9042 assert_error(&ga);
9043 ga_clear(&ga);
9044 }
9045}
9046
Bram Moolenaar61c04492016-07-23 15:35:35 +02009047 void
9048assert_inrange(typval_T *argvars)
9049{
9050 garray_T ga;
9051 int error = FALSE;
9052 varnumber_T lower = get_tv_number_chk(&argvars[0], &error);
9053 varnumber_T upper = get_tv_number_chk(&argvars[1], &error);
9054 varnumber_T actual = get_tv_number_chk(&argvars[2], &error);
9055 char_u *tofree;
9056 char msg[200];
9057 char_u numbuf[NUMBUFLEN];
9058
9059 if (error)
9060 return;
9061 if (actual < lower || actual > upper)
9062 {
9063 prepare_assert_error(&ga);
9064 if (argvars[3].v_type != VAR_UNKNOWN)
9065 {
9066 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9067 vim_free(tofree);
9068 }
9069 else
9070 {
9071 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
9072 (long)lower, (long)upper, (long)actual);
9073 ga_concat(&ga, (char_u *)msg);
9074 }
9075 assert_error(&ga);
9076 ga_clear(&ga);
9077 }
9078}
9079
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009080/*
9081 * Common for assert_true() and assert_false().
9082 */
9083 void
9084assert_bool(typval_T *argvars, int isTrue)
9085{
9086 int error = FALSE;
9087 garray_T ga;
9088
9089 if (argvars[0].v_type == VAR_SPECIAL
9090 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
9091 return;
9092 if (argvars[0].v_type != VAR_NUMBER
9093 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9094 || error)
9095 {
9096 prepare_assert_error(&ga);
9097 fill_assert_error(&ga, &argvars[1],
9098 (char_u *)(isTrue ? "True" : "False"),
9099 NULL, &argvars[0], ASSERT_OTHER);
9100 assert_error(&ga);
9101 ga_clear(&ga);
9102 }
9103}
9104
9105 void
9106assert_exception(typval_T *argvars)
9107{
9108 garray_T ga;
9109 char_u *error = get_tv_string_chk(&argvars[0]);
9110
9111 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9112 {
9113 prepare_assert_error(&ga);
9114 ga_concat(&ga, (char_u *)"v:exception is not set");
9115 assert_error(&ga);
9116 ga_clear(&ga);
9117 }
9118 else if (error != NULL
9119 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9120 {
9121 prepare_assert_error(&ga);
9122 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9123 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9124 assert_error(&ga);
9125 ga_clear(&ga);
9126 }
9127}
9128
9129 void
9130assert_fails(typval_T *argvars)
9131{
9132 char_u *cmd = get_tv_string_chk(&argvars[0]);
9133 garray_T ga;
9134
9135 called_emsg = FALSE;
9136 suppress_errthrow = TRUE;
9137 emsg_silent = TRUE;
9138 do_cmdline_cmd(cmd);
9139 if (!called_emsg)
9140 {
9141 prepare_assert_error(&ga);
9142 ga_concat(&ga, (char_u *)"command did not fail: ");
9143 ga_concat(&ga, cmd);
9144 assert_error(&ga);
9145 ga_clear(&ga);
9146 }
9147 else if (argvars[1].v_type != VAR_UNKNOWN)
9148 {
9149 char_u buf[NUMBUFLEN];
9150 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9151
9152 if (error == NULL
9153 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9154 {
9155 prepare_assert_error(&ga);
9156 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9157 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
9158 assert_error(&ga);
9159 ga_clear(&ga);
9160 }
9161 }
9162
9163 called_emsg = FALSE;
9164 suppress_errthrow = FALSE;
9165 emsg_silent = FALSE;
9166 emsg_on_display = FALSE;
9167 set_vim_var_string(VV_ERRMSG, NULL, 0);
9168}
9169
9170/*
9171 * Append "str" to "gap", escaping unprintable characters.
9172 * Changes NL to \n, CR to \r, etc.
9173 */
9174 static void
9175ga_concat_esc(garray_T *gap, char_u *str)
9176{
9177 char_u *p;
9178 char_u buf[NUMBUFLEN];
9179
9180 if (str == NULL)
9181 {
9182 ga_concat(gap, (char_u *)"NULL");
9183 return;
9184 }
9185
9186 for (p = str; *p != NUL; ++p)
9187 switch (*p)
9188 {
9189 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9190 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9191 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9192 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9193 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9194 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9195 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9196 default:
9197 if (*p < ' ')
9198 {
9199 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9200 ga_concat(gap, buf);
9201 }
9202 else
9203 ga_append(gap, *p);
9204 break;
9205 }
9206}
9207
9208/*
9209 * Fill "gap" with information about an assert error.
9210 */
9211 void
9212fill_assert_error(
9213 garray_T *gap,
9214 typval_T *opt_msg_tv,
9215 char_u *exp_str,
9216 typval_T *exp_tv,
9217 typval_T *got_tv,
9218 assert_type_T atype)
9219{
9220 char_u numbuf[NUMBUFLEN];
9221 char_u *tofree;
9222
9223 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9224 {
9225 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9226 vim_free(tofree);
9227 }
9228 else
9229 {
9230 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9231 ga_concat(gap, (char_u *)"Pattern ");
9232 else
9233 ga_concat(gap, (char_u *)"Expected ");
9234 if (exp_str == NULL)
9235 {
9236 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9237 vim_free(tofree);
9238 }
9239 else
9240 ga_concat_esc(gap, exp_str);
9241 if (atype == ASSERT_MATCH)
9242 ga_concat(gap, (char_u *)" does not match ");
9243 else if (atype == ASSERT_NOTMATCH)
9244 ga_concat(gap, (char_u *)" does match ");
9245 else if (atype == ASSERT_NOTEQUAL)
9246 ga_concat(gap, (char_u *)" differs from ");
9247 else
9248 ga_concat(gap, (char_u *)" but got ");
9249 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
9250 vim_free(tofree);
9251 }
9252}
9253
Bram Moolenaar53744302015-07-17 17:38:22 +02009254
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255#endif /* FEAT_EVAL */
9256
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009258#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259
9260#ifdef WIN3264
9261/*
9262 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9263 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009264static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
9265static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
9266static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267
9268/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009269 * Get the short path (8.3) for the filename in "fnamep".
9270 * Only works for a valid file name.
9271 * When the path gets longer "fnamep" is changed and the allocated buffer
9272 * is put in "bufp".
9273 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9274 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275 */
9276 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009277get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009279 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280 char_u *newbuf;
9281
9282 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009283 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284 if (l > len - 1)
9285 {
9286 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009287 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288 newbuf = vim_strnsave(*fnamep, l);
9289 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009290 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291
9292 vim_free(*bufp);
9293 *fnamep = *bufp = newbuf;
9294
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009295 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009296 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297 }
9298
9299 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009300 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301}
9302
9303/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009304 * Get the short path (8.3) for the filename in "fname". The converted
9305 * path is returned in "bufp".
9306 *
9307 * Some of the directories specified in "fname" may not exist. This function
9308 * will shorten the existing directories at the beginning of the path and then
9309 * append the remaining non-existing path.
9310 *
9311 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009312 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009313 * bufp - Pointer to an allocated buffer for the filename.
9314 * fnamelen - Length of the filename pointed to by fname
9315 *
9316 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317 */
9318 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009319shortpath_for_invalid_fname(
9320 char_u **fname,
9321 char_u **bufp,
9322 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009324 char_u *short_fname, *save_fname, *pbuf_unused;
9325 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009327 int old_len, len;
9328 int new_len, sfx_len;
9329 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330
9331 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009332 old_len = *fnamelen;
9333 save_fname = vim_strnsave(*fname, old_len);
9334 pbuf_unused = NULL;
9335 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009337 endp = save_fname + old_len - 1; /* Find the end of the copy */
9338 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009340 /*
9341 * Try shortening the supplied path till it succeeds by removing one
9342 * directory at a time from the tail of the path.
9343 */
9344 len = 0;
9345 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009347 /* go back one path-separator */
9348 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9349 --endp;
9350 if (endp <= save_fname)
9351 break; /* processed the complete path */
9352
9353 /*
9354 * Replace the path separator with a NUL and try to shorten the
9355 * resulting path.
9356 */
9357 ch = *endp;
9358 *endp = 0;
9359 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009360 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009361 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9362 {
9363 retval = FAIL;
9364 goto theend;
9365 }
9366 *endp = ch; /* preserve the string */
9367
9368 if (len > 0)
9369 break; /* successfully shortened the path */
9370
9371 /* failed to shorten the path. Skip the path separator */
9372 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373 }
9374
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009375 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009377 /*
9378 * Succeeded in shortening the path. Now concatenate the shortened
9379 * path with the remaining path at the tail.
9380 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009382 /* Compute the length of the new path. */
9383 sfx_len = (int)(save_endp - endp) + 1;
9384 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009386 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009388 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009390 /* There is not enough space in the currently allocated string,
9391 * copy it to a buffer big enough. */
9392 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009394 {
9395 retval = FAIL;
9396 goto theend;
9397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 }
9399 else
9400 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009401 /* Transfer short_fname to the main buffer (it's big enough),
9402 * unless get_short_pathname() did its work in-place. */
9403 *fname = *bufp = save_fname;
9404 if (short_fname != save_fname)
9405 vim_strncpy(save_fname, short_fname, len);
9406 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009408
9409 /* concat the not-shortened part of the path */
9410 vim_strncpy(*fname + len, endp, sfx_len);
9411 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009413
9414theend:
9415 vim_free(pbuf_unused);
9416 vim_free(save_fname);
9417
9418 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419}
9420
9421/*
9422 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009423 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424 */
9425 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009426shortpath_for_partial(
9427 char_u **fnamep,
9428 char_u **bufp,
9429 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430{
9431 int sepcount, len, tflen;
9432 char_u *p;
9433 char_u *pbuf, *tfname;
9434 int hasTilde;
9435
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009436 /* Count up the path separators from the RHS.. so we know which part
9437 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009439 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440 if (vim_ispathsep(*p))
9441 ++sepcount;
9442
9443 /* Need full path first (use expand_env() to remove a "~/") */
9444 hasTilde = (**fnamep == '~');
9445 if (hasTilde)
9446 pbuf = tfname = expand_env_save(*fnamep);
9447 else
9448 pbuf = tfname = FullName_save(*fnamep, FALSE);
9449
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009450 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009452 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
9453 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454
9455 if (len == 0)
9456 {
9457 /* Don't have a valid filename, so shorten the rest of the
9458 * path if we can. This CAN give us invalid 8.3 filenames, but
9459 * there's not a lot of point in guessing what it might be.
9460 */
9461 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009462 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
9463 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464 }
9465
9466 /* Count the paths backward to find the beginning of the desired string. */
9467 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009468 {
9469#ifdef FEAT_MBYTE
9470 if (has_mbyte)
9471 p -= mb_head_off(tfname, p);
9472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473 if (vim_ispathsep(*p))
9474 {
9475 if (sepcount == 0 || (hasTilde && sepcount == 1))
9476 break;
9477 else
9478 sepcount --;
9479 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 if (hasTilde)
9482 {
9483 --p;
9484 if (p >= tfname)
9485 *p = '~';
9486 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009487 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 }
9489 else
9490 ++p;
9491
9492 /* Copy in the string - p indexes into tfname - allocated at pbuf */
9493 vim_free(*bufp);
9494 *fnamelen = (int)STRLEN(p);
9495 *bufp = pbuf;
9496 *fnamep = p;
9497
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009498 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499}
9500#endif /* WIN3264 */
9501
9502/*
9503 * Adjust a filename, according to a string of modifiers.
9504 * *fnamep must be NUL terminated when called. When returning, the length is
9505 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009506 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507 * When there is an error, *fnamep is set to NULL.
9508 */
9509 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009510modify_fname(
9511 char_u *src, /* string with modifiers */
9512 int *usedlen, /* characters after src that are used */
9513 char_u **fnamep, /* file name so far */
9514 char_u **bufp, /* buffer for allocated file name or NULL */
9515 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516{
9517 int valid = 0;
9518 char_u *tail;
9519 char_u *s, *p, *pbuf;
9520 char_u dirname[MAXPATHL];
9521 int c;
9522 int has_fullname = 0;
9523#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009524 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525 int has_shortname = 0;
9526#endif
9527
9528repeat:
9529 /* ":p" - full path/file_name */
9530 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
9531 {
9532 has_fullname = 1;
9533
9534 valid |= VALID_PATH;
9535 *usedlen += 2;
9536
9537 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
9538 if ((*fnamep)[0] == '~'
9539#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
9540 && ((*fnamep)[1] == '/'
9541# ifdef BACKSLASH_IN_FILENAME
9542 || (*fnamep)[1] == '\\'
9543# endif
9544 || (*fnamep)[1] == NUL)
9545
9546#endif
9547 )
9548 {
9549 *fnamep = expand_env_save(*fnamep);
9550 vim_free(*bufp); /* free any allocated file name */
9551 *bufp = *fnamep;
9552 if (*fnamep == NULL)
9553 return -1;
9554 }
9555
9556 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009557 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 {
9559 if (vim_ispathsep(*p)
9560 && p[1] == '.'
9561 && (p[2] == NUL
9562 || vim_ispathsep(p[2])
9563 || (p[2] == '.'
9564 && (p[3] == NUL || vim_ispathsep(p[3])))))
9565 break;
9566 }
9567
9568 /* FullName_save() is slow, don't use it when not needed. */
9569 if (*p != NUL || !vim_isAbsName(*fnamep))
9570 {
9571 *fnamep = FullName_save(*fnamep, *p != NUL);
9572 vim_free(*bufp); /* free any allocated file name */
9573 *bufp = *fnamep;
9574 if (*fnamep == NULL)
9575 return -1;
9576 }
9577
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009578#ifdef WIN3264
9579# if _WIN32_WINNT >= 0x0500
9580 if (vim_strchr(*fnamep, '~') != NULL)
9581 {
9582 /* Expand 8.3 filename to full path. Needed to make sure the same
9583 * file does not have two different names.
9584 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
9585 p = alloc(_MAX_PATH + 1);
9586 if (p != NULL)
9587 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009588 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009589 {
9590 vim_free(*bufp);
9591 *bufp = *fnamep = p;
9592 }
9593 else
9594 vim_free(p);
9595 }
9596 }
9597# endif
9598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 /* Append a path separator to a directory. */
9600 if (mch_isdir(*fnamep))
9601 {
9602 /* Make room for one or two extra characters. */
9603 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
9604 vim_free(*bufp); /* free any allocated file name */
9605 *bufp = *fnamep;
9606 if (*fnamep == NULL)
9607 return -1;
9608 add_pathsep(*fnamep);
9609 }
9610 }
9611
9612 /* ":." - path relative to the current directory */
9613 /* ":~" - path relative to the home directory */
9614 /* ":8" - shortname path - postponed till after */
9615 while (src[*usedlen] == ':'
9616 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
9617 {
9618 *usedlen += 2;
9619 if (c == '8')
9620 {
9621#ifdef WIN3264
9622 has_shortname = 1; /* Postpone this. */
9623#endif
9624 continue;
9625 }
9626 pbuf = NULL;
9627 /* Need full path first (use expand_env() to remove a "~/") */
9628 if (!has_fullname)
9629 {
9630 if (c == '.' && **fnamep == '~')
9631 p = pbuf = expand_env_save(*fnamep);
9632 else
9633 p = pbuf = FullName_save(*fnamep, FALSE);
9634 }
9635 else
9636 p = *fnamep;
9637
9638 has_fullname = 0;
9639
9640 if (p != NULL)
9641 {
9642 if (c == '.')
9643 {
9644 mch_dirname(dirname, MAXPATHL);
9645 s = shorten_fname(p, dirname);
9646 if (s != NULL)
9647 {
9648 *fnamep = s;
9649 if (pbuf != NULL)
9650 {
9651 vim_free(*bufp); /* free any allocated file name */
9652 *bufp = pbuf;
9653 pbuf = NULL;
9654 }
9655 }
9656 }
9657 else
9658 {
9659 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
9660 /* Only replace it when it starts with '~' */
9661 if (*dirname == '~')
9662 {
9663 s = vim_strsave(dirname);
9664 if (s != NULL)
9665 {
9666 *fnamep = s;
9667 vim_free(*bufp);
9668 *bufp = s;
9669 }
9670 }
9671 }
9672 vim_free(pbuf);
9673 }
9674 }
9675
9676 tail = gettail(*fnamep);
9677 *fnamelen = (int)STRLEN(*fnamep);
9678
9679 /* ":h" - head, remove "/file_name", can be repeated */
9680 /* Don't remove the first "/" or "c:\" */
9681 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
9682 {
9683 valid |= VALID_HEAD;
9684 *usedlen += 2;
9685 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009686 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009687 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688 *fnamelen = (int)(tail - *fnamep);
9689#ifdef VMS
9690 if (*fnamelen > 0)
9691 *fnamelen += 1; /* the path separator is part of the path */
9692#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009693 if (*fnamelen == 0)
9694 {
9695 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
9696 p = vim_strsave((char_u *)".");
9697 if (p == NULL)
9698 return -1;
9699 vim_free(*bufp);
9700 *bufp = *fnamep = tail = p;
9701 *fnamelen = 1;
9702 }
9703 else
9704 {
9705 while (tail > s && !after_pathsep(s, tail))
9706 mb_ptr_back(*fnamep, tail);
9707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 }
9709
9710 /* ":8" - shortname */
9711 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
9712 {
9713 *usedlen += 2;
9714#ifdef WIN3264
9715 has_shortname = 1;
9716#endif
9717 }
9718
9719#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009720 /*
9721 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 */
9723 if (has_shortname)
9724 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009725 /* Copy the string if it is shortened by :h and when it wasn't copied
9726 * yet, because we are going to change it in place. Avoids changing
9727 * the buffer name for "%:8". */
9728 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729 {
9730 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +02009731 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 return -1;
9733 vim_free(*bufp);
9734 *bufp = *fnamep = p;
9735 }
9736
9737 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +02009738 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 if (!has_fullname && !vim_isAbsName(*fnamep))
9740 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009741 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742 return -1;
9743 }
9744 else
9745 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009746 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747
Bram Moolenaardc935552011-08-17 15:23:23 +02009748 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009750 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 return -1;
9752
9753 if (l == 0)
9754 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009755 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009757 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 return -1;
9759 }
9760 *fnamelen = l;
9761 }
9762 }
9763#endif /* WIN3264 */
9764
9765 /* ":t" - tail, just the basename */
9766 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
9767 {
9768 *usedlen += 2;
9769 *fnamelen -= (int)(tail - *fnamep);
9770 *fnamep = tail;
9771 }
9772
9773 /* ":e" - extension, can be repeated */
9774 /* ":r" - root, without extension, can be repeated */
9775 while (src[*usedlen] == ':'
9776 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
9777 {
9778 /* find a '.' in the tail:
9779 * - for second :e: before the current fname
9780 * - otherwise: The last '.'
9781 */
9782 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
9783 s = *fnamep - 2;
9784 else
9785 s = *fnamep + *fnamelen - 1;
9786 for ( ; s > tail; --s)
9787 if (s[0] == '.')
9788 break;
9789 if (src[*usedlen + 1] == 'e') /* :e */
9790 {
9791 if (s > tail)
9792 {
9793 *fnamelen += (int)(*fnamep - (s + 1));
9794 *fnamep = s + 1;
9795#ifdef VMS
9796 /* cut version from the extension */
9797 s = *fnamep + *fnamelen - 1;
9798 for ( ; s > *fnamep; --s)
9799 if (s[0] == ';')
9800 break;
9801 if (s > *fnamep)
9802 *fnamelen = s - *fnamep;
9803#endif
9804 }
9805 else if (*fnamep <= tail)
9806 *fnamelen = 0;
9807 }
9808 else /* :r */
9809 {
9810 if (s > tail) /* remove one extension */
9811 *fnamelen = (int)(s - *fnamep);
9812 }
9813 *usedlen += 2;
9814 }
9815
9816 /* ":s?pat?foo?" - substitute */
9817 /* ":gs?pat?foo?" - global substitute */
9818 if (src[*usedlen] == ':'
9819 && (src[*usedlen + 1] == 's'
9820 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
9821 {
9822 char_u *str;
9823 char_u *pat;
9824 char_u *sub;
9825 int sep;
9826 char_u *flags;
9827 int didit = FALSE;
9828
9829 flags = (char_u *)"";
9830 s = src + *usedlen + 2;
9831 if (src[*usedlen + 1] == 'g')
9832 {
9833 flags = (char_u *)"g";
9834 ++s;
9835 }
9836
9837 sep = *s++;
9838 if (sep)
9839 {
9840 /* find end of pattern */
9841 p = vim_strchr(s, sep);
9842 if (p != NULL)
9843 {
9844 pat = vim_strnsave(s, (int)(p - s));
9845 if (pat != NULL)
9846 {
9847 s = p + 1;
9848 /* find end of substitution */
9849 p = vim_strchr(s, sep);
9850 if (p != NULL)
9851 {
9852 sub = vim_strnsave(s, (int)(p - s));
9853 str = vim_strnsave(*fnamep, *fnamelen);
9854 if (sub != NULL && str != NULL)
9855 {
9856 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009857 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 if (s != NULL)
9859 {
9860 *fnamep = s;
9861 *fnamelen = (int)STRLEN(s);
9862 vim_free(*bufp);
9863 *bufp = s;
9864 didit = TRUE;
9865 }
9866 }
9867 vim_free(sub);
9868 vim_free(str);
9869 }
9870 vim_free(pat);
9871 }
9872 }
9873 /* after using ":s", repeat all the modifiers */
9874 if (didit)
9875 goto repeat;
9876 }
9877 }
9878
Bram Moolenaar26df0922014-02-23 23:39:13 +01009879 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
9880 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +01009881 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +01009882 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +01009883 if (c != NUL)
9884 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +01009885 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +01009886 if (c != NUL)
9887 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +01009888 if (p == NULL)
9889 return -1;
9890 vim_free(*bufp);
9891 *bufp = *fnamep = p;
9892 *fnamelen = (int)STRLEN(p);
9893 *usedlen += 2;
9894 }
9895
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896 return valid;
9897}
9898
9899/*
9900 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009901 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902 * "flags" can be "g" to do a global substitute.
9903 * Returns an allocated string, NULL for error.
9904 */
9905 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009906do_string_sub(
9907 char_u *str,
9908 char_u *pat,
9909 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009910 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01009911 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912{
9913 int sublen;
9914 regmatch_T regmatch;
9915 int i;
9916 int do_all;
9917 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01009918 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919 garray_T ga;
9920 char_u *ret;
9921 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01009922 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923
9924 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
9925 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00009926 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927
9928 ga_init2(&ga, 1, 200);
9929
9930 do_all = (flags[0] == 'g');
9931
9932 regmatch.rm_ic = p_ic;
9933 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
9934 if (regmatch.regprog != NULL)
9935 {
9936 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01009937 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
9939 {
Bram Moolenaar8af26912014-01-23 20:09:34 +01009940 /* Skip empty match except for first match. */
9941 if (regmatch.startp[0] == regmatch.endp[0])
9942 {
9943 if (zero_width == regmatch.startp[0])
9944 {
9945 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02009946 i = MB_PTR2LEN(tail);
9947 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
9948 (size_t)i);
9949 ga.ga_len += i;
9950 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01009951 continue;
9952 }
9953 zero_width = regmatch.startp[0];
9954 }
9955
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 /*
9957 * Get some space for a temporary buffer to do the substitution
9958 * into. It will contain:
9959 * - The text up to where the match is.
9960 * - The substituted text.
9961 * - The text after the match.
9962 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009963 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01009964 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
9966 {
9967 ga_clear(&ga);
9968 break;
9969 }
9970
9971 /* copy the text up to where the match is */
9972 i = (int)(regmatch.startp[0] - tail);
9973 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
9974 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009975 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976 + ga.ga_len + i, TRUE, TRUE, FALSE);
9977 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02009978 tail = regmatch.endp[0];
9979 if (*tail == NUL)
9980 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981 if (!do_all)
9982 break;
9983 }
9984
9985 if (ga.ga_data != NULL)
9986 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
9987
Bram Moolenaar473de612013-06-08 18:19:48 +02009988 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 }
9990
9991 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
9992 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00009993 if (p_cpo == empty_option)
9994 p_cpo = save_cpo;
9995 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009996 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00009997 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998
9999 return ret;
10000}
10001
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010002 static int
10003filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10004{
10005 typval_T rettv;
10006 typval_T argv[3];
10007 char_u buf[NUMBUFLEN];
10008 char_u *s;
10009 int retval = FAIL;
10010 int dummy;
10011
10012 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10013 argv[0] = vimvars[VV_KEY].vv_tv;
10014 argv[1] = vimvars[VV_VAL].vv_tv;
10015 if (expr->v_type == VAR_FUNC)
10016 {
10017 s = expr->vval.v_string;
Bram Moolenaardf48fb42016-07-22 21:50:18 +020010018 if (call_func(s, (int)STRLEN(s), &rettv, 2, argv, NULL,
10019 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010020 goto theend;
10021 }
10022 else if (expr->v_type == VAR_PARTIAL)
10023 {
10024 partial_T *partial = expr->vval.v_partial;
10025
10026 s = partial->pt_name;
Bram Moolenaardf48fb42016-07-22 21:50:18 +020010027 if (call_func(s, (int)STRLEN(s), &rettv, 2, argv, NULL,
10028 0L, 0L, &dummy, TRUE, partial, NULL) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010029 goto theend;
10030 }
10031 else
10032 {
10033 s = get_tv_string_buf_chk(expr, buf);
10034 if (s == NULL)
10035 goto theend;
10036 s = skipwhite(s);
10037 if (eval1(&s, &rettv, TRUE) == FAIL)
10038 goto theend;
10039 if (*s != NUL) /* check for trailing chars after expr */
10040 {
10041 EMSG2(_(e_invexpr2), s);
10042 goto theend;
10043 }
10044 }
10045 if (map)
10046 {
10047 /* map(): replace the list item value */
10048 clear_tv(tv);
10049 rettv.v_lock = 0;
10050 *tv = rettv;
10051 }
10052 else
10053 {
10054 int error = FALSE;
10055
10056 /* filter(): when expr is zero remove the item */
10057 *remp = (get_tv_number_chk(&rettv, &error) == 0);
10058 clear_tv(&rettv);
10059 /* On type error, nothing has been removed; return FAIL to stop the
10060 * loop. The error message was given by get_tv_number_chk(). */
10061 if (error)
10062 goto theend;
10063 }
10064 retval = OK;
10065theend:
10066 clear_tv(&vimvars[VV_VAL].vv_tv);
10067 return retval;
10068}
10069
10070
10071/*
10072 * Implementation of map() and filter().
10073 */
10074 void
10075filter_map(typval_T *argvars, typval_T *rettv, int map)
10076{
10077 typval_T *expr;
10078 listitem_T *li, *nli;
10079 list_T *l = NULL;
10080 dictitem_T *di;
10081 hashtab_T *ht;
10082 hashitem_T *hi;
10083 dict_T *d = NULL;
10084 typval_T save_val;
10085 typval_T save_key;
10086 int rem;
10087 int todo;
10088 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10089 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10090 : N_("filter() argument"));
10091 int save_did_emsg;
10092 int idx = 0;
10093
10094 if (argvars[0].v_type == VAR_LIST)
10095 {
10096 if ((l = argvars[0].vval.v_list) == NULL
10097 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
10098 return;
10099 }
10100 else if (argvars[0].v_type == VAR_DICT)
10101 {
10102 if ((d = argvars[0].vval.v_dict) == NULL
10103 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
10104 return;
10105 }
10106 else
10107 {
10108 EMSG2(_(e_listdictarg), ermsg);
10109 return;
10110 }
10111
10112 expr = &argvars[1];
10113 /* On type errors, the preceding call has already displayed an error
10114 * message. Avoid a misleading error message for an empty string that
10115 * was not passed as argument. */
10116 if (expr->v_type != VAR_UNKNOWN)
10117 {
10118 prepare_vimvar(VV_VAL, &save_val);
10119
10120 /* We reset "did_emsg" to be able to detect whether an error
10121 * occurred during evaluation of the expression. */
10122 save_did_emsg = did_emsg;
10123 did_emsg = FALSE;
10124
10125 prepare_vimvar(VV_KEY, &save_key);
10126 if (argvars[0].v_type == VAR_DICT)
10127 {
10128 vimvars[VV_KEY].vv_type = VAR_STRING;
10129
10130 ht = &d->dv_hashtab;
10131 hash_lock(ht);
10132 todo = (int)ht->ht_used;
10133 for (hi = ht->ht_array; todo > 0; ++hi)
10134 {
10135 if (!HASHITEM_EMPTY(hi))
10136 {
10137 int r;
10138
10139 --todo;
10140 di = HI2DI(hi);
10141 if (map &&
10142 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10143 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
10144 break;
10145 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10146 r = filter_map_one(&di->di_tv, expr, map, &rem);
10147 clear_tv(&vimvars[VV_KEY].vv_tv);
10148 if (r == FAIL || did_emsg)
10149 break;
10150 if (!map && rem)
10151 {
10152 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10153 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10154 break;
10155 dictitem_remove(d, di);
10156 }
10157 }
10158 }
10159 hash_unlock(ht);
10160 }
10161 else
10162 {
10163 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10164
10165 for (li = l->lv_first; li != NULL; li = nli)
10166 {
10167 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
10168 break;
10169 nli = li->li_next;
10170 vimvars[VV_KEY].vv_nr = idx;
10171 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10172 || did_emsg)
10173 break;
10174 if (!map && rem)
10175 listitem_remove(l, li);
10176 ++idx;
10177 }
10178 }
10179
10180 restore_vimvar(VV_KEY, &save_key);
10181 restore_vimvar(VV_VAL, &save_val);
10182
10183 did_emsg |= save_did_emsg;
10184 }
10185
10186 copy_tv(&argvars[0], rettv);
10187}
10188
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */