blob: 3cd73b6ea3ba45674afa959048c7da0d6944e749 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar33570922005-01-25 22:26:29 +000023#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026static char *e_undefvar = N_("E121: Undefined variable: %s");
27static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000028static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
29static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar92124a32005-06-17 22:03:40 +000030static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020031#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020032static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020033#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000034
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010035#define NAMESPACE_CHAR (char_u *)"abglstvw"
36
Bram Moolenaar230bb3f2013-04-24 14:07:45 +020037static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +000038#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +000039
40/*
Bram Moolenaar532c7802005-01-27 14:44:31 +000041 * Old Vim variables such as "v:version" are also available without the "v:".
42 * Also in functions. We need a special hashtable for them.
43 */
Bram Moolenaar4debb442005-06-01 21:57:40 +000044static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +000045
46/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000047 * When recursively copying lists and dicts we need to remember which ones we
48 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000049 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000050 */
51static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020052
Bram Moolenaard9fba312005-06-26 22:34:35 +000053/*
Bram Moolenaar33570922005-01-25 22:26:29 +000054 * Array to hold the hashtab with variables local to each sourced script.
55 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 */
Bram Moolenaar33570922005-01-25 22:26:29 +000057typedef struct
58{
59 dictitem_T sv_var;
60 dict_T sv_dict;
61} scriptvar_T;
62
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020063static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
64#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
65#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +000066
67static int echo_attr = 0; /* attributes used for ":echo" */
68
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000069/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000070static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
71
Bram Moolenaar071d4272004-06-13 20:20:40 +000072/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000073 * Info used by a ":for" loop.
74 */
Bram Moolenaar33570922005-01-25 22:26:29 +000075typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000076{
77 int fi_semicolon; /* TRUE if ending in '; var]' */
78 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +000079 listwatch_T fi_lw; /* keep an eye on the item used. */
80 list_T *fi_list; /* list being used */
81} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000082
Bram Moolenaara7043832005-01-21 11:56:39 +000083
84/*
Bram Moolenaar33570922005-01-25 22:26:29 +000085 * Array to hold the value of v: variables.
86 * The value is in a dictitem, so that it can also be used in the v: scope.
87 * The reason to use this table anyway is for very quick access to the
88 * variables with the VV_ defines.
89 */
Bram Moolenaar33570922005-01-25 22:26:29 +000090
91/* values for vv_flags: */
92#define VV_COMPAT 1 /* compatible, also used without "v:" */
93#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000094#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +000095
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +010096#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +000097
98static struct vimvar
99{
100 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100101 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000102 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
103} vimvars[VV_LEN] =
104{
105 /*
106 * The order here must match the VV_ defines in vim.h!
107 * Initializing a union does not work, leave tv.vval empty to get zero's.
108 */
109 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
110 {VV_NAME("count1", VAR_NUMBER), VV_RO},
111 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
112 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
113 {VV_NAME("warningmsg", VAR_STRING), 0},
114 {VV_NAME("statusmsg", VAR_STRING), 0},
115 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
116 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
117 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
118 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
119 {VV_NAME("termresponse", VAR_STRING), VV_RO},
120 {VV_NAME("fname", VAR_STRING), VV_RO},
121 {VV_NAME("lang", VAR_STRING), VV_RO},
122 {VV_NAME("lc_time", VAR_STRING), VV_RO},
123 {VV_NAME("ctype", VAR_STRING), VV_RO},
124 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
125 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
126 {VV_NAME("fname_in", VAR_STRING), VV_RO},
127 {VV_NAME("fname_out", VAR_STRING), VV_RO},
128 {VV_NAME("fname_new", VAR_STRING), VV_RO},
129 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
130 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
131 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
132 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
133 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
134 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
135 {VV_NAME("progname", VAR_STRING), VV_RO},
136 {VV_NAME("servername", VAR_STRING), VV_RO},
137 {VV_NAME("dying", VAR_NUMBER), VV_RO},
138 {VV_NAME("exception", VAR_STRING), VV_RO},
139 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
140 {VV_NAME("register", VAR_STRING), VV_RO},
141 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
142 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000143 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
144 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000145 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000146 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
147 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000148 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
149 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
Bram Moolenaarc9721bd2016-06-04 17:41:03 +0200150 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000151 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
152 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
153 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000154 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000155 {VV_NAME("swapname", VAR_STRING), VV_RO},
156 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000157 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200158 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000159 {VV_NAME("mouse_win", VAR_NUMBER), 0},
Bram Moolenaar511972d2016-06-04 18:09:59 +0200160 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000161 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
162 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000163 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000164 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100165 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000166 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200167 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200168 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200169 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200170 {VV_NAME("option_new", VAR_STRING), VV_RO},
171 {VV_NAME("option_old", VAR_STRING), VV_RO},
172 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100173 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100174 {VV_NAME("false", VAR_SPECIAL), VV_RO},
175 {VV_NAME("true", VAR_SPECIAL), VV_RO},
176 {VV_NAME("null", VAR_SPECIAL), VV_RO},
177 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100178 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200179 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaarf562e722016-07-19 17:25:25 +0200180 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
181 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
182 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
183 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
184 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
185 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
186 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
187 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
188 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
189 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000190};
191
192/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000193#define vv_type vv_di.di_tv.v_type
194#define vv_nr vv_di.di_tv.vval.v_number
195#define vv_float vv_di.di_tv.vval.v_float
196#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000197#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200198#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000199#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000200
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200201static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000202#define vimvarht vimvardict.dv_hashtab
203
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100204static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
205static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
206static char_u *skip_var_one(char_u *arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100207static void list_glob_vars(int *first);
208static void list_buf_vars(int *first);
209static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000210#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100211static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000212#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100213static void list_vim_vars(int *first);
214static void list_script_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100215static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
216static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100217static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
218static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100219static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
220static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
221static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
222static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000223
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100224static int eval2(char_u **arg, typval_T *rettv, int evaluate);
225static int eval3(char_u **arg, typval_T *rettv, int evaluate);
226static int eval4(char_u **arg, typval_T *rettv, int evaluate);
227static int eval5(char_u **arg, typval_T *rettv, int evaluate);
228static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
229static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000230
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100231static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100232static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
233static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100234static int free_unref_items(int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100235static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100236static int get_env_len(char_u **arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100237static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200238static void check_vars(char_u *name, int len);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100239static typval_T *alloc_string_tv(char_u *string);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100240static void delete_var(hashtab_T *ht, hashitem_T *hi);
241static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
242static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100243static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200244
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200245/* for VIM_VERSION_ defines */
246#include "version.h"
247
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100248
249#if defined(EBCDIC) || defined(PROTO)
250/*
251 * Compare struct fst by function name.
252 */
253 static int
254compare_func_name(const void *s1, const void *s2)
255{
256 struct fst *p1 = (struct fst *)s1;
257 struct fst *p2 = (struct fst *)s2;
258
259 return STRCMP(p1->f_name, p2->f_name);
260}
261
262/*
263 * Sort the function table by function name.
264 * The sorting of the table above is ASCII dependant.
265 * On machines using EBCDIC we have to sort it.
266 */
267 static void
268sortFunctions(void)
269{
270 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
271
272 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
273}
274#endif
275
276
Bram Moolenaar33570922005-01-25 22:26:29 +0000277/*
278 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000279 */
280 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100281eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000282{
Bram Moolenaar33570922005-01-25 22:26:29 +0000283 int i;
284 struct vimvar *p;
285
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200286 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
287 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200288 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000289 hash_init(&compat_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200290 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000291
292 for (i = 0; i < VV_LEN; ++i)
293 {
294 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200295 if (STRLEN(p->vv_name) > 16)
296 {
Bram Moolenaarde330112017-01-08 20:50:52 +0100297 IEMSG("INTERNAL: name too long, increase size of dictitem16_T");
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200298 getout(1);
299 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000300 STRCPY(p->vv_di.di_key, p->vv_name);
301 if (p->vv_flags & VV_RO)
302 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
303 else if (p->vv_flags & VV_RO_SBX)
304 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
305 else
306 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000307
308 /* add to v: scope dict, unless the value is not always available */
309 if (p->vv_type != VAR_UNKNOWN)
310 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000311 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000312 /* add to compat scope dict */
313 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000314 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100315 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
316
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000317 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100318 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200319 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100320 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100321
322 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
323 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
324 set_vim_var_nr(VV_NONE, VVAL_NONE);
325 set_vim_var_nr(VV_NULL, VVAL_NULL);
326
Bram Moolenaarf562e722016-07-19 17:25:25 +0200327 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
328 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
329 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
330 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
331 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
332 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
333 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
334 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
335 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
336 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
337
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200338 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200339
340#ifdef EBCDIC
341 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100342 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200343 */
344 sortFunctions();
345#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000346}
347
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000348#if defined(EXITFREE) || defined(PROTO)
349 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100350eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000351{
352 int i;
353 struct vimvar *p;
354
355 for (i = 0; i < VV_LEN; ++i)
356 {
357 p = &vimvars[i];
358 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000359 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000360 vim_free(p->vv_str);
361 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000362 }
363 else if (p->vv_di.di_tv.v_type == VAR_LIST)
364 {
365 list_unref(p->vv_list);
366 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000367 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000368 }
369 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000370 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000371 hash_clear(&compat_hashtab);
372
Bram Moolenaard9fba312005-06-26 22:34:35 +0000373 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100374# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200375 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100376# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000377
378 /* global variables */
379 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000380
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000381 /* autoloaded script names */
382 ga_clear_strings(&ga_loaded);
383
Bram Moolenaarcca74132013-09-25 21:00:28 +0200384 /* Script-local variables. First clear all the variables and in a second
385 * loop free the scriptvar_T, because a variable in one script might hold
386 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200387 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200388 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200389 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200390 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200391 ga_clear(&ga_scripts);
392
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000393 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200394 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000395
396 /* functions */
397 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000398}
399#endif
400
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401
402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 * Set an internal variable to a string value. Creates the variable if it does
404 * not already exist.
405 */
406 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100407set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000409 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000410 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411
412 val = vim_strsave(value);
413 if (val != NULL)
414 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000415 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000416 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000418 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000419 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 }
421 }
422}
423
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000424static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200425#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000426static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000427static char_u *redir_endp = NULL;
428static char_u *redir_varname = NULL;
429
430/*
431 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100432 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000433 * Returns OK if successfully completed the setup. FAIL otherwise.
434 */
435 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100436var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000437{
438 int save_emsg;
439 int err;
440 typval_T tv;
441
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000442 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000443 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000444 {
445 EMSG(_(e_invarg));
446 return FAIL;
447 }
448
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000449 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000450 redir_varname = vim_strsave(name);
451 if (redir_varname == NULL)
452 return FAIL;
453
454 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
455 if (redir_lval == NULL)
456 {
457 var_redir_stop();
458 return FAIL;
459 }
460
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000461 /* The output is stored in growarray "redir_ga" until redirection ends. */
462 ga_init2(&redir_ga, (int)sizeof(char), 500);
463
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000464 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100465 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000466 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000467 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
468 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200469 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000470 if (redir_endp != NULL && *redir_endp != NUL)
471 /* Trailing characters are present after the variable name */
472 EMSG(_(e_trailing));
473 else
474 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000475 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000476 var_redir_stop();
477 return FAIL;
478 }
479
480 /* check if we can write to the variable: set it to or append an empty
481 * string */
482 save_emsg = did_emsg;
483 did_emsg = FALSE;
484 tv.v_type = VAR_STRING;
485 tv.vval.v_string = (char_u *)"";
486 if (append)
487 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
488 else
489 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200490 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000491 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000492 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000493 if (err)
494 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000495 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000496 var_redir_stop();
497 return FAIL;
498 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000499
500 return OK;
501}
502
503/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000504 * Append "value[value_len]" to the variable set by var_redir_start().
505 * The actual appending is postponed until redirection ends, because the value
506 * appended may in fact be the string we write to, changing it may cause freed
507 * memory to be used:
508 * :redir => foo
509 * :let foo
510 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000511 */
512 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100513var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000514{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000515 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000516
517 if (redir_lval == NULL)
518 return;
519
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000520 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000521 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000522 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000523 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000524
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000525 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000526 {
527 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000528 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000529 }
530 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000531 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000532}
533
534/*
535 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000536 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000537 */
538 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100539var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000540{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000541 typval_T tv;
542
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200543 if (EVALCMD_BUSY)
544 {
545 redir_lval = NULL;
546 return;
547 }
548
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000549 if (redir_lval != NULL)
550 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000551 /* If there was no error: assign the text to the variable. */
552 if (redir_endp != NULL)
553 {
554 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
555 tv.v_type = VAR_STRING;
556 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200557 /* Call get_lval() again, if it's inside a Dict or List it may
558 * have changed. */
559 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100560 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200561 if (redir_endp != NULL && redir_lval->ll_name != NULL)
562 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
563 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000564 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000565
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000566 /* free the collected output */
567 vim_free(redir_ga.ga_data);
568 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000569
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000570 vim_free(redir_lval);
571 redir_lval = NULL;
572 }
573 vim_free(redir_varname);
574 redir_varname = NULL;
575}
576
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577# if defined(FEAT_MBYTE) || defined(PROTO)
578 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100579eval_charconvert(
580 char_u *enc_from,
581 char_u *enc_to,
582 char_u *fname_from,
583 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584{
585 int err = FALSE;
586
587 set_vim_var_string(VV_CC_FROM, enc_from, -1);
588 set_vim_var_string(VV_CC_TO, enc_to, -1);
589 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
590 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
591 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
592 err = TRUE;
593 set_vim_var_string(VV_CC_FROM, NULL, -1);
594 set_vim_var_string(VV_CC_TO, NULL, -1);
595 set_vim_var_string(VV_FNAME_IN, NULL, -1);
596 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
597
598 if (err)
599 return FAIL;
600 return OK;
601}
602# endif
603
604# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
605 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100606eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607{
608 int err = FALSE;
609
610 set_vim_var_string(VV_FNAME_IN, fname, -1);
611 set_vim_var_string(VV_CMDARG, args, -1);
612 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
613 err = TRUE;
614 set_vim_var_string(VV_FNAME_IN, NULL, -1);
615 set_vim_var_string(VV_CMDARG, NULL, -1);
616
617 if (err)
618 {
619 mch_remove(fname);
620 return FAIL;
621 }
622 return OK;
623}
624# endif
625
626# if defined(FEAT_DIFF) || defined(PROTO)
627 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100628eval_diff(
629 char_u *origfile,
630 char_u *newfile,
631 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632{
633 int err = FALSE;
634
635 set_vim_var_string(VV_FNAME_IN, origfile, -1);
636 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
637 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
638 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
639 set_vim_var_string(VV_FNAME_IN, NULL, -1);
640 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
641 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
642}
643
644 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100645eval_patch(
646 char_u *origfile,
647 char_u *difffile,
648 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649{
650 int err;
651
652 set_vim_var_string(VV_FNAME_IN, origfile, -1);
653 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
654 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
655 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
656 set_vim_var_string(VV_FNAME_IN, NULL, -1);
657 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
658 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
659}
660# endif
661
662/*
663 * Top level evaluation function, returning a boolean.
664 * Sets "error" to TRUE if there was an error.
665 * Return TRUE or FALSE.
666 */
667 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100668eval_to_bool(
669 char_u *arg,
670 int *error,
671 char_u **nextcmd,
672 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673{
Bram Moolenaar33570922005-01-25 22:26:29 +0000674 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200675 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676
677 if (skip)
678 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000679 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 else
682 {
683 *error = FALSE;
684 if (!skip)
685 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000686 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000687 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 }
689 }
690 if (skip)
691 --emsg_skip;
692
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200693 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694}
695
696/*
697 * Top level evaluation function, returning a string. If "skip" is TRUE,
698 * only parsing to "nextcmd" is done, without reporting errors. Return
699 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
700 */
701 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100702eval_to_string_skip(
703 char_u *arg,
704 char_u **nextcmd,
705 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706{
Bram Moolenaar33570922005-01-25 22:26:29 +0000707 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 char_u *retval;
709
710 if (skip)
711 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000712 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 retval = NULL;
714 else
715 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000716 retval = vim_strsave(get_tv_string(&tv));
717 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 }
719 if (skip)
720 --emsg_skip;
721
722 return retval;
723}
724
725/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000726 * Skip over an expression at "*pp".
727 * Return FAIL for an error, OK otherwise.
728 */
729 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100730skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000731{
Bram Moolenaar33570922005-01-25 22:26:29 +0000732 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000733
734 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000735 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000736}
737
738/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000740 * When "convert" is TRUE convert a List into a sequence of lines and convert
741 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 * Return pointer to allocated memory, or NULL for failure.
743 */
744 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100745eval_to_string(
746 char_u *arg,
747 char_u **nextcmd,
748 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749{
Bram Moolenaar33570922005-01-25 22:26:29 +0000750 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000752 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000753#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000754 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000757 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 retval = NULL;
759 else
760 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000761 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000762 {
763 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000764 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200765 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200766 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200767 if (tv.vval.v_list->lv_len > 0)
768 ga_append(&ga, NL);
769 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000770 ga_append(&ga, NUL);
771 retval = (char_u *)ga.ga_data;
772 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000773#ifdef FEAT_FLOAT
774 else if (convert && tv.v_type == VAR_FLOAT)
775 {
776 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
777 retval = vim_strsave(numbuf);
778 }
779#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000780 else
781 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000782 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 }
784
785 return retval;
786}
787
788/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000789 * Call eval_to_string() without using current local variables and using
790 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 */
792 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100793eval_to_string_safe(
794 char_u *arg,
795 char_u **nextcmd,
796 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797{
798 char_u *retval;
799 void *save_funccalp;
800
801 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000802 if (use_sandbox)
803 ++sandbox;
804 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000805 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000806 if (use_sandbox)
807 --sandbox;
808 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 restore_funccal(save_funccalp);
810 return retval;
811}
812
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813/*
814 * Top level evaluation function, returning a number.
815 * Evaluates "expr" silently.
816 * Returns -1 for an error.
817 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200818 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100819eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820{
Bram Moolenaar33570922005-01-25 22:26:29 +0000821 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200822 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000823 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824
825 ++emsg_off;
826
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000827 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 retval = -1;
829 else
830 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000831 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000832 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 }
834 --emsg_off;
835
836 return retval;
837}
838
Bram Moolenaara40058a2005-07-11 22:42:07 +0000839/*
840 * Prepare v: variable "idx" to be used.
841 * Save the current typeval in "save_tv".
842 * When not used yet add the variable to the v: hashtable.
843 */
844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100845prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000846{
847 *save_tv = vimvars[idx].vv_tv;
848 if (vimvars[idx].vv_type == VAR_UNKNOWN)
849 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
850}
851
852/*
853 * Restore v: variable "idx" to typeval "save_tv".
854 * When no longer defined, remove the variable from the v: hashtable.
855 */
856 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100857restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000858{
859 hashitem_T *hi;
860
Bram Moolenaara40058a2005-07-11 22:42:07 +0000861 vimvars[idx].vv_tv = *save_tv;
862 if (vimvars[idx].vv_type == VAR_UNKNOWN)
863 {
864 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
865 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100866 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +0000867 else
868 hash_remove(&vimvarht, hi);
869 }
870}
871
Bram Moolenaar3c56a962006-03-12 22:19:04 +0000872#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000873/*
874 * Evaluate an expression to a list with suggestions.
875 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000876 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000877 */
878 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100879eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000880{
881 typval_T save_val;
882 typval_T rettv;
883 list_T *list = NULL;
884 char_u *p = skipwhite(expr);
885
886 /* Set "v:val" to the bad word. */
887 prepare_vimvar(VV_VAL, &save_val);
888 vimvars[VV_VAL].vv_type = VAR_STRING;
889 vimvars[VV_VAL].vv_str = badword;
890 if (p_verbose == 0)
891 ++emsg_off;
892
893 if (eval1(&p, &rettv, TRUE) == OK)
894 {
895 if (rettv.v_type != VAR_LIST)
896 clear_tv(&rettv);
897 else
898 list = rettv.vval.v_list;
899 }
900
901 if (p_verbose == 0)
902 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000903 restore_vimvar(VV_VAL, &save_val);
904
905 return list;
906}
907
908/*
909 * "list" is supposed to contain two items: a word and a number. Return the
910 * word in "pp" and the number as the return value.
911 * Return -1 if anything isn't right.
912 * Used to get the good word and score from the eval_spell_expr() result.
913 */
914 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100915get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000916{
917 listitem_T *li;
918
919 li = list->lv_first;
920 if (li == NULL)
921 return -1;
922 *pp = get_tv_string(&li->li_tv);
923
924 li = li->li_next;
925 if (li == NULL)
926 return -1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200927 return (int)get_tv_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000928}
929#endif
930
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000931/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000932 * Top level evaluation function.
933 * Returns an allocated typval_T with the result.
934 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000935 */
936 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100937eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000938{
939 typval_T *tv;
940
941 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000942 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000943 {
944 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +0000945 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000946 }
947
948 return tv;
949}
950
951
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000953 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000954 * Uses argv[argc] for the function arguments. Only Number and String
955 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000956 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200958 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100959call_vim_function(
960 char_u *func,
961 int argc,
962 char_u **argv,
963 int safe, /* use the sandbox */
964 int str_arg_only, /* all arguments are strings */
965 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966{
Bram Moolenaar33570922005-01-25 22:26:29 +0000967 typval_T *argvars;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200968 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 int len;
970 int i;
971 int doesrange;
972 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000973 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000975 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000977 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978
979 for (i = 0; i < argc; i++)
980 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000981 /* Pass a NULL or empty argument as an empty string */
982 if (argv[i] == NULL || *argv[i] == NUL)
983 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000984 argvars[i].v_type = VAR_STRING;
985 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000986 continue;
987 }
988
Bram Moolenaar0cbba942012-07-25 16:47:03 +0200989 if (str_arg_only)
990 len = 0;
991 else
992 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100993 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 if (len != 0 && len == (int)STRLEN(argv[i]))
995 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000996 argvars[i].v_type = VAR_NUMBER;
997 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 }
999 else
1000 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001001 argvars[i].v_type = VAR_STRING;
1002 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 }
1004 }
1005
1006 if (safe)
1007 {
1008 save_funccalp = save_funccal();
1009 ++sandbox;
1010 }
1011
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001012 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001013 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001015 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 if (safe)
1017 {
1018 --sandbox;
1019 restore_funccal(save_funccalp);
1020 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001021 vim_free(argvars);
1022
1023 if (ret == FAIL)
1024 clear_tv(rettv);
1025
1026 return ret;
1027}
1028
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001029/*
1030 * Call vimL function "func" and return the result as a number.
1031 * Returns -1 when calling the function fails.
1032 * Uses argv[argc] for the function arguments.
1033 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001034 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001035call_func_retnr(
1036 char_u *func,
1037 int argc,
1038 char_u **argv,
1039 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001040{
1041 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001042 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001043
1044 /* All arguments are passed as strings, no conversion to number. */
1045 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1046 return -1;
1047
1048 retval = get_tv_number_chk(&rettv, NULL);
1049 clear_tv(&rettv);
1050 return retval;
1051}
1052
1053#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1054 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1055
Bram Moolenaar4f688582007-07-24 12:34:30 +00001056# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001057/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001058 * Call vimL function "func" and return the result as a string.
1059 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001060 * Uses argv[argc] for the function arguments.
1061 */
1062 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001063call_func_retstr(
1064 char_u *func,
1065 int argc,
1066 char_u **argv,
1067 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001068{
1069 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001070 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001071
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001072 /* All arguments are passed as strings, no conversion to number. */
1073 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001074 return NULL;
1075
1076 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001077 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 return retval;
1079}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001080# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001081
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001082/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001083 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001084 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001085 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001086 */
1087 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001088call_func_retlist(
1089 char_u *func,
1090 int argc,
1091 char_u **argv,
1092 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001093{
1094 typval_T rettv;
1095
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001096 /* All arguments are passed as strings, no conversion to number. */
1097 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001098 return NULL;
1099
1100 if (rettv.v_type != VAR_LIST)
1101 {
1102 clear_tv(&rettv);
1103 return NULL;
1104 }
1105
1106 return rettv.vval.v_list;
1107}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108#endif
1109
Bram Moolenaar05159a02005-02-26 23:04:13 +00001110
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111#ifdef FEAT_FOLDING
1112/*
1113 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1114 * it in "*cp". Doesn't give error messages.
1115 */
1116 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001117eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118{
Bram Moolenaar33570922005-01-25 22:26:29 +00001119 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001120 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001122 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1123 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124
1125 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001126 if (use_sandbox)
1127 ++sandbox;
1128 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001130 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 retval = 0;
1132 else
1133 {
1134 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001135 if (tv.v_type == VAR_NUMBER)
1136 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001137 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 retval = 0;
1139 else
1140 {
1141 /* If the result is a string, check if there is a non-digit before
1142 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001143 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 if (!VIM_ISDIGIT(*s) && *s != '-')
1145 *cp = *s++;
1146 retval = atol((char *)s);
1147 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001148 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 }
1150 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001151 if (use_sandbox)
1152 --sandbox;
1153 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001155 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156}
1157#endif
1158
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001160 * ":let" list all variable values
1161 * ":let var1 var2" list variable values
1162 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001163 * ":let var += expr" assignment command.
1164 * ":let var -= expr" assignment command.
1165 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001166 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 */
1168 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001169ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170{
1171 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001172 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001173 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001175 int var_count = 0;
1176 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001177 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001178 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001179 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180
Bram Moolenaardb552d602006-03-23 22:59:57 +00001181 argend = skip_var_list(arg, &var_count, &semicolon);
1182 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001183 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001184 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1185 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001186 expr = skipwhite(argend);
1187 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1188 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001190 /*
1191 * ":let" without "=": list variables
1192 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001193 if (*arg == '[')
1194 EMSG(_(e_invarg));
1195 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001196 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001197 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001198 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001199 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001200 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001201 list_glob_vars(&first);
1202 list_buf_vars(&first);
1203 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001204#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001205 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001206#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001207 list_script_vars(&first);
1208 list_func_vars(&first);
1209 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 eap->nextcmd = check_nextcmd(arg);
1212 }
1213 else
1214 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001215 op[0] = '=';
1216 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001217 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001218 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001219 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1220 op[0] = *expr; /* +=, -= or .= */
1221 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001222 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001223 else
1224 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001225
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 if (eap->skip)
1227 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001228 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 if (eap->skip)
1230 {
1231 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001232 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 --emsg_skip;
1234 }
1235 else if (i != FAIL)
1236 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001237 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001238 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001239 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 }
1241 }
1242}
1243
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001244/*
1245 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1246 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001247 * When "nextchars" is not NULL it points to a string with characters that
1248 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1249 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001250 * Returns OK or FAIL;
1251 */
1252 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001253ex_let_vars(
1254 char_u *arg_start,
1255 typval_T *tv,
1256 int copy, /* copy values from "tv", don't move */
1257 int semicolon, /* from skip_var_list() */
1258 int var_count, /* from skip_var_list() */
1259 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001260{
1261 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001262 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001263 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001264 listitem_T *item;
1265 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001266
1267 if (*arg != '[')
1268 {
1269 /*
1270 * ":let var = expr" or ":for var in list"
1271 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001272 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001273 return FAIL;
1274 return OK;
1275 }
1276
1277 /*
1278 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1279 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001280 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001281 {
1282 EMSG(_(e_listreq));
1283 return FAIL;
1284 }
1285
1286 i = list_len(l);
1287 if (semicolon == 0 && var_count < i)
1288 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001289 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001290 return FAIL;
1291 }
1292 if (var_count - semicolon > i)
1293 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001294 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001295 return FAIL;
1296 }
1297
1298 item = l->lv_first;
1299 while (*arg != ']')
1300 {
1301 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001302 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001303 item = item->li_next;
1304 if (arg == NULL)
1305 return FAIL;
1306
1307 arg = skipwhite(arg);
1308 if (*arg == ';')
1309 {
1310 /* Put the rest of the list (may be empty) in the var after ';'.
1311 * Create a new list for this. */
1312 l = list_alloc();
1313 if (l == NULL)
1314 return FAIL;
1315 while (item != NULL)
1316 {
1317 list_append_tv(l, &item->li_tv);
1318 item = item->li_next;
1319 }
1320
1321 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001322 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001323 ltv.vval.v_list = l;
1324 l->lv_refcount = 1;
1325
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001326 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1327 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001328 clear_tv(&ltv);
1329 if (arg == NULL)
1330 return FAIL;
1331 break;
1332 }
1333 else if (*arg != ',' && *arg != ']')
1334 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001335 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001336 return FAIL;
1337 }
1338 }
1339
1340 return OK;
1341}
1342
1343/*
1344 * Skip over assignable variable "var" or list of variables "[var, var]".
1345 * Used for ":let varvar = expr" and ":for varvar in expr".
1346 * For "[var, var]" increment "*var_count" for each variable.
1347 * for "[var, var; var]" set "semicolon".
1348 * Return NULL for an error.
1349 */
1350 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001351skip_var_list(
1352 char_u *arg,
1353 int *var_count,
1354 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001355{
1356 char_u *p, *s;
1357
1358 if (*arg == '[')
1359 {
1360 /* "[var, var]": find the matching ']'. */
1361 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001362 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001363 {
1364 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1365 s = skip_var_one(p);
1366 if (s == p)
1367 {
1368 EMSG2(_(e_invarg2), p);
1369 return NULL;
1370 }
1371 ++*var_count;
1372
1373 p = skipwhite(s);
1374 if (*p == ']')
1375 break;
1376 else if (*p == ';')
1377 {
1378 if (*semicolon == 1)
1379 {
1380 EMSG(_("Double ; in list of variables"));
1381 return NULL;
1382 }
1383 *semicolon = 1;
1384 }
1385 else if (*p != ',')
1386 {
1387 EMSG2(_(e_invarg2), p);
1388 return NULL;
1389 }
1390 }
1391 return p + 1;
1392 }
1393 else
1394 return skip_var_one(arg);
1395}
1396
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001397/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001398 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001399 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001400 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001401 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001402skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001403{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001404 if (*arg == '@' && arg[1] != NUL)
1405 return arg + 2;
1406 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1407 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001408}
1409
Bram Moolenaara7043832005-01-21 11:56:39 +00001410/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001411 * List variables for hashtab "ht" with prefix "prefix".
1412 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001413 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001414 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001415list_hashtable_vars(
1416 hashtab_T *ht,
1417 char_u *prefix,
1418 int empty,
1419 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001420{
Bram Moolenaar33570922005-01-25 22:26:29 +00001421 hashitem_T *hi;
1422 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001423 int todo;
1424
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001425 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001426 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1427 {
1428 if (!HASHITEM_EMPTY(hi))
1429 {
1430 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001431 di = HI2DI(hi);
1432 if (empty || di->di_tv.v_type != VAR_STRING
1433 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001434 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001435 }
1436 }
1437}
1438
1439/*
1440 * List global variables.
1441 */
1442 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001443list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001444{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001445 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001446}
1447
1448/*
1449 * List buffer variables.
1450 */
1451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001452list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001453{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001454 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001455 TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001456}
1457
1458/*
1459 * List window variables.
1460 */
1461 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001462list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001463{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001464 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001465 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001466}
1467
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001468#ifdef FEAT_WINDOWS
1469/*
1470 * List tab page variables.
1471 */
1472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001473list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001474{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001475 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001476 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001477}
1478#endif
1479
Bram Moolenaara7043832005-01-21 11:56:39 +00001480/*
1481 * List Vim variables.
1482 */
1483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001484list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001485{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001486 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001487}
1488
1489/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001490 * List script-local variables, if there is a script.
1491 */
1492 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001493list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001494{
1495 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001496 list_hashtable_vars(&SCRIPT_VARS(current_SID),
1497 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001498}
1499
1500/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001501 * List variables in "arg".
1502 */
1503 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001504list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001505{
1506 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001507 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001508 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001509 char_u *name_start;
1510 char_u *arg_subsc;
1511 char_u *tofree;
1512 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001513
1514 while (!ends_excmd(*arg) && !got_int)
1515 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001516 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001517 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001518 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001519 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1520 {
1521 emsg_severe = TRUE;
1522 EMSG(_(e_trailing));
1523 break;
1524 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001525 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001526 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001527 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001528 /* get_name_len() takes care of expanding curly braces */
1529 name_start = name = arg;
1530 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1531 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001532 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001533 /* This is mainly to keep test 49 working: when expanding
1534 * curly braces fails overrule the exception error message. */
1535 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001536 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001537 emsg_severe = TRUE;
1538 EMSG2(_(e_invarg2), arg);
1539 break;
1540 }
1541 error = TRUE;
1542 }
1543 else
1544 {
1545 if (tofree != NULL)
1546 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001547 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001548 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001549 else
1550 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001551 /* handle d.key, l[idx], f(expr) */
1552 arg_subsc = arg;
1553 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001554 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001555 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001556 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001557 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001558 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001559 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001560 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001561 case 'g': list_glob_vars(first); break;
1562 case 'b': list_buf_vars(first); break;
1563 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001564#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001565 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001566#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001567 case 'v': list_vim_vars(first); break;
1568 case 's': list_script_vars(first); break;
1569 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001570 default:
1571 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001572 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001573 }
1574 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001575 {
1576 char_u numbuf[NUMBUFLEN];
1577 char_u *tf;
1578 int c;
1579 char_u *s;
1580
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001581 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001582 c = *arg;
1583 *arg = NUL;
1584 list_one_var_a((char_u *)"",
1585 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001586 tv.v_type,
1587 s == NULL ? (char_u *)"" : s,
1588 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001589 *arg = c;
1590 vim_free(tf);
1591 }
1592 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001593 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001594 }
1595 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001596
1597 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001598 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001599
1600 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001601 }
1602
1603 return arg;
1604}
1605
1606/*
1607 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1608 * Returns a pointer to the char just after the var name.
1609 * Returns NULL if there is an error.
1610 */
1611 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001612ex_let_one(
1613 char_u *arg, /* points to variable name */
1614 typval_T *tv, /* value to assign to variable */
1615 int copy, /* copy value from "tv" */
1616 char_u *endchars, /* valid chars after variable name or NULL */
1617 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001618{
1619 int c1;
1620 char_u *name;
1621 char_u *p;
1622 char_u *arg_end = NULL;
1623 int len;
1624 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001625 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001626
1627 /*
1628 * ":let $VAR = expr": Set environment variable.
1629 */
1630 if (*arg == '$')
1631 {
1632 /* Find the end of the name. */
1633 ++arg;
1634 name = arg;
1635 len = get_env_len(&arg);
1636 if (len == 0)
1637 EMSG2(_(e_invarg2), name - 1);
1638 else
1639 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001640 if (op != NULL && (*op == '+' || *op == '-'))
1641 EMSG2(_(e_letwrong), op);
1642 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001643 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001644 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001645 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001646 {
1647 c1 = name[len];
1648 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001649 p = get_tv_string_chk(tv);
1650 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001651 {
1652 int mustfree = FALSE;
1653 char_u *s = vim_getenv(name, &mustfree);
1654
1655 if (s != NULL)
1656 {
1657 p = tofree = concat_str(s, p);
1658 if (mustfree)
1659 vim_free(s);
1660 }
1661 }
1662 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001663 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001664 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001665 if (STRICMP(name, "HOME") == 0)
1666 init_homedir();
1667 else if (didset_vim && STRICMP(name, "VIM") == 0)
1668 didset_vim = FALSE;
1669 else if (didset_vimruntime
1670 && STRICMP(name, "VIMRUNTIME") == 0)
1671 didset_vimruntime = FALSE;
1672 arg_end = arg;
1673 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001674 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001675 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001676 }
1677 }
1678 }
1679
1680 /*
1681 * ":let &option = expr": Set option value.
1682 * ":let &l:option = expr": Set local option value.
1683 * ":let &g:option = expr": Set global option value.
1684 */
1685 else if (*arg == '&')
1686 {
1687 /* Find the end of the name. */
1688 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001689 if (p == NULL || (endchars != NULL
1690 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001691 EMSG(_(e_letunexp));
1692 else
1693 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001694 long n;
1695 int opt_type;
1696 long numval;
1697 char_u *stringval = NULL;
1698 char_u *s;
1699
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001700 c1 = *p;
1701 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001702
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001703 n = (long)get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001704 s = get_tv_string_chk(tv); /* != NULL if number or string */
1705 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001706 {
1707 opt_type = get_option_value(arg, &numval,
1708 &stringval, opt_flags);
1709 if ((opt_type == 1 && *op == '.')
1710 || (opt_type == 0 && *op != '.'))
1711 EMSG2(_(e_letwrong), op);
1712 else
1713 {
1714 if (opt_type == 1) /* number */
1715 {
1716 if (*op == '+')
1717 n = numval + n;
1718 else
1719 n = numval - n;
1720 }
1721 else if (opt_type == 0 && stringval != NULL) /* string */
1722 {
1723 s = concat_str(stringval, s);
1724 vim_free(stringval);
1725 stringval = s;
1726 }
1727 }
1728 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001729 if (s != NULL)
1730 {
1731 set_option_value(arg, n, s, opt_flags);
1732 arg_end = p;
1733 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001734 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001735 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001736 }
1737 }
1738
1739 /*
1740 * ":let @r = expr": Set register contents.
1741 */
1742 else if (*arg == '@')
1743 {
1744 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001745 if (op != NULL && (*op == '+' || *op == '-'))
1746 EMSG2(_(e_letwrong), op);
1747 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001748 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001749 EMSG(_(e_letunexp));
1750 else
1751 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001752 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001753 char_u *s;
1754
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001755 p = get_tv_string_chk(tv);
1756 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001757 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02001758 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001759 if (s != NULL)
1760 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001761 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001762 vim_free(s);
1763 }
1764 }
1765 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001766 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001767 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001768 arg_end = arg + 1;
1769 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001770 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001771 }
1772 }
1773
1774 /*
1775 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001776 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001777 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001778 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001779 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001780 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001781
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001782 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001783 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001784 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001785 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1786 EMSG(_(e_letunexp));
1787 else
1788 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001789 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001790 arg_end = p;
1791 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001792 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001793 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001794 }
1795
1796 else
1797 EMSG2(_(e_invarg2), arg);
1798
1799 return arg_end;
1800}
1801
1802/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001803 * Get an lval: variable, Dict item or List item that can be assigned a value
1804 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1805 * "name.key", "name.key[expr]" etc.
1806 * Indexing only works if "name" is an existing List or Dictionary.
1807 * "name" points to the start of the name.
1808 * If "rettv" is not NULL it points to the value to be assigned.
1809 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1810 * wrong; must end in space or cmd separator.
1811 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001812 * flags:
1813 * GLV_QUIET: do not give error messages
1814 * GLV_NO_AUTOLOAD: do not use script autoloading
1815 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001816 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001817 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001818 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001819 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001820 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001821get_lval(
1822 char_u *name,
1823 typval_T *rettv,
1824 lval_T *lp,
1825 int unlet,
1826 int skip,
1827 int flags, /* GLV_ values */
1828 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001829{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001830 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001831 char_u *expr_start, *expr_end;
1832 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001833 dictitem_T *v;
1834 typval_T var1;
1835 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001836 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001837 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001838 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001839 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001840 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001841 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001842
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001843 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001844 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001845
1846 if (skip)
1847 {
1848 /* When skipping just find the end of the name. */
1849 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001850 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001851 }
1852
1853 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001854 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001855 if (expr_start != NULL)
1856 {
1857 /* Don't expand the name when we already know there is an error. */
1858 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
1859 && *p != '[' && *p != '.')
1860 {
1861 EMSG(_(e_trailing));
1862 return NULL;
1863 }
1864
1865 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1866 if (lp->ll_exp_name == NULL)
1867 {
1868 /* Report an invalid expression in braces, unless the
1869 * expression evaluation has been cancelled due to an
1870 * aborting error, an interrupt, or an exception. */
1871 if (!aborting() && !quiet)
1872 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001873 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001874 EMSG2(_(e_invarg2), name);
1875 return NULL;
1876 }
1877 }
1878 lp->ll_name = lp->ll_exp_name;
1879 }
1880 else
1881 lp->ll_name = name;
1882
1883 /* Without [idx] or .key we are done. */
1884 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1885 return p;
1886
1887 cc = *p;
1888 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001889 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001890 if (v == NULL && !quiet)
1891 EMSG2(_(e_undefvar), lp->ll_name);
1892 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893 if (v == NULL)
1894 return NULL;
1895
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001896 /*
1897 * Loop until no more [idx] or .key is following.
1898 */
Bram Moolenaar33570922005-01-25 22:26:29 +00001899 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001900 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001902 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1903 && !(lp->ll_tv->v_type == VAR_DICT
1904 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001905 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001906 if (!quiet)
1907 EMSG(_("E689: Can only index a List or Dictionary"));
1908 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001909 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001910 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001911 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001912 if (!quiet)
1913 EMSG(_("E708: [:] must come last"));
1914 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001915 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001916
Bram Moolenaar8c711452005-01-14 21:53:12 +00001917 len = -1;
1918 if (*p == '.')
1919 {
1920 key = p + 1;
1921 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1922 ;
1923 if (len == 0)
1924 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001925 if (!quiet)
1926 EMSG(_(e_emptykey));
1927 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001928 }
1929 p = key + len;
1930 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001931 else
1932 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001933 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001934 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001935 if (*p == ':')
1936 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001937 else
1938 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001939 empty1 = FALSE;
1940 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001941 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001942 if (get_tv_string_chk(&var1) == NULL)
1943 {
1944 /* not a number or string */
1945 clear_tv(&var1);
1946 return NULL;
1947 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001948 }
1949
1950 /* Optionally get the second index [ :expr]. */
1951 if (*p == ':')
1952 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001953 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001954 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001955 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001956 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001957 if (!empty1)
1958 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001959 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001960 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001961 if (rettv != NULL && (rettv->v_type != VAR_LIST
1962 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001963 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001964 if (!quiet)
1965 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00001966 if (!empty1)
1967 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001968 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001969 }
1970 p = skipwhite(p + 1);
1971 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001972 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001973 else
1974 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001975 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001976 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
1977 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001978 if (!empty1)
1979 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001980 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001981 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001982 if (get_tv_string_chk(&var2) == NULL)
1983 {
1984 /* not a number or string */
1985 if (!empty1)
1986 clear_tv(&var1);
1987 clear_tv(&var2);
1988 return NULL;
1989 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001990 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001991 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001992 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001993 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001994 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001995
Bram Moolenaar8c711452005-01-14 21:53:12 +00001996 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001997 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001998 if (!quiet)
1999 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002000 if (!empty1)
2001 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002002 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002003 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002004 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002005 }
2006
2007 /* Skip to past ']'. */
2008 ++p;
2009 }
2010
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002011 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002012 {
2013 if (len == -1)
2014 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002015 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002016 key = get_tv_string_chk(&var1); /* is number or string */
2017 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002018 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002019 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002020 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002021 }
2022 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002023 lp->ll_list = NULL;
2024 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002025 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002026
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002027 /* When assigning to a scope dictionary check that a function and
2028 * variable name is valid (only variable name unless it is l: or
2029 * g: dictionary). Disallow overwriting a builtin function. */
2030 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002031 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002032 int prevval;
2033 int wrong;
2034
2035 if (len != -1)
2036 {
2037 prevval = key[len];
2038 key[len] = NUL;
2039 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002040 else
2041 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002042 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2043 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002044 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002045 || !valid_varname(key);
2046 if (len != -1)
2047 key[len] = prevval;
2048 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002049 return NULL;
2050 }
2051
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002052 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002053 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002054 /* Can't add "v:" variable. */
2055 if (lp->ll_dict == &vimvardict)
2056 {
2057 EMSG2(_(e_illvar), name);
2058 return NULL;
2059 }
2060
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002061 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002062 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002063 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002064 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002065 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002066 if (len == -1)
2067 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002068 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002069 }
2070 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002071 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002072 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002073 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002074 if (len == -1)
2075 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002076 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002077 p = NULL;
2078 break;
2079 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002080 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002081 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002082 return NULL;
2083
Bram Moolenaar8c711452005-01-14 21:53:12 +00002084 if (len == -1)
2085 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002086 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002087 }
2088 else
2089 {
2090 /*
2091 * Get the number and item for the only or first index of the List.
2092 */
2093 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002094 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002095 else
2096 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002097 lp->ll_n1 = (long)get_tv_number(&var1);
2098 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002099 clear_tv(&var1);
2100 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002101 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002102 lp->ll_list = lp->ll_tv->vval.v_list;
2103 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2104 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002105 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002106 if (lp->ll_n1 < 0)
2107 {
2108 lp->ll_n1 = 0;
2109 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2110 }
2111 }
2112 if (lp->ll_li == NULL)
2113 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002114 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002115 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002116 if (!quiet)
2117 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002118 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002119 }
2120
2121 /*
2122 * May need to find the item or absolute index for the second
2123 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002124 * When no index given: "lp->ll_empty2" is TRUE.
2125 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002126 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002127 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002128 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002129 lp->ll_n2 = (long)get_tv_number(&var2);
2130 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002131 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002132 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002133 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002134 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002135 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002136 {
2137 if (!quiet)
2138 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002139 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002140 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002141 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002142 }
2143
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002144 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2145 if (lp->ll_n1 < 0)
2146 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2147 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002148 {
2149 if (!quiet)
2150 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002151 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002152 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002153 }
2154
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002155 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002156 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002157 }
2158
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002159 return p;
2160}
2161
2162/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002163 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002164 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002165 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002166clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002167{
2168 vim_free(lp->ll_exp_name);
2169 vim_free(lp->ll_newkey);
2170}
2171
2172/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002173 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002174 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002175 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002176 */
2177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002178set_var_lval(
2179 lval_T *lp,
2180 char_u *endp,
2181 typval_T *rettv,
2182 int copy,
2183 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002184{
2185 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002186 listitem_T *ri;
2187 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002188
2189 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002190 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002191 cc = *endp;
2192 *endp = NUL;
2193 if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002194 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002195 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002196
Bram Moolenaar79518e22017-02-17 16:31:35 +01002197 /* handle +=, -= and .= */
2198 di = NULL;
2199 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2200 &tv, &di, TRUE, FALSE) == OK)
2201 {
2202 if ((di == NULL
2203 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2204 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2205 FALSE)))
2206 && tv_op(&tv, rettv, op) == OK)
2207 set_var(lp->ll_name, &tv, FALSE);
2208 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002209 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002211 else
2212 set_var(lp->ll_name, rettv, copy);
2213 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002214 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002215 else if (tv_check_lock(lp->ll_newkey == NULL
2216 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002217 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002218 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002219 else if (lp->ll_range)
2220 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002221 listitem_T *ll_li = lp->ll_li;
2222 int ll_n1 = lp->ll_n1;
2223
2224 /*
2225 * Check whether any of the list items is locked
2226 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002227 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002228 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002229 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002230 return;
2231 ri = ri->li_next;
2232 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2233 break;
2234 ll_li = ll_li->li_next;
2235 ++ll_n1;
2236 }
2237
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002238 /*
2239 * Assign the List values to the list items.
2240 */
2241 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002242 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002243 if (op != NULL && *op != '=')
2244 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2245 else
2246 {
2247 clear_tv(&lp->ll_li->li_tv);
2248 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2249 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002250 ri = ri->li_next;
2251 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2252 break;
2253 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002254 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002255 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002256 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002257 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002258 ri = NULL;
2259 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002260 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002261 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002262 lp->ll_li = lp->ll_li->li_next;
2263 ++lp->ll_n1;
2264 }
2265 if (ri != NULL)
2266 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002267 else if (lp->ll_empty2
2268 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002269 : lp->ll_n1 != lp->ll_n2)
2270 EMSG(_("E711: List value has not enough items"));
2271 }
2272 else
2273 {
2274 /*
2275 * Assign to a List or Dictionary item.
2276 */
2277 if (lp->ll_newkey != NULL)
2278 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002279 if (op != NULL && *op != '=')
2280 {
2281 EMSG2(_(e_letwrong), op);
2282 return;
2283 }
2284
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002285 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002286 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002287 if (di == NULL)
2288 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002289 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2290 {
2291 vim_free(di);
2292 return;
2293 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002294 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002295 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002296 else if (op != NULL && *op != '=')
2297 {
2298 tv_op(lp->ll_tv, rettv, op);
2299 return;
2300 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002301 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002302 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002303
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002304 /*
2305 * Assign the value to the variable or list item.
2306 */
2307 if (copy)
2308 copy_tv(rettv, lp->ll_tv);
2309 else
2310 {
2311 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002312 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002313 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002314 }
2315 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002316}
2317
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002318/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002319 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2320 * Returns OK or FAIL.
2321 */
2322 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002323tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002324{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002325 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002326 char_u numbuf[NUMBUFLEN];
2327 char_u *s;
2328
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002329 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2330 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2331 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002332 {
2333 switch (tv1->v_type)
2334 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002335 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002336 case VAR_DICT:
2337 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002338 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002339 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002340 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002341 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002342 break;
2343
2344 case VAR_LIST:
2345 if (*op != '+' || tv2->v_type != VAR_LIST)
2346 break;
2347 /* List += List */
2348 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2349 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2350 return OK;
2351
2352 case VAR_NUMBER:
2353 case VAR_STRING:
2354 if (tv2->v_type == VAR_LIST)
2355 break;
2356 if (*op == '+' || *op == '-')
2357 {
2358 /* nr += nr or nr -= nr*/
2359 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002360#ifdef FEAT_FLOAT
2361 if (tv2->v_type == VAR_FLOAT)
2362 {
2363 float_T f = n;
2364
2365 if (*op == '+')
2366 f += tv2->vval.v_float;
2367 else
2368 f -= tv2->vval.v_float;
2369 clear_tv(tv1);
2370 tv1->v_type = VAR_FLOAT;
2371 tv1->vval.v_float = f;
2372 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002373 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002374#endif
2375 {
2376 if (*op == '+')
2377 n += get_tv_number(tv2);
2378 else
2379 n -= get_tv_number(tv2);
2380 clear_tv(tv1);
2381 tv1->v_type = VAR_NUMBER;
2382 tv1->vval.v_number = n;
2383 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002384 }
2385 else
2386 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002387 if (tv2->v_type == VAR_FLOAT)
2388 break;
2389
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002390 /* str .= str */
2391 s = get_tv_string(tv1);
2392 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2393 clear_tv(tv1);
2394 tv1->v_type = VAR_STRING;
2395 tv1->vval.v_string = s;
2396 }
2397 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002398
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002399 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002400#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002401 {
2402 float_T f;
2403
2404 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2405 && tv2->v_type != VAR_NUMBER
2406 && tv2->v_type != VAR_STRING))
2407 break;
2408 if (tv2->v_type == VAR_FLOAT)
2409 f = tv2->vval.v_float;
2410 else
2411 f = get_tv_number(tv2);
2412 if (*op == '+')
2413 tv1->vval.v_float += f;
2414 else
2415 tv1->vval.v_float -= f;
2416 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002417#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002418 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002419 }
2420 }
2421
2422 EMSG2(_(e_letwrong), op);
2423 return FAIL;
2424}
2425
2426/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002427 * Evaluate the expression used in a ":for var in expr" command.
2428 * "arg" points to "var".
2429 * Set "*errp" to TRUE for an error, FALSE otherwise;
2430 * Return a pointer that holds the info. Null when there is an error.
2431 */
2432 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002433eval_for_line(
2434 char_u *arg,
2435 int *errp,
2436 char_u **nextcmdp,
2437 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002438{
Bram Moolenaar33570922005-01-25 22:26:29 +00002439 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002440 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002441 typval_T tv;
2442 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002443
2444 *errp = TRUE; /* default: there is an error */
2445
Bram Moolenaar33570922005-01-25 22:26:29 +00002446 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002447 if (fi == NULL)
2448 return NULL;
2449
2450 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2451 if (expr == NULL)
2452 return fi;
2453
2454 expr = skipwhite(expr);
2455 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2456 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002457 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002458 return fi;
2459 }
2460
2461 if (skip)
2462 ++emsg_skip;
2463 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2464 {
2465 *errp = FALSE;
2466 if (!skip)
2467 {
2468 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002469 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002470 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002471 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002472 clear_tv(&tv);
2473 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002474 else if (l == NULL)
2475 {
2476 /* a null list is like an empty list: do nothing */
2477 clear_tv(&tv);
2478 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002479 else
2480 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002481 /* No need to increment the refcount, it's already set for the
2482 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002483 fi->fi_list = l;
2484 list_add_watch(l, &fi->fi_lw);
2485 fi->fi_lw.lw_item = l->lv_first;
2486 }
2487 }
2488 }
2489 if (skip)
2490 --emsg_skip;
2491
2492 return fi;
2493}
2494
2495/*
2496 * Use the first item in a ":for" list. Advance to the next.
2497 * Assign the values to the variable (list). "arg" points to the first one.
2498 * Return TRUE when a valid item was found, FALSE when at end of list or
2499 * something wrong.
2500 */
2501 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002502next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002503{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002504 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002505 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002506 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002507
2508 item = fi->fi_lw.lw_item;
2509 if (item == NULL)
2510 result = FALSE;
2511 else
2512 {
2513 fi->fi_lw.lw_item = item->li_next;
2514 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2515 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2516 }
2517 return result;
2518}
2519
2520/*
2521 * Free the structure used to store info used by ":for".
2522 */
2523 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002524free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002525{
Bram Moolenaar33570922005-01-25 22:26:29 +00002526 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002527
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002528 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002529 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002530 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002531 list_unref(fi->fi_list);
2532 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002533 vim_free(fi);
2534}
2535
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2537
2538 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002539set_context_for_expression(
2540 expand_T *xp,
2541 char_u *arg,
2542 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543{
2544 int got_eq = FALSE;
2545 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002546 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002548 if (cmdidx == CMD_let)
2549 {
2550 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002551 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002552 {
2553 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002554 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002555 {
2556 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002557 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002558 if (vim_iswhite(*p))
2559 break;
2560 }
2561 return;
2562 }
2563 }
2564 else
2565 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2566 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 while ((xp->xp_pattern = vim_strpbrk(arg,
2568 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2569 {
2570 c = *xp->xp_pattern;
2571 if (c == '&')
2572 {
2573 c = xp->xp_pattern[1];
2574 if (c == '&')
2575 {
2576 ++xp->xp_pattern;
2577 xp->xp_context = cmdidx != CMD_let || got_eq
2578 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2579 }
2580 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002581 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002583 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2584 xp->xp_pattern += 2;
2585
2586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 }
2588 else if (c == '$')
2589 {
2590 /* environment variable */
2591 xp->xp_context = EXPAND_ENV_VARS;
2592 }
2593 else if (c == '=')
2594 {
2595 got_eq = TRUE;
2596 xp->xp_context = EXPAND_EXPRESSION;
2597 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002598 else if (c == '#'
2599 && xp->xp_context == EXPAND_EXPRESSION)
2600 {
2601 /* Autoload function/variable contains '#'. */
2602 break;
2603 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002604 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 && xp->xp_context == EXPAND_FUNCTIONS
2606 && vim_strchr(xp->xp_pattern, '(') == NULL)
2607 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002608 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 break;
2610 }
2611 else if (cmdidx != CMD_let || got_eq)
2612 {
2613 if (c == '"') /* string */
2614 {
2615 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2616 if (c == '\\' && xp->xp_pattern[1] != NUL)
2617 ++xp->xp_pattern;
2618 xp->xp_context = EXPAND_NOTHING;
2619 }
2620 else if (c == '\'') /* literal string */
2621 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002622 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2624 /* skip */ ;
2625 xp->xp_context = EXPAND_NOTHING;
2626 }
2627 else if (c == '|')
2628 {
2629 if (xp->xp_pattern[1] == '|')
2630 {
2631 ++xp->xp_pattern;
2632 xp->xp_context = EXPAND_EXPRESSION;
2633 }
2634 else
2635 xp->xp_context = EXPAND_COMMANDS;
2636 }
2637 else
2638 xp->xp_context = EXPAND_EXPRESSION;
2639 }
2640 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002641 /* Doesn't look like something valid, expand as an expression
2642 * anyway. */
2643 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 arg = xp->xp_pattern;
2645 if (*arg != NUL)
2646 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2647 /* skip */ ;
2648 }
2649 xp->xp_pattern = arg;
2650}
2651
2652#endif /* FEAT_CMDL_COMPL */
2653
2654/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 * ":unlet[!] var1 ... " command.
2656 */
2657 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002658ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002660 ex_unletlock(eap, eap->arg, 0);
2661}
2662
2663/*
2664 * ":lockvar" and ":unlockvar" commands
2665 */
2666 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002667ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002668{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002670 int deep = 2;
2671
2672 if (eap->forceit)
2673 deep = -1;
2674 else if (vim_isdigit(*arg))
2675 {
2676 deep = getdigits(&arg);
2677 arg = skipwhite(arg);
2678 }
2679
2680 ex_unletlock(eap, arg, deep);
2681}
2682
2683/*
2684 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2685 */
2686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002687ex_unletlock(
2688 exarg_T *eap,
2689 char_u *argstart,
2690 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002691{
2692 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002695 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696
2697 do
2698 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002700 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002701 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 if (lv.ll_name == NULL)
2703 error = TRUE; /* error but continue parsing */
2704 if (name_end == NULL || (!vim_iswhite(*name_end)
2705 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 if (name_end != NULL)
2708 {
2709 emsg_severe = TRUE;
2710 EMSG(_(e_trailing));
2711 }
2712 if (!(eap->skip || error))
2713 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 break;
2715 }
2716
2717 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002718 {
2719 if (eap->cmdidx == CMD_unlet)
2720 {
2721 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2722 error = TRUE;
2723 }
2724 else
2725 {
2726 if (do_lock_var(&lv, name_end, deep,
2727 eap->cmdidx == CMD_lockvar) == FAIL)
2728 error = TRUE;
2729 }
2730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 if (!eap->skip)
2733 clear_lval(&lv);
2734
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 arg = skipwhite(name_end);
2736 } while (!ends_excmd(*arg));
2737
2738 eap->nextcmd = check_nextcmd(arg);
2739}
2740
Bram Moolenaar8c711452005-01-14 21:53:12 +00002741 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002742do_unlet_var(
2743 lval_T *lp,
2744 char_u *name_end,
2745 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002746{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 int ret = OK;
2748 int cc;
2749
2750 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 cc = *name_end;
2753 *name_end = NUL;
2754
2755 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002756 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002757 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002760 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002761 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002762 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002763 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002764 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765 else if (lp->ll_range)
2766 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002767 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002768 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01002769 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002770
2771 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
2772 {
2773 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02002774 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002775 return FAIL;
2776 ll_li = li;
2777 ++ll_n1;
2778 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002779
2780 /* Delete a range of List items. */
2781 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2782 {
2783 li = lp->ll_li->li_next;
2784 listitem_remove(lp->ll_list, lp->ll_li);
2785 lp->ll_li = li;
2786 ++lp->ll_n1;
2787 }
2788 }
2789 else
2790 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 /* unlet a List item. */
2793 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002795 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002796 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797 }
2798
2799 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002800}
2801
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802/*
2803 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002804 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 */
2806 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002807do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808{
Bram Moolenaar33570922005-01-25 22:26:29 +00002809 hashtab_T *ht;
2810 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002811 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002812 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002813 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814
Bram Moolenaar33570922005-01-25 22:26:29 +00002815 ht = find_var_ht(name, &varname);
2816 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002818 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002819 if (d == NULL)
2820 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002821 if (ht == &globvarht)
2822 d = &globvardict;
2823 else if (ht == &compat_hashtab)
2824 d = &vimvardict;
2825 else
2826 {
2827 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2828 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2829 }
2830 if (d == NULL)
2831 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01002832 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002833 return FAIL;
2834 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002835 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002836 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002837 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02002838 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002839 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00002840 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002841 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02002842 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002843 || var_check_ro(di->di_flags, name, FALSE)
2844 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01002845 return FAIL;
2846
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002847 delete_var(ht, hi);
2848 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00002849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002851 if (forceit)
2852 return OK;
2853 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 return FAIL;
2855}
2856
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002857/*
2858 * Lock or unlock variable indicated by "lp".
2859 * "deep" is the levels to go (-1 for unlimited);
2860 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2861 */
2862 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002863do_lock_var(
2864 lval_T *lp,
2865 char_u *name_end,
2866 int deep,
2867 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002868{
2869 int ret = OK;
2870 int cc;
2871 dictitem_T *di;
2872
2873 if (deep == 0) /* nothing to do */
2874 return OK;
2875
2876 if (lp->ll_tv == NULL)
2877 {
2878 cc = *name_end;
2879 *name_end = NUL;
2880
2881 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002882 di = find_var(lp->ll_name, NULL, TRUE);
2883 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002884 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01002885 else if ((di->di_flags & DI_FLAGS_FIX)
2886 && di->di_tv.v_type != VAR_DICT
2887 && di->di_tv.v_type != VAR_LIST)
2888 /* For historic reasons this error is not given for a list or dict.
2889 * E.g., the b: dict could be locked/unlocked. */
2890 EMSG2(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002891 else
2892 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002893 if (lock)
2894 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002895 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01002896 di->di_flags &= ~DI_FLAGS_LOCK;
2897 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002898 }
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
3119 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003120 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003121 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003123 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003124 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003125 else
3126 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003127 while (HASHITEM_EMPTY(hi))
3128 ++hi;
3129 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003131
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003132#ifdef FEAT_WINDOWS
3133 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003134 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003135 if (tdone < ht->ht_used)
3136 {
3137 if (tdone++ == 0)
3138 hi = ht->ht_array;
3139 else
3140 ++hi;
3141 while (HASHITEM_EMPTY(hi))
3142 ++hi;
3143 return cat_prefix_varname('t', hi->hi_key);
3144 }
3145#endif
3146
Bram Moolenaar33570922005-01-25 22:26:29 +00003147 /* v: variables */
3148 if (vidx < VV_LEN)
3149 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150
3151 vim_free(varnamebuf);
3152 varnamebuf = NULL;
3153 varnamebuflen = 0;
3154 return NULL;
3155}
3156
3157#endif /* FEAT_CMDL_COMPL */
3158
3159/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003160 * Return TRUE if "pat" matches "text".
3161 * Does not use 'cpo' and always uses 'magic'.
3162 */
3163 static int
3164pattern_match(char_u *pat, char_u *text, int ic)
3165{
3166 int matches = FALSE;
3167 char_u *save_cpo;
3168 regmatch_T regmatch;
3169
3170 /* avoid 'l' flag in 'cpoptions' */
3171 save_cpo = p_cpo;
3172 p_cpo = (char_u *)"";
3173 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3174 if (regmatch.regprog != NULL)
3175 {
3176 regmatch.rm_ic = ic;
3177 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3178 vim_regfree(regmatch.regprog);
3179 }
3180 p_cpo = save_cpo;
3181 return matches;
3182}
3183
3184/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 * types for expressions.
3186 */
3187typedef enum
3188{
3189 TYPE_UNKNOWN = 0
3190 , TYPE_EQUAL /* == */
3191 , TYPE_NEQUAL /* != */
3192 , TYPE_GREATER /* > */
3193 , TYPE_GEQUAL /* >= */
3194 , TYPE_SMALLER /* < */
3195 , TYPE_SEQUAL /* <= */
3196 , TYPE_MATCH /* =~ */
3197 , TYPE_NOMATCH /* !~ */
3198} exptype_T;
3199
3200/*
3201 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003202 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3204 */
3205
3206/*
3207 * Handle zero level expression.
3208 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003209 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003210 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 * Return OK or FAIL.
3212 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003213 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003214eval0(
3215 char_u *arg,
3216 typval_T *rettv,
3217 char_u **nextcmd,
3218 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219{
3220 int ret;
3221 char_u *p;
3222
3223 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003224 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 if (ret == FAIL || !ends_excmd(*p))
3226 {
3227 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003228 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 /*
3230 * Report the invalid expression unless the expression evaluation has
3231 * been cancelled due to an aborting error, an interrupt, or an
3232 * exception.
3233 */
3234 if (!aborting())
3235 EMSG2(_(e_invexpr2), arg);
3236 ret = FAIL;
3237 }
3238 if (nextcmd != NULL)
3239 *nextcmd = check_nextcmd(p);
3240
3241 return ret;
3242}
3243
3244/*
3245 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003246 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 *
3248 * "arg" must point to the first non-white of the expression.
3249 * "arg" is advanced to the next non-white after the recognized expression.
3250 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003251 * Note: "rettv.v_lock" is not set.
3252 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 * Return OK or FAIL.
3254 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003255 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003256eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257{
3258 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003259 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260
3261 /*
3262 * Get the first variable.
3263 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003264 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 return FAIL;
3266
3267 if ((*arg)[0] == '?')
3268 {
3269 result = FALSE;
3270 if (evaluate)
3271 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003272 int error = FALSE;
3273
3274 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003276 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003277 if (error)
3278 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 }
3280
3281 /*
3282 * Get the second variable.
3283 */
3284 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003285 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 return FAIL;
3287
3288 /*
3289 * Check for the ":".
3290 */
3291 if ((*arg)[0] != ':')
3292 {
3293 EMSG(_("E109: Missing ':' after '?'"));
3294 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003295 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 return FAIL;
3297 }
3298
3299 /*
3300 * Get the third variable.
3301 */
3302 *arg = skipwhite(*arg + 1);
3303 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3304 {
3305 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003306 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 return FAIL;
3308 }
3309 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003310 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 }
3312
3313 return OK;
3314}
3315
3316/*
3317 * Handle first level expression:
3318 * expr2 || expr2 || expr2 logical OR
3319 *
3320 * "arg" must point to the first non-white of the expression.
3321 * "arg" is advanced to the next non-white after the recognized expression.
3322 *
3323 * Return OK or FAIL.
3324 */
3325 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003326eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327{
Bram Moolenaar33570922005-01-25 22:26:29 +00003328 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 long result;
3330 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003331 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332
3333 /*
3334 * Get the first variable.
3335 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003336 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 return FAIL;
3338
3339 /*
3340 * Repeat until there is no following "||".
3341 */
3342 first = TRUE;
3343 result = FALSE;
3344 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3345 {
3346 if (evaluate && first)
3347 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003348 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003350 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003351 if (error)
3352 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 first = FALSE;
3354 }
3355
3356 /*
3357 * Get the second variable.
3358 */
3359 *arg = skipwhite(*arg + 2);
3360 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3361 return FAIL;
3362
3363 /*
3364 * Compute the result.
3365 */
3366 if (evaluate && !result)
3367 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003368 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003370 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003371 if (error)
3372 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 }
3374 if (evaluate)
3375 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003376 rettv->v_type = VAR_NUMBER;
3377 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 }
3379 }
3380
3381 return OK;
3382}
3383
3384/*
3385 * Handle second level expression:
3386 * expr3 && expr3 && expr3 logical AND
3387 *
3388 * "arg" must point to the first non-white of the expression.
3389 * "arg" is advanced to the next non-white after the recognized expression.
3390 *
3391 * Return OK or FAIL.
3392 */
3393 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003394eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395{
Bram Moolenaar33570922005-01-25 22:26:29 +00003396 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 long result;
3398 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003399 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400
3401 /*
3402 * Get the first variable.
3403 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003404 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 return FAIL;
3406
3407 /*
3408 * Repeat until there is no following "&&".
3409 */
3410 first = TRUE;
3411 result = TRUE;
3412 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3413 {
3414 if (evaluate && first)
3415 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003416 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003418 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003419 if (error)
3420 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 first = FALSE;
3422 }
3423
3424 /*
3425 * Get the second variable.
3426 */
3427 *arg = skipwhite(*arg + 2);
3428 if (eval4(arg, &var2, evaluate && result) == FAIL)
3429 return FAIL;
3430
3431 /*
3432 * Compute the result.
3433 */
3434 if (evaluate && result)
3435 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003436 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003438 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003439 if (error)
3440 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 }
3442 if (evaluate)
3443 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003444 rettv->v_type = VAR_NUMBER;
3445 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 }
3447 }
3448
3449 return OK;
3450}
3451
3452/*
3453 * Handle third level expression:
3454 * var1 == var2
3455 * var1 =~ var2
3456 * var1 != var2
3457 * var1 !~ var2
3458 * var1 > var2
3459 * var1 >= var2
3460 * var1 < var2
3461 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003462 * var1 is var2
3463 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 *
3465 * "arg" must point to the first non-white of the expression.
3466 * "arg" is advanced to the next non-white after the recognized expression.
3467 *
3468 * Return OK or FAIL.
3469 */
3470 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003471eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472{
Bram Moolenaar33570922005-01-25 22:26:29 +00003473 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 char_u *p;
3475 int i;
3476 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003477 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 int len = 2;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003479 varnumber_T n1, n2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 char_u *s1, *s2;
3481 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483
3484 /*
3485 * Get the first variable.
3486 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003487 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 return FAIL;
3489
3490 p = *arg;
3491 switch (p[0])
3492 {
3493 case '=': if (p[1] == '=')
3494 type = TYPE_EQUAL;
3495 else if (p[1] == '~')
3496 type = TYPE_MATCH;
3497 break;
3498 case '!': if (p[1] == '=')
3499 type = TYPE_NEQUAL;
3500 else if (p[1] == '~')
3501 type = TYPE_NOMATCH;
3502 break;
3503 case '>': if (p[1] != '=')
3504 {
3505 type = TYPE_GREATER;
3506 len = 1;
3507 }
3508 else
3509 type = TYPE_GEQUAL;
3510 break;
3511 case '<': if (p[1] != '=')
3512 {
3513 type = TYPE_SMALLER;
3514 len = 1;
3515 }
3516 else
3517 type = TYPE_SEQUAL;
3518 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003519 case 'i': if (p[1] == 's')
3520 {
3521 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3522 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003523 i = p[len];
3524 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003525 {
3526 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3527 type_is = TRUE;
3528 }
3529 }
3530 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 }
3532
3533 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003534 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 */
3536 if (type != TYPE_UNKNOWN)
3537 {
3538 /* extra question mark appended: ignore case */
3539 if (p[len] == '?')
3540 {
3541 ic = TRUE;
3542 ++len;
3543 }
3544 /* extra '#' appended: match case */
3545 else if (p[len] == '#')
3546 {
3547 ic = FALSE;
3548 ++len;
3549 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003550 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 else
3552 ic = p_ic;
3553
3554 /*
3555 * Get the second variable.
3556 */
3557 *arg = skipwhite(p + len);
3558 if (eval5(arg, &var2, evaluate) == FAIL)
3559 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003560 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 return FAIL;
3562 }
3563
3564 if (evaluate)
3565 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003566 if (type_is && rettv->v_type != var2.v_type)
3567 {
3568 /* For "is" a different type always means FALSE, for "notis"
3569 * it means TRUE. */
3570 n1 = (type == TYPE_NEQUAL);
3571 }
3572 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3573 {
3574 if (type_is)
3575 {
3576 n1 = (rettv->v_type == var2.v_type
3577 && rettv->vval.v_list == var2.vval.v_list);
3578 if (type == TYPE_NEQUAL)
3579 n1 = !n1;
3580 }
3581 else if (rettv->v_type != var2.v_type
3582 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3583 {
3584 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003585 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003586 else
Bram Moolenaar59838522014-05-13 13:46:33 +02003587 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003588 clear_tv(rettv);
3589 clear_tv(&var2);
3590 return FAIL;
3591 }
3592 else
3593 {
3594 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003595 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
3596 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003597 if (type == TYPE_NEQUAL)
3598 n1 = !n1;
3599 }
3600 }
3601
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003602 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3603 {
3604 if (type_is)
3605 {
3606 n1 = (rettv->v_type == var2.v_type
3607 && rettv->vval.v_dict == var2.vval.v_dict);
3608 if (type == TYPE_NEQUAL)
3609 n1 = !n1;
3610 }
3611 else if (rettv->v_type != var2.v_type
3612 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3613 {
3614 if (rettv->v_type != var2.v_type)
3615 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3616 else
3617 EMSG(_("E736: Invalid operation for Dictionary"));
3618 clear_tv(rettv);
3619 clear_tv(&var2);
3620 return FAIL;
3621 }
3622 else
3623 {
3624 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003625 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
3626 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003627 if (type == TYPE_NEQUAL)
3628 n1 = !n1;
3629 }
3630 }
3631
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003632 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
3633 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003634 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003635 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003636 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003637 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003638 clear_tv(rettv);
3639 clear_tv(&var2);
3640 return FAIL;
3641 }
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003642 if ((rettv->v_type == VAR_PARTIAL
3643 && rettv->vval.v_partial == NULL)
3644 || (var2.v_type == VAR_PARTIAL
3645 && var2.vval.v_partial == NULL))
3646 /* when a partial is NULL assume not equal */
3647 n1 = FALSE;
3648 else if (type_is)
3649 {
3650 if (rettv->v_type == VAR_FUNC && var2.v_type == VAR_FUNC)
3651 /* strings are considered the same if their value is
3652 * the same */
3653 n1 = tv_equal(rettv, &var2, ic, FALSE);
3654 else if (rettv->v_type == VAR_PARTIAL
3655 && var2.v_type == VAR_PARTIAL)
3656 n1 = (rettv->vval.v_partial == var2.vval.v_partial);
3657 else
3658 n1 = FALSE;
3659 }
3660 else
3661 n1 = tv_equal(rettv, &var2, ic, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003662 if (type == TYPE_NEQUAL)
3663 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003664 }
3665
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003666#ifdef FEAT_FLOAT
3667 /*
3668 * If one of the two variables is a float, compare as a float.
3669 * When using "=~" or "!~", always compare as string.
3670 */
3671 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3672 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3673 {
3674 float_T f1, f2;
3675
3676 if (rettv->v_type == VAR_FLOAT)
3677 f1 = rettv->vval.v_float;
3678 else
3679 f1 = get_tv_number(rettv);
3680 if (var2.v_type == VAR_FLOAT)
3681 f2 = var2.vval.v_float;
3682 else
3683 f2 = get_tv_number(&var2);
3684 n1 = FALSE;
3685 switch (type)
3686 {
3687 case TYPE_EQUAL: n1 = (f1 == f2); break;
3688 case TYPE_NEQUAL: n1 = (f1 != f2); break;
3689 case TYPE_GREATER: n1 = (f1 > f2); break;
3690 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
3691 case TYPE_SMALLER: n1 = (f1 < f2); break;
3692 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
3693 case TYPE_UNKNOWN:
3694 case TYPE_MATCH:
3695 case TYPE_NOMATCH: break; /* avoid gcc warning */
3696 }
3697 }
3698#endif
3699
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 /*
3701 * If one of the two variables is a number, compare as a number.
3702 * When using "=~" or "!~", always compare as string.
3703 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003704 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3706 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003707 n1 = get_tv_number(rettv);
3708 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 switch (type)
3710 {
3711 case TYPE_EQUAL: n1 = (n1 == n2); break;
3712 case TYPE_NEQUAL: n1 = (n1 != n2); break;
3713 case TYPE_GREATER: n1 = (n1 > n2); break;
3714 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
3715 case TYPE_SMALLER: n1 = (n1 < n2); break;
3716 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
3717 case TYPE_UNKNOWN:
3718 case TYPE_MATCH:
3719 case TYPE_NOMATCH: break; /* avoid gcc warning */
3720 }
3721 }
3722 else
3723 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003724 s1 = get_tv_string_buf(rettv, buf1);
3725 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
3727 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
3728 else
3729 i = 0;
3730 n1 = FALSE;
3731 switch (type)
3732 {
3733 case TYPE_EQUAL: n1 = (i == 0); break;
3734 case TYPE_NEQUAL: n1 = (i != 0); break;
3735 case TYPE_GREATER: n1 = (i > 0); break;
3736 case TYPE_GEQUAL: n1 = (i >= 0); break;
3737 case TYPE_SMALLER: n1 = (i < 0); break;
3738 case TYPE_SEQUAL: n1 = (i <= 0); break;
3739
3740 case TYPE_MATCH:
3741 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003742 n1 = pattern_match(s2, s1, ic);
3743 if (type == TYPE_NOMATCH)
3744 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 break;
3746
3747 case TYPE_UNKNOWN: break; /* avoid gcc warning */
3748 }
3749 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003750 clear_tv(rettv);
3751 clear_tv(&var2);
3752 rettv->v_type = VAR_NUMBER;
3753 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 }
3755 }
3756
3757 return OK;
3758}
3759
3760/*
3761 * Handle fourth level expression:
3762 * + number addition
3763 * - number subtraction
3764 * . string concatenation
3765 *
3766 * "arg" must point to the first non-white of the expression.
3767 * "arg" is advanced to the next non-white after the recognized expression.
3768 *
3769 * Return OK or FAIL.
3770 */
3771 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003772eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773{
Bram Moolenaar33570922005-01-25 22:26:29 +00003774 typval_T var2;
3775 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003777 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003778#ifdef FEAT_FLOAT
3779 float_T f1 = 0, f2 = 0;
3780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 char_u *s1, *s2;
3782 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3783 char_u *p;
3784
3785 /*
3786 * Get the first variable.
3787 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003788 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 return FAIL;
3790
3791 /*
3792 * Repeat computing, until no '+', '-' or '.' is following.
3793 */
3794 for (;;)
3795 {
3796 op = **arg;
3797 if (op != '+' && op != '-' && op != '.')
3798 break;
3799
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003800 if ((op != '+' || rettv->v_type != VAR_LIST)
3801#ifdef FEAT_FLOAT
3802 && (op == '.' || rettv->v_type != VAR_FLOAT)
3803#endif
3804 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003805 {
3806 /* For "list + ...", an illegal use of the first operand as
3807 * a number cannot be determined before evaluating the 2nd
3808 * operand: if this is also a list, all is ok.
3809 * For "something . ...", "something - ..." or "non-list + ...",
3810 * we know that the first operand needs to be a string or number
3811 * without evaluating the 2nd operand. So check before to avoid
3812 * side effects after an error. */
3813 if (evaluate && get_tv_string_chk(rettv) == NULL)
3814 {
3815 clear_tv(rettv);
3816 return FAIL;
3817 }
3818 }
3819
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 /*
3821 * Get the second variable.
3822 */
3823 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003824 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003826 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 return FAIL;
3828 }
3829
3830 if (evaluate)
3831 {
3832 /*
3833 * Compute the result.
3834 */
3835 if (op == '.')
3836 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003837 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
3838 s2 = get_tv_string_buf_chk(&var2, buf2);
3839 if (s2 == NULL) /* type error ? */
3840 {
3841 clear_tv(rettv);
3842 clear_tv(&var2);
3843 return FAIL;
3844 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003845 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003846 clear_tv(rettv);
3847 rettv->v_type = VAR_STRING;
3848 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003850 else if (op == '+' && rettv->v_type == VAR_LIST
3851 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003852 {
3853 /* concatenate Lists */
3854 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3855 &var3) == FAIL)
3856 {
3857 clear_tv(rettv);
3858 clear_tv(&var2);
3859 return FAIL;
3860 }
3861 clear_tv(rettv);
3862 *rettv = var3;
3863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 else
3865 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003866 int error = FALSE;
3867
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003868#ifdef FEAT_FLOAT
3869 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003870 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003871 f1 = rettv->vval.v_float;
3872 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003873 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003874 else
3875#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003876 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003877 n1 = get_tv_number_chk(rettv, &error);
3878 if (error)
3879 {
3880 /* This can only happen for "list + non-list". For
3881 * "non-list + ..." or "something - ...", we returned
3882 * before evaluating the 2nd operand. */
3883 clear_tv(rettv);
3884 return FAIL;
3885 }
3886#ifdef FEAT_FLOAT
3887 if (var2.v_type == VAR_FLOAT)
3888 f1 = n1;
3889#endif
3890 }
3891#ifdef FEAT_FLOAT
3892 if (var2.v_type == VAR_FLOAT)
3893 {
3894 f2 = var2.vval.v_float;
3895 n2 = 0;
3896 }
3897 else
3898#endif
3899 {
3900 n2 = get_tv_number_chk(&var2, &error);
3901 if (error)
3902 {
3903 clear_tv(rettv);
3904 clear_tv(&var2);
3905 return FAIL;
3906 }
3907#ifdef FEAT_FLOAT
3908 if (rettv->v_type == VAR_FLOAT)
3909 f2 = n2;
3910#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003911 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003912 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003913
3914#ifdef FEAT_FLOAT
3915 /* If there is a float on either side the result is a float. */
3916 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3917 {
3918 if (op == '+')
3919 f1 = f1 + f2;
3920 else
3921 f1 = f1 - f2;
3922 rettv->v_type = VAR_FLOAT;
3923 rettv->vval.v_float = f1;
3924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003926#endif
3927 {
3928 if (op == '+')
3929 n1 = n1 + n2;
3930 else
3931 n1 = n1 - n2;
3932 rettv->v_type = VAR_NUMBER;
3933 rettv->vval.v_number = n1;
3934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003936 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 }
3938 }
3939 return OK;
3940}
3941
3942/*
3943 * Handle fifth level expression:
3944 * * number multiplication
3945 * / number division
3946 * % number modulo
3947 *
3948 * "arg" must point to the first non-white of the expression.
3949 * "arg" is advanced to the next non-white after the recognized expression.
3950 *
3951 * Return OK or FAIL.
3952 */
3953 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003954eval6(
3955 char_u **arg,
3956 typval_T *rettv,
3957 int evaluate,
3958 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959{
Bram Moolenaar33570922005-01-25 22:26:29 +00003960 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003962 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003963#ifdef FEAT_FLOAT
3964 int use_float = FALSE;
3965 float_T f1 = 0, f2;
3966#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003967 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968
3969 /*
3970 * Get the first variable.
3971 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003972 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 return FAIL;
3974
3975 /*
3976 * Repeat computing, until no '*', '/' or '%' is following.
3977 */
3978 for (;;)
3979 {
3980 op = **arg;
3981 if (op != '*' && op != '/' && op != '%')
3982 break;
3983
3984 if (evaluate)
3985 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003986#ifdef FEAT_FLOAT
3987 if (rettv->v_type == VAR_FLOAT)
3988 {
3989 f1 = rettv->vval.v_float;
3990 use_float = TRUE;
3991 n1 = 0;
3992 }
3993 else
3994#endif
3995 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003996 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003997 if (error)
3998 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 }
4000 else
4001 n1 = 0;
4002
4003 /*
4004 * Get the second variable.
4005 */
4006 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004007 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 return FAIL;
4009
4010 if (evaluate)
4011 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004012#ifdef FEAT_FLOAT
4013 if (var2.v_type == VAR_FLOAT)
4014 {
4015 if (!use_float)
4016 {
4017 f1 = n1;
4018 use_float = TRUE;
4019 }
4020 f2 = var2.vval.v_float;
4021 n2 = 0;
4022 }
4023 else
4024#endif
4025 {
4026 n2 = get_tv_number_chk(&var2, &error);
4027 clear_tv(&var2);
4028 if (error)
4029 return FAIL;
4030#ifdef FEAT_FLOAT
4031 if (use_float)
4032 f2 = n2;
4033#endif
4034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035
4036 /*
4037 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004038 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004040#ifdef FEAT_FLOAT
4041 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004043 if (op == '*')
4044 f1 = f1 * f2;
4045 else if (op == '/')
4046 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004047# ifdef VMS
4048 /* VMS crashes on divide by zero, work around it */
4049 if (f2 == 0.0)
4050 {
4051 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004052 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004053 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004054 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004055 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004056 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004057 }
4058 else
4059 f1 = f1 / f2;
4060# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004061 /* We rely on the floating point library to handle divide
4062 * by zero to result in "inf" and not a crash. */
4063 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004064# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004067 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004068 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004069 return FAIL;
4070 }
4071 rettv->v_type = VAR_FLOAT;
4072 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 }
4074 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004077 if (op == '*')
4078 n1 = n1 * n2;
4079 else if (op == '/')
4080 {
4081 if (n2 == 0) /* give an error message? */
4082 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004083 if (n1 == 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004084 n1 = VARNUM_MIN; /* similar to NaN */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004085 else if (n1 < 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004086 n1 = -VARNUM_MAX;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004087 else
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004088 n1 = VARNUM_MAX;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004089 }
4090 else
4091 n1 = n1 / n2;
4092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004094 {
4095 if (n2 == 0) /* give an error message? */
4096 n1 = 0;
4097 else
4098 n1 = n1 % n2;
4099 }
4100 rettv->v_type = VAR_NUMBER;
4101 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 }
4104 }
4105
4106 return OK;
4107}
4108
4109/*
4110 * Handle sixth level expression:
4111 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004112 * "string" string constant
4113 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 * &option-name option value
4115 * @r register contents
4116 * identifier variable value
4117 * function() function call
4118 * $VAR environment variable
4119 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004120 * [expr, expr] List
4121 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 *
4123 * Also handle:
4124 * ! in front logical NOT
4125 * - in front unary minus
4126 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004127 * trailing [] subscript in String or List
4128 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 *
4130 * "arg" must point to the first non-white of the expression.
4131 * "arg" is advanced to the next non-white after the recognized expression.
4132 *
4133 * Return OK or FAIL.
4134 */
4135 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004136eval7(
4137 char_u **arg,
4138 typval_T *rettv,
4139 int evaluate,
4140 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004142 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 int len;
4144 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 char_u *start_leader, *end_leader;
4146 int ret = OK;
4147 char_u *alias;
4148
4149 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004150 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004151 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004153 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154
4155 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004156 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 */
4158 start_leader = *arg;
4159 while (**arg == '!' || **arg == '-' || **arg == '+')
4160 *arg = skipwhite(*arg + 1);
4161 end_leader = *arg;
4162
4163 switch (**arg)
4164 {
4165 /*
4166 * Number constant.
4167 */
4168 case '0':
4169 case '1':
4170 case '2':
4171 case '3':
4172 case '4':
4173 case '5':
4174 case '6':
4175 case '7':
4176 case '8':
4177 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004178 {
4179#ifdef FEAT_FLOAT
4180 char_u *p = skipdigits(*arg + 1);
4181 int get_float = FALSE;
4182
4183 /* We accept a float when the format matches
4184 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004185 * strict to avoid backwards compatibility problems.
4186 * Don't look for a float after the "." operator, so that
4187 * ":let vers = 1.2.3" doesn't fail. */
4188 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004190 get_float = TRUE;
4191 p = skipdigits(p + 2);
4192 if (*p == 'e' || *p == 'E')
4193 {
4194 ++p;
4195 if (*p == '-' || *p == '+')
4196 ++p;
4197 if (!vim_isdigit(*p))
4198 get_float = FALSE;
4199 else
4200 p = skipdigits(p + 1);
4201 }
4202 if (ASCII_ISALPHA(*p) || *p == '.')
4203 get_float = FALSE;
4204 }
4205 if (get_float)
4206 {
4207 float_T f;
4208
4209 *arg += string2float(*arg, &f);
4210 if (evaluate)
4211 {
4212 rettv->v_type = VAR_FLOAT;
4213 rettv->vval.v_float = f;
4214 }
4215 }
4216 else
4217#endif
4218 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01004219 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004220 *arg += len;
4221 if (evaluate)
4222 {
4223 rettv->v_type = VAR_NUMBER;
4224 rettv->vval.v_number = n;
4225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 }
4227 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229
4230 /*
4231 * String constant: "string".
4232 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004233 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 break;
4235
4236 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004237 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004239 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004240 break;
4241
4242 /*
4243 * List: [expr, expr]
4244 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004245 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 break;
4247
4248 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004249 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004250 * Dictionary: {key: val, key: val}
4251 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004252 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4253 if (ret == NOTDONE)
4254 ret = get_dict_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004255 break;
4256
4257 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004258 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004260 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 break;
4262
4263 /*
4264 * Environment variable: $VAR.
4265 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004266 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 break;
4268
4269 /*
4270 * Register contents: @r.
4271 */
4272 case '@': ++*arg;
4273 if (evaluate)
4274 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004275 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004276 rettv->vval.v_string = get_reg_contents(**arg,
4277 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 }
4279 if (**arg != NUL)
4280 ++*arg;
4281 break;
4282
4283 /*
4284 * nested expression: (expression).
4285 */
4286 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004287 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 if (**arg == ')')
4289 ++*arg;
4290 else if (ret == OK)
4291 {
4292 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004293 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 ret = FAIL;
4295 }
4296 break;
4297
Bram Moolenaar8c711452005-01-14 21:53:12 +00004298 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 break;
4300 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004301
4302 if (ret == NOTDONE)
4303 {
4304 /*
4305 * Must be a variable or function name.
4306 * Can also be a curly-braces kind of name: {expr}.
4307 */
4308 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004309 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004310 if (alias != NULL)
4311 s = alias;
4312
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004313 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004314 ret = FAIL;
4315 else
4316 {
4317 if (**arg == '(') /* recursive! */
4318 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004319 partial_T *partial;
4320
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004321 if (!evaluate)
4322 check_vars(s, len);
4323
Bram Moolenaar8c711452005-01-14 21:53:12 +00004324 /* If "s" is the name of a variable of type VAR_FUNC
4325 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004326 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004327
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004328 /* Need to make a copy, in case evaluating the arguments makes
4329 * the name invalid. */
4330 s = vim_strsave(s);
4331 if (s == NULL)
4332 ret = FAIL;
4333 else
4334 /* Invoke the function. */
4335 ret = get_func_tv(s, len, rettv, arg,
4336 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4337 &len, evaluate, partial, NULL);
4338 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004339
4340 /* If evaluate is FALSE rettv->v_type was not set in
4341 * get_func_tv, but it's needed in handle_subscript() to parse
4342 * what follows. So set it here. */
4343 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4344 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004345 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004346 rettv->v_type = VAR_FUNC;
4347 }
4348
Bram Moolenaar8c711452005-01-14 21:53:12 +00004349 /* Stop the expression evaluation when immediately
4350 * aborting on error, or when an interrupt occurred or
4351 * an exception was thrown but not caught. */
4352 if (aborting())
4353 {
4354 if (ret == OK)
4355 clear_tv(rettv);
4356 ret = FAIL;
4357 }
4358 }
4359 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004360 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004361 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004362 {
4363 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004364 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004365 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004366 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004367 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004368 }
4369
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 *arg = skipwhite(*arg);
4371
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004372 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4373 * expr(expr). */
4374 if (ret == OK)
4375 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376
4377 /*
4378 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4379 */
4380 if (ret == OK && evaluate && end_leader > start_leader)
4381 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004382 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004383 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004384#ifdef FEAT_FLOAT
4385 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004386
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004387 if (rettv->v_type == VAR_FLOAT)
4388 f = rettv->vval.v_float;
4389 else
4390#endif
4391 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004392 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004394 clear_tv(rettv);
4395 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004397 else
4398 {
4399 while (end_leader > start_leader)
4400 {
4401 --end_leader;
4402 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004403 {
4404#ifdef FEAT_FLOAT
4405 if (rettv->v_type == VAR_FLOAT)
4406 f = !f;
4407 else
4408#endif
4409 val = !val;
4410 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004411 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004412 {
4413#ifdef FEAT_FLOAT
4414 if (rettv->v_type == VAR_FLOAT)
4415 f = -f;
4416 else
4417#endif
4418 val = -val;
4419 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004420 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004421#ifdef FEAT_FLOAT
4422 if (rettv->v_type == VAR_FLOAT)
4423 {
4424 clear_tv(rettv);
4425 rettv->vval.v_float = f;
4426 }
4427 else
4428#endif
4429 {
4430 clear_tv(rettv);
4431 rettv->v_type = VAR_NUMBER;
4432 rettv->vval.v_number = val;
4433 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 }
4436
4437 return ret;
4438}
4439
4440/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004441 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4442 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004443 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4444 */
4445 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004446eval_index(
4447 char_u **arg,
4448 typval_T *rettv,
4449 int evaluate,
4450 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004451{
4452 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004453 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004454 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004455 long len = -1;
4456 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004457 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004458 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004459
Bram Moolenaara03f2332016-02-06 18:09:59 +01004460 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004461 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004462 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004463 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004464 if (verbose)
4465 EMSG(_("E695: Cannot index a Funcref"));
4466 return FAIL;
4467 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004468#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004469 if (verbose)
4470 EMSG(_(e_float_as_string));
4471 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004472#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004473 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004474 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004475 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004476 if (verbose)
4477 EMSG(_("E909: Cannot index a special variable"));
4478 return FAIL;
4479 case VAR_UNKNOWN:
4480 if (evaluate)
4481 return FAIL;
4482 /* FALLTHROUGH */
4483
4484 case VAR_STRING:
4485 case VAR_NUMBER:
4486 case VAR_LIST:
4487 case VAR_DICT:
4488 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004489 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004490
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004491 init_tv(&var1);
4492 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004493 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004494 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004495 /*
4496 * dict.name
4497 */
4498 key = *arg + 1;
4499 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4500 ;
4501 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004502 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004503 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004504 }
4505 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004506 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004507 /*
4508 * something[idx]
4509 *
4510 * Get the (first) variable from inside the [].
4511 */
4512 *arg = skipwhite(*arg + 1);
4513 if (**arg == ':')
4514 empty1 = TRUE;
4515 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4516 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004517 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4518 {
4519 /* not a number or string */
4520 clear_tv(&var1);
4521 return FAIL;
4522 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004523
4524 /*
4525 * Get the second variable from inside the [:].
4526 */
4527 if (**arg == ':')
4528 {
4529 range = TRUE;
4530 *arg = skipwhite(*arg + 1);
4531 if (**arg == ']')
4532 empty2 = TRUE;
4533 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4534 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004535 if (!empty1)
4536 clear_tv(&var1);
4537 return FAIL;
4538 }
4539 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4540 {
4541 /* not a number or string */
4542 if (!empty1)
4543 clear_tv(&var1);
4544 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004545 return FAIL;
4546 }
4547 }
4548
4549 /* Check for the ']'. */
4550 if (**arg != ']')
4551 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004552 if (verbose)
4553 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004554 clear_tv(&var1);
4555 if (range)
4556 clear_tv(&var2);
4557 return FAIL;
4558 }
4559 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004560 }
4561
4562 if (evaluate)
4563 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004564 n1 = 0;
4565 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004566 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004567 n1 = get_tv_number(&var1);
4568 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004569 }
4570 if (range)
4571 {
4572 if (empty2)
4573 n2 = -1;
4574 else
4575 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004576 n2 = get_tv_number(&var2);
4577 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004578 }
4579 }
4580
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004581 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004582 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004583 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004584 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004585 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004586 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004587 case VAR_SPECIAL:
4588 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004589 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004590 break; /* not evaluating, skipping over subscript */
4591
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004592 case VAR_NUMBER:
4593 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004594 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004595 len = (long)STRLEN(s);
4596 if (range)
4597 {
4598 /* The resulting variable is a substring. If the indexes
4599 * are out of range the result is empty. */
4600 if (n1 < 0)
4601 {
4602 n1 = len + n1;
4603 if (n1 < 0)
4604 n1 = 0;
4605 }
4606 if (n2 < 0)
4607 n2 = len + n2;
4608 else if (n2 >= len)
4609 n2 = len;
4610 if (n1 >= len || n2 < 0 || n1 > n2)
4611 s = NULL;
4612 else
4613 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4614 }
4615 else
4616 {
4617 /* The resulting variable is a string of a single
4618 * character. If the index is too big or negative the
4619 * result is empty. */
4620 if (n1 >= len || n1 < 0)
4621 s = NULL;
4622 else
4623 s = vim_strnsave(s + n1, 1);
4624 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004625 clear_tv(rettv);
4626 rettv->v_type = VAR_STRING;
4627 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004628 break;
4629
4630 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004631 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004632 if (n1 < 0)
4633 n1 = len + n1;
4634 if (!empty1 && (n1 < 0 || n1 >= len))
4635 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004636 /* For a range we allow invalid values and return an empty
4637 * list. A list index out of range is an error. */
4638 if (!range)
4639 {
4640 if (verbose)
4641 EMSGN(_(e_listidx), n1);
4642 return FAIL;
4643 }
4644 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004645 }
4646 if (range)
4647 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004648 list_T *l;
4649 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004650
4651 if (n2 < 0)
4652 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004653 else if (n2 >= len)
4654 n2 = len - 1;
4655 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004656 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004657 l = list_alloc();
4658 if (l == NULL)
4659 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004660 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004661 n1 <= n2; ++n1)
4662 {
4663 if (list_append_tv(l, &item->li_tv) == FAIL)
4664 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004665 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004666 return FAIL;
4667 }
4668 item = item->li_next;
4669 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004670 clear_tv(rettv);
4671 rettv->v_type = VAR_LIST;
4672 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004673 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004674 }
4675 else
4676 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004677 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004678 clear_tv(rettv);
4679 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004680 }
4681 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004682
4683 case VAR_DICT:
4684 if (range)
4685 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004686 if (verbose)
4687 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004688 if (len == -1)
4689 clear_tv(&var1);
4690 return FAIL;
4691 }
4692 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004693 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004694
4695 if (len == -1)
4696 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02004697 key = get_tv_string_chk(&var1);
4698 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004699 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004700 clear_tv(&var1);
4701 return FAIL;
4702 }
4703 }
4704
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004705 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004706
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004707 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004708 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004709 if (len == -1)
4710 clear_tv(&var1);
4711 if (item == NULL)
4712 return FAIL;
4713
4714 copy_tv(&item->di_tv, &var1);
4715 clear_tv(rettv);
4716 *rettv = var1;
4717 }
4718 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004719 }
4720 }
4721
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004722 return OK;
4723}
4724
4725/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 * Get an option value.
4727 * "arg" points to the '&' or '+' before the option name.
4728 * "arg" is advanced to character after the option name.
4729 * Return OK or FAIL.
4730 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004731 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004732get_option_tv(
4733 char_u **arg,
4734 typval_T *rettv, /* when NULL, only check if option exists */
4735 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736{
4737 char_u *option_end;
4738 long numval;
4739 char_u *stringval;
4740 int opt_type;
4741 int c;
4742 int working = (**arg == '+'); /* has("+option") */
4743 int ret = OK;
4744 int opt_flags;
4745
4746 /*
4747 * Isolate the option name and find its value.
4748 */
4749 option_end = find_option_end(arg, &opt_flags);
4750 if (option_end == NULL)
4751 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004752 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 EMSG2(_("E112: Option name missing: %s"), *arg);
4754 return FAIL;
4755 }
4756
4757 if (!evaluate)
4758 {
4759 *arg = option_end;
4760 return OK;
4761 }
4762
4763 c = *option_end;
4764 *option_end = NUL;
4765 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004766 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767
4768 if (opt_type == -3) /* invalid name */
4769 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004770 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 EMSG2(_("E113: Unknown option: %s"), *arg);
4772 ret = FAIL;
4773 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004774 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 {
4776 if (opt_type == -2) /* hidden string option */
4777 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004778 rettv->v_type = VAR_STRING;
4779 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 }
4781 else if (opt_type == -1) /* hidden number option */
4782 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004783 rettv->v_type = VAR_NUMBER;
4784 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 }
4786 else if (opt_type == 1) /* number option */
4787 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004788 rettv->v_type = VAR_NUMBER;
4789 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 }
4791 else /* string option */
4792 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004793 rettv->v_type = VAR_STRING;
4794 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 }
4796 }
4797 else if (working && (opt_type == -2 || opt_type == -1))
4798 ret = FAIL;
4799
4800 *option_end = c; /* put back for error messages */
4801 *arg = option_end;
4802
4803 return ret;
4804}
4805
4806/*
4807 * Allocate a variable for a string constant.
4808 * Return OK or FAIL.
4809 */
4810 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004811get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812{
4813 char_u *p;
4814 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 int extra = 0;
4816
4817 /*
4818 * Find the end of the string, skipping backslashed characters.
4819 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004820 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 {
4822 if (*p == '\\' && p[1] != NUL)
4823 {
4824 ++p;
4825 /* A "\<x>" form occupies at least 4 characters, and produces up
4826 * to 6 characters: reserve space for 2 extra */
4827 if (*p == '<')
4828 extra += 2;
4829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 }
4831
4832 if (*p != '"')
4833 {
4834 EMSG2(_("E114: Missing quote: %s"), *arg);
4835 return FAIL;
4836 }
4837
4838 /* If only parsing, set *arg and return here */
4839 if (!evaluate)
4840 {
4841 *arg = p + 1;
4842 return OK;
4843 }
4844
4845 /*
4846 * Copy the string into allocated memory, handling backslashed
4847 * characters.
4848 */
4849 name = alloc((unsigned)(p - *arg + extra));
4850 if (name == NULL)
4851 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004852 rettv->v_type = VAR_STRING;
4853 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854
Bram Moolenaar8c711452005-01-14 21:53:12 +00004855 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 {
4857 if (*p == '\\')
4858 {
4859 switch (*++p)
4860 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004861 case 'b': *name++ = BS; ++p; break;
4862 case 'e': *name++ = ESC; ++p; break;
4863 case 'f': *name++ = FF; ++p; break;
4864 case 'n': *name++ = NL; ++p; break;
4865 case 'r': *name++ = CAR; ++p; break;
4866 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867
4868 case 'X': /* hex: "\x1", "\x12" */
4869 case 'x':
4870 case 'u': /* Unicode: "\u0023" */
4871 case 'U':
4872 if (vim_isxdigit(p[1]))
4873 {
4874 int n, nr;
4875 int c = toupper(*p);
4876
4877 if (c == 'X')
4878 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004879 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004881 else
4882 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 nr = 0;
4884 while (--n >= 0 && vim_isxdigit(p[1]))
4885 {
4886 ++p;
4887 nr = (nr << 4) + hex2nr(*p);
4888 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004889 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890#ifdef FEAT_MBYTE
4891 /* For "\u" store the number according to
4892 * 'encoding'. */
4893 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004894 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 else
4896#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004897 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 break;
4900
4901 /* octal: "\1", "\12", "\123" */
4902 case '0':
4903 case '1':
4904 case '2':
4905 case '3':
4906 case '4':
4907 case '5':
4908 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004909 case '7': *name = *p++ - '0';
4910 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004912 *name = (*name << 3) + *p++ - '0';
4913 if (*p >= '0' && *p <= '7')
4914 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004916 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 break;
4918
4919 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02004920 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 if (extra != 0)
4922 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004923 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 break;
4925 }
4926 /* FALLTHROUGH */
4927
Bram Moolenaar8c711452005-01-14 21:53:12 +00004928 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 break;
4930 }
4931 }
4932 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004933 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004936 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02004937 if (*p != NUL) /* just in case */
4938 ++p;
4939 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 return OK;
4942}
4943
4944/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004945 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 * Return OK or FAIL.
4947 */
4948 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004949get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950{
4951 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004952 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004953 int reduce = 0;
4954
4955 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004956 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004957 */
4958 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
4959 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004960 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004961 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004962 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004963 break;
4964 ++reduce;
4965 ++p;
4966 }
4967 }
4968
Bram Moolenaar8c711452005-01-14 21:53:12 +00004969 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004970 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004971 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004972 return FAIL;
4973 }
4974
Bram Moolenaar8c711452005-01-14 21:53:12 +00004975 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004976 if (!evaluate)
4977 {
4978 *arg = p + 1;
4979 return OK;
4980 }
4981
4982 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004983 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004984 */
4985 str = alloc((unsigned)((p - *arg) - reduce));
4986 if (str == NULL)
4987 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004988 rettv->v_type = VAR_STRING;
4989 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004990
Bram Moolenaar8c711452005-01-14 21:53:12 +00004991 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004992 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004993 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004994 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004995 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004996 break;
4997 ++p;
4998 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004999 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005000 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005001 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005002 *arg = p + 1;
5003
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005004 return OK;
5005}
5006
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005007/*
5008 * Return the function name of the partial.
5009 */
5010 char_u *
5011partial_name(partial_T *pt)
5012{
5013 if (pt->pt_name != NULL)
5014 return pt->pt_name;
5015 return pt->pt_func->uf_name;
5016}
5017
Bram Moolenaarddecc252016-04-06 22:59:37 +02005018 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005019partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005020{
5021 int i;
5022
5023 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005024 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005025 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005026 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005027 if (pt->pt_name != NULL)
5028 {
5029 func_unref(pt->pt_name);
5030 vim_free(pt->pt_name);
5031 }
5032 else
5033 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005034 vim_free(pt);
5035}
5036
5037/*
5038 * Unreference a closure: decrement the reference count and free it when it
5039 * becomes zero.
5040 */
5041 void
5042partial_unref(partial_T *pt)
5043{
5044 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005045 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005046}
5047
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005048static int tv_equal_recurse_limit;
5049
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005050 static int
5051func_equal(
5052 typval_T *tv1,
5053 typval_T *tv2,
5054 int ic) /* ignore case */
5055{
5056 char_u *s1, *s2;
5057 dict_T *d1, *d2;
5058 int a1, a2;
5059 int i;
5060
5061 /* empty and NULL function name considered the same */
5062 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005063 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005064 if (s1 != NULL && *s1 == NUL)
5065 s1 = NULL;
5066 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005067 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005068 if (s2 != NULL && *s2 == NUL)
5069 s2 = NULL;
5070 if (s1 == NULL || s2 == NULL)
5071 {
5072 if (s1 != s2)
5073 return FALSE;
5074 }
5075 else if (STRCMP(s1, s2) != 0)
5076 return FALSE;
5077
5078 /* empty dict and NULL dict is different */
5079 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5080 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5081 if (d1 == NULL || d2 == NULL)
5082 {
5083 if (d1 != d2)
5084 return FALSE;
5085 }
5086 else if (!dict_equal(d1, d2, ic, TRUE))
5087 return FALSE;
5088
5089 /* empty list and no list considered the same */
5090 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5091 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5092 if (a1 != a2)
5093 return FALSE;
5094 for (i = 0; i < a1; ++i)
5095 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5096 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5097 return FALSE;
5098
5099 return TRUE;
5100}
5101
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005102/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005103 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005104 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005105 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005106 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005107 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005108tv_equal(
5109 typval_T *tv1,
5110 typval_T *tv2,
5111 int ic, /* ignore case */
5112 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005113{
5114 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005115 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005116 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005117 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005118
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005119 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005120 * recursiveness to a limit. We guess they are equal then.
5121 * A fixed limit has the problem of still taking an awful long time.
5122 * Reduce the limit every time running into it. That should work fine for
5123 * deeply linked structures that are not recursively linked and catch
5124 * recursiveness quickly. */
5125 if (!recursive)
5126 tv_equal_recurse_limit = 1000;
5127 if (recursive_cnt >= tv_equal_recurse_limit)
5128 {
5129 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005130 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005131 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005132
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005133 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5134 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005135 if ((tv1->v_type == VAR_FUNC
5136 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5137 && (tv2->v_type == VAR_FUNC
5138 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5139 {
5140 ++recursive_cnt;
5141 r = func_equal(tv1, tv2, ic);
5142 --recursive_cnt;
5143 return r;
5144 }
5145
5146 if (tv1->v_type != tv2->v_type)
5147 return FALSE;
5148
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005149 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005150 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005151 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005152 ++recursive_cnt;
5153 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5154 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005155 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005156
5157 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005158 ++recursive_cnt;
5159 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5160 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005161 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005162
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005163 case VAR_NUMBER:
5164 return tv1->vval.v_number == tv2->vval.v_number;
5165
5166 case VAR_STRING:
5167 s1 = get_tv_string_buf(tv1, buf1);
5168 s2 = get_tv_string_buf(tv2, buf2);
5169 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005170
5171 case VAR_SPECIAL:
5172 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005173
5174 case VAR_FLOAT:
5175#ifdef FEAT_FLOAT
5176 return tv1->vval.v_float == tv2->vval.v_float;
5177#endif
5178 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005179#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005180 return tv1->vval.v_job == tv2->vval.v_job;
5181#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005182 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005183#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005184 return tv1->vval.v_channel == tv2->vval.v_channel;
5185#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005186 case VAR_FUNC:
5187 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005188 case VAR_UNKNOWN:
5189 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005190 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005191
Bram Moolenaara03f2332016-02-06 18:09:59 +01005192 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5193 * does not equal anything, not even itself. */
5194 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005195}
5196
5197/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005198 * Return the next (unique) copy ID.
5199 * Used for serializing nested structures.
5200 */
5201 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005202get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005203{
5204 current_copyID += COPYID_INC;
5205 return current_copyID;
5206}
5207
5208/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005209 * Garbage collection for lists and dictionaries.
5210 *
5211 * We use reference counts to be able to free most items right away when they
5212 * are no longer used. But for composite items it's possible that it becomes
5213 * unused while the reference count is > 0: When there is a recursive
5214 * reference. Example:
5215 * :let l = [1, 2, 3]
5216 * :let d = {9: l}
5217 * :let l[1] = d
5218 *
5219 * Since this is quite unusual we handle this with garbage collection: every
5220 * once in a while find out which lists and dicts are not referenced from any
5221 * variable.
5222 *
5223 * Here is a good reference text about garbage collection (refers to Python
5224 * but it applies to all reference-counting mechanisms):
5225 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005226 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005227
5228/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005229 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005230 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005231 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005232 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005233 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005234garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005235{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005236 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005237 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005238 buf_T *buf;
5239 win_T *wp;
5240 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005241 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005242#ifdef FEAT_WINDOWS
5243 tabpage_T *tp;
5244#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005245
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005246 if (!testing)
5247 {
5248 /* Only do this once. */
5249 want_garbage_collect = FALSE;
5250 may_garbage_collect = FALSE;
5251 garbage_collect_at_exit = FALSE;
5252 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005253
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005254 /* We advance by two because we add one for items referenced through
5255 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005256 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005257
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005258 /*
5259 * 1. Go through all accessible variables and mark all lists and dicts
5260 * with copyID.
5261 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005262
5263 /* Don't free variables in the previous_funccal list unless they are only
5264 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005265 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005266 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005267
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005268 /* script-local variables */
5269 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005270 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005271
5272 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005273 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005274 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5275 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005276
5277 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005278 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005279 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5280 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005281#ifdef FEAT_AUTOCMD
5282 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005283 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5284 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005285#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005286
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005287#ifdef FEAT_WINDOWS
5288 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005289 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005290 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5291 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005292#endif
5293
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005294 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005295 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005296
5297 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005298 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005299
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005300 /* named functions (matters for closures) */
5301 abort = abort || set_ref_in_functions(copyID);
5302
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005303 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005304 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005305
Bram Moolenaard812df62008-11-09 12:46:09 +00005306 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005307 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005308
Bram Moolenaar1dced572012-04-05 16:54:08 +02005309#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005310 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005311#endif
5312
Bram Moolenaardb913952012-06-29 12:54:53 +02005313#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005314 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005315#endif
5316
5317#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005318 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005319#endif
5320
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005321#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005322 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005323 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005324#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005325#ifdef FEAT_NETBEANS_INTG
5326 abort = abort || set_ref_in_nb_channel(copyID);
5327#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005328
Bram Moolenaare3188e22016-05-31 21:13:04 +02005329#ifdef FEAT_TIMERS
5330 abort = abort || set_ref_in_timer(copyID);
5331#endif
5332
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005333 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005334 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005335 /*
5336 * 2. Free lists and dictionaries that are not referenced.
5337 */
5338 did_free = free_unref_items(copyID);
5339
5340 /*
5341 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005342 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005343 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005344 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005345 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005346 else if (p_verbose > 0)
5347 {
5348 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
5349 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005350
5351 return did_free;
5352}
5353
5354/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005355 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005356 */
5357 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005358free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005359{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005360 int did_free = FALSE;
5361
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005362 /* Let all "free" functions know that we are here. This means no
5363 * dictionaries, lists, channels or jobs are to be freed, because we will
5364 * do that here. */
5365 in_free_unref_items = TRUE;
5366
5367 /*
5368 * PASS 1: free the contents of the items. We don't free the items
5369 * themselves yet, so that it is possible to decrement refcount counters
5370 */
5371
Bram Moolenaarcd524592016-07-17 14:57:05 +02005372 /* Go through the list of dicts and free items without the copyID. */
5373 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005374
Bram Moolenaarda861d62016-07-17 15:46:27 +02005375 /* Go through the list of lists and free items without the copyID. */
5376 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005377
5378#ifdef FEAT_JOB_CHANNEL
5379 /* Go through the list of jobs and free items without the copyID. This
5380 * must happen before doing channels, because jobs refer to channels, but
5381 * the reference from the channel to the job isn't tracked. */
5382 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5383
5384 /* Go through the list of channels and free items without the copyID. */
5385 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5386#endif
5387
5388 /*
5389 * PASS 2: free the items themselves.
5390 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005391 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005392 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005393
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005394#ifdef FEAT_JOB_CHANNEL
5395 /* Go through the list of jobs and free items without the copyID. This
5396 * must happen before doing channels, because jobs refer to channels, but
5397 * the reference from the channel to the job isn't tracked. */
5398 free_unused_jobs(copyID, COPYID_MASK);
5399
5400 /* Go through the list of channels and free items without the copyID. */
5401 free_unused_channels(copyID, COPYID_MASK);
5402#endif
5403
5404 in_free_unref_items = FALSE;
5405
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005406 return did_free;
5407}
5408
5409/*
5410 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005411 * "list_stack" is used to add lists to be marked. Can be NULL.
5412 *
5413 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005414 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005415 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005416set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005417{
5418 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005419 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005420 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005421 hashtab_T *cur_ht;
5422 ht_stack_T *ht_stack = NULL;
5423 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005424
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005425 cur_ht = ht;
5426 for (;;)
5427 {
5428 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005429 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005430 /* Mark each item in the hashtab. If the item contains a hashtab
5431 * it is added to ht_stack, if it contains a list it is added to
5432 * list_stack. */
5433 todo = (int)cur_ht->ht_used;
5434 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5435 if (!HASHITEM_EMPTY(hi))
5436 {
5437 --todo;
5438 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5439 &ht_stack, list_stack);
5440 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005441 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005442
5443 if (ht_stack == NULL)
5444 break;
5445
5446 /* take an item from the stack */
5447 cur_ht = ht_stack->ht;
5448 tempitem = ht_stack;
5449 ht_stack = ht_stack->prev;
5450 free(tempitem);
5451 }
5452
5453 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005454}
5455
5456/*
5457 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005458 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5459 *
5460 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005461 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005462 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005463set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005464{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005465 listitem_T *li;
5466 int abort = FALSE;
5467 list_T *cur_l;
5468 list_stack_T *list_stack = NULL;
5469 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005470
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005471 cur_l = l;
5472 for (;;)
5473 {
5474 if (!abort)
5475 /* Mark each item in the list. If the item contains a hashtab
5476 * it is added to ht_stack, if it contains a list it is added to
5477 * list_stack. */
5478 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5479 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5480 ht_stack, &list_stack);
5481 if (list_stack == NULL)
5482 break;
5483
5484 /* take an item from the stack */
5485 cur_l = list_stack->list;
5486 tempitem = list_stack;
5487 list_stack = list_stack->prev;
5488 free(tempitem);
5489 }
5490
5491 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005492}
5493
5494/*
5495 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005496 * "list_stack" is used to add lists to be marked. Can be NULL.
5497 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5498 *
5499 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005500 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005501 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005502set_ref_in_item(
5503 typval_T *tv,
5504 int copyID,
5505 ht_stack_T **ht_stack,
5506 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005507{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005508 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005509
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005510 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005511 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005512 dict_T *dd = tv->vval.v_dict;
5513
Bram Moolenaara03f2332016-02-06 18:09:59 +01005514 if (dd != NULL && dd->dv_copyID != copyID)
5515 {
5516 /* Didn't see this dict yet. */
5517 dd->dv_copyID = copyID;
5518 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005519 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005520 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5521 }
5522 else
5523 {
5524 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5525 if (newitem == NULL)
5526 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005527 else
5528 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005529 newitem->ht = &dd->dv_hashtab;
5530 newitem->prev = *ht_stack;
5531 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005532 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005533 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005534 }
5535 }
5536 else if (tv->v_type == VAR_LIST)
5537 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005538 list_T *ll = tv->vval.v_list;
5539
Bram Moolenaara03f2332016-02-06 18:09:59 +01005540 if (ll != NULL && ll->lv_copyID != copyID)
5541 {
5542 /* Didn't see this list yet. */
5543 ll->lv_copyID = copyID;
5544 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005545 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005546 abort = set_ref_in_list(ll, copyID, ht_stack);
5547 }
5548 else
5549 {
5550 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005551 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005552 if (newitem == NULL)
5553 abort = TRUE;
5554 else
5555 {
5556 newitem->list = ll;
5557 newitem->prev = *list_stack;
5558 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005559 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005560 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005561 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005562 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005563 else if (tv->v_type == VAR_FUNC)
5564 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005565 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005566 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005567 else if (tv->v_type == VAR_PARTIAL)
5568 {
5569 partial_T *pt = tv->vval.v_partial;
5570 int i;
5571
5572 /* A partial does not have a copyID, because it cannot contain itself.
5573 */
5574 if (pt != NULL)
5575 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005576 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005577
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005578 if (pt->pt_dict != NULL)
5579 {
5580 typval_T dtv;
5581
5582 dtv.v_type = VAR_DICT;
5583 dtv.vval.v_dict = pt->pt_dict;
5584 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5585 }
5586
5587 for (i = 0; i < pt->pt_argc; ++i)
5588 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5589 ht_stack, list_stack);
5590 }
5591 }
5592#ifdef FEAT_JOB_CHANNEL
5593 else if (tv->v_type == VAR_JOB)
5594 {
5595 job_T *job = tv->vval.v_job;
5596 typval_T dtv;
5597
5598 if (job != NULL && job->jv_copyID != copyID)
5599 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005600 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005601 if (job->jv_channel != NULL)
5602 {
5603 dtv.v_type = VAR_CHANNEL;
5604 dtv.vval.v_channel = job->jv_channel;
5605 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5606 }
5607 if (job->jv_exit_partial != NULL)
5608 {
5609 dtv.v_type = VAR_PARTIAL;
5610 dtv.vval.v_partial = job->jv_exit_partial;
5611 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5612 }
5613 }
5614 }
5615 else if (tv->v_type == VAR_CHANNEL)
5616 {
5617 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005618 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005619 typval_T dtv;
5620 jsonq_T *jq;
5621 cbq_T *cq;
5622
5623 if (ch != NULL && ch->ch_copyID != copyID)
5624 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005625 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005626 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005627 {
5628 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5629 jq = jq->jq_next)
5630 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5631 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5632 cq = cq->cq_next)
5633 if (cq->cq_partial != NULL)
5634 {
5635 dtv.v_type = VAR_PARTIAL;
5636 dtv.vval.v_partial = cq->cq_partial;
5637 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5638 }
5639 if (ch->ch_part[part].ch_partial != NULL)
5640 {
5641 dtv.v_type = VAR_PARTIAL;
5642 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5643 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5644 }
5645 }
5646 if (ch->ch_partial != NULL)
5647 {
5648 dtv.v_type = VAR_PARTIAL;
5649 dtv.vval.v_partial = ch->ch_partial;
5650 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5651 }
5652 if (ch->ch_close_partial != NULL)
5653 {
5654 dtv.v_type = VAR_PARTIAL;
5655 dtv.vval.v_partial = ch->ch_close_partial;
5656 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5657 }
5658 }
5659 }
5660#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005661 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005662}
5663
Bram Moolenaar17a13432016-01-24 14:22:10 +01005664 static char *
5665get_var_special_name(int nr)
5666{
5667 switch (nr)
5668 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005669 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005670 case VVAL_TRUE: return "v:true";
5671 case VVAL_NONE: return "v:none";
5672 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005673 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01005674 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01005675 return "42";
5676}
5677
Bram Moolenaar8c711452005-01-14 21:53:12 +00005678/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005679 * Return a string with the string representation of a variable.
5680 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005681 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005682 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005683 * When both "echo_style" and "dict_val" are FALSE, put quotes around stings as
5684 * "string()", otherwise does not put quotes around strings, as ":echo"
5685 * displays values.
5686 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5687 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005688 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005689 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005690 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005691echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005692 typval_T *tv,
5693 char_u **tofree,
5694 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005695 int copyID,
5696 int echo_style,
5697 int restore_copyID,
5698 int dict_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005699{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005700 static int recurse = 0;
5701 char_u *r = NULL;
5702
Bram Moolenaar33570922005-01-25 22:26:29 +00005703 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005704 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005705 if (!did_echo_string_emsg)
5706 {
5707 /* Only give this message once for a recursive call to avoid
5708 * flooding the user with errors. And stop iterating over lists
5709 * and dicts. */
5710 did_echo_string_emsg = TRUE;
5711 EMSG(_("E724: variable nested too deep for displaying"));
5712 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005713 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005714 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005715 }
5716 ++recurse;
5717
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005718 switch (tv->v_type)
5719 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005720 case VAR_STRING:
5721 if (echo_style && !dict_val)
5722 {
5723 *tofree = NULL;
5724 r = get_tv_string_buf(tv, numbuf);
5725 }
5726 else
5727 {
5728 *tofree = string_quote(tv->vval.v_string, FALSE);
5729 r = *tofree;
5730 }
5731 break;
5732
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005733 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005734 if (echo_style)
5735 {
5736 *tofree = NULL;
5737 r = tv->vval.v_string;
5738 }
5739 else
5740 {
5741 *tofree = string_quote(tv->vval.v_string, TRUE);
5742 r = *tofree;
5743 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005744 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005745
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005746 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005747 {
5748 partial_T *pt = tv->vval.v_partial;
5749 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005750 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005751 garray_T ga;
5752 int i;
5753 char_u *tf;
5754
5755 ga_init2(&ga, 1, 100);
5756 ga_concat(&ga, (char_u *)"function(");
5757 if (fname != NULL)
5758 {
5759 ga_concat(&ga, fname);
5760 vim_free(fname);
5761 }
5762 if (pt != NULL && pt->pt_argc > 0)
5763 {
5764 ga_concat(&ga, (char_u *)", [");
5765 for (i = 0; i < pt->pt_argc; ++i)
5766 {
5767 if (i > 0)
5768 ga_concat(&ga, (char_u *)", ");
5769 ga_concat(&ga,
5770 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5771 vim_free(tf);
5772 }
5773 ga_concat(&ga, (char_u *)"]");
5774 }
5775 if (pt != NULL && pt->pt_dict != NULL)
5776 {
5777 typval_T dtv;
5778
5779 ga_concat(&ga, (char_u *)", ");
5780 dtv.v_type = VAR_DICT;
5781 dtv.vval.v_dict = pt->pt_dict;
5782 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5783 vim_free(tf);
5784 }
5785 ga_concat(&ga, (char_u *)")");
5786
5787 *tofree = ga.ga_data;
5788 r = *tofree;
5789 break;
5790 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005791
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005792 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005793 if (tv->vval.v_list == NULL)
5794 {
5795 *tofree = NULL;
5796 r = NULL;
5797 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005798 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5799 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005800 {
5801 *tofree = NULL;
5802 r = (char_u *)"[...]";
5803 }
5804 else
5805 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005806 int old_copyID = tv->vval.v_list->lv_copyID;
5807
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005808 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005809 *tofree = list2string(tv, copyID, restore_copyID);
5810 if (restore_copyID)
5811 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005812 r = *tofree;
5813 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005814 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005815
Bram Moolenaar8c711452005-01-14 21:53:12 +00005816 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005817 if (tv->vval.v_dict == NULL)
5818 {
5819 *tofree = NULL;
5820 r = NULL;
5821 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005822 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5823 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005824 {
5825 *tofree = NULL;
5826 r = (char_u *)"{...}";
5827 }
5828 else
5829 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005830 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005831 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005832 *tofree = dict2string(tv, copyID, restore_copyID);
5833 if (restore_copyID)
5834 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005835 r = *tofree;
5836 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005837 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005838
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005839 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005840 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005841 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005842 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005843 *tofree = NULL;
5844 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005845 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005846
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005847 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005848#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005849 *tofree = NULL;
5850 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5851 r = numbuf;
5852 break;
5853#endif
5854
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005855 case VAR_SPECIAL:
5856 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005857 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005858 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005859 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005860
Bram Moolenaar8502c702014-06-17 12:51:16 +02005861 if (--recurse == 0)
5862 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005863 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005864}
5865
5866/*
5867 * Return a string with the string representation of a variable.
5868 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5869 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005870 * Does not put quotes around strings, as ":echo" displays values.
5871 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5872 * May return NULL.
5873 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005874 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005875echo_string(
5876 typval_T *tv,
5877 char_u **tofree,
5878 char_u *numbuf,
5879 int copyID)
5880{
5881 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5882}
5883
5884/*
5885 * Return a string with the string representation of a variable.
5886 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5887 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005888 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005889 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005890 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02005891 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005892tv2string(
5893 typval_T *tv,
5894 char_u **tofree,
5895 char_u *numbuf,
5896 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005897{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005898 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005899}
5900
5901/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005902 * Return string "str" in ' quotes, doubling ' characters.
5903 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005904 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005905 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005906 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005907string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005908{
Bram Moolenaar33570922005-01-25 22:26:29 +00005909 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005910 char_u *p, *r, *s;
5911
Bram Moolenaar33570922005-01-25 22:26:29 +00005912 len = (function ? 13 : 3);
5913 if (str != NULL)
5914 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005915 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00005916 for (p = str; *p != NUL; mb_ptr_adv(p))
5917 if (*p == '\'')
5918 ++len;
5919 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005920 s = r = alloc(len);
5921 if (r != NULL)
5922 {
5923 if (function)
5924 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005925 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005926 r += 10;
5927 }
5928 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005929 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005930 if (str != NULL)
5931 for (p = str; *p != NUL; )
5932 {
5933 if (*p == '\'')
5934 *r++ = '\'';
5935 MB_COPY_CHAR(p, r);
5936 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005937 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005938 if (function)
5939 *r++ = ')';
5940 *r++ = NUL;
5941 }
5942 return s;
5943}
5944
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005945#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005946/*
5947 * Convert the string "text" to a floating point number.
5948 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5949 * this always uses a decimal point.
5950 * Returns the length of the text that was consumed.
5951 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005952 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005953string2float(
5954 char_u *text,
5955 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005956{
5957 char *s = (char *)text;
5958 float_T f;
5959
Bram Moolenaar62473612017-01-08 19:25:40 +01005960 /* MS-Windows does not deal with "inf" and "nan" properly. */
5961 if (STRNICMP(text, "inf", 3) == 0)
5962 {
5963 *value = INFINITY;
5964 return 3;
5965 }
5966 if (STRNICMP(text, "-inf", 3) == 0)
5967 {
5968 *value = -INFINITY;
5969 return 4;
5970 }
5971 if (STRNICMP(text, "nan", 3) == 0)
5972 {
5973 *value = NAN;
5974 return 3;
5975 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005976 f = strtod(s, &s);
5977 *value = f;
5978 return (int)((char_u *)s - text);
5979}
5980#endif
5981
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005982/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 * Get the value of an environment variable.
5984 * "arg" is pointing to the '$'. It is advanced to after the name.
5985 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005986 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 */
5988 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005989get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990{
5991 char_u *string = NULL;
5992 int len;
5993 int cc;
5994 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005995 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996
5997 ++*arg;
5998 name = *arg;
5999 len = get_env_len(arg);
6000 if (evaluate)
6001 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006002 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006003 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006004
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006005 cc = name[len];
6006 name[len] = NUL;
6007 /* first try vim_getenv(), fast for normal environment vars */
6008 string = vim_getenv(name, &mustfree);
6009 if (string != NULL && *string != NUL)
6010 {
6011 if (!mustfree)
6012 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006014 else
6015 {
6016 if (mustfree)
6017 vim_free(string);
6018
6019 /* next try expanding things like $VIM and ${HOME} */
6020 string = expand_env_save(name - 1);
6021 if (string != NULL && *string == '$')
6022 {
6023 vim_free(string);
6024 string = NULL;
6025 }
6026 }
6027 name[len] = cc;
6028
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006029 rettv->v_type = VAR_STRING;
6030 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 }
6032
6033 return OK;
6034}
6035
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006036
6037
6038/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006040 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006042 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006043var2fpos(
6044 typval_T *varp,
6045 int dollar_lnum, /* TRUE when $ is last line */
6046 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006048 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006050 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051
Bram Moolenaara5525202006-03-02 22:52:09 +00006052 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006053 if (varp->v_type == VAR_LIST)
6054 {
6055 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006056 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006057 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006058 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006059
6060 l = varp->vval.v_list;
6061 if (l == NULL)
6062 return NULL;
6063
6064 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006065 pos.lnum = list_find_nr(l, 0L, &error);
6066 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006067 return NULL; /* invalid line number */
6068
6069 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006070 pos.col = list_find_nr(l, 1L, &error);
6071 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006072 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006073 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006074
6075 /* We accept "$" for the column number: last column. */
6076 li = list_find(l, 1L);
6077 if (li != NULL && li->li_tv.v_type == VAR_STRING
6078 && li->li_tv.vval.v_string != NULL
6079 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6080 pos.col = len + 1;
6081
Bram Moolenaara5525202006-03-02 22:52:09 +00006082 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006083 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006084 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006085 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006086
Bram Moolenaara5525202006-03-02 22:52:09 +00006087#ifdef FEAT_VIRTUALEDIT
6088 /* Get the virtual offset. Defaults to zero. */
6089 pos.coladd = list_find_nr(l, 2L, &error);
6090 if (error)
6091 pos.coladd = 0;
6092#endif
6093
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006094 return &pos;
6095 }
6096
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006097 name = get_tv_string_chk(varp);
6098 if (name == NULL)
6099 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006100 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006102 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6103 {
6104 if (VIsual_active)
6105 return &VIsual;
6106 return &curwin->w_cursor;
6107 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006108 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006110 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6112 return NULL;
6113 return pp;
6114 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006115
6116#ifdef FEAT_VIRTUALEDIT
6117 pos.coladd = 0;
6118#endif
6119
Bram Moolenaar477933c2007-07-17 14:32:23 +00006120 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006121 {
6122 pos.col = 0;
6123 if (name[1] == '0') /* "w0": first visible line */
6124 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006125 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006126 pos.lnum = curwin->w_topline;
6127 return &pos;
6128 }
6129 else if (name[1] == '$') /* "w$": last visible line */
6130 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006131 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006132 pos.lnum = curwin->w_botline - 1;
6133 return &pos;
6134 }
6135 }
6136 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006138 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 {
6140 pos.lnum = curbuf->b_ml.ml_line_count;
6141 pos.col = 0;
6142 }
6143 else
6144 {
6145 pos.lnum = curwin->w_cursor.lnum;
6146 pos.col = (colnr_T)STRLEN(ml_get_curline());
6147 }
6148 return &pos;
6149 }
6150 return NULL;
6151}
6152
6153/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006154 * Convert list in "arg" into a position and optional file number.
6155 * When "fnump" is NULL there is no file number, only 3 items.
6156 * Note that the column is passed on as-is, the caller may want to decrement
6157 * it to use 1 for the first column.
6158 * Return FAIL when conversion is not possible, doesn't check the position for
6159 * validity.
6160 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006161 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006162list2fpos(
6163 typval_T *arg,
6164 pos_T *posp,
6165 int *fnump,
6166 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006167{
6168 list_T *l = arg->vval.v_list;
6169 long i = 0;
6170 long n;
6171
Bram Moolenaar493c1782014-05-28 14:34:46 +02006172 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6173 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006174 if (arg->v_type != VAR_LIST
6175 || l == NULL
6176 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006177 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006178 return FAIL;
6179
6180 if (fnump != NULL)
6181 {
6182 n = list_find_nr(l, i++, NULL); /* fnum */
6183 if (n < 0)
6184 return FAIL;
6185 if (n == 0)
6186 n = curbuf->b_fnum; /* current buffer */
6187 *fnump = n;
6188 }
6189
6190 n = list_find_nr(l, i++, NULL); /* lnum */
6191 if (n < 0)
6192 return FAIL;
6193 posp->lnum = n;
6194
6195 n = list_find_nr(l, i++, NULL); /* col */
6196 if (n < 0)
6197 return FAIL;
6198 posp->col = n;
6199
6200#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +02006201 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006202 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006203 posp->coladd = 0;
6204 else
6205 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006206#endif
6207
Bram Moolenaar493c1782014-05-28 14:34:46 +02006208 if (curswantp != NULL)
6209 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6210
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006211 return OK;
6212}
6213
6214/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 * Get the length of an environment variable name.
6216 * Advance "arg" to the first character after the name.
6217 * Return 0 for error.
6218 */
6219 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006220get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221{
6222 char_u *p;
6223 int len;
6224
6225 for (p = *arg; vim_isIDc(*p); ++p)
6226 ;
6227 if (p == *arg) /* no name found */
6228 return 0;
6229
6230 len = (int)(p - *arg);
6231 *arg = p;
6232 return len;
6233}
6234
6235/*
6236 * Get the length of the name of a function or internal variable.
6237 * "arg" is advanced to the first non-white character after the name.
6238 * Return 0 if something is wrong.
6239 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006240 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006241get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242{
6243 char_u *p;
6244 int len;
6245
6246 /* Find the end of the name. */
6247 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006248 {
6249 if (*p == ':')
6250 {
6251 /* "s:" is start of "s:var", but "n:" is not and can be used in
6252 * slice "[n:]". Also "xx:" is not a namespace. */
6253 len = (int)(p - *arg);
6254 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6255 || len > 1)
6256 break;
6257 }
6258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 if (p == *arg) /* no name found */
6260 return 0;
6261
6262 len = (int)(p - *arg);
6263 *arg = skipwhite(p);
6264
6265 return len;
6266}
6267
6268/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006269 * Get the length of the name of a variable or function.
6270 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006272 * Return -1 if curly braces expansion failed.
6273 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 * If the name contains 'magic' {}'s, expand them and return the
6275 * expanded name in an allocated string via 'alias' - caller must free.
6276 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006277 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006278get_name_len(
6279 char_u **arg,
6280 char_u **alias,
6281 int evaluate,
6282 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283{
6284 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 char_u *p;
6286 char_u *expr_start;
6287 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288
6289 *alias = NULL; /* default to no alias */
6290
6291 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6292 && (*arg)[2] == (int)KE_SNR)
6293 {
6294 /* hard coded <SNR>, already translated */
6295 *arg += 3;
6296 return get_id_len(arg) + 3;
6297 }
6298 len = eval_fname_script(*arg);
6299 if (len > 0)
6300 {
6301 /* literal "<SID>", "s:" or "<SNR>" */
6302 *arg += len;
6303 }
6304
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006306 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006308 p = find_name_end(*arg, &expr_start, &expr_end,
6309 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 if (expr_start != NULL)
6311 {
6312 char_u *temp_string;
6313
6314 if (!evaluate)
6315 {
6316 len += (int)(p - *arg);
6317 *arg = skipwhite(p);
6318 return len;
6319 }
6320
6321 /*
6322 * Include any <SID> etc in the expanded string:
6323 * Thus the -len here.
6324 */
6325 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6326 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006327 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 *alias = temp_string;
6329 *arg = skipwhite(p);
6330 return (int)STRLEN(temp_string);
6331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332
6333 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006334 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 EMSG2(_(e_invexpr2), *arg);
6336
6337 return len;
6338}
6339
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006340/*
6341 * Find the end of a variable or function name, taking care of magic braces.
6342 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6343 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006344 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006345 * Return a pointer to just after the name. Equal to "arg" if there is no
6346 * valid name.
6347 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006348 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006349find_name_end(
6350 char_u *arg,
6351 char_u **expr_start,
6352 char_u **expr_end,
6353 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006355 int mb_nest = 0;
6356 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006358 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006360 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006362 *expr_start = NULL;
6363 *expr_end = NULL;
6364 }
6365
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006366 /* Quick check for valid starting character. */
6367 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6368 return arg;
6369
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006370 for (p = arg; *p != NUL
6371 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006372 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006373 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006374 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +00006375 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006376 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006377 if (*p == '\'')
6378 {
6379 /* skip over 'string' to avoid counting [ and ] inside it. */
6380 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
6381 ;
6382 if (*p == NUL)
6383 break;
6384 }
6385 else if (*p == '"')
6386 {
6387 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
6388 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
6389 if (*p == '\\' && p[1] != NUL)
6390 ++p;
6391 if (*p == NUL)
6392 break;
6393 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006394 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6395 {
6396 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006397 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006398 len = (int)(p - arg);
6399 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006400 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006401 break;
6402 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006403
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006404 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006406 if (*p == '[')
6407 ++br_nest;
6408 else if (*p == ']')
6409 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006411
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006412 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006414 if (*p == '{')
6415 {
6416 mb_nest++;
6417 if (expr_start != NULL && *expr_start == NULL)
6418 *expr_start = p;
6419 }
6420 else if (*p == '}')
6421 {
6422 mb_nest--;
6423 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6424 *expr_end = p;
6425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 }
6428
6429 return p;
6430}
6431
6432/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006433 * Expands out the 'magic' {}'s in a variable/function name.
6434 * Note that this can call itself recursively, to deal with
6435 * constructs like foo{bar}{baz}{bam}
6436 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6437 * "in_start" ^
6438 * "expr_start" ^
6439 * "expr_end" ^
6440 * "in_end" ^
6441 *
6442 * Returns a new allocated string, which the caller must free.
6443 * Returns NULL for failure.
6444 */
6445 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006446make_expanded_name(
6447 char_u *in_start,
6448 char_u *expr_start,
6449 char_u *expr_end,
6450 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006451{
6452 char_u c1;
6453 char_u *retval = NULL;
6454 char_u *temp_result;
6455 char_u *nextcmd = NULL;
6456
6457 if (expr_end == NULL || in_end == NULL)
6458 return NULL;
6459 *expr_start = NUL;
6460 *expr_end = NUL;
6461 c1 = *in_end;
6462 *in_end = NUL;
6463
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006464 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006465 if (temp_result != NULL && nextcmd == NULL)
6466 {
6467 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6468 + (in_end - expr_end) + 1));
6469 if (retval != NULL)
6470 {
6471 STRCPY(retval, in_start);
6472 STRCAT(retval, temp_result);
6473 STRCAT(retval, expr_end + 1);
6474 }
6475 }
6476 vim_free(temp_result);
6477
6478 *in_end = c1; /* put char back for error messages */
6479 *expr_start = '{';
6480 *expr_end = '}';
6481
6482 if (retval != NULL)
6483 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006484 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006485 if (expr_start != NULL)
6486 {
6487 /* Further expansion! */
6488 temp_result = make_expanded_name(retval, expr_start,
6489 expr_end, temp_result);
6490 vim_free(retval);
6491 retval = temp_result;
6492 }
6493 }
6494
6495 return retval;
6496}
6497
6498/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006500 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006502 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006503eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006505 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6506}
6507
6508/*
6509 * Return TRUE if character "c" can be used as the first character in a
6510 * variable or function name (excluding '{' and '}').
6511 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006512 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006513eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006514{
6515 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516}
6517
6518/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 * Set number v: variable to "val".
6520 */
6521 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006522set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006524 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525}
6526
6527/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006528 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006530 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006531get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006533 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534}
6535
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006536/*
6537 * Get string v: variable value. Uses a static buffer, can only be used once.
6538 */
6539 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006540get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006541{
6542 return get_tv_string(&vimvars[idx].vv_tv);
6543}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006544
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006546 * Get List v: variable value. Caller must take care of reference count when
6547 * needed.
6548 */
6549 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006550get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006551{
6552 return vimvars[idx].vv_list;
6553}
6554
6555/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006556 * Set v:char to character "c".
6557 */
6558 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006559set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006560{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006561 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006562
6563#ifdef FEAT_MBYTE
6564 if (has_mbyte)
6565 buf[(*mb_char2bytes)(c, buf)] = NUL;
6566 else
6567#endif
6568 {
6569 buf[0] = c;
6570 buf[1] = NUL;
6571 }
6572 set_vim_var_string(VV_CHAR, buf, -1);
6573}
6574
6575/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006576 * Set v:count to "count" and v:count1 to "count1".
6577 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 */
6579 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006580set_vcount(
6581 long count,
6582 long count1,
6583 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006585 if (set_prevcount)
6586 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006587 vimvars[VV_COUNT].vv_nr = count;
6588 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589}
6590
6591/*
6592 * Set string v: variable to a copy of "val".
6593 */
6594 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006595set_vim_var_string(
6596 int idx,
6597 char_u *val,
6598 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599{
Bram Moolenaara542c682016-01-31 16:28:04 +01006600 clear_tv(&vimvars[idx].vv_di.di_tv);
6601 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006603 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006605 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006607 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608}
6609
6610/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006611 * Set List v: variable to "val".
6612 */
6613 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006614set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006615{
Bram Moolenaara542c682016-01-31 16:28:04 +01006616 clear_tv(&vimvars[idx].vv_di.di_tv);
6617 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006618 vimvars[idx].vv_list = val;
6619 if (val != NULL)
6620 ++val->lv_refcount;
6621}
6622
6623/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006624 * Set Dictionary v: variable to "val".
6625 */
6626 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006627set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006628{
6629 int todo;
6630 hashitem_T *hi;
6631
Bram Moolenaara542c682016-01-31 16:28:04 +01006632 clear_tv(&vimvars[idx].vv_di.di_tv);
6633 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006634 vimvars[idx].vv_dict = val;
6635 if (val != NULL)
6636 {
6637 ++val->dv_refcount;
6638
6639 /* Set readonly */
6640 todo = (int)val->dv_hashtab.ht_used;
6641 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
6642 {
6643 if (HASHITEM_EMPTY(hi))
6644 continue;
6645 --todo;
6646 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
6647 }
6648 }
6649}
6650
6651/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 * Set v:register if needed.
6653 */
6654 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006655set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656{
6657 char_u regname;
6658
6659 if (c == 0 || c == ' ')
6660 regname = '"';
6661 else
6662 regname = c;
6663 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006664 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 set_vim_var_string(VV_REG, &regname, 1);
6666}
6667
6668/*
6669 * Get or set v:exception. If "oldval" == NULL, return the current value.
6670 * Otherwise, restore the value to "oldval" and return NULL.
6671 * Must always be called in pairs to save and restore v:exception! Does not
6672 * take care of memory allocations.
6673 */
6674 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006675v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676{
6677 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006678 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679
Bram Moolenaare9a41262005-01-15 22:18:47 +00006680 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 return NULL;
6682}
6683
6684/*
6685 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
6686 * Otherwise, restore the value to "oldval" and return NULL.
6687 * Must always be called in pairs to save and restore v:throwpoint! Does not
6688 * take care of memory allocations.
6689 */
6690 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006691v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692{
6693 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006694 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695
Bram Moolenaare9a41262005-01-15 22:18:47 +00006696 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 return NULL;
6698}
6699
6700#if defined(FEAT_AUTOCMD) || defined(PROTO)
6701/*
6702 * Set v:cmdarg.
6703 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6704 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6705 * Must always be called in pairs!
6706 */
6707 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006708set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709{
6710 char_u *oldval;
6711 char_u *newval;
6712 unsigned len;
6713
Bram Moolenaare9a41262005-01-15 22:18:47 +00006714 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006715 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006717 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006718 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006719 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 }
6721
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006722 if (eap->force_bin == FORCE_BIN)
6723 len = 6;
6724 else if (eap->force_bin == FORCE_NOBIN)
6725 len = 8;
6726 else
6727 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006728
6729 if (eap->read_edit)
6730 len += 7;
6731
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006732 if (eap->force_ff != 0)
6733 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
6734# ifdef FEAT_MBYTE
6735 if (eap->force_enc != 0)
6736 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006737 if (eap->bad_char != 0)
6738 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006739# endif
6740
6741 newval = alloc(len + 1);
6742 if (newval == NULL)
6743 return NULL;
6744
6745 if (eap->force_bin == FORCE_BIN)
6746 sprintf((char *)newval, " ++bin");
6747 else if (eap->force_bin == FORCE_NOBIN)
6748 sprintf((char *)newval, " ++nobin");
6749 else
6750 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006751
6752 if (eap->read_edit)
6753 STRCAT(newval, " ++edit");
6754
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006755 if (eap->force_ff != 0)
6756 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
6757 eap->cmd + eap->force_ff);
6758# ifdef FEAT_MBYTE
6759 if (eap->force_enc != 0)
6760 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6761 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006762 if (eap->bad_char == BAD_KEEP)
6763 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6764 else if (eap->bad_char == BAD_DROP)
6765 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6766 else if (eap->bad_char != 0)
6767 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006768# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00006769 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006770 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771}
6772#endif
6773
6774/*
6775 * Get the value of internal variable "name".
6776 * Return OK or FAIL.
6777 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006778 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006779get_var_tv(
6780 char_u *name,
6781 int len, /* length of "name" */
6782 typval_T *rettv, /* NULL when only checking existence */
6783 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
6784 int verbose, /* may give error message */
6785 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786{
6787 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006788 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006789 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791
6792 /* truncate the name, so that we can use strcmp() */
6793 cc = name[len];
6794 name[len] = NUL;
6795
6796 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 * Check for user-defined variables.
6798 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01006799 v = find_var(name, NULL, no_autoload);
6800 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006802 tv = &v->di_tv;
6803 if (dip != NULL)
6804 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 }
6806
Bram Moolenaare9a41262005-01-15 22:18:47 +00006807 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006809 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006810 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 ret = FAIL;
6812 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006813 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006814 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815
6816 name[len] = cc;
6817
6818 return ret;
6819}
6820
6821/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006822 * Check if variable "name[len]" is a local variable or an argument.
6823 * If so, "*eval_lavars_used" is set to TRUE.
6824 */
6825 static void
6826check_vars(char_u *name, int len)
6827{
6828 int cc;
6829 char_u *varname;
6830 hashtab_T *ht;
6831
6832 if (eval_lavars_used == NULL)
6833 return;
6834
6835 /* truncate the name, so that we can use strcmp() */
6836 cc = name[len];
6837 name[len] = NUL;
6838
6839 ht = find_var_ht(name, &varname);
6840 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
6841 {
6842 if (find_var(name, NULL, TRUE) != NULL)
6843 *eval_lavars_used = TRUE;
6844 }
6845
6846 name[len] = cc;
6847}
6848
6849/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006850 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
6851 * Also handle function call with Funcref variable: func(expr)
6852 * Can all be combined: dict.func(expr)[idx]['func'](expr)
6853 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006854 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006855handle_subscript(
6856 char_u **arg,
6857 typval_T *rettv,
6858 int evaluate, /* do more than finding the end */
6859 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006860{
6861 int ret = OK;
6862 dict_T *selfdict = NULL;
6863 char_u *s;
6864 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006865 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006866
6867 while (ret == OK
6868 && (**arg == '['
6869 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006870 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6871 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006872 && !vim_iswhite(*(*arg - 1)))
6873 {
6874 if (**arg == '(')
6875 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006876 partial_T *pt = NULL;
6877
Bram Moolenaard9fba312005-06-26 22:34:35 +00006878 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006879 if (evaluate)
6880 {
6881 functv = *rettv;
6882 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006883
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006884 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006885 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006886 {
6887 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006888 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006889 }
6890 else
6891 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006892 }
6893 else
6894 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006895 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00006896 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006897 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006898
6899 /* Clear the funcref afterwards, so that deleting it while
6900 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006901 if (evaluate)
6902 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006903
6904 /* Stop the expression evaluation when immediately aborting on
6905 * error, or when an interrupt occurred or an exception was thrown
6906 * but not caught. */
6907 if (aborting())
6908 {
6909 if (ret == OK)
6910 clear_tv(rettv);
6911 ret = FAIL;
6912 }
6913 dict_unref(selfdict);
6914 selfdict = NULL;
6915 }
6916 else /* **arg == '[' || **arg == '.' */
6917 {
6918 dict_unref(selfdict);
6919 if (rettv->v_type == VAR_DICT)
6920 {
6921 selfdict = rettv->vval.v_dict;
6922 if (selfdict != NULL)
6923 ++selfdict->dv_refcount;
6924 }
6925 else
6926 selfdict = NULL;
6927 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
6928 {
6929 clear_tv(rettv);
6930 ret = FAIL;
6931 }
6932 }
6933 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006934
Bram Moolenaar1d429612016-05-24 15:44:17 +02006935 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
6936 * Don't do this when "Func" is already a partial that was bound
6937 * explicitly (pt_auto is FALSE). */
6938 if (selfdict != NULL
6939 && (rettv->v_type == VAR_FUNC
6940 || (rettv->v_type == VAR_PARTIAL
6941 && (rettv->vval.v_partial->pt_auto
6942 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006943 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006944
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006945 dict_unref(selfdict);
6946 return ret;
6947}
6948
6949/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006950 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006951 * value).
6952 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01006953 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006954alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006955{
Bram Moolenaar33570922005-01-25 22:26:29 +00006956 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006957}
6958
6959/*
6960 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 * The string "s" must have been allocated, it is consumed.
6962 * Return NULL for out of memory, the variable otherwise.
6963 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006964 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006965alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966{
Bram Moolenaar33570922005-01-25 22:26:29 +00006967 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006969 rettv = alloc_tv();
6970 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006972 rettv->v_type = VAR_STRING;
6973 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 }
6975 else
6976 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006977 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978}
6979
6980/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006981 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006983 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006984free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985{
6986 if (varp != NULL)
6987 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006988 switch (varp->v_type)
6989 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006990 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006991 func_unref(varp->vval.v_string);
6992 /*FALLTHROUGH*/
6993 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006994 vim_free(varp->vval.v_string);
6995 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006996 case VAR_PARTIAL:
6997 partial_unref(varp->vval.v_partial);
6998 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006999 case VAR_LIST:
7000 list_unref(varp->vval.v_list);
7001 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007002 case VAR_DICT:
7003 dict_unref(varp->vval.v_dict);
7004 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007005 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007006#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007007 job_unref(varp->vval.v_job);
7008 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007009#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007010 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007011#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007012 channel_unref(varp->vval.v_channel);
7013 break;
7014#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007015 case VAR_NUMBER:
7016 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007017 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007018 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007019 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 vim_free(varp);
7022 }
7023}
7024
7025/*
7026 * Free the memory for a variable value and set the value to NULL or 0.
7027 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007028 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007029clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030{
7031 if (varp != NULL)
7032 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007033 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007035 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007036 func_unref(varp->vval.v_string);
7037 /*FALLTHROUGH*/
7038 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007039 vim_free(varp->vval.v_string);
7040 varp->vval.v_string = NULL;
7041 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007042 case VAR_PARTIAL:
7043 partial_unref(varp->vval.v_partial);
7044 varp->vval.v_partial = NULL;
7045 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007046 case VAR_LIST:
7047 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007048 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007049 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007050 case VAR_DICT:
7051 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007052 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007053 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007054 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007055 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007056 varp->vval.v_number = 0;
7057 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007058 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007059#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007060 varp->vval.v_float = 0.0;
7061 break;
7062#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007063 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007064#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007065 job_unref(varp->vval.v_job);
7066 varp->vval.v_job = NULL;
7067#endif
7068 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007069 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007070#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007071 channel_unref(varp->vval.v_channel);
7072 varp->vval.v_channel = NULL;
7073#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007074 case VAR_UNKNOWN:
7075 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007077 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 }
7079}
7080
7081/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007082 * Set the value of a variable to NULL without freeing items.
7083 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007084 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007085init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007086{
7087 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007088 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007089}
7090
7091/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 * Get the number value of a variable.
7093 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007094 * For incompatible types, return 0.
7095 * get_tv_number_chk() is similar to get_tv_number(), but informs the
7096 * caller of incompatible types: it sets *denote to TRUE if "denote"
7097 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007099 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007100get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007102 int error = FALSE;
7103
7104 return get_tv_number_chk(varp, &error); /* return 0L on error */
7105}
7106
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007107 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007108get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007109{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007110 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007112 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007114 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007115 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007116 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007117#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +00007118 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007119 break;
7120#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007121 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007122 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007123 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007124 break;
7125 case VAR_STRING:
7126 if (varp->vval.v_string != NULL)
7127 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01007128 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007129 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007130 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007131 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007132 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007133 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007134 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007135 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007136 case VAR_SPECIAL:
7137 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7138 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007139 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007140#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007141 EMSG(_("E910: Using a Job as a Number"));
7142 break;
7143#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007144 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007145#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007146 EMSG(_("E913: Using a Channel as a Number"));
7147 break;
7148#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007149 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007150 internal_error("get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007151 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007153 if (denote == NULL) /* useful for values that must be unsigned */
7154 n = -1;
7155 else
7156 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007157 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158}
7159
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007160#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007161 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007162get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007163{
7164 switch (varp->v_type)
7165 {
7166 case VAR_NUMBER:
7167 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007168 case VAR_FLOAT:
7169 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007170 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007171 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007172 EMSG(_("E891: Using a Funcref as a Float"));
7173 break;
7174 case VAR_STRING:
7175 EMSG(_("E892: Using a String as a Float"));
7176 break;
7177 case VAR_LIST:
7178 EMSG(_("E893: Using a List as a Float"));
7179 break;
7180 case VAR_DICT:
7181 EMSG(_("E894: Using a Dictionary as a Float"));
7182 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007183 case VAR_SPECIAL:
7184 EMSG(_("E907: Using a special value as a Float"));
7185 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007186 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007187# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007188 EMSG(_("E911: Using a Job as a Float"));
7189 break;
7190# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007191 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007192# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007193 EMSG(_("E914: Using a Channel as a Float"));
7194 break;
7195# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007196 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007197 internal_error("get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007198 break;
7199 }
7200 return 0;
7201}
7202#endif
7203
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 * Get the string value of a variable.
7206 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +00007207 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7208 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 * If the String variable has never been set, return an empty string.
7210 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007211 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
7212 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007214 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007215get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216{
7217 static char_u mybuf[NUMBUFLEN];
7218
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007219 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220}
7221
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007222 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007223get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007225 char_u *res = get_tv_string_buf_chk(varp, buf);
7226
7227 return res != NULL ? res : (char_u *)"";
7228}
7229
Bram Moolenaar7d647822014-04-05 21:28:56 +02007230/*
7231 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7232 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007233 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007234get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007235{
7236 static char_u mybuf[NUMBUFLEN];
7237
7238 return get_tv_string_buf_chk(varp, mybuf);
7239}
7240
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007241 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007242get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007243{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007244 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007246 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007247 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
7248 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007249 return buf;
7250 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007251 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007252 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007253 break;
7254 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007255 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007256 break;
7257 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007258 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007259 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007260 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007261#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +02007262 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007263 break;
7264#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007265 case VAR_STRING:
7266 if (varp->vval.v_string != NULL)
7267 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007268 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007269 case VAR_SPECIAL:
7270 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7271 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007272 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007273#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007274 {
7275 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007276 char *status;
7277
7278 if (job == NULL)
7279 return (char_u *)"no process";
7280 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007281 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007282 : "run";
7283# ifdef UNIX
7284 vim_snprintf((char *)buf, NUMBUFLEN,
7285 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007286# elif defined(WIN32)
7287 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007288 "process %ld %s",
7289 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007290 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007291# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007292 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007293 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7294# endif
7295 return buf;
7296 }
7297#endif
7298 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007299 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007300#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007301 {
7302 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007303 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007304
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007305 if (channel == NULL)
7306 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7307 else
7308 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007309 "channel %d %s", channel->ch_id, status);
7310 return buf;
7311 }
7312#endif
7313 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007314 case VAR_UNKNOWN:
7315 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007316 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007318 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319}
7320
7321/*
7322 * Find variable "name" in the list of variables.
7323 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007324 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007325 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007326 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007328 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007329find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007332 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007333 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334
Bram Moolenaara7043832005-01-21 11:56:39 +00007335 ht = find_var_ht(name, &varname);
7336 if (htp != NULL)
7337 *htp = ht;
7338 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007340 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7341 if (ret != NULL)
7342 return ret;
7343
7344 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007345 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346}
7347
7348/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007349 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007350 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007352 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007353find_var_in_ht(
7354 hashtab_T *ht,
7355 int htname,
7356 char_u *varname,
7357 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007358{
Bram Moolenaar33570922005-01-25 22:26:29 +00007359 hashitem_T *hi;
7360
7361 if (*varname == NUL)
7362 {
7363 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007364 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007365 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007366 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007367 case 'g': return &globvars_var;
7368 case 'v': return &vimvars_var;
7369 case 'b': return &curbuf->b_bufvar;
7370 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007371#ifdef FEAT_WINDOWS
7372 case 't': return &curtab->tp_winvar;
7373#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007374 case 'l': return get_funccal_local_var();
7375 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007376 }
7377 return NULL;
7378 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007379
7380 hi = hash_find(ht, varname);
7381 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007382 {
7383 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007384 * worked find the variable again. Don't auto-load a script if it was
7385 * loaded already, otherwise it would be loaded every time when
7386 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007387 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007388 {
7389 /* Note: script_autoload() may make "hi" invalid. It must either
7390 * be obtained again or not used. */
7391 if (!script_autoload(varname, FALSE) || aborting())
7392 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007393 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007394 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007395 if (HASHITEM_EMPTY(hi))
7396 return NULL;
7397 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007398 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007399}
7400
7401/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007402 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007403 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007404 * Set "varname" to the start of name without ':'.
7405 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007406 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007407find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007409 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007410 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007411
Bram Moolenaar73627d02015-08-11 15:46:09 +02007412 if (name[0] == NUL)
7413 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 if (name[1] != ':')
7415 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007416 /* The name must not start with a colon or #. */
7417 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 return NULL;
7419 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007420
7421 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007422 hi = hash_find(&compat_hashtab, name);
7423 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +00007424 return &compat_hashtab;
7425
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007426 ht = get_funccal_local_ht();
7427 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007428 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007429 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 }
7431 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007432 if (*name == 'g') /* global variable */
7433 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007434 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7435 */
7436 if (vim_strchr(name + 2, ':') != NULL
7437 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007438 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007440 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007442 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007443#ifdef FEAT_WINDOWS
7444 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007445 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007446#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007447 if (*name == 'v') /* v: variable */
7448 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007449 if (*name == 'a') /* a: function argument */
7450 return get_funccal_args_ht();
7451 if (*name == 'l') /* l: local function variable */
7452 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 if (*name == 's' /* script variable */
7454 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
7455 return &SCRIPT_VARS(current_SID);
7456 return NULL;
7457}
7458
7459/*
7460 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +02007461 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 * Returns NULL when it doesn't exist.
7463 */
7464 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007465get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466{
Bram Moolenaar33570922005-01-25 22:26:29 +00007467 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007469 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 if (v == NULL)
7471 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007472 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473}
7474
7475/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007476 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 * sourcing this script and when executing functions defined in the script.
7478 */
7479 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007480new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481{
Bram Moolenaara7043832005-01-21 11:56:39 +00007482 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007483 hashtab_T *ht;
7484 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007485
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7487 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007488 /* Re-allocating ga_data means that an ht_array pointing to
7489 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007490 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007491 for (i = 1; i <= ga_scripts.ga_len; ++i)
7492 {
7493 ht = &SCRIPT_VARS(i);
7494 if (ht->ht_mask == HT_INIT_SIZE - 1)
7495 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007496 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007497 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007498 }
7499
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 while (ga_scripts.ga_len < id)
7501 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007502 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007503 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007504 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 }
7507 }
7508}
7509
7510/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007511 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7512 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 */
7514 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007515init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516{
Bram Moolenaar33570922005-01-25 22:26:29 +00007517 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007518 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007519 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007520 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007521 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007522 dict_var->di_tv.vval.v_dict = dict;
7523 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007524 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007525 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7526 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527}
7528
7529/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007530 * Unreference a dictionary initialized by init_var_dict().
7531 */
7532 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007533unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007534{
7535 /* Now the dict needs to be freed if no one else is using it, go back to
7536 * normal reference counting. */
7537 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7538 dict_unref(dict);
7539}
7540
7541/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007543 * Frees all allocated variables and the value they contain.
7544 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 */
7546 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007547vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007548{
7549 vars_clear_ext(ht, TRUE);
7550}
7551
7552/*
7553 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7554 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007555 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007556vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557{
Bram Moolenaara7043832005-01-21 11:56:39 +00007558 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007559 hashitem_T *hi;
7560 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561
Bram Moolenaar33570922005-01-25 22:26:29 +00007562 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007563 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007564 for (hi = ht->ht_array; todo > 0; ++hi)
7565 {
7566 if (!HASHITEM_EMPTY(hi))
7567 {
7568 --todo;
7569
Bram Moolenaar33570922005-01-25 22:26:29 +00007570 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007571 * ht_array might change then. hash_clear() takes care of it
7572 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007573 v = HI2DI(hi);
7574 if (free_val)
7575 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007576 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007577 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007578 }
7579 }
7580 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007581 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582}
7583
Bram Moolenaara7043832005-01-21 11:56:39 +00007584/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007585 * Delete a variable from hashtab "ht" at item "hi".
7586 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007587 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007589delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590{
Bram Moolenaar33570922005-01-25 22:26:29 +00007591 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007592
7593 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007594 clear_tv(&di->di_tv);
7595 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596}
7597
7598/*
7599 * List the value of one internal variable.
7600 */
7601 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007602list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007604 char_u *tofree;
7605 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007606 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007607
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007608 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007609 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007610 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007611 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612}
7613
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007615list_one_var_a(
7616 char_u *prefix,
7617 char_u *name,
7618 int type,
7619 char_u *string,
7620 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621{
Bram Moolenaar31859182007-08-14 20:41:13 +00007622 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7623 msg_start();
7624 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 if (name != NULL) /* "a:" vars don't have a name stored */
7626 msg_puts(name);
7627 msg_putchar(' ');
7628 msg_advance(22);
7629 if (type == VAR_NUMBER)
7630 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007631 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007632 msg_putchar('*');
7633 else if (type == VAR_LIST)
7634 {
7635 msg_putchar('[');
7636 if (*string == '[')
7637 ++string;
7638 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007639 else if (type == VAR_DICT)
7640 {
7641 msg_putchar('{');
7642 if (*string == '{')
7643 ++string;
7644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 else
7646 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007647
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007649
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007650 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007651 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007652 if (*first)
7653 {
7654 msg_clr_eos();
7655 *first = FALSE;
7656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657}
7658
7659/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007660 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 * If the variable already exists, the value is updated.
7662 * Otherwise the variable is created.
7663 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007664 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007665set_var(
7666 char_u *name,
7667 typval_T *tv,
7668 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669{
Bram Moolenaar33570922005-01-25 22:26:29 +00007670 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007672 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007674 ht = find_var_ht(name, &varname);
7675 if (ht == NULL || *varname == NUL)
7676 {
7677 EMSG2(_(e_illvar), name);
7678 return;
7679 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007680 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007681
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007682 /* Search in parent scope which is possible to reference from lambda */
7683 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007684 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007685
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007686 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7687 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007688 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007689
Bram Moolenaar33570922005-01-25 22:26:29 +00007690 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007692 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007693 if (var_check_ro(v->di_flags, name, FALSE)
7694 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007695 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007696
7697 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007698 * Handle setting internal v: variables separately where needed to
7699 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007700 */
7701 if (ht == &vimvarht)
7702 {
7703 if (v->di_tv.v_type == VAR_STRING)
7704 {
7705 vim_free(v->di_tv.vval.v_string);
7706 if (copy || tv->v_type != VAR_STRING)
7707 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
7708 else
7709 {
7710 /* Take over the string to avoid an extra alloc/free. */
7711 v->di_tv.vval.v_string = tv->vval.v_string;
7712 tv->vval.v_string = NULL;
7713 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007714 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007715 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007716 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007717 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007718 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007719 if (STRCMP(varname, "searchforward") == 0)
7720 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007721#ifdef FEAT_SEARCH_EXTRA
7722 else if (STRCMP(varname, "hlsearch") == 0)
7723 {
7724 no_hlsearch = !v->di_tv.vval.v_number;
7725 redraw_all_later(SOME_VALID);
7726 }
7727#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007728 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007729 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007730 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar95f09602016-11-10 20:01:45 +01007731 internal_error("set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +00007732 }
7733
7734 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 }
7736 else /* add a new variable */
7737 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007738 /* Can't add "v:" variable. */
7739 if (ht == &vimvarht)
7740 {
7741 EMSG2(_(e_illvar), name);
7742 return;
7743 }
7744
Bram Moolenaar92124a32005-06-17 22:03:40 +00007745 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007746 if (!valid_varname(varname))
7747 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00007748
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007749 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7750 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00007751 if (v == NULL)
7752 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007753 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00007754 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007756 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 return;
7758 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007759 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007761
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007762 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00007763 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007764 else
7765 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007766 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007767 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007768 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770}
7771
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007772/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007773 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00007774 * Also give an error message.
7775 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007776 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007777var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00007778{
7779 if (flags & DI_FLAGS_RO)
7780 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007781 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007782 return TRUE;
7783 }
7784 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
7785 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007786 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007787 return TRUE;
7788 }
7789 return FALSE;
7790}
7791
7792/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007793 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
7794 * Also give an error message.
7795 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007796 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007797var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007798{
7799 if (flags & DI_FLAGS_FIX)
7800 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007801 EMSG2(_("E795: Cannot delete variable %s"),
7802 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007803 return TRUE;
7804 }
7805 return FALSE;
7806}
7807
7808/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007809 * Check if a funcref is assigned to a valid variable name.
7810 * Return TRUE and give an error if not.
7811 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007812 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007813var_check_func_name(
7814 char_u *name, /* points to start of variable name */
7815 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007816{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02007817 /* Allow for w: b: s: and t:. */
7818 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007819 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
7820 ? name[2] : name[0]))
7821 {
7822 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
7823 name);
7824 return TRUE;
7825 }
7826 /* Don't allow hiding a function. When "v" is not NULL we might be
7827 * assigning another function to the same var, the type is checked
7828 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02007829 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007830 {
7831 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
7832 name);
7833 return TRUE;
7834 }
7835 return FALSE;
7836}
7837
7838/*
7839 * Check if a variable name is valid.
7840 * Return FALSE and give an error if not.
7841 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007842 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007843valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007844{
7845 char_u *p;
7846
7847 for (p = varname; *p != NUL; ++p)
7848 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
7849 && *p != AUTOLOAD_CHAR)
7850 {
7851 EMSG2(_(e_illvar), varname);
7852 return FALSE;
7853 }
7854 return TRUE;
7855}
7856
7857/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007858 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02007859 * Also give an error message, using "name" or _("name") when use_gettext is
7860 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007861 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007862 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007863tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007864{
7865 if (lock & VAR_LOCKED)
7866 {
7867 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007868 name == NULL ? (char_u *)_("Unknown")
7869 : use_gettext ? (char_u *)_(name)
7870 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007871 return TRUE;
7872 }
7873 if (lock & VAR_FIXED)
7874 {
7875 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007876 name == NULL ? (char_u *)_("Unknown")
7877 : use_gettext ? (char_u *)_(name)
7878 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007879 return TRUE;
7880 }
7881 return FALSE;
7882}
7883
7884/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007885 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007886 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007887 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007888 * It is OK for "from" and "to" to point to the same item. This is used to
7889 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007890 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007891 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007892copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007894 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007895 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007896 switch (from->v_type)
7897 {
7898 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007899 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007900 to->vval.v_number = from->vval.v_number;
7901 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007902 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007903#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007904 to->vval.v_float = from->vval.v_float;
7905 break;
7906#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007907 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007908#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007909 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007910 if (to->vval.v_job != NULL)
7911 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007912 break;
7913#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007914 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007915#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007916 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007917 if (to->vval.v_channel != NULL)
7918 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01007919 break;
7920#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007921 case VAR_STRING:
7922 case VAR_FUNC:
7923 if (from->vval.v_string == NULL)
7924 to->vval.v_string = NULL;
7925 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007926 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007927 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007928 if (from->v_type == VAR_FUNC)
7929 func_ref(to->vval.v_string);
7930 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007931 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007932 case VAR_PARTIAL:
7933 if (from->vval.v_partial == NULL)
7934 to->vval.v_partial = NULL;
7935 else
7936 {
7937 to->vval.v_partial = from->vval.v_partial;
7938 ++to->vval.v_partial->pt_refcount;
7939 }
7940 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007941 case VAR_LIST:
7942 if (from->vval.v_list == NULL)
7943 to->vval.v_list = NULL;
7944 else
7945 {
7946 to->vval.v_list = from->vval.v_list;
7947 ++to->vval.v_list->lv_refcount;
7948 }
7949 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007950 case VAR_DICT:
7951 if (from->vval.v_dict == NULL)
7952 to->vval.v_dict = NULL;
7953 else
7954 {
7955 to->vval.v_dict = from->vval.v_dict;
7956 ++to->vval.v_dict->dv_refcount;
7957 }
7958 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007959 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007960 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007961 break;
7962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963}
7964
7965/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007966 * Make a copy of an item.
7967 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007968 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7969 * reference to an already copied list/dict can be used.
7970 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007971 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007972 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007973item_copy(
7974 typval_T *from,
7975 typval_T *to,
7976 int deep,
7977 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007978{
7979 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007980 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007981
Bram Moolenaar33570922005-01-25 22:26:29 +00007982 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007983 {
7984 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007985 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007986 }
7987 ++recurse;
7988
7989 switch (from->v_type)
7990 {
7991 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007992 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007993 case VAR_STRING:
7994 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007995 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007996 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007997 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007998 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007999 copy_tv(from, to);
8000 break;
8001 case VAR_LIST:
8002 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008003 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008004 if (from->vval.v_list == NULL)
8005 to->vval.v_list = NULL;
8006 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8007 {
8008 /* use the copy made earlier */
8009 to->vval.v_list = from->vval.v_list->lv_copylist;
8010 ++to->vval.v_list->lv_refcount;
8011 }
8012 else
8013 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8014 if (to->vval.v_list == NULL)
8015 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008016 break;
8017 case VAR_DICT:
8018 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008019 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008020 if (from->vval.v_dict == NULL)
8021 to->vval.v_dict = NULL;
8022 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8023 {
8024 /* use the copy made earlier */
8025 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8026 ++to->vval.v_dict->dv_refcount;
8027 }
8028 else
8029 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8030 if (to->vval.v_dict == NULL)
8031 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008032 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008033 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008034 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008035 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008036 }
8037 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008038 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008039}
8040
8041/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008042 * This function is used by f_input() and f_inputdialog() functions. The third
8043 * argument to f_input() specifies the type of completion to use at the
8044 * prompt. The third argument to f_inputdialog() specifies the value to return
8045 * when the user cancels the prompt.
8046 */
8047 void
8048get_user_input(
8049 typval_T *argvars,
8050 typval_T *rettv,
8051 int inputdialog,
8052 int secret)
8053{
8054 char_u *prompt = get_tv_string_chk(&argvars[0]);
8055 char_u *p = NULL;
8056 int c;
8057 char_u buf[NUMBUFLEN];
8058 int cmd_silent_save = cmd_silent;
8059 char_u *defstr = (char_u *)"";
8060 int xp_type = EXPAND_NOTHING;
8061 char_u *xp_arg = NULL;
8062
8063 rettv->v_type = VAR_STRING;
8064 rettv->vval.v_string = NULL;
8065
8066#ifdef NO_CONSOLE_INPUT
8067 /* While starting up, there is no place to enter text. */
8068 if (no_console_input())
8069 return;
8070#endif
8071
8072 cmd_silent = FALSE; /* Want to see the prompt. */
8073 if (prompt != NULL)
8074 {
8075 /* Only the part of the message after the last NL is considered as
8076 * prompt for the command line */
8077 p = vim_strrchr(prompt, '\n');
8078 if (p == NULL)
8079 p = prompt;
8080 else
8081 {
8082 ++p;
8083 c = *p;
8084 *p = NUL;
8085 msg_start();
8086 msg_clr_eos();
8087 msg_puts_attr(prompt, echo_attr);
8088 msg_didout = FALSE;
8089 msg_starthere();
8090 *p = c;
8091 }
8092 cmdline_row = msg_row;
8093
8094 if (argvars[1].v_type != VAR_UNKNOWN)
8095 {
8096 defstr = get_tv_string_buf_chk(&argvars[1], buf);
8097 if (defstr != NULL)
8098 stuffReadbuffSpec(defstr);
8099
8100 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8101 {
8102 char_u *xp_name;
8103 int xp_namelen;
8104 long argt;
8105
8106 /* input() with a third argument: completion */
8107 rettv->vval.v_string = NULL;
8108
8109 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
8110 if (xp_name == NULL)
8111 return;
8112
8113 xp_namelen = (int)STRLEN(xp_name);
8114
8115 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8116 &xp_arg) == FAIL)
8117 return;
8118 }
8119 }
8120
8121 if (defstr != NULL)
8122 {
8123 int save_ex_normal_busy = ex_normal_busy;
8124 ex_normal_busy = 0;
8125 rettv->vval.v_string =
8126 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8127 xp_type, xp_arg);
8128 ex_normal_busy = save_ex_normal_busy;
8129 }
8130 if (inputdialog && rettv->vval.v_string == NULL
8131 && argvars[1].v_type != VAR_UNKNOWN
8132 && argvars[2].v_type != VAR_UNKNOWN)
8133 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
8134 &argvars[2], buf));
8135
8136 vim_free(xp_arg);
8137
8138 /* since the user typed this, no need to wait for return */
8139 need_wait_return = FALSE;
8140 msg_didout = FALSE;
8141 }
8142 cmd_silent = cmd_silent_save;
8143}
8144
8145/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 * ":echo expr1 ..." print each argument separated with a space, add a
8147 * newline at the end.
8148 * ":echon expr1 ..." print each argument plain.
8149 */
8150 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008151ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152{
8153 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008154 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008155 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 char_u *p;
8157 int needclr = TRUE;
8158 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008159 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160
8161 if (eap->skip)
8162 ++emsg_skip;
8163 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8164 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008165 /* If eval1() causes an error message the text from the command may
8166 * still need to be cleared. E.g., "echo 22,44". */
8167 need_clr_eos = needclr;
8168
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008170 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 {
8172 /*
8173 * Report the invalid expression unless the expression evaluation
8174 * has been cancelled due to an aborting error, an interrupt, or an
8175 * exception.
8176 */
8177 if (!aborting())
8178 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008179 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 break;
8181 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008182 need_clr_eos = FALSE;
8183
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 if (!eap->skip)
8185 {
8186 if (atstart)
8187 {
8188 atstart = FALSE;
8189 /* Call msg_start() after eval1(), evaluating the expression
8190 * may cause a message to appear. */
8191 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008192 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008193 /* Mark the saved text as finishing the line, so that what
8194 * follows is displayed on a new line when scrolling back
8195 * at the more prompt. */
8196 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 }
8200 else if (eap->cmdidx == CMD_echo)
8201 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008202 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008203 if (p != NULL)
8204 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008206 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008208 if (*p != TAB && needclr)
8209 {
8210 /* remove any text still there from the command */
8211 msg_clr_eos();
8212 needclr = FALSE;
8213 }
8214 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 }
8216 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008217 {
8218#ifdef FEAT_MBYTE
8219 if (has_mbyte)
8220 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008221 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008222
8223 (void)msg_outtrans_len_attr(p, i, echo_attr);
8224 p += i - 1;
8225 }
8226 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008228 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008231 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008233 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 arg = skipwhite(arg);
8235 }
8236 eap->nextcmd = check_nextcmd(arg);
8237
8238 if (eap->skip)
8239 --emsg_skip;
8240 else
8241 {
8242 /* remove text that may still be there from the command */
8243 if (needclr)
8244 msg_clr_eos();
8245 if (eap->cmdidx == CMD_echo)
8246 msg_end();
8247 }
8248}
8249
8250/*
8251 * ":echohl {name}".
8252 */
8253 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008254ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255{
8256 int id;
8257
8258 id = syn_name2id(eap->arg);
8259 if (id == 0)
8260 echo_attr = 0;
8261 else
8262 echo_attr = syn_id2attr(id);
8263}
8264
8265/*
8266 * ":execute expr1 ..." execute the result of an expression.
8267 * ":echomsg expr1 ..." Print a message
8268 * ":echoerr expr1 ..." Print an error
8269 * Each gets spaces around each argument and a newline at the end for
8270 * echo commands
8271 */
8272 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008273ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274{
8275 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008276 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 int ret = OK;
8278 char_u *p;
8279 garray_T ga;
8280 int len;
8281 int save_did_emsg;
8282
8283 ga_init2(&ga, 1, 80);
8284
8285 if (eap->skip)
8286 ++emsg_skip;
8287 while (*arg != NUL && *arg != '|' && *arg != '\n')
8288 {
8289 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008290 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 {
8292 /*
8293 * Report the invalid expression unless the expression evaluation
8294 * has been cancelled due to an aborting error, an interrupt, or an
8295 * exception.
8296 */
8297 if (!aborting())
8298 EMSG2(_(e_invexpr2), p);
8299 ret = FAIL;
8300 break;
8301 }
8302
8303 if (!eap->skip)
8304 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008305 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 len = (int)STRLEN(p);
8307 if (ga_grow(&ga, len + 2) == FAIL)
8308 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008309 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 ret = FAIL;
8311 break;
8312 }
8313 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 ga.ga_len += len;
8317 }
8318
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008319 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 arg = skipwhite(arg);
8321 }
8322
8323 if (ret != FAIL && ga.ga_data != NULL)
8324 {
8325 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008326 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008328 out_flush();
8329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 else if (eap->cmdidx == CMD_echoerr)
8331 {
8332 /* We don't want to abort following commands, restore did_emsg. */
8333 save_did_emsg = did_emsg;
8334 EMSG((char_u *)ga.ga_data);
8335 if (!force_abort)
8336 did_emsg = save_did_emsg;
8337 }
8338 else if (eap->cmdidx == CMD_execute)
8339 do_cmdline((char_u *)ga.ga_data,
8340 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8341 }
8342
8343 ga_clear(&ga);
8344
8345 if (eap->skip)
8346 --emsg_skip;
8347
8348 eap->nextcmd = check_nextcmd(arg);
8349}
8350
8351/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008352 * Find window specified by "vp" in tabpage "tp".
8353 */
8354 win_T *
8355find_win_by_nr(
8356 typval_T *vp,
8357 tabpage_T *tp UNUSED) /* NULL for current tab page */
8358{
8359#ifdef FEAT_WINDOWS
8360 win_T *wp;
8361#endif
8362 int nr;
8363
8364 nr = (int)get_tv_number_chk(vp, NULL);
8365
8366#ifdef FEAT_WINDOWS
8367 if (nr < 0)
8368 return NULL;
8369 if (nr == 0)
8370 return curwin;
8371
Bram Moolenaar29323592016-07-24 22:04:11 +02008372 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8373 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008374 if (nr >= LOWEST_WIN_ID)
8375 {
8376 if (wp->w_id == nr)
8377 return wp;
8378 }
8379 else if (--nr <= 0)
8380 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008381 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008382 if (nr >= LOWEST_WIN_ID)
8383 return NULL;
8384 return wp;
8385#else
8386 if (nr == 0 || nr == 1 || nr == curwin->w_id)
8387 return curwin;
8388 return NULL;
8389#endif
8390}
8391
8392/*
8393 * Find window specified by "wvp" in tabpage "tvp".
8394 */
8395 win_T *
8396find_tabwin(
8397 typval_T *wvp, /* VAR_UNKNOWN for current window */
8398 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
8399{
8400 win_T *wp = NULL;
8401 tabpage_T *tp = NULL;
8402 long n;
8403
8404 if (wvp->v_type != VAR_UNKNOWN)
8405 {
8406 if (tvp->v_type != VAR_UNKNOWN)
8407 {
8408 n = (long)get_tv_number(tvp);
8409 if (n >= 0)
8410 tp = find_tabpage(n);
8411 }
8412 else
8413 tp = curtab;
8414
8415 if (tp != NULL)
8416 wp = find_win_by_nr(wvp, tp);
8417 }
8418 else
8419 wp = curwin;
8420
8421 return wp;
8422}
8423
8424/*
8425 * getwinvar() and gettabwinvar()
8426 */
8427 void
8428getwinvar(
8429 typval_T *argvars,
8430 typval_T *rettv,
8431 int off) /* 1 for gettabwinvar() */
8432{
8433 win_T *win;
8434 char_u *varname;
8435 dictitem_T *v;
8436 tabpage_T *tp = NULL;
8437 int done = FALSE;
8438#ifdef FEAT_WINDOWS
8439 win_T *oldcurwin;
8440 tabpage_T *oldtabpage;
8441 int need_switch_win;
8442#endif
8443
8444#ifdef FEAT_WINDOWS
8445 if (off == 1)
8446 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8447 else
8448 tp = curtab;
8449#endif
8450 win = find_win_by_nr(&argvars[off], tp);
8451 varname = get_tv_string_chk(&argvars[off + 1]);
8452 ++emsg_off;
8453
8454 rettv->v_type = VAR_STRING;
8455 rettv->vval.v_string = NULL;
8456
8457 if (win != NULL && varname != NULL)
8458 {
8459#ifdef FEAT_WINDOWS
8460 /* Set curwin to be our win, temporarily. Also set the tabpage,
8461 * otherwise the window is not valid. Only do this when needed,
8462 * autocommands get blocked. */
8463 need_switch_win = !(tp == curtab && win == curwin);
8464 if (!need_switch_win
8465 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
8466#endif
8467 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008468 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008469 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008470 if (varname[1] == NUL)
8471 {
8472 /* get all window-local options in a dict */
8473 dict_T *opts = get_winbuf_options(FALSE);
8474
8475 if (opts != NULL)
8476 {
8477 rettv->v_type = VAR_DICT;
8478 rettv->vval.v_dict = opts;
8479 ++opts->dv_refcount;
8480 done = TRUE;
8481 }
8482 }
8483 else if (get_option_tv(&varname, rettv, 1) == OK)
8484 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008485 done = TRUE;
8486 }
8487 else
8488 {
8489 /* Look up the variable. */
8490 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8491 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8492 varname, FALSE);
8493 if (v != NULL)
8494 {
8495 copy_tv(&v->di_tv, rettv);
8496 done = TRUE;
8497 }
8498 }
8499 }
8500
8501#ifdef FEAT_WINDOWS
8502 if (need_switch_win)
8503 /* restore previous notion of curwin */
8504 restore_win(oldcurwin, oldtabpage, TRUE);
8505#endif
8506 }
8507
8508 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8509 /* use the default return value */
8510 copy_tv(&argvars[off + 2], rettv);
8511
8512 --emsg_off;
8513}
8514
8515/*
8516 * "setwinvar()" and "settabwinvar()" functions
8517 */
8518 void
8519setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8520{
8521 win_T *win;
8522#ifdef FEAT_WINDOWS
8523 win_T *save_curwin;
8524 tabpage_T *save_curtab;
8525 int need_switch_win;
8526#endif
8527 char_u *varname, *winvarname;
8528 typval_T *varp;
8529 char_u nbuf[NUMBUFLEN];
8530 tabpage_T *tp = NULL;
8531
8532 if (check_restricted() || check_secure())
8533 return;
8534
8535#ifdef FEAT_WINDOWS
8536 if (off == 1)
8537 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8538 else
8539 tp = curtab;
8540#endif
8541 win = find_win_by_nr(&argvars[off], tp);
8542 varname = get_tv_string_chk(&argvars[off + 1]);
8543 varp = &argvars[off + 2];
8544
8545 if (win != NULL && varname != NULL && varp != NULL)
8546 {
8547#ifdef FEAT_WINDOWS
8548 need_switch_win = !(tp == curtab && win == curwin);
8549 if (!need_switch_win
8550 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
8551#endif
8552 {
8553 if (*varname == '&')
8554 {
8555 long numval;
8556 char_u *strval;
8557 int error = FALSE;
8558
8559 ++varname;
8560 numval = (long)get_tv_number_chk(varp, &error);
8561 strval = get_tv_string_buf_chk(varp, nbuf);
8562 if (!error && strval != NULL)
8563 set_option_value(varname, numval, strval, OPT_LOCAL);
8564 }
8565 else
8566 {
8567 winvarname = alloc((unsigned)STRLEN(varname) + 3);
8568 if (winvarname != NULL)
8569 {
8570 STRCPY(winvarname, "w:");
8571 STRCPY(winvarname + 2, varname);
8572 set_var(winvarname, varp, TRUE);
8573 vim_free(winvarname);
8574 }
8575 }
8576 }
8577#ifdef FEAT_WINDOWS
8578 if (need_switch_win)
8579 restore_win(save_curwin, save_curtab, TRUE);
8580#endif
8581 }
8582}
8583
8584/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8586 * "arg" points to the "&" or '+' when called, to "option" when returning.
8587 * Returns NULL when no option name found. Otherwise pointer to the char
8588 * after the option name.
8589 */
8590 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008591find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592{
8593 char_u *p = *arg;
8594
8595 ++p;
8596 if (*p == 'g' && p[1] == ':')
8597 {
8598 *opt_flags = OPT_GLOBAL;
8599 p += 2;
8600 }
8601 else if (*p == 'l' && p[1] == ':')
8602 {
8603 *opt_flags = OPT_LOCAL;
8604 p += 2;
8605 }
8606 else
8607 *opt_flags = 0;
8608
8609 if (!ASCII_ISALPHA(*p))
8610 return NULL;
8611 *arg = p;
8612
8613 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
8614 p += 4; /* termcap option */
8615 else
8616 while (ASCII_ISALPHA(*p))
8617 ++p;
8618 return p;
8619}
8620
8621/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008622 * Return the autoload script name for a function or variable name.
8623 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02008625 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008626autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02008627{
Bram Moolenaara1544c02013-05-30 12:35:52 +02008628 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008629 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02008630
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008631 /* Get the script file name: replace '#' with '/', append ".vim". */
8632 scriptname = alloc((unsigned)(STRLEN(name) + 14));
8633 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008634 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008635 STRCPY(scriptname, "autoload/");
8636 STRCAT(scriptname, name);
8637 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
8638 STRCAT(scriptname, ".vim");
8639 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
8640 *p = '/';
8641 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008642}
8643
8644/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008645 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008646 * Return TRUE if a package was loaded.
8647 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008648 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008649script_autoload(
8650 char_u *name,
8651 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008652{
8653 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008654 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008655 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008656 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008657
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008658 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008659 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008660 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008661 return FALSE;
8662
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008663 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008664
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008665 /* Find the name in the list of previously loaded package names. Skip
8666 * "autoload/", it's always the same. */
8667 for (i = 0; i < ga_loaded.ga_len; ++i)
8668 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
8669 break;
8670 if (!reload && i < ga_loaded.ga_len)
8671 ret = FALSE; /* was loaded already */
8672 else
8673 {
8674 /* Remember the name if it wasn't loaded already. */
8675 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
8676 {
8677 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
8678 tofree = NULL;
8679 }
8680
8681 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008682 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008683 ret = TRUE;
8684 }
8685
8686 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008687 return ret;
8688}
8689
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
8691typedef enum
8692{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008693 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
8694 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
8695 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696} var_flavour_T;
8697
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008698static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699
8700 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01008701var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702{
8703 char_u *p = varname;
8704
8705 if (ASCII_ISUPPER(*p))
8706 {
8707 while (*(++p))
8708 if (ASCII_ISLOWER(*p))
8709 return VAR_FLAVOUR_SESSION;
8710 return VAR_FLAVOUR_VIMINFO;
8711 }
8712 else
8713 return VAR_FLAVOUR_DEFAULT;
8714}
8715#endif
8716
8717#if defined(FEAT_VIMINFO) || defined(PROTO)
8718/*
8719 * Restore global vars that start with a capital from the viminfo file
8720 */
8721 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008722read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723{
8724 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008725 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00008726 typval_T tv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008727 void *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728
8729 if (!writing && (find_viminfo_parameter('!') != NULL))
8730 {
8731 tab = vim_strchr(virp->vir_line + 1, '\t');
8732 if (tab != NULL)
8733 {
8734 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008735 switch (*tab)
8736 {
8737 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008738#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008739 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008740#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008741 case 'D': type = VAR_DICT; break;
8742 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008743 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745
8746 tab = vim_strchr(tab, '\t');
8747 if (tab != NULL)
8748 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008749 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008750 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008751 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008753#ifdef FEAT_FLOAT
8754 else if (type == VAR_FLOAT)
8755 (void)string2float(tab + 1, &tv.vval.v_float);
8756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008758 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008759 if (type == VAR_DICT || type == VAR_LIST)
8760 {
8761 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
8762
8763 if (etv == NULL)
8764 /* Failed to parse back the dict or list, use it as a
8765 * string. */
8766 tv.v_type = VAR_STRING;
8767 else
8768 {
8769 vim_free(tv.vval.v_string);
8770 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01008771 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008772 }
8773 }
8774
Bram Moolenaarb20e3342016-01-18 23:29:01 +01008775 /* when in a function use global variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008776 save_funccal = clear_current_funccal();
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008777 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008778 restore_current_funccal(save_funccal);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008779
8780 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008781 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008782 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
8783 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 }
8785 }
8786 }
8787
8788 return viminfo_readline(virp);
8789}
8790
8791/*
8792 * Write global vars that start with a capital to the viminfo file
8793 */
8794 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008795write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796{
Bram Moolenaar33570922005-01-25 22:26:29 +00008797 hashitem_T *hi;
8798 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008799 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01008800 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008801 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008802 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008803 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804
8805 if (find_viminfo_parameter('!') == NULL)
8806 return;
8807
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02008808 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00008809
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008810 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008811 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008813 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008815 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008816 this_var = HI2DI(hi);
8817 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008818 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008819 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00008820 {
8821 case VAR_STRING: s = "STR"; break;
8822 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008823 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008824 case VAR_DICT: s = "DIC"; break;
8825 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008826 case VAR_SPECIAL: s = "XPL"; break;
8827
8828 case VAR_UNKNOWN:
8829 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008830 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008831 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008832 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008833 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00008834 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008835 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008836 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008837 if (p != NULL)
8838 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00008839 vim_free(tofree);
8840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 }
8842 }
8843}
8844#endif
8845
8846#if defined(FEAT_SESSION) || defined(PROTO)
8847 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008848store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849{
Bram Moolenaar33570922005-01-25 22:26:29 +00008850 hashitem_T *hi;
8851 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008852 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 char_u *p, *t;
8854
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008855 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008856 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008858 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008860 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008861 this_var = HI2DI(hi);
8862 if ((this_var->di_tv.v_type == VAR_NUMBER
8863 || this_var->di_tv.v_type == VAR_STRING)
8864 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008865 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008866 /* Escape special characters with a backslash. Turn a LF and
8867 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008868 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00008869 (char_u *)"\\\"\n\r");
8870 if (p == NULL) /* out of memory */
8871 break;
8872 for (t = p; *t != NUL; ++t)
8873 if (*t == '\n')
8874 *t = 'n';
8875 else if (*t == '\r')
8876 *t = 'r';
8877 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00008878 this_var->di_key,
8879 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8880 : ' ',
8881 p,
8882 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8883 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00008884 || put_eol(fd) == FAIL)
8885 {
8886 vim_free(p);
8887 return FAIL;
8888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 vim_free(p);
8890 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008891#ifdef FEAT_FLOAT
8892 else if (this_var->di_tv.v_type == VAR_FLOAT
8893 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
8894 {
8895 float_T f = this_var->di_tv.vval.v_float;
8896 int sign = ' ';
8897
8898 if (f < 0)
8899 {
8900 f = -f;
8901 sign = '-';
8902 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01008903 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008904 this_var->di_key, sign, f) < 0)
8905 || put_eol(fd) == FAIL)
8906 return FAIL;
8907 }
8908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 }
8910 }
8911 return OK;
8912}
8913#endif
8914
Bram Moolenaar661b1822005-07-28 22:36:45 +00008915/*
8916 * Display script name where an item was last set.
8917 * Should only be invoked when 'verbose' is non-zero.
8918 */
8919 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008920last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +00008921{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008922 char_u *p;
8923
Bram Moolenaar661b1822005-07-28 22:36:45 +00008924 if (scriptID != 0)
8925 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008926 p = home_replace_save(NULL, get_scriptname(scriptID));
8927 if (p != NULL)
8928 {
8929 verbose_enter();
8930 MSG_PUTS(_("\n\tLast set from "));
8931 MSG_PUTS(p);
8932 vim_free(p);
8933 verbose_leave();
8934 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00008935 }
8936}
8937
Bram Moolenaar53744302015-07-17 17:38:22 +02008938/* reset v:option_new, v:option_old and v:option_type */
8939 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008940reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02008941{
8942 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
8943 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
8944 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
8945}
8946
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008947/*
8948 * Prepare "gap" for an assert error and add the sourcing position.
8949 */
8950 void
8951prepare_assert_error(garray_T *gap)
8952{
8953 char buf[NUMBUFLEN];
8954
8955 ga_init2(gap, 1, 100);
8956 if (sourcing_name != NULL)
8957 {
8958 ga_concat(gap, sourcing_name);
8959 if (sourcing_lnum > 0)
8960 ga_concat(gap, (char_u *)" ");
8961 }
8962 if (sourcing_lnum > 0)
8963 {
8964 sprintf(buf, "line %ld", (long)sourcing_lnum);
8965 ga_concat(gap, (char_u *)buf);
8966 }
8967 if (sourcing_name != NULL || sourcing_lnum > 0)
8968 ga_concat(gap, (char_u *)": ");
8969}
8970
8971/*
8972 * Add an assert error to v:errors.
8973 */
8974 void
8975assert_error(garray_T *gap)
8976{
8977 struct vimvar *vp = &vimvars[VV_ERRORS];
8978
8979 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
8980 /* Make sure v:errors is a list. */
8981 set_vim_var_list(VV_ERRORS, list_alloc());
8982 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
8983}
8984
8985 void
8986assert_equal_common(typval_T *argvars, assert_type_T atype)
8987{
8988 garray_T ga;
8989
8990 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
8991 != (atype == ASSERT_EQUAL))
8992 {
8993 prepare_assert_error(&ga);
8994 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
8995 atype);
8996 assert_error(&ga);
8997 ga_clear(&ga);
8998 }
8999}
9000
9001 void
9002assert_match_common(typval_T *argvars, assert_type_T atype)
9003{
9004 garray_T ga;
9005 char_u buf1[NUMBUFLEN];
9006 char_u buf2[NUMBUFLEN];
9007 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9008 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9009
9010 if (pat == NULL || text == NULL)
9011 EMSG(_(e_invarg));
9012 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
9013 {
9014 prepare_assert_error(&ga);
9015 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9016 atype);
9017 assert_error(&ga);
9018 ga_clear(&ga);
9019 }
9020}
9021
Bram Moolenaar61c04492016-07-23 15:35:35 +02009022 void
9023assert_inrange(typval_T *argvars)
9024{
9025 garray_T ga;
9026 int error = FALSE;
9027 varnumber_T lower = get_tv_number_chk(&argvars[0], &error);
9028 varnumber_T upper = get_tv_number_chk(&argvars[1], &error);
9029 varnumber_T actual = get_tv_number_chk(&argvars[2], &error);
9030 char_u *tofree;
9031 char msg[200];
9032 char_u numbuf[NUMBUFLEN];
9033
9034 if (error)
9035 return;
9036 if (actual < lower || actual > upper)
9037 {
9038 prepare_assert_error(&ga);
9039 if (argvars[3].v_type != VAR_UNKNOWN)
9040 {
9041 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9042 vim_free(tofree);
9043 }
9044 else
9045 {
9046 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
9047 (long)lower, (long)upper, (long)actual);
9048 ga_concat(&ga, (char_u *)msg);
9049 }
9050 assert_error(&ga);
9051 ga_clear(&ga);
9052 }
9053}
9054
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009055/*
9056 * Common for assert_true() and assert_false().
9057 */
9058 void
9059assert_bool(typval_T *argvars, int isTrue)
9060{
9061 int error = FALSE;
9062 garray_T ga;
9063
9064 if (argvars[0].v_type == VAR_SPECIAL
9065 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
9066 return;
9067 if (argvars[0].v_type != VAR_NUMBER
9068 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9069 || error)
9070 {
9071 prepare_assert_error(&ga);
9072 fill_assert_error(&ga, &argvars[1],
9073 (char_u *)(isTrue ? "True" : "False"),
9074 NULL, &argvars[0], ASSERT_OTHER);
9075 assert_error(&ga);
9076 ga_clear(&ga);
9077 }
9078}
9079
9080 void
9081assert_exception(typval_T *argvars)
9082{
9083 garray_T ga;
9084 char_u *error = get_tv_string_chk(&argvars[0]);
9085
9086 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9087 {
9088 prepare_assert_error(&ga);
9089 ga_concat(&ga, (char_u *)"v:exception is not set");
9090 assert_error(&ga);
9091 ga_clear(&ga);
9092 }
9093 else if (error != NULL
9094 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9095 {
9096 prepare_assert_error(&ga);
9097 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9098 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9099 assert_error(&ga);
9100 ga_clear(&ga);
9101 }
9102}
9103
9104 void
9105assert_fails(typval_T *argvars)
9106{
9107 char_u *cmd = get_tv_string_chk(&argvars[0]);
9108 garray_T ga;
9109
9110 called_emsg = FALSE;
9111 suppress_errthrow = TRUE;
9112 emsg_silent = TRUE;
9113 do_cmdline_cmd(cmd);
9114 if (!called_emsg)
9115 {
9116 prepare_assert_error(&ga);
9117 ga_concat(&ga, (char_u *)"command did not fail: ");
9118 ga_concat(&ga, cmd);
9119 assert_error(&ga);
9120 ga_clear(&ga);
9121 }
9122 else if (argvars[1].v_type != VAR_UNKNOWN)
9123 {
9124 char_u buf[NUMBUFLEN];
9125 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9126
9127 if (error == NULL
9128 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9129 {
9130 prepare_assert_error(&ga);
9131 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9132 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
9133 assert_error(&ga);
9134 ga_clear(&ga);
9135 }
9136 }
9137
9138 called_emsg = FALSE;
9139 suppress_errthrow = FALSE;
9140 emsg_silent = FALSE;
9141 emsg_on_display = FALSE;
9142 set_vim_var_string(VV_ERRMSG, NULL, 0);
9143}
9144
9145/*
9146 * Append "str" to "gap", escaping unprintable characters.
9147 * Changes NL to \n, CR to \r, etc.
9148 */
9149 static void
9150ga_concat_esc(garray_T *gap, char_u *str)
9151{
9152 char_u *p;
9153 char_u buf[NUMBUFLEN];
9154
9155 if (str == NULL)
9156 {
9157 ga_concat(gap, (char_u *)"NULL");
9158 return;
9159 }
9160
9161 for (p = str; *p != NUL; ++p)
9162 switch (*p)
9163 {
9164 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9165 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9166 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9167 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9168 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9169 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9170 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9171 default:
9172 if (*p < ' ')
9173 {
9174 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9175 ga_concat(gap, buf);
9176 }
9177 else
9178 ga_append(gap, *p);
9179 break;
9180 }
9181}
9182
9183/*
9184 * Fill "gap" with information about an assert error.
9185 */
9186 void
9187fill_assert_error(
9188 garray_T *gap,
9189 typval_T *opt_msg_tv,
9190 char_u *exp_str,
9191 typval_T *exp_tv,
9192 typval_T *got_tv,
9193 assert_type_T atype)
9194{
9195 char_u numbuf[NUMBUFLEN];
9196 char_u *tofree;
9197
9198 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9199 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009200 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9201 vim_free(tofree);
9202 ga_concat(gap, (char_u *)": ");
9203 }
9204
9205 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9206 ga_concat(gap, (char_u *)"Pattern ");
9207 else if (atype == ASSERT_NOTEQUAL)
9208 ga_concat(gap, (char_u *)"Expected not equal to ");
9209 else
9210 ga_concat(gap, (char_u *)"Expected ");
9211 if (exp_str == NULL)
9212 {
9213 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009214 vim_free(tofree);
9215 }
9216 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009217 ga_concat_esc(gap, exp_str);
9218 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009219 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009220 if (atype == ASSERT_MATCH)
9221 ga_concat(gap, (char_u *)" does not match ");
9222 else if (atype == ASSERT_NOTMATCH)
9223 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009224 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009225 ga_concat(gap, (char_u *)" but got ");
9226 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
9227 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009228 }
9229}
9230
Bram Moolenaar53744302015-07-17 17:38:22 +02009231
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232#endif /* FEAT_EVAL */
9233
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009235#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236
9237#ifdef WIN3264
9238/*
9239 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9240 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009241static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
9242static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
9243static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244
9245/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009246 * Get the short path (8.3) for the filename in "fnamep".
9247 * Only works for a valid file name.
9248 * When the path gets longer "fnamep" is changed and the allocated buffer
9249 * is put in "bufp".
9250 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9251 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252 */
9253 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009254get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009256 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 char_u *newbuf;
9258
9259 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009260 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261 if (l > len - 1)
9262 {
9263 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009264 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265 newbuf = vim_strnsave(*fnamep, l);
9266 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009267 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268
9269 vim_free(*bufp);
9270 *fnamep = *bufp = newbuf;
9271
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009272 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009273 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274 }
9275
9276 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009277 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278}
9279
9280/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009281 * Get the short path (8.3) for the filename in "fname". The converted
9282 * path is returned in "bufp".
9283 *
9284 * Some of the directories specified in "fname" may not exist. This function
9285 * will shorten the existing directories at the beginning of the path and then
9286 * append the remaining non-existing path.
9287 *
9288 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009289 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009290 * bufp - Pointer to an allocated buffer for the filename.
9291 * fnamelen - Length of the filename pointed to by fname
9292 *
9293 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294 */
9295 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009296shortpath_for_invalid_fname(
9297 char_u **fname,
9298 char_u **bufp,
9299 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009301 char_u *short_fname, *save_fname, *pbuf_unused;
9302 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009304 int old_len, len;
9305 int new_len, sfx_len;
9306 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307
9308 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009309 old_len = *fnamelen;
9310 save_fname = vim_strnsave(*fname, old_len);
9311 pbuf_unused = NULL;
9312 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009314 endp = save_fname + old_len - 1; /* Find the end of the copy */
9315 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009317 /*
9318 * Try shortening the supplied path till it succeeds by removing one
9319 * directory at a time from the tail of the path.
9320 */
9321 len = 0;
9322 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009324 /* go back one path-separator */
9325 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9326 --endp;
9327 if (endp <= save_fname)
9328 break; /* processed the complete path */
9329
9330 /*
9331 * Replace the path separator with a NUL and try to shorten the
9332 * resulting path.
9333 */
9334 ch = *endp;
9335 *endp = 0;
9336 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009337 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009338 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9339 {
9340 retval = FAIL;
9341 goto theend;
9342 }
9343 *endp = ch; /* preserve the string */
9344
9345 if (len > 0)
9346 break; /* successfully shortened the path */
9347
9348 /* failed to shorten the path. Skip the path separator */
9349 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 }
9351
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009352 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009354 /*
9355 * Succeeded in shortening the path. Now concatenate the shortened
9356 * path with the remaining path at the tail.
9357 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009359 /* Compute the length of the new path. */
9360 sfx_len = (int)(save_endp - endp) + 1;
9361 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009363 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009365 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009367 /* There is not enough space in the currently allocated string,
9368 * copy it to a buffer big enough. */
9369 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009371 {
9372 retval = FAIL;
9373 goto theend;
9374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375 }
9376 else
9377 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009378 /* Transfer short_fname to the main buffer (it's big enough),
9379 * unless get_short_pathname() did its work in-place. */
9380 *fname = *bufp = save_fname;
9381 if (short_fname != save_fname)
9382 vim_strncpy(save_fname, short_fname, len);
9383 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009385
9386 /* concat the not-shortened part of the path */
9387 vim_strncpy(*fname + len, endp, sfx_len);
9388 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009390
9391theend:
9392 vim_free(pbuf_unused);
9393 vim_free(save_fname);
9394
9395 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396}
9397
9398/*
9399 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009400 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 */
9402 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009403shortpath_for_partial(
9404 char_u **fnamep,
9405 char_u **bufp,
9406 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407{
9408 int sepcount, len, tflen;
9409 char_u *p;
9410 char_u *pbuf, *tfname;
9411 int hasTilde;
9412
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009413 /* Count up the path separators from the RHS.. so we know which part
9414 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009416 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417 if (vim_ispathsep(*p))
9418 ++sepcount;
9419
9420 /* Need full path first (use expand_env() to remove a "~/") */
9421 hasTilde = (**fnamep == '~');
9422 if (hasTilde)
9423 pbuf = tfname = expand_env_save(*fnamep);
9424 else
9425 pbuf = tfname = FullName_save(*fnamep, FALSE);
9426
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009427 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009429 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
9430 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431
9432 if (len == 0)
9433 {
9434 /* Don't have a valid filename, so shorten the rest of the
9435 * path if we can. This CAN give us invalid 8.3 filenames, but
9436 * there's not a lot of point in guessing what it might be.
9437 */
9438 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009439 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
9440 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 }
9442
9443 /* Count the paths backward to find the beginning of the desired string. */
9444 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009445 {
9446#ifdef FEAT_MBYTE
9447 if (has_mbyte)
9448 p -= mb_head_off(tfname, p);
9449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 if (vim_ispathsep(*p))
9451 {
9452 if (sepcount == 0 || (hasTilde && sepcount == 1))
9453 break;
9454 else
9455 sepcount --;
9456 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 if (hasTilde)
9459 {
9460 --p;
9461 if (p >= tfname)
9462 *p = '~';
9463 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009464 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465 }
9466 else
9467 ++p;
9468
9469 /* Copy in the string - p indexes into tfname - allocated at pbuf */
9470 vim_free(*bufp);
9471 *fnamelen = (int)STRLEN(p);
9472 *bufp = pbuf;
9473 *fnamep = p;
9474
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009475 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476}
9477#endif /* WIN3264 */
9478
9479/*
9480 * Adjust a filename, according to a string of modifiers.
9481 * *fnamep must be NUL terminated when called. When returning, the length is
9482 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009483 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 * When there is an error, *fnamep is set to NULL.
9485 */
9486 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009487modify_fname(
9488 char_u *src, /* string with modifiers */
9489 int *usedlen, /* characters after src that are used */
9490 char_u **fnamep, /* file name so far */
9491 char_u **bufp, /* buffer for allocated file name or NULL */
9492 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493{
9494 int valid = 0;
9495 char_u *tail;
9496 char_u *s, *p, *pbuf;
9497 char_u dirname[MAXPATHL];
9498 int c;
9499 int has_fullname = 0;
9500#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009501 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502 int has_shortname = 0;
9503#endif
9504
9505repeat:
9506 /* ":p" - full path/file_name */
9507 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
9508 {
9509 has_fullname = 1;
9510
9511 valid |= VALID_PATH;
9512 *usedlen += 2;
9513
9514 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
9515 if ((*fnamep)[0] == '~'
9516#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
9517 && ((*fnamep)[1] == '/'
9518# ifdef BACKSLASH_IN_FILENAME
9519 || (*fnamep)[1] == '\\'
9520# endif
9521 || (*fnamep)[1] == NUL)
9522
9523#endif
9524 )
9525 {
9526 *fnamep = expand_env_save(*fnamep);
9527 vim_free(*bufp); /* free any allocated file name */
9528 *bufp = *fnamep;
9529 if (*fnamep == NULL)
9530 return -1;
9531 }
9532
9533 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009534 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535 {
9536 if (vim_ispathsep(*p)
9537 && p[1] == '.'
9538 && (p[2] == NUL
9539 || vim_ispathsep(p[2])
9540 || (p[2] == '.'
9541 && (p[3] == NUL || vim_ispathsep(p[3])))))
9542 break;
9543 }
9544
9545 /* FullName_save() is slow, don't use it when not needed. */
9546 if (*p != NUL || !vim_isAbsName(*fnamep))
9547 {
9548 *fnamep = FullName_save(*fnamep, *p != NUL);
9549 vim_free(*bufp); /* free any allocated file name */
9550 *bufp = *fnamep;
9551 if (*fnamep == NULL)
9552 return -1;
9553 }
9554
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009555#ifdef WIN3264
9556# if _WIN32_WINNT >= 0x0500
9557 if (vim_strchr(*fnamep, '~') != NULL)
9558 {
9559 /* Expand 8.3 filename to full path. Needed to make sure the same
9560 * file does not have two different names.
9561 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
9562 p = alloc(_MAX_PATH + 1);
9563 if (p != NULL)
9564 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009565 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009566 {
9567 vim_free(*bufp);
9568 *bufp = *fnamep = p;
9569 }
9570 else
9571 vim_free(p);
9572 }
9573 }
9574# endif
9575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576 /* Append a path separator to a directory. */
9577 if (mch_isdir(*fnamep))
9578 {
9579 /* Make room for one or two extra characters. */
9580 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
9581 vim_free(*bufp); /* free any allocated file name */
9582 *bufp = *fnamep;
9583 if (*fnamep == NULL)
9584 return -1;
9585 add_pathsep(*fnamep);
9586 }
9587 }
9588
9589 /* ":." - path relative to the current directory */
9590 /* ":~" - path relative to the home directory */
9591 /* ":8" - shortname path - postponed till after */
9592 while (src[*usedlen] == ':'
9593 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
9594 {
9595 *usedlen += 2;
9596 if (c == '8')
9597 {
9598#ifdef WIN3264
9599 has_shortname = 1; /* Postpone this. */
9600#endif
9601 continue;
9602 }
9603 pbuf = NULL;
9604 /* Need full path first (use expand_env() to remove a "~/") */
9605 if (!has_fullname)
9606 {
9607 if (c == '.' && **fnamep == '~')
9608 p = pbuf = expand_env_save(*fnamep);
9609 else
9610 p = pbuf = FullName_save(*fnamep, FALSE);
9611 }
9612 else
9613 p = *fnamep;
9614
9615 has_fullname = 0;
9616
9617 if (p != NULL)
9618 {
9619 if (c == '.')
9620 {
9621 mch_dirname(dirname, MAXPATHL);
9622 s = shorten_fname(p, dirname);
9623 if (s != NULL)
9624 {
9625 *fnamep = s;
9626 if (pbuf != NULL)
9627 {
9628 vim_free(*bufp); /* free any allocated file name */
9629 *bufp = pbuf;
9630 pbuf = NULL;
9631 }
9632 }
9633 }
9634 else
9635 {
9636 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
9637 /* Only replace it when it starts with '~' */
9638 if (*dirname == '~')
9639 {
9640 s = vim_strsave(dirname);
9641 if (s != NULL)
9642 {
9643 *fnamep = s;
9644 vim_free(*bufp);
9645 *bufp = s;
9646 }
9647 }
9648 }
9649 vim_free(pbuf);
9650 }
9651 }
9652
9653 tail = gettail(*fnamep);
9654 *fnamelen = (int)STRLEN(*fnamep);
9655
9656 /* ":h" - head, remove "/file_name", can be repeated */
9657 /* Don't remove the first "/" or "c:\" */
9658 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
9659 {
9660 valid |= VALID_HEAD;
9661 *usedlen += 2;
9662 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009663 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009664 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665 *fnamelen = (int)(tail - *fnamep);
9666#ifdef VMS
9667 if (*fnamelen > 0)
9668 *fnamelen += 1; /* the path separator is part of the path */
9669#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009670 if (*fnamelen == 0)
9671 {
9672 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
9673 p = vim_strsave((char_u *)".");
9674 if (p == NULL)
9675 return -1;
9676 vim_free(*bufp);
9677 *bufp = *fnamep = tail = p;
9678 *fnamelen = 1;
9679 }
9680 else
9681 {
9682 while (tail > s && !after_pathsep(s, tail))
9683 mb_ptr_back(*fnamep, tail);
9684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685 }
9686
9687 /* ":8" - shortname */
9688 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
9689 {
9690 *usedlen += 2;
9691#ifdef WIN3264
9692 has_shortname = 1;
9693#endif
9694 }
9695
9696#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009697 /*
9698 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699 */
9700 if (has_shortname)
9701 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009702 /* Copy the string if it is shortened by :h and when it wasn't copied
9703 * yet, because we are going to change it in place. Avoids changing
9704 * the buffer name for "%:8". */
9705 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 {
9707 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +02009708 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 return -1;
9710 vim_free(*bufp);
9711 *bufp = *fnamep = p;
9712 }
9713
9714 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +02009715 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716 if (!has_fullname && !vim_isAbsName(*fnamep))
9717 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009718 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719 return -1;
9720 }
9721 else
9722 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009723 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724
Bram Moolenaardc935552011-08-17 15:23:23 +02009725 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009727 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 return -1;
9729
9730 if (l == 0)
9731 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009732 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009734 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 return -1;
9736 }
9737 *fnamelen = l;
9738 }
9739 }
9740#endif /* WIN3264 */
9741
9742 /* ":t" - tail, just the basename */
9743 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
9744 {
9745 *usedlen += 2;
9746 *fnamelen -= (int)(tail - *fnamep);
9747 *fnamep = tail;
9748 }
9749
9750 /* ":e" - extension, can be repeated */
9751 /* ":r" - root, without extension, can be repeated */
9752 while (src[*usedlen] == ':'
9753 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
9754 {
9755 /* find a '.' in the tail:
9756 * - for second :e: before the current fname
9757 * - otherwise: The last '.'
9758 */
9759 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
9760 s = *fnamep - 2;
9761 else
9762 s = *fnamep + *fnamelen - 1;
9763 for ( ; s > tail; --s)
9764 if (s[0] == '.')
9765 break;
9766 if (src[*usedlen + 1] == 'e') /* :e */
9767 {
9768 if (s > tail)
9769 {
9770 *fnamelen += (int)(*fnamep - (s + 1));
9771 *fnamep = s + 1;
9772#ifdef VMS
9773 /* cut version from the extension */
9774 s = *fnamep + *fnamelen - 1;
9775 for ( ; s > *fnamep; --s)
9776 if (s[0] == ';')
9777 break;
9778 if (s > *fnamep)
9779 *fnamelen = s - *fnamep;
9780#endif
9781 }
9782 else if (*fnamep <= tail)
9783 *fnamelen = 0;
9784 }
9785 else /* :r */
9786 {
9787 if (s > tail) /* remove one extension */
9788 *fnamelen = (int)(s - *fnamep);
9789 }
9790 *usedlen += 2;
9791 }
9792
9793 /* ":s?pat?foo?" - substitute */
9794 /* ":gs?pat?foo?" - global substitute */
9795 if (src[*usedlen] == ':'
9796 && (src[*usedlen + 1] == 's'
9797 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
9798 {
9799 char_u *str;
9800 char_u *pat;
9801 char_u *sub;
9802 int sep;
9803 char_u *flags;
9804 int didit = FALSE;
9805
9806 flags = (char_u *)"";
9807 s = src + *usedlen + 2;
9808 if (src[*usedlen + 1] == 'g')
9809 {
9810 flags = (char_u *)"g";
9811 ++s;
9812 }
9813
9814 sep = *s++;
9815 if (sep)
9816 {
9817 /* find end of pattern */
9818 p = vim_strchr(s, sep);
9819 if (p != NULL)
9820 {
9821 pat = vim_strnsave(s, (int)(p - s));
9822 if (pat != NULL)
9823 {
9824 s = p + 1;
9825 /* find end of substitution */
9826 p = vim_strchr(s, sep);
9827 if (p != NULL)
9828 {
9829 sub = vim_strnsave(s, (int)(p - s));
9830 str = vim_strnsave(*fnamep, *fnamelen);
9831 if (sub != NULL && str != NULL)
9832 {
9833 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009834 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835 if (s != NULL)
9836 {
9837 *fnamep = s;
9838 *fnamelen = (int)STRLEN(s);
9839 vim_free(*bufp);
9840 *bufp = s;
9841 didit = TRUE;
9842 }
9843 }
9844 vim_free(sub);
9845 vim_free(str);
9846 }
9847 vim_free(pat);
9848 }
9849 }
9850 /* after using ":s", repeat all the modifiers */
9851 if (didit)
9852 goto repeat;
9853 }
9854 }
9855
Bram Moolenaar26df0922014-02-23 23:39:13 +01009856 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
9857 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +01009858 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +01009859 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +01009860 if (c != NUL)
9861 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +01009862 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +01009863 if (c != NUL)
9864 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +01009865 if (p == NULL)
9866 return -1;
9867 vim_free(*bufp);
9868 *bufp = *fnamep = p;
9869 *fnamelen = (int)STRLEN(p);
9870 *usedlen += 2;
9871 }
9872
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873 return valid;
9874}
9875
9876/*
9877 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009878 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879 * "flags" can be "g" to do a global substitute.
9880 * Returns an allocated string, NULL for error.
9881 */
9882 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009883do_string_sub(
9884 char_u *str,
9885 char_u *pat,
9886 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009887 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01009888 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889{
9890 int sublen;
9891 regmatch_T regmatch;
9892 int i;
9893 int do_all;
9894 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01009895 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896 garray_T ga;
9897 char_u *ret;
9898 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01009899 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900
9901 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
9902 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00009903 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904
9905 ga_init2(&ga, 1, 200);
9906
9907 do_all = (flags[0] == 'g');
9908
9909 regmatch.rm_ic = p_ic;
9910 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
9911 if (regmatch.regprog != NULL)
9912 {
9913 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01009914 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
9916 {
Bram Moolenaar8af26912014-01-23 20:09:34 +01009917 /* Skip empty match except for first match. */
9918 if (regmatch.startp[0] == regmatch.endp[0])
9919 {
9920 if (zero_width == regmatch.startp[0])
9921 {
9922 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02009923 i = MB_PTR2LEN(tail);
9924 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
9925 (size_t)i);
9926 ga.ga_len += i;
9927 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01009928 continue;
9929 }
9930 zero_width = regmatch.startp[0];
9931 }
9932
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933 /*
9934 * Get some space for a temporary buffer to do the substitution
9935 * into. It will contain:
9936 * - The text up to where the match is.
9937 * - The substituted text.
9938 * - The text after the match.
9939 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009940 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01009941 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
9943 {
9944 ga_clear(&ga);
9945 break;
9946 }
9947
9948 /* copy the text up to where the match is */
9949 i = (int)(regmatch.startp[0] - tail);
9950 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
9951 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009952 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 + ga.ga_len + i, TRUE, TRUE, FALSE);
9954 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02009955 tail = regmatch.endp[0];
9956 if (*tail == NUL)
9957 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958 if (!do_all)
9959 break;
9960 }
9961
9962 if (ga.ga_data != NULL)
9963 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
9964
Bram Moolenaar473de612013-06-08 18:19:48 +02009965 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 }
9967
9968 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
9969 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00009970 if (p_cpo == empty_option)
9971 p_cpo = save_cpo;
9972 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009973 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00009974 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975
9976 return ret;
9977}
9978
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009979 static int
9980filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
9981{
9982 typval_T rettv;
9983 typval_T argv[3];
9984 char_u buf[NUMBUFLEN];
9985 char_u *s;
9986 int retval = FAIL;
9987 int dummy;
9988
9989 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
9990 argv[0] = vimvars[VV_KEY].vv_tv;
9991 argv[1] = vimvars[VV_VAL].vv_tv;
9992 if (expr->v_type == VAR_FUNC)
9993 {
9994 s = expr->vval.v_string;
Bram Moolenaardf48fb42016-07-22 21:50:18 +02009995 if (call_func(s, (int)STRLEN(s), &rettv, 2, argv, NULL,
9996 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009997 goto theend;
9998 }
9999 else if (expr->v_type == VAR_PARTIAL)
10000 {
10001 partial_T *partial = expr->vval.v_partial;
10002
Bram Moolenaar437bafe2016-08-01 15:40:54 +020010003 s = partial_name(partial);
Bram Moolenaardf48fb42016-07-22 21:50:18 +020010004 if (call_func(s, (int)STRLEN(s), &rettv, 2, argv, NULL,
10005 0L, 0L, &dummy, TRUE, partial, NULL) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010006 goto theend;
10007 }
10008 else
10009 {
10010 s = get_tv_string_buf_chk(expr, buf);
10011 if (s == NULL)
10012 goto theend;
10013 s = skipwhite(s);
10014 if (eval1(&s, &rettv, TRUE) == FAIL)
10015 goto theend;
10016 if (*s != NUL) /* check for trailing chars after expr */
10017 {
10018 EMSG2(_(e_invexpr2), s);
10019 goto theend;
10020 }
10021 }
10022 if (map)
10023 {
10024 /* map(): replace the list item value */
10025 clear_tv(tv);
10026 rettv.v_lock = 0;
10027 *tv = rettv;
10028 }
10029 else
10030 {
10031 int error = FALSE;
10032
10033 /* filter(): when expr is zero remove the item */
10034 *remp = (get_tv_number_chk(&rettv, &error) == 0);
10035 clear_tv(&rettv);
10036 /* On type error, nothing has been removed; return FAIL to stop the
10037 * loop. The error message was given by get_tv_number_chk(). */
10038 if (error)
10039 goto theend;
10040 }
10041 retval = OK;
10042theend:
10043 clear_tv(&vimvars[VV_VAL].vv_tv);
10044 return retval;
10045}
10046
10047
10048/*
10049 * Implementation of map() and filter().
10050 */
10051 void
10052filter_map(typval_T *argvars, typval_T *rettv, int map)
10053{
10054 typval_T *expr;
10055 listitem_T *li, *nli;
10056 list_T *l = NULL;
10057 dictitem_T *di;
10058 hashtab_T *ht;
10059 hashitem_T *hi;
10060 dict_T *d = NULL;
10061 typval_T save_val;
10062 typval_T save_key;
10063 int rem;
10064 int todo;
10065 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10066 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10067 : N_("filter() argument"));
10068 int save_did_emsg;
10069 int idx = 0;
10070
10071 if (argvars[0].v_type == VAR_LIST)
10072 {
10073 if ((l = argvars[0].vval.v_list) == NULL
10074 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
10075 return;
10076 }
10077 else if (argvars[0].v_type == VAR_DICT)
10078 {
10079 if ((d = argvars[0].vval.v_dict) == NULL
10080 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
10081 return;
10082 }
10083 else
10084 {
10085 EMSG2(_(e_listdictarg), ermsg);
10086 return;
10087 }
10088
10089 expr = &argvars[1];
10090 /* On type errors, the preceding call has already displayed an error
10091 * message. Avoid a misleading error message for an empty string that
10092 * was not passed as argument. */
10093 if (expr->v_type != VAR_UNKNOWN)
10094 {
10095 prepare_vimvar(VV_VAL, &save_val);
10096
10097 /* We reset "did_emsg" to be able to detect whether an error
10098 * occurred during evaluation of the expression. */
10099 save_did_emsg = did_emsg;
10100 did_emsg = FALSE;
10101
10102 prepare_vimvar(VV_KEY, &save_key);
10103 if (argvars[0].v_type == VAR_DICT)
10104 {
10105 vimvars[VV_KEY].vv_type = VAR_STRING;
10106
10107 ht = &d->dv_hashtab;
10108 hash_lock(ht);
10109 todo = (int)ht->ht_used;
10110 for (hi = ht->ht_array; todo > 0; ++hi)
10111 {
10112 if (!HASHITEM_EMPTY(hi))
10113 {
10114 int r;
10115
10116 --todo;
10117 di = HI2DI(hi);
10118 if (map &&
10119 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10120 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
10121 break;
10122 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10123 r = filter_map_one(&di->di_tv, expr, map, &rem);
10124 clear_tv(&vimvars[VV_KEY].vv_tv);
10125 if (r == FAIL || did_emsg)
10126 break;
10127 if (!map && rem)
10128 {
10129 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10130 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10131 break;
10132 dictitem_remove(d, di);
10133 }
10134 }
10135 }
10136 hash_unlock(ht);
10137 }
10138 else
10139 {
10140 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10141
10142 for (li = l->lv_first; li != NULL; li = nli)
10143 {
10144 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
10145 break;
10146 nli = li->li_next;
10147 vimvars[VV_KEY].vv_nr = idx;
10148 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10149 || did_emsg)
10150 break;
10151 if (!map && rem)
10152 listitem_remove(l, li);
10153 ++idx;
10154 }
10155 }
10156
10157 restore_vimvar(VV_KEY, &save_key);
10158 restore_vimvar(VV_VAL, &save_val);
10159
10160 did_emsg |= save_did_emsg;
10161 }
10162
10163 copy_tv(&argvars[0], rettv);
10164}
10165
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */